#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define int long long
#define pii pair<int, int>
using namespace std;
const int N = 5e5 + 5, M = 1e9 + 7;
int T, n, s, dp[2003], dp1[2003];
main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
cin >> s >> n;
int i, j, st;
for(i = 0; i < n; i++)
{
int v, w, k;
cin >> v >> w >> k;
for(st = s; st > max(-1ll, s - w); st--)
{
int cnt = 0, r = st, l = st, p = 0, c = 0;
deque<int> d;
while(r >= 0)
{
c = (st - l) / w;
if(l >= 0)
{
while(d.size() && d.back() < dp[l] + c * v)
d.pop_back();
d.push_back(dp[l] + c * v);
dp1[l] = dp[l] + c * v;
cnt++;
}
if(cnt == k + 1 || l < 0)
{
if(d.size()) dp[r] = max(dp[r], d.front() - p * v);
if(d.front() == dp1[r]) d.pop_front();
r -= w;
cnt--;
p++;
}
if(l >= 0) l -= w;
}
}
}
cout << dp[s] << '\n';
}
Compilation message (stderr)
knapsack.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main()
| ^~~~
# | 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... |