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>
#define long long long
using namespace std;
/*
优美二进制分组
nklogk
*/
const int K = 2000;
const int MIN = numeric_limits<int>::min();
int n, k;
long f[K + 1];
vector<pair<long, int>> a;
void split(int v, int w, int c) {
for (int i = __lg(c); i >= 0; i--) {
a.push_back({v * (1LL << i), w * (1 << i)});
}
c = c - (1 << __lg(c));
if (c > 0) {
a.push_back({v * c, w * c});
}
}
void solve() {
cin >> k >> n;
for (int i = 0; i < n; i++) {
int v, w, c;
cin >> v >> w >> c;
c = min(c, k / w);
split(v, w, c);
}
fill(f + 1, f + k + 1, MIN);
for (auto [v, w] : a) {
for (int i = k; i >= w; i--) {
if (f[i - w] > MIN) {
f[i] = max(f[i], f[i - w] + v);
}
}
}
cout << *max_element(f, f + k + 1) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
t = 1;
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:38:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for (auto [v, w] : a) {
| ^
# | 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... |