fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll NEG = -1e18;
  5. const ll INF = 1e18;
  6. const ll N = 1e6 + 5;
  7. signed main ()
  8. {
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11. cout.tie(nullptr);
  12.  
  13. ll t;
  14. cin >> t;
  15. while (t--)
  16. {
  17. ll n;
  18. cin >> n;
  19. if (n == 0)
  20. {
  21. cout << 0 << '\n';
  22. continue;
  23. }
  24.  
  25. // dp[i][1] : lấy ở hiện tại (dp[i - 1][0] + a[i])
  26. // dp[i][0] : bỏ qua không lấy ở lúc này (dp[i - 1][1])
  27.  
  28. ll x;
  29. cin >> x;
  30. ll c0 = 0;
  31. ll c1 = x;
  32. for (int i = 2; i <= n; i++)
  33. {
  34. cin >> x;
  35. ll cur1 = max(c0, c1);
  36. ll cur2 = c0 + x;
  37. c0 = cur1;
  38. c1 = cur2;
  39. }
  40. cout << max(c0, c1) << '\n';
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
140735772392728