-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsnak01.cpp
More file actions
35 lines (33 loc) · 842 Bytes
/
knapsnak01.cpp
File metadata and controls
35 lines (33 loc) · 842 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
#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){
dp[i][j]=max(val[i]+dp[i-1][j-val[i]],dp[i-1][j]);
}
else
dp[i][j]=dp[i-1][j];
//cout<<dp[i][j]<<" ";
}
//cout<<endl;
}
cout<<dp[n][m]<<endl;
}
return 0;
}