fork(1) download
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. int dp[1000001],n;
  5. signed main()
  6. {
  7. cin>>n;
  8. int a[n+1];
  9. for (int i=1;i<=n;i++)
  10. {
  11. cin>>a[i];
  12. }
  13. dp[0]=0;
  14. dp[1]=a[1];
  15. for (int i=2;i<=n;i++)
  16. {
  17. dp[i]=max(dp[i-1],max(dp[i-2]+a[i],dp[i-3]+a[i]+a[i-1]));
  18. }
  19. cout<<dp[n];
  20. }
  21.  
Success #stdin #stdout 0.01s 5300KB
stdin
4
9 3 5 4
stdout
18