#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
int S,n;
vector<pair<int,int>> we[2005];
vector<int> dp(2005,-1);
vector<pair<int,int>> items;
signed main(){
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>S>>n;
for(int i=1; i<=n; i++){
int v,w,k;
cin>>v>>w>>k;
we[w].push_back({v,k});
}
for(int i=1; i<=S; i++){
sort(we[i].begin(),we[i].end(),greater<pair<int,int>>());
int lim=S/i;
bool yes=0;
for(int j=0; j<we[i].size(); j++){
while(we[i][j].second--){
items.push_back({i,we[i][j].first});
lim--;
if(lim==0){
yes=1;
break;
}
}
if(yes) break;
}
}
// for(pair<int,int> i: items){
// cout<<i.first<<" "<<i.second<<endl;
// }
dp[0]=0;
for(int i=0; i<items.size(); i++){
for(int j=S; j>=0; j--){
if(dp[j]!=-1 and j+items[i].first<=S){
dp[j+items[i].first]=max(dp[j+items[i].first],dp[j]+items[i].second);
}
}
}
int ans=0;
for(int i=0; i<=S; i++){
ans=max(ans,dp[i]);
}
cout<<ans<<endl;
}
# | 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... |