# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
985875 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 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 i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int S, N;
std::cin >> S >> N;
std::map<int, std::vector<std::pair<int, int>>> item;
for (int i = 0; i < N; i++) {
int v, w, k;
std::cin >> v >> w >> k;
item[w].push_back({v, k});
}
std::vector<i64> dp(1 + S);
for (auto &[w, vec] : item) {
std::sort(vec.begin(), vec.end(), std::greater<std::pair<int, int>>());
for (int j = S; j >= 0; j--) {
i64 val = 0;
int cap = 0;
for (auto [v, k] : vec) {
for (int i = 0; i < k; i++) {
val += v, cap += w;
if (cap > S) {
goto outer;
}
if (j + cap <= S) {
dp[j + cap] = std::max(dp[j + cap], dp[j] + val);
}
}
}
outer:
}
}
std::cout << dp[S] << "\n";
return 0;
}