#include<stdio.h>
int max(int a, int b) { return (a > b)? a : b; }
int knapSack(int W, int wt[], int val[], int n){
int i, w;
int k[n+1][W+1];
for (i = 0; i <= n; i++){
for (w = 0; w <= W; w++){
if (i==0 || w==0)
k[i][w] = 0;
else if (wt[i-1] <= w)
k[i][w] = max(val[i-1] + k[i-1][w-wt[i-1]], k[i-1][w]);
else
k[i][w] = k[i-1][w];
}
}
return k[n][W];
}
int main(){
int a,t,b,ar[10000],i,j,n,k,w,br[10000];
scanf("%d",&t);
while(t--){
scanf("%d %d",&w,&n);
for(i=0;i<n;i++){
scanf("%d %d",&ar[i],&br[i]);
}
printf("Hey stupid robber, you can get %d.\n", knapSack(w, ar, br, n));
}
return 0;
}
int max(int a, int b) { return (a > b)? a : b; }
int knapSack(int W, int wt[], int val[], int n){
int i, w;
int k[n+1][W+1];
for (i = 0; i <= n; i++){
for (w = 0; w <= W; w++){
if (i==0 || w==0)
k[i][w] = 0;
else if (wt[i-1] <= w)
k[i][w] = max(val[i-1] + k[i-1][w-wt[i-1]], k[i-1][w]);
else
k[i][w] = k[i-1][w];
}
}
return k[n][W];
}
int main(){
int a,t,b,ar[10000],i,j,n,k,w,br[10000];
scanf("%d",&t);
while(t--){
scanf("%d %d",&w,&n);
for(i=0;i<n;i++){
scanf("%d %d",&ar[i],&br[i]);
}
printf("Hey stupid robber, you can get %d.\n", knapSack(w, ar, br, n));
}
return 0;
}
No comments:
Post a Comment