fork download
  1. // You are given an array of N integers.
  2. //You can either take an element and add it to your sum or skip it.
  3. //However, if you take an element, you cannot take the next element.
  4. //Find the maximum sum you can obtain.
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. int main() {
  10. int n;cin>>n; int arr[n];
  11. for(int i=0;i<n;i++)
  12. cin>>arr[i];
  13.  
  14. vector<int>dp(n,0);
  15. dp[0]=arr[0];
  16. dp[1]=max(arr[0], arr[1]);
  17. for(int i=2;i<n;i++){
  18. dp[i]=max(arr[i]+dp[i-2], dp[i-1]);
  19. }
  20. cout<<dp[n-1]<<endl;
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5284KB
stdin
8
1
-2
3
12
-5
1
6
-3
stdout
19