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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int S, N;
cin >> S >> N;
vector<vector<pair<int, int>>> items(S + 1); // items grouped by weight
// Read input and group by weight
for (int i = 0; i < N; i++) {
int V, W, K;
cin >> V >> W >> K;
items[W].emplace_back(V, K);
}
vector<long long> dp(S + 1, 0);
for (int w = 1; w <= S; w++) {
if (!items[w].empty()) {
// Sort by value in descending order
sort(items[w].rbegin(), items[w].rend());
// Process the most valuable ⌊S/w⌋ items
int limit = S / w;
for (int j = 0; j < min((int)items[w].size(), limit); j++) {
int value = items[w][j].first;
int count = items[w][j].second;
for (int k = 1; count > 0 && k <= count; k *= 2) {
int use = min(k, count);
count -= use;
for (int s = S; s >= use * w; s--) {
dp[s] = max(dp[s], dp[s - use * w] + use * value);
}
}
}
}
}
cout << dp[S] << endl;
return 0;
}
# | 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... |