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;
struct node {
long long v, w, k;
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long s, n;
cin >> s >> n;
vector<node> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].v >> v[i].w >> v[i].k;
sort(begin(v), end(v), [](node a, node b) {
if (a.v == b.v) return a.w < b.w;
return a.v > b.v;
});
long long dp[n + 1][s + 1];
memset(dp, 0, sizeof dp);
long long ans = 0;
for (int i = 0; i < n; i++) {
for (long long j = s; j >= 0; j--) {
long long num = min(v[i].k, (s - j) / v[i].w);
dp[i + 1][j + v[i].w * num] = max(dp[i + 1][j + v[i].w * num], dp[i][j] + num * v[i].v);
ans = max(ans, dp[i + 1][j + v[i].w * num]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
ans = max(ans, dp[i + 1][j]);
if (j + v[i].w <= s) {
dp[i + 1][j + v[i].w] = max(dp[i + 1][j + v[i].w], dp[i][j] + v[i].v);
ans = max(ans, dp[i + 1][j + v[i].w]);
}
}
}
cout << ans << '\n';
}
# | 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... |