#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int S, N;
cin >> S >> N;
vector<pair<int, int>> items; // {weight, value}
for (int i = 0; i < N; i++) {
int V, W, K;
cin >> V >> W >> K;
// Binary representation optimization
int cnt = 1;
while (cnt <= K) {
items.push_back({W * cnt, V * cnt});
K -= cnt;
cnt *= 2;
}
if (K > 0) {
items.push_back({W * K, V * K});
}
}
// Standard 0/1 knapsack with space optimization
vector<int> dp(S + 1, 0);
for (auto& [weight, value] : items) {
for (int w = S; w >= weight; w--) {
dp[w] = max(dp[w], dp[w - weight] + value);
}
}
cout << dp[S] << '\n';
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... |