-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10130 - SuperSale.cpp
50 lines (50 loc) · 1.05 KB
/
10130 - SuperSale.cpp
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
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int knapsack(int n,int m,int p[],int w[]);
int main()
{
int t,n,p[1000],w[1000];
int man,m;
int max;
cin>>t;
while(t--)
{
int result=0;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>p[i]>>w[i];
}
cin>>man;
for(int i=0; i<man; i++)
{
cin>>m;
max=knapsack(n,m,p,w);
result+=max;
}
cout<<result<<endl;
}
return 0;
}
int knapsack(int n,int m,int p[],int w[])
{
int b[1000][102],i,j;
for(i=0; i<=n; i++)
b[i][0]=0;
for(i=0; i<=m; i++)
b[0][i]=0;
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
{
if(w[i]<=j)
{
if(p[i]+b[i-1][j-w[i]] > b[i-1][j])
b[i][j]=p[i]+b[i-1][j-w[i]];
else
b[i][j]=b[i-1][j];
}
else
b[i][j]=b[i-1][j];
}
return (b[n][m]);
}