-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10130.wrong.cpp
More file actions
79 lines (72 loc) · 1.39 KB
/
10130.wrong.cpp
File metadata and controls
79 lines (72 loc) · 1.39 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//10130 - SuperSale
//DP, pack
#include <iostream>
#include <map>
using namespace std;
int N, G;
struct cmp {
bool operator () (int*a, int* b){
for(int i=0;i<=G;++i){
if (a[i]<b[i]) return true;
if (a[i]>b[i]) return false;
}
return false;
}
};
map<int*, int, cmp> table;
int price[1000+10];
int weight[1000+10];
int solve(int* arg){
map<int*,int, cmp>::iterator it=table.find(arg);
if (it!=table.end()){
delete[] arg;
return it->second;
}
int vmax=0;
int index=arg[0];
if (index==0){
for(int i=1;i<=G;++i){
if(arg[i]>=weight[0]) {
vmax = price[0];
break;
}
}
} else {
int* newArg= new int[G+1]; memcpy(newArg, arg, sizeof(int)*(G+1));
newArg[0]--;
vmax = solve(newArg);
for (int i=1;i<=G;++i){
if (arg[i]>=weight[index]){
newArg= new int[G+1]; memcpy(newArg, arg, sizeof(int)*(G+1));
newArg[0]--;
newArg[i]-=weight[index];
int r= solve(newArg)+price[i];
if(r>vmax) vmax=r;
}
}
}
table.insert(make_pair(arg,vmax));
return vmax;
}
int main(){
int T;
cin>>T;
for(int kase=0;kase<T;++kase){
cin>>N;
for(int i=0;i<N;++i){
cin>>price[i]>>weight[i];
}
cin>>G;
int* arg =new int[G+1];
for(int i=1;i<=G;++i)
cin>>arg[i];
arg[0]=N-1;
int result=solve(arg);
cout<<result<<endl;
for (map<int*,int,cmp>::iterator it=table.begin(); it!=table.end(); ++it){
delete[] (it->first);
}
table.clear();
}
return 0;
}