Skip to content

Instantly share code, notes, and snippets.

@prashantgpt91
Last active October 2, 2017 19:14
Show Gist options
  • Save prashantgpt91/4677c7add1cfe0251804d942338f7647 to your computer and use it in GitHub Desktop.
Save prashantgpt91/4677c7add1cfe0251804d942338f7647 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int i,n;
cin>>n;
int a[n],dp[n];
for(i=0;i<n;i++)
cin>>a[i];
memset(dp,0, sizeof(dp));
dp[0] = a[0];
dp[1] = a[1];
for (int j = 2; j < n; ++j) {
dp[j] = max(dp[j-2]+a[j],dp[j-1]);
}
cout<<dp[n-1];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment