This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dp[2][2001];
vector<pair<int,int>> a;
vector<pair<int,int>> tmp[2001];
int s,n;
int main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> s >> n;
for (int i=1;i<=n;i++) {
int v,w,k; cin >> v >> w >> k;
tmp[w].push_back(make_pair(v,k));
}
a.push_back(make_pair(0,0));
for (int i=1;i<=2000;i++) {
sort(tmp[i].begin(),tmp[i].end(),greater<pair<int,int>>());
int ptr = 0;
int cnt = s / i;
while (ptr < tmp[i].size()) {
while (cnt > 0 && tmp[i][ptr].second > 0) {
a.push_back(make_pair(i,tmp[i][ptr].first));
tmp[i][ptr].second--;
cnt--;
}
if (cnt == 0) break;
else ptr++;
}
}
n = a.size() - 1;
for (int i=1;i<=n;i++) {
for (int j=1;j<=s;j++) {
dp[i & 1][j] = dp[(i-1) & 1][j];
if (j >= a[i].first) {
dp[i & 1][j] = max(dp[i & 1][j], dp[(i - 1) & 1][j - a[i].first] + a[i].second);
}
}
}
cout << dp[n & 1][s];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:22: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]
22 | while (ptr < tmp[i].size()) {
| ~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |