fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long int
  3.  
  4. #define print(a) for (auto x : a) cout << x << " "; cout << endl
  5. #define print_upto(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
  6. #define take(x,n) for(int i=0;i<n;i++) cin>>x[i];
  7.  
  8. #define watch(x) cout << (#x) << " is " << (x) << "\n"
  9. #define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"
  10.  
  11. using namespace std;
  12.  
  13. ll a[1000008];
  14. ll dp[1000008];
  15.  
  16. int main() {
  17.  
  18. ios_base::sync_with_stdio(false);
  19. cin.tie(0);
  20. cout.tie(0);
  21.  
  22. #ifndef ONLINE_JUDGE
  23. freopen("input.txt", "r", stdin);
  24. freopen("output.txt", "w", stdout);
  25. freopen("error.txt" , "w" , stderr);
  26. #endif
  27.  
  28. ll n;
  29. cin >> n;
  30.  
  31.  
  32. for (ll i = 1; i <= n; i++) {
  33. cin >> a[i];
  34. }
  35.  
  36. for (ll i = 1; i + 2 <= n; i++) {
  37. dp[i] = 1;
  38. if (a[i + 1] > a[i + 2] && dp[i - 1] == 0) {
  39. dp[i + 1] = 1;
  40. continue;
  41. } else {
  42. dp[i + 2] = 1;
  43. i++;
  44. }
  45. }
  46.  
  47. if (dp[n - 2] == 0) {
  48. dp[n - 1] = 1;
  49. dp[n] = 1;
  50. }
  51.  
  52. ll ans = 0;
  53. for (ll i = 0; i <= n; i++) {
  54. if (dp[i] == 1) {
  55. ans += a[i];
  56. }
  57. }
  58.  
  59. cout << ans << "\n";
  60.  
  61.  
  62. return 0;
  63. }
Time limit exceeded #stdin #stdout 5s 4396KB
stdin
Standard input is empty
stdout
Standard output is empty