fork(28) download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int n;
  8. cin >> n;
  9. int arr[n], opt[n+2];
  10. memset(opt, 0, sizeof(opt));
  11.  
  12. for(int i = 0; i < n; i++)
  13. cin >> arr[i];
  14.  
  15. for(int i = n-1; i >= 0; i--)
  16. opt[i] = arr[i] + max(opt[i+2], opt[i+1]);
  17.  
  18. cout << max(opt[0], opt[1]) << endl;
  19. return 0;
  20. }
Success #stdin #stdout 0s 16064KB
stdin
9
10 20  30 -10  -50  40  -50 -1 -3
stdout
89