# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1106582 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 43 ms | 3656 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 ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int W, N;
cin >> W >> N;
vector<vector<pair<int, int>>> g(W + 1);
vector<int> dp(W + 1);
for (int i = 0; i < N; i++) {
int v, w, k;
cin >> v >> w >> k;
if (w > W) continue;
g[w].push_back({ -v,k });
}
for (int i = 1; i <= W; i++) {
if (g[i].empty()) continue;
sort(g[i].begin(), g[i].end());
}
for (int w = 1; w <= W; w++) {
if (g[w].empty()) continue;
int j = 0;
for (int i = 1; i <= W / w; i++) {
auto&& [v, k] = g[w][j];
for (int x = W; x - w >= 0; x--) {
dp[x] = max(dp[x], dp[x - w] - v);
}
k--;
if (k == 0) {
j++;
}
if (j >= g[w].size()) {
break;
}
}
}
cout << *max_element(dp.begin(), dp.end());
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... |