Skip to content

Instantly share code, notes, and snippets.

@Darkborderman
Created June 30, 2018 11:31
Show Gist options
  • Select an option

  • Save Darkborderman/82e3cd6010675ae54badd2fba993ff3b to your computer and use it in GitHub Desktop.

Select an option

Save Darkborderman/82e3cd6010675ae54badd2fba993ff3b to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
int main(){
//1 2 3 4
//1 2 5 6
int price[4]={1,2,5,6};
int size;
//input size and initialize DP table
cin>>size;
int table[size];
for(int i=0;i<=size-1;i++) table[i]=-1000;
for(int i=0;i<=(sizeof(price)/sizeof(int))-1;i++) table[i]=price[i];
//DP
for(int i=1;i<=size-1;i++){
int max=table[i];
for(int j=0;j<=i-1;j++){
if(max<table[j]+table[i-j-1]){
max=table[j]+table[i-j-1];
}
}
table[i]=max;
}
//print result
for(int i=0;i<=size-1;i++) cout<<table[i]<<" ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment