This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define II pair < int , int >
#define pb push_back
#define mset(a , b) memset(a , b , sizeof a)
#define all(a) (a).begin() , (a).end()
const int N = 1e5 + 5;
int s, n, v[N], w[N], k[N], dp[2][N];
deque<II> dq;
void input()
{
cin >> s >> n;
for(int i = 1; i <= n; ++i) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], s);
}
}
void solve()
{
for(int curS = 0; curS <= s; ++curS) dp[0][curS] = -1;
dp[0][0] = 0;
int res = 0;
for(int i = 1; i <= n; ++i) {
int id = i % 2;
for(int r = 0; r < w[i]; ++r) {
while(!dq.empty()) dq.pop_back();
for(int curS = r; curS <= s; curS += w[i]) {
if(dp[1 - id][curS] >= 0) {
II curV = {dp[1 - id][curS] - (curS / w[i]) * v[i], curS};
while(!dq.empty() && dq.front().fi <= curV.fi) dq.pop_front();
dq.push_front(curV);
}
while(!dq.empty() && dq.back().se < curS - k[i] * w[i]) dq.pop_back();
if(!dq.empty()) {
int preS = dq.back().se;
dp[id][curS] = dp[1 - id][preS] + v[i] * (curS - preS) / w[i];
}
else {
dp[id][curS] = -1;
}
res = max(res, dp[id][curS]);
}
}
}
cout << res;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
input();
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... |