#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = -1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int S, N;
cin >> S >> N;
vector<ll> dp(S + 1, 0);
for (int i = 0; i < N; i++) {
int v, w, k;
cin >> v >> w >> k;
for (int r = 0; r < w; r++) {
deque<pair<ll,ll>> dq; // {dp value, index}
for (int j = 0; j * w + r <= S; j++) {
ll idx = j * w + r;
ll val = dp[idx] - j * v;
while (!dq.empty() && dq.back().first <= val) dq.pop_back();
dq.push_back({val, j});
while (!dq.empty() && j - dq.front().second > k) dq.pop_front();
dp[idx] = dq.front().first + j * 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... |