# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1053978 | ssitaram | Knapsack (NOI18_knapsack) | C++17 | 1096 ms | 2816 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;
typedef long long ll;
struct item {
ll v, w, k;
};
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
ll s, n; cin >> s >> n;
vector<item> v(n);
for (ll i = 0; i < n; ++i) {
cin >> v[i].v >> v[i].w >> v[i].k;
}
vector<ll> best(s + 1), valof(s + 1);
for (ll i = 0; i < n; ++i) {
vector<set<tuple<ll, ll, ll>>> prevs(v[i].w);
ll last = 3001;
for (ll j = 0; j <= s; ++j) {
if (!(j % v[i].w)) --last;
valof[j] = best[j] + v[i].v * last;
prevs[j % v[i].w].insert({valof[j], j, best[j]});
if (prevs[j % v[i].w].size() == v[i].k + 2) {
ll idx = j - v[i].w * (v[i].k + 1);
auto it = prevs[j % v[i].w].lower_bound({valof[idx], idx, 0});
prevs[j % v[i].w].erase(it);
}
auto it = prev(prevs[j % v[i].w].end());
best[j] = get<2>(*it) + (j - get<1>(*it)) / v[i].w * v[i].v;
}
}
cout << best.back() << '\n';
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... |