#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int s, n; cin >> s >> n;
vector<int> dp(s + 1, 0);
vector<pair<int,int>> items; // {weight, value}
for (int i = 0; i < n; ++i) {
int v, w, k; cin >> v >> w >> k;
k = min(k, s / w); // No more than what fits
for (int x = 1; k > 0; x *= 2) {
int take = min(x, k);
items.emplace_back(take * w, take * v);
k -= take;
}
}
// Standard 0-1 knapsack
for (auto [w, v] : items) {
for (int j = s; j >= w; --j) {
dp[j] = max(dp[j], dp[j - w] + v);
}
}
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... |