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() {
int s, n;
cin >> s >> n;
vector<int64_t> dp(s+1, 0);
vector<int> div(s+1);
if (n == 1) {
int64_t v, w, k;
cin >> v >> w >> k;
int times = min(s / w, k);
cout << v * times << "\n";
return 0;
}
// calculate div
for (int i = 1; i <= s; i++) {
div[i] = s / i;
}
vector<vector<int>> value(s+1, vector<int>());
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
int m = min(div[w], k);
for (int j = 0; j < m; j++) {
value[w].push_back(v);
}
}
for (int i = 1; i <= s; i++) {
sort(value[i].begin(), value[i].end(), greater<int>());
div[i] = min(div[i], (int)value[i].size());
}
for (int i = s; i > 0; i--) {
for (int j = 0; j < div[i]; j++) {
for (int k = s; k >= i; k--) {
if (dp[k] < dp[k - i] + value[i][j]) {
dp[k] = max(dp[k], dp[k - i] + value[i][j]);
}
}
}
}
cout << dp[s] << "\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... |