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;
#define int long long
#define MAX 2005
int f[MAX][MAX];
map<int, vector<pair<int, int>>> a;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout);
int s, n;
cin >> s >> n;
for (int v, w, k, i = 0; i < n; i++) {
cin >> v >> w >> k;
if (w <= s) {
a[w].emplace_back(v, k);
}
}
int i = 0;
for (auto &[w, items] : a) {
std::sort(items.begin(), items.end(), greater<pair<int, int>>());
++i;
for (int j = 0; j <= s; j++) {
f[i][j] = f[i - 1][j];
int cur = 0, num = 0, take = 0, val = 0;
while (cur < items.size() && (num + 1) * w <= j) {
++num;
val += items[cur].first;
f[i][j] = max(f[i][j], f[i - 1][j - num * w] + val);
if (++take == items[cur].second) {
take = 0;
++cur;
}
}
}
}
cout << f[i][s];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:29:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while (cur < items.size() && (num + 1) * w <= j) {
| ~~~~^~~~~~~~~~~~~~
# | 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... |