# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
679654 | nicolexxuu | Knapsack (NOI18_knapsack) | C++17 | 1 ms | 212 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;
int main() {
int S, N;
cin >> S >> N;
map<int, vector<pair<int, int>>> by_weight;
for(int t = 0; t < N; t++) {
int V, W, K;
cin >> V >> W >> K;
by_weight[W].push_back({V, K});
}
vector<vector<int>> dp(by_weight.size()+1, vector<int>(S+1));
int res = 0;
int i = 1;
for(auto& hi : by_weight) {
auto w = hi.first;
auto items = hi.second;
for(int s = 1; s <= S; s++) {
dp[i][s] = dp[i-1][s];
int curr_i = 0, curr_cnt = 0, val = 0;
for(int cnt = 1; cnt * w <= s && curr_i < items.size(); cnt++) {
val += items[curr_i].first;
if(++curr_cnt == items[curr_i].second) {
curr_cnt = 0;
curr_i++;
}
dp[i][s] = max(dp[i][s], dp[i-1][s-cnt*w] + val);
}
}
res = max(dp[i][S], res);
i++;
}
cout << res << endl;
}
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... |