# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
689421 | tcmmichaelb139 | Knapsack (NOI18_knapsack) | C++17 | 1 ms | 468 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;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int s, n;
cin >> s >> n;
vector<vector<pair<long long, long long>>> v(s + 1);
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
if (c == 0 || b > s) continue;
v[b].push_back({a, c});
}
long long dp[s + 1];
for (int i = 0; i <= s; i++)
dp[i] = -1;
dp[0] = 0;
for (int i = 1; i <= s; i++) {
sort(rbegin(v[i]), rend(v[i]));
if (v[i].size() == 0) continue;
for (int j = s; j >= 0; j--) {
if (dp[j] == -1) continue;
long long cur = j;
long long num = 0;
long long done = 0;
long long val = 0;
while (cur <= s) {
if (v[i][num].second == done) num++, done = 0;
if (num == v[i].size()) break;
cur += i;
val += v[i][num].first;
done++;
dp[cur] = max(dp[cur], dp[j] + val);
}
}
}
cout << *max_element(dp, dp + s + 1) << '\n';
}
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... |