#include<bits/stdc++.h>
#define ll int
#define F first
#define S second
#define pb push_back
#define mpr make_pair
const ll N=2e5 + 10 , mod=1e9 + 7;
const double eps=1e-6;
using namespace std;
ll n,m,a,b,c,s,q,x,k;
ll t[N];
string ch;
vector<vector<ll>>v;
ll res=0;
int main(){
ios_base::sync_with_stdio (0); cin.tie(0),cout.tie(0);
// freopen("time.in","r",stdin);
// freopen("time.out","w",stdout);
ll T=1;
//cin>>T;
while(T--){
cin>>s>>n;
map<ll,vector<pair<ll,ll>>>mp;
for(ll i=0;i<n;i++){
ll x,y,z;
cin>>x>>y>>z;
mp[y].pb(make_pair(x,z));
}
n=mp.size();
vector<vector<ll>>dp(n+5,vector<ll>(s+5));
dp[0][0]=0;
ll x=1;
for(auto [w,v]:mp){
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
for(ll j=0;j<=s;j++){
dp[x][j]=dp[x-1][j];
ll c=0,s=0,i=0,tc=0;
while((tc+1)*w<=j && i<v.size()){
c++;s+=v[i].F;tc++;
dp[x][j]=max(dp[x][j],dp[x-1][j-w]+s);
if(c==v[i].S){
i++;
c=0;
}
}
// cout<<w<<" "<<v.top().F<<"\n";
}
x++;
}
cout<<dp[n][s]<<"\n";
}
return 0;
}
/*
20 3
5000 15 1
100 1 3
50 2 4
*/
# | 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... |