Created
June 30, 2018 11:31
-
-
Save Darkborderman/82e3cd6010675ae54badd2fba993ff3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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