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