fork download
  1. // max sum of subsets in an array (no 2 elements should be consecutive)
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. main(){
  6. int n;
  7. cin>>n;
  8. vector<int>v(n),dp(n);
  9. for(auto &it:v)cin>>it;
  10. dp[0]=max(0,v[0]);
  11. dp[1]=max(dp[0],v[1]);
  12. for(int i=2;i<n;i++){
  13. dp[i]=max(dp[i-1],dp[i-2]+v[i]);
  14. }
  15. cout<<dp[n-1]<<"\n";
  16. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
2 4 6 7 8
stdout
16