# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1033439 | cryptobunny | Knapsack (NOI18_knapsack) | C++14 | 1 ms | 436 KiB |
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 <iostream>
#include <vector>
#include <cassert>
using namespace std;
int main() {
int s, n;
cin >> s >> n;
vector<int> val, cost;
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
int j;
for (j = 30; j >= 1; j--) {
if (k >= (1 << j)) {
break;
}
}
for (int x = 0; x < j; x++) {
val.push_back(v << x);
cost.push_back(w << x);
k -= 1 << x;
}
if (k) {
val.push_back(v * k);
cost.push_back(w * k);
}
}
vector<int> dp(s + 1);
int ans = 0;
for (int i = 0; i < val.size(); i++) {
assert(i < cost.size());
// cout << val[i] << " " << cost[i] << endl;
for (int w = s; w >= cost[i]; w--) {
dp[w] = max(dp[w], dp[w - cost[i]] + val[i]);
ans = max(ans, dp[w]);
}
}
cout << ans << endl;
}
Compilation message (stderr)
# | 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... |