# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
724048 | Bodisha | Knapsack (NOI18_knapsack) | C++17 | 164 ms | 33060 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>
#define ll long long int
using namespace std;
map<int, vector<pair<int, int>>> by_weight;
int main() {
int s, n;
cin >> s >> n;
for(int i = 0; i < n; i++) {
int temp_v, temp_w, temp_k;
cin >> temp_v >> temp_w >> temp_k;
if(temp_w <= s && temp_k > 0) {
by_weight[temp_w].push_back({temp_v, temp_k});
}
}
ll dp[by_weight.size() + 1][s + 1];
for(int i = 0; i <= by_weight.size(); i++) {
for(int j = 0; j <= s; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 0;
int at = 1;
for(auto &[w, items] : by_weight) {
sort(items.begin(), items.end(), greater<pair<int, int>>());
for(int i = 0; i <= s; i++) {
dp[at][i] = dp[at - 1][i];
int curr_type = 0;
int used_weight = 0;
int copy_count = 0;
ll profit = 0;
while(used_weight + w <= i && curr_type < items.size()) {
copy_count++;
if(copy_count > items[curr_type].second) {
copy_count = 0;
curr_type++;
continue;
}
used_weight += w;
profit += items[curr_type].first;
dp[at][i] = max(dp[at][i], dp[at - 1][i - used_weight] + profit);
}
}
at++;
}
ll ans = 0;
for(int i = 0; i <= s; i++) {
ans = max(ans, dp[by_weight.size()][i]);
}
cout << 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... |