This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* Plat on the Open.
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define pb push_back
using T = pair<int, int>;
void solve() {
int s, n;
cin >> s >> n;
vector<int> v(n), w(n), k(n);
vector<vector<T>> buckets(s + 1);
for (int i = 0; i < n; i++) {
cin >> v[i] >> w[i] >> k[i];
buckets[w[i]].pb({v[i], k[i]});
}
vector<vector<T>> items(s + 1);
for (int i = 1; i <= s; i++) sort(all(buckets[i])), reverse(all(buckets[i]));
for (int i = 1; i <= s; i++) {
int sz = buckets[i].size();
vector<int> psums_vals(sz + 1), psums_items(sz + 1);
for (int j = 1; j <= sz; j++) {
auto [x, y] = buckets[i][j - 1];
psums_vals[j] = psums_vals[j - 1] + (x * y);
psums_items[j] = psums_items[j - 1] + y;
}
for (int j = i; j <= min(i * psums_items[sz], s); j += i) {
int needed_items = j / i;
int lo = 0, hi = sz, ind = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (psums_items[mid] <= needed_items) {
ind = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
if (ind != -1) {
int x = (ind == sz) ? 0 : buckets[i][ind].f;
int val = psums_vals[ind] + ((needed_items - psums_items[ind]) * x);
items[i].pb(make_pair(j, val));
} else {
assert(0);
int val = needed_items * buckets[i][0].f;
items[i].pb(make_pair(j, val));
}
}
}
vector<int> dp(s + 1, 0), ndp(s + 1, 0);
for (int i = 1; i <= s; i++) {
for (int j = 0; j <= s; j++) {
ndp[j] = dp[j];
if (j >= 1) ndp[j] = max(ndp[j], ndp[j - 1]);
for (auto& [weight, val] : items[i]) {
if (j >= weight) {
ndp[j] = max(ndp[j], dp[j - weight] + val);
}
}
}
swap(ndp, dp);
}
cout << dp[s] << '\n';
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
}
# | 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... |