#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> matrix;
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define down(i, b, a) for(ll i = b - 1; i >= a; i--)
struct info{
ll v, w, k;
info(){}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll s, n;
cin >> s >> n;
vector<info> a(n);
rep(i, 0, n){
cin >> a[i].v >> a[i].w >> a[i].k;
}
vi dp(s + 1, 0);
vi best(s + 1, 0);
rep(i, 0, n){
vector<deque<ll>> q(a[i].w, deque<ll>());
rep(j, 0, s + 1){
ll curr = j / a[i].w;
ll mod = j % a[i].w;
while(!q[mod].empty() && curr - q[mod].front() > a[i].k){
q[mod].pop_front();
}
while(!q[mod].empty() && dp[q[mod].back() * a[i].w + mod] + (curr - q[mod].back()) * a[i].v <= dp[j]){
q[mod].pop_back();
}
q[mod].push_back(curr);
ll f = q[mod].front();
best[j] = (curr - f) * a[i].v + dp[f * a[i].w + mod];
}
rep(j, 0, s + 1){
dp[j] = max(dp[j], best[j]);
}
}
cout << dp.back() << endl;
}
| # | 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... |