| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1319433 | f0rsworn | Knapsack (NOI18_knapsack) | C++20 | 68 ms | 33144 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(), a.end()
ll dp[2001][2001];
const ll N = 0xc0c0c0c0c0c0c0c0;
int main() {
int S, n;
scanf("%d%d", &S, &n);
memset(dp, 0xc0, sizeof(dp));
map<int, vector<pair<int, int>>> by_weight;
for (int i = 0; i < n; ++i) {
int v, w, k;
scanf("%d%d%d", &v, &w, &k);
if (w <= S && k > 0) {
by_weight[w].push_back({v, k});
}
}
dp[0][0] = 0;
int at = 1;
for (auto &[w, items] : by_weight) {
sort(all(items), greater<pair<int, int>>());
for (int i = 0; i <= S; ++i) {
dp[at][i] = dp[at - 1][i];
int copies = 0;
int type_at = 0;
int curr_used = 0;
ll profit = 0;
while ((copies + 1) * w <= i && type_at < items.size()) {
copies++;
profit += items[type_at].first;
if (dp[at - 1][i - copies * w] != N) {
dp[at][i] = max(dp[at][i], dp[at - 1][i - copies * w] + profit);
}
curr_used++;
if (curr_used == items[type_at].second) {
curr_used = 0;
type_at++;
}
}
}
at++;
}
ll ans = N;
for (int i = 0; i <= S; ++i)
ans = max(ans, dp[by_weight.size()][i]);
printf("%lld\n", ans);
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... | ||||
