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>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vp vector<pii>
using namespace std;
const int N=1e6+5;
ll dp[2][2005]{0};
deque<pll>dq[2005];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int s,n;cin>>s>>n;
ll a[n],b[n],c[n];
for(int i=0;i<n;i++)cin>>a[i]>>b[i]>>c[i];
int nw=0,pr=0;
for(int i=0;i<n;i++){
pr=nw;nw=1-nw;
for(int j=0;j<=s;j++){int id=(j)/b[i];
dp[nw][j]=dp[pr][j];
if(j-b[i]>=0){
while(!dq[j%b[i]].empty()&&dq[j%b[i]].back().f+min(id-dq[j%b[i]].back().s+1,c[i])*a[i]<=dp[pr][j-b[i]]+a[i])dq[j%b[i]].pop_back();
while(dq[j%b[i]].size()>1){
pll u=dq[j%b[i]][0];
pll v=dq[j%b[i]][1];
if(u.f+min(id-u.s+1,c[i])*a[i]<=v.f+min(id-v.s+1,c[i]))dq[j%b[i]].pop_front();
}
dq[j%b[i]].pb({dp[pr][j-b[i]],id});
}if(!dq[j%b[i]].empty())dp[nw][j]=max(dp[nw][j],dq[j%b[i]].front().f+min(id-dq[j%b[i]].front().s+1,c[i])*a[i]);
}
for(int j=0;j<b[i];j++)dq[j].clear();
}cout<<dp[nw][s];
}
# | 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... |