forked from AraragiSenpai7/Important-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodCutting.cpp
More file actions
45 lines (35 loc) · 893 Bytes
/
RodCutting.cpp
File metadata and controls
45 lines (35 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n; cin>>n;
int len[n], pro[n];
for(int i = 0; i<n; i++){
cin>>pro[i];
}
for(int i = 0; i<n; i++){
int no = i+1;
len[i] = no;
}
int dp[n+1][n+1];
for(int i = 0; i<=n; i++){
dp[0][i] = 0;
}
for(int i = 0; i<=n; i++){
dp[i][0] = 0;
}
for(int i = 1; i<=n; i++){
for(int j = 1; j<= n; j++){
if(len[i-1] <= j){
dp[i][j] = max(pro[i-1] + dp[i][j-len[i-1]], dp[i-1][j]);
}
else
dp[i][j] = dp[i-1][j];
}
}
cout<<dp[n][n]<<endl;
}
return 0;
}