# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
951800 | TrendBattles | Knapsack (NOI18_knapsack) | C++14 | 101 ms | 5864 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 <bits/stdc++.h>
using namespace std;
using lli = int64_t;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int W, N; cin >> W >> N;
vector <vector <int>> store(W + 1);
for (int i = 1; i <= N; ++i) {
int value, weight, cnt; cin >> value >> weight >> cnt;
lli k = 1;
while (cnt >= k) {
if (weight * k <= W) {
store[weight * k].push_back(value * k);
}
cnt -= k;
k <<= 1;
}
if (cnt and (lli) weight * cnt <= W) {
store[weight * cnt].push_back(value * cnt);
}
}
vector <lli> dp(W + 1);
for (int i = 1; i <= W; ++i) {
sort(store[i].begin(), store[i].end(), greater <int> ());
if (store[i].size() > W / i) {
store[i].erase(store[i].begin() + W / i, store[i].end());
}
for (int v : store[i]) {
for (int cur_w = W; cur_w >= i; --cur_w) {
dp[cur_w] = max(dp[cur_w], dp[cur_w - i] + v);
}
}
}
cout << dp[W];
return 0;
}
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... |