#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define nline '\n'
#define lsb(x) ((x) & -(x))
#define int ll
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
using namespace std;
const int sz = (int)1e5 + 5;
const int inf = 2e9 + 5;
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
int powmod(int n, int m, int mod) {
int res = 1;
while (m) {
if (m & 1) res = 1ll * res * n % mod;
n = 1ll * n * n % mod;
m >>= 1;
}
return res;
}
struct Items {
int v, w;
};
void solve() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int s, n;
cin >> s >> n;
vector<int> v(n + 1), w(n + 1), k(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i] >> w[i] >> k[i];
vector<int> dp(s + 1, 0), ndp(s + 1, 0);
for (int i = 1; i <= n; i++) {
ndp = dp;
for (int r = 0; r < w[i]; r++) {
deque<pair<int,int>> q;
for (int t = 0; r + t * w[i] <= s; t++) {
int j = r + t * w[i];
int val = dp[j] - t * v[i];
// köhnə elementləri çıxar
while (!q.empty() && t - q.front().first > k[i]) q.pop_front();
// cavabı yenilə
if (!q.empty())
ndp[j] = max(ndp[j], q.front().second + t * v[i]);
// monotonicliyi saxla
while (!q.empty() && q.back().second <= val) q.pop_back();
q.push_back({t, val});
}
}
dp.swap(ndp);
}
cout << dp[s] << "\n";
}
signed main() {
fastio;
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| # | 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... |