-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1412.cpp
More file actions
68 lines (65 loc) · 1.29 KB
/
Copy path1412.cpp
File metadata and controls
68 lines (65 loc) · 1.29 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
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#define MAXV 5005
#define MAXN 205
#define MAXK 55
#define NEGINF -10000000
using namespace std;
int dp[MAXV][MAXK];
int used[MAXV];
int main(){
memset(dp, 0, sizeof(dp));
queue<int> q1, q2;
int k, v, n, w[MAXN], val[MAXN], tot = 0;
cin >> k >> v >> n;
for(int i=0; i<n; i++){
cin >> w[i] >> val[i];
}
for(int i=0; i<=v; i++){
for(int j=0; j<k; j++){
dp[i][j] = NEGINF;
}
}
dp[0][0] = 0;
for(int i=0; i<n; i++){
for(int j=v; j>=w[i]; j--){
for(int ik=0; ik<k; ik++){
q1.push(dp[j][ik]);
q2.push(dp[j-w[i]][ik]+val[i]);
}
for(int ik=0; ik<k; ik++){
if(q1.front() > q2.front()){
dp[j][ik] = q1.front();
q1.pop();
}else{
dp[j][ik] = q2.front();
q2.pop();
}
}
while(!q1.empty()){
q1.pop();
}
while(!q2.empty()){
q2.pop();
}
}
}
// for(int i=0; i<k; i++){
// // for(int j=0; j<=n; j++){
// for(int l=0; l<=v; l++){
// cout << dp[l][i] << " ";
// }
// cout << endl;
// // }
// cout << endl << endl;
// }
for(int i=0; i<k; i++){
if(dp[v][i] > 0)
tot += dp[v][i];
}
cout << tot;
return 0;
}