# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
715222 | cfjason | Knapsack (NOI18_knapsack) | C++17 | 111 ms | 4864 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(int argc, char *argv[]) {
ll s, n;
cin >> s >> n;
vector<ll> dp(s + 1);
vector<vector<array<ll, 2>>> itemsByWeight(s + 1);
for (int i = 0; i < n; i++) {
ll v, w, k;
cin >> v >> w >> k;
itemsByWeight[w].push_back({v, k});
}
for (int i = 0; i <= s; i++) {
sort(itemsByWeight[i].rbegin(), itemsByWeight[i].rend());
}
for (int i = 0; i < itemsByWeight.size(); i++) {
for (int j = s; j >= 0; j--) {
int cpIndex = j;
int earnings = 0;
for (int k = 0; k < itemsByWeight[i].size(); k++) {
for (int d = 0; d < itemsByWeight[i][k][1]; d++) {
if (cpIndex - i < 0)
break;
cpIndex -= i;
earnings += itemsByWeight[i][k][0];
dp[j] = max(dp[j], dp[cpIndex] + earnings);
}
if (cpIndex - i < 0)
break;
}
}
}
cout << dp[s] << endl;
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... |