#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int S, N;
cin >> S >> N;
vector<long long> dp(S + 1, 0);
for (int i = 0; i < N; i++) {
long long V, W, K;
cin >> V >> W >> K;
if (K * W >= S) {
// Unbounded knapsack (много предметов, больше чем вместимость)
for (int w = W; w <= S; w++) {
dp[w] = max(dp[w], dp[w - W] + V);
}
} else {
// Binary splitting
long long cnt = 1;
while (K > 0) {
long long take = min(cnt, K);
long long val = take * V;
long long wt = take * W;
for (int w = S; w >= wt; w--) {
dp[w] = max(dp[w], dp[w - wt] + val);
}
K -= take;
cnt <<= 1;
}
}
}
cout << *max_element(dp.begin(), dp.end()) << "\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... |