Skip to content

Instantly share code, notes, and snippets.

@abhisheksingh24
Created June 29, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhisheksingh24/74fb8494feb93bdd28aab88056b98729 to your computer and use it in GitHub Desktop.
Save abhisheksingh24/74fb8494feb93bdd28aab88056b98729 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<vector>
using namespace std;
int n, incl[1000000][2], excl[1000000][2];
vector<int> cost;
int exclude(int,int);
void init(){
for(int i = 0; i<n; i++){
for(int j = 0; j < 2; j++){
incl[i][j] = excl[i][j] = -1;
}
}
}
int include(int beg, int end){
if(beg>=end) return 0;
if(incl[beg][n-end]==-1) incl[beg][n-end] = cost[beg] + exclude(beg+1,end);
return(incl[beg][n-end]);
}
int exclude(int beg, int end){
if(beg>=end) return 0;
if(excl[beg][n-end]==-1) excl[beg][n-end] = min(include(beg, end), include(beg+1, end));
return(excl[beg][n-end]);
}
int main(){
int temp;
cin >> n;
int minSum;
init();
for(int i = 0; i < n; i++){
cin >> temp;
cost.push_back(temp);
}
minSum = min(include(0,n), cost[n-1]+include(1,n-1));
cout << minSum << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment