-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsnak_repeating_value.cpp
More file actions
42 lines (40 loc) · 1.08 KB
/
knapsnak_repeating_value.cpp
File metadata and controls
42 lines (40 loc) · 1.08 KB
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i,j,k,t,m,w,dp[2005][2005],val[2006],x,y;
cin>>t;
while(t--){
cin>>n>>m;
w=m;
for(i=1;i<=n;i++)
cin>>val[i];
for(i=0;i<=n;i++){
for(j=0;j<=m;j++){
if(i==0 || j==0){
dp[i][j]=0;
}
else if( val[i]<=j){
x=j;
dp[i][j]=max(val[i]+dp[i-1][j-val[i]],dp[i-1][j]);
x=j-val[i];
y=2;
while(x>=val[i]){
dp[i][j]=max(y*val[i]+dp[i-1][x-val[i]],dp[i][j]);
x=x-val[i];y++;
}
}
else
dp[i][j]=dp[i-1][j];
//cout<<dp[i][j]<<" ";
}
//cout<<endl;
}
cout<<dp[n][m]<<endl;
}
return 0;
}