fork download
  1. #include <bits/stdc++.h>
  2. long long n,a[100009],i,d[100009];
  3. using namespace std;
  4.  
  5. int main()
  6. {ios_base::sync_with_stdio(false);
  7. cin.tie(0);cout.tie(0);
  8. freopen("stdin","r",stdin);
  9. freopen("stdout","w",stdout);
  10. cin>>n;
  11. for(i=1;i<=n;i++)
  12. cin>>a[i];
  13. d[1]=a[1];
  14. d[2]=a[2];
  15. d[3]=max(a[1]+a[3],a[2]);
  16. for(i=4;i<=n;i++);
  17. d[i]=max(d[i-1],d[i-2]+a[i]);
  18. cout <<max(d[n],d[n-1]);
  19. return 0;
  20. }
  21.  
  22.  
Success #stdin #stdout 0.01s 5288KB
stdin
4
6
2
1
5
stdout
Standard output is empty