#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int S, N;
cin >> S >> N;
vector<int> dp(S + 1, 0);
for (int i = 0; i < N; i++) {
int V, W, K;
cin >> V >> W >> K;
// Binary lifting optimization for large K values
vector<pair<int, int>> items; // {weight, value}
int power = 1;
while (power < K) {
items.push_back({W * power, V * power});
K -= power;
power *= 2;
}
if (K > 0) {
items.push_back({W * K, V * K});
}
// Process each binary representation item
for (auto& item : items) {
int weight = item.first;
int value = item.second;
// Standard 0/1 knapsack for each item (reverse iteration)
for (int w = S; w >= weight; w--) {
dp[w] = max(dp[w], dp[w - weight] + 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... |