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(false);
cin.tie(0);
int S, N;
cin >> S >> N;
vector<int> dp(S+1, 0);
map<int,vector<pair<int,int>>> m;
for (int i = 0; i < N; ++i) {
int v, w, k;
cin >> v >> w >> k;
m[w].push_back({v, k});
}
for (auto& it : m) {
sort(it.second.begin(), it.second.end());
reverse(it.second.begin(), it.second.end());
}
for (auto& it : m) {
int w = it.first;
for (int s = S; s >= 0; --s) {
int prev_k = 0;
int prev_v = 0;
for (auto& p : it.second) {
int v = p.first;
int k = p.second;
for (int j = 1; j <= k and (prev_k+j)*w <= s; ++j) {
dp[s] = max(dp[s], dp[s-(prev_k+j)*w] + prev_v + j*v);
}
if (k > S) break;
prev_k += k;
prev_v += k*v;
}
}
}
cout << dp[S] << endl;
}
# | 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... |