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 sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define lli long long
#define mod 1000000007
#define mod2 998244353
#define si size()
#define dou double
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
/*void init_code(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}*/
int main(){
fast();
//init_code();
int s,n;
cin>>s>>n;
long long cnt[s+5]={0};
//long long lol[s+5]={0};
int memo[s+5]={0};
memo[0]=1;
vector<pair<long long,long long>> time[s+5];
for(int i=1;i<=n;i++){
long long int v,w,k;
cin>>v>>w>>k;
/*for(int j=s;j>=0;j--){
if(memo[j]){
if(j+w<=s){
cnt[j+w]=max(cnt[j]+v,cnt[j+w]);
memo[j+w]=1;
}
}
}
k--;*/
k=min(s/w+1,k);
time[w].push_back({v,k});
}
for(int i=1;i<=s;i++){
sort(time[i].begin(),time[i].end());
vector<pair<ll,ll>> v=time[i];
int count=s/i;
while(!v.empty() && count>0){
int lol=v.back().second;
lol=min(count,lol);
long long val=v.back().first;
for(int j=0;j<lol;j++){
for(int k=s-i;k>=0;k--){
if(memo[k]){
cnt[k+i]=max(cnt[k]+val,cnt[k+i]);
memo[k+i]=1;
}
}
}
v.pop_back();
count-=lol;
}
}
long long ans=0;
for(int i=0;i<=s;i++){
ans=max(cnt[i],ans);
}
cout<<ans<<"\n";
}
# | 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... |