이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// https://oj.uz/problem/view/NOI18_knapsack
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int s, n;
cin >> s >> n;
vector<vector<pair<int, int>>> by_weight(s + 1);
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
by_weight[w].push_back({v, k});
}
vector<vector<int>> dp(s + 1, vector<int>(s + 1));
for (int w = 1; w <= s; w++) {
sort(by_weight[w].begin(), by_weight[w].end(), greater<pair<int, int>>());
for (int capacity = 1; capacity <= s; capacity++) {
dp[w][capacity] = max(dp[w - 1][capacity], dp[w][capacity - 1]);
int gained = 0;
int consumed = 0;
for (auto& [v, k] : by_weight[w]) {
if (consumed + w > capacity) {
break;
}
for (int taken = 0; consumed + w <= capacity && taken < k; taken++) {
gained += v;
consumed += w;
dp[w][capacity] = max(dp[w][capacity], gained + dp[w - 1][capacity - consumed]);
}
}
}
}
cout << dp[s][s] << '\n';
}
# | 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... |