#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using vi = vector<int>;
#define all(x) (x).begin(), (x).end()
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
ll s, n;
cin >> s >> n;
vector<ll> v(n), w(n), k(n);
for (ll i = 0; i < n; ++i) {
cin >> v[i] >> w[i] >> k[i];
}
vector<vector<ll>> dp(2, vector<ll>(s + 1));
for (ll i = 0; i < n; ++i) {
for (ll r= 0; r < w[i]; ++r) {
queue<pair<ll, int>> q;
ll added = 0, removed = 0;
for (ll a = 0; a * w[i] + r <= s; ++a) {
while (!q.empty() && a - q.front().second > k[i]) {
q.pop();
removed++;
}
while (!q.empty() && q.front().first <= dp[(i + 1) % 2][a * w[i] + r] - v[i] * a) {
q.pop();
removed++;
}
q.push({dp[(i + 1) % 2][a * w[i] + r] - v[i] * a, a});
added++;
dp[i % 2][a * w[i] + r] = max(dp[i % 2][a * w[i] + r], q.front().first + a * v[i]);
}
}
}
cout << dp[(n + 1) % 2][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... |