#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
// 2 ^ 10 ~= 10 ^ 3 => 2 ^ 30 > 10 ^ 9
const int MAX_N = 1e5, MAX_S = 2e3, MAX_K = 1e9, MAX_K_BIT = 30;
int v[MAX_N + 1], wi[MAX_N + 1], k[MAX_N + 1];
vector<pair<int, int>> items; // (value, weight)
map<int, vector<pair<int, int>>> w_groups;
int dp[MAX_S + 1];
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int s, n, i, j, w, new_w, put2, num_obj, max_objs;
cin >> s >> n;
for (i = 1; i <= n; i++) {
cin >> v[i] >> wi[i] >> k[i];
w_groups[wi[i]].push_back(make_pair(v[i], k[i]));
}
for (auto [group_w, group_items] : w_groups) {
sort(group_items.begin(), group_items.end(), greater<>());
max_objs = s / group_w;
num_obj = 0;
i = 0;
while (i < group_items.size() && (num_obj + group_items[i].second) <= max_objs) {
num_obj += group_items[i].second;
for (j = 1; j <= group_items[i].second; j++) {
items.push_back(make_pair(group_items[i].first, group_w));
}
i++;
}
if (i < group_items.size() && num_obj < max_objs) {
for (j = 1; j <= (max_objs - num_obj); j++) {
items.push_back(make_pair(group_items[i].first, group_w));
}
}
}
// bounded
for (auto [item_v, item_w] : items) {
// cout << "[" << item_w << ", " << item_v << "]\n";
for (w = s; w >= 0; w--) {
new_w = w + item_w;
if (new_w <= s) {
dp[new_w] = max(dp[new_w], dp[w] + item_v);
}
}
}
cout << dp[s];
return 0;
}
# | 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... |