fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getSum(vector<int>arr,int n){
  4. vector<int>dp(n,0);
  5. dp[0]=arr[0];
  6. dp[1]=dp[0]+arr[1];
  7. for(int i=2;i<n;i++){
  8. dp[i]=max(dp[i-1]+arr[i],dp[i-2]+arr[i]); //as we can make 1 or 2 jumps back and current element must be included
  9. }
  10. return dp[n-1];
  11. }
  12.  
  13. int main() {
  14. // your code goes here
  15. vector<int>arr={1,2,-1,3,4};
  16. int n=arr.size();
  17. cout<<"The maximum sum of all elements by performing required 1 or 2 jumps is:"<<getSum(arr,n);
  18. return 0;
  19. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
The maximum sum of all elements by performing required 1 or 2 jumps is:10