//https://oj.uz/problem/view/NOI18_knapsack
#include <bits/stdc++.h>
#define loop(i, l, r) for(int i = (l); i < (r); ++i)
#define pool(i, l, r) for(int i = (l); i > (r); --i)
#define debug(x) cerr << #x << ": " << x << endl;
using namespace std;
int n,s;
const int MAXS = 2005;
long long dp[MAXS];
map<int,vector<pair<int,int>>> items;
bool cmp(pair<int,int> a,pair<int,int> b){
return a.first>b.first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>s>>n;
long long v,w,k;
loop(i,0,n){
cin>>v>>w>>k;
items[w].push_back({v,k});
}
for(auto& i:items){
vector<pair<int,int>>& a = i.second;
sort(a.begin(),a.end(),cmp);
pool(j,s,-1){
int value = 0;
int c = 0;
int count = 0;
int l = 1;
while(c<a.size()){
w = l*i.first;
if(j>=w){
value+=a[c].first;
count++;
if(count>=a[c].second){
c++;
count = 0;
}
dp[j] = max(dp[j],dp[j-w]+value);
}else{
break;
}
l++;
}
}
}
cout<<dp[s]<<endl;
return 0;
}
Compilation message
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(c<a.size()){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
356 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
0 ms |
360 KB |
Output is correct |
26 |
Correct |
3 ms |
480 KB |
Output is correct |
27 |
Correct |
1 ms |
360 KB |
Output is correct |
28 |
Correct |
0 ms |
612 KB |
Output is correct |
29 |
Correct |
1 ms |
360 KB |
Output is correct |
30 |
Correct |
1 ms |
360 KB |
Output is correct |
31 |
Correct |
1 ms |
360 KB |
Output is correct |
32 |
Correct |
1 ms |
360 KB |
Output is correct |
33 |
Correct |
1 ms |
360 KB |
Output is correct |
34 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
0 ms |
360 KB |
Output is correct |
26 |
Correct |
3 ms |
480 KB |
Output is correct |
27 |
Correct |
1 ms |
360 KB |
Output is correct |
28 |
Correct |
0 ms |
612 KB |
Output is correct |
29 |
Correct |
1 ms |
360 KB |
Output is correct |
30 |
Correct |
1 ms |
360 KB |
Output is correct |
31 |
Correct |
1 ms |
360 KB |
Output is correct |
32 |
Correct |
1 ms |
360 KB |
Output is correct |
33 |
Correct |
1 ms |
360 KB |
Output is correct |
34 |
Correct |
1 ms |
360 KB |
Output is correct |
35 |
Correct |
26 ms |
2280 KB |
Output is correct |
36 |
Correct |
27 ms |
2536 KB |
Output is correct |
37 |
Correct |
26 ms |
2280 KB |
Output is correct |
38 |
Correct |
26 ms |
2712 KB |
Output is correct |
39 |
Correct |
29 ms |
3532 KB |
Output is correct |
40 |
Correct |
54 ms |
3924 KB |
Output is correct |
41 |
Correct |
44 ms |
3152 KB |
Output is correct |
42 |
Correct |
55 ms |
3152 KB |
Output is correct |
43 |
Correct |
52 ms |
3156 KB |
Output is correct |
44 |
Correct |
51 ms |
3164 KB |
Output is correct |
45 |
Correct |
39 ms |
3656 KB |
Output is correct |
46 |
Correct |
22 ms |
3308 KB |
Output is correct |
47 |
Correct |
33 ms |
3668 KB |
Output is correct |
48 |
Correct |
41 ms |
3796 KB |
Output is correct |
49 |
Correct |
23 ms |
3420 KB |
Output is correct |
50 |
Correct |
43 ms |
3428 KB |
Output is correct |