# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1053985 | ssitaram | Knapsack (NOI18_knapsack) | C++17 | 56 ms | 68176 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;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
ll s, n; cin >> s >> n;
vector<vector<ll>> items(s + 1);
vector<tuple<ll, ll, ll>> init(n);
for (ll i = 0; i < n; ++i) {
cin >> get<0>(init[i]) >> get<1>(init[i]) >> get<2>(init[i]);
}
sort(init.rbegin(), init.rend());
for (ll i = 0; i < n; ++i) {
ll v = get<0>(init[i]);
ll w = get<1>(init[i]);
ll k = get<2>(init[i]);
while (items[w].size() <= s && k > 0) {
items[w].push_back(v);
--k;
}
}
vector<vector<ll>> best(s + 1, vector<ll>(s + 1));
for (ll i = 1; i <= s; ++i) {
for (ll j = 0; j <= s; ++j) {
best[i][j] = best[i - 1][j];
ll amnt = 0;
for (ll k = 0; k < items[i].size(); ++k) {
amnt += items[i][k];
if (j - (k + 1) * i < 0) break;
best[i][j] = max(best[i][j], best[i - 1][j - (k + 1) * i] + amnt);
}
}
}
cout << best[s][s] << '\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... |