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<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second
ll knapsack(vector<pair<int,int>>arr,int s){ // (weight, reward)
vector<vector<ll>>dp(arr.size()+1,vector<ll>(s+1)); // dp[i][j] = if we take first i elements, how much can we take with j space left
for(int i=1;i<=arr.size();i++){
for(int j=0;j<=s;j++){
dp[i][j]=dp[i-1][j];
if(j-arr[i-1].first>=0){
dp[i][j]=max(dp[i][j],dp[i-1][j-arr[i-1].first]+arr[i-1].second);
}
}
}
return dp[(int)arr.size()][s];
}
void solve(){
int s,n;
cin>>s>>n;
vector<vector<pair<int,int>>>arr(2001);
for(int i=0;i<n;i++){
int w,r,c;
cin>>r>>w>>c;
arr[w].emplace_back(r,c);
}
vector<pair<int,int>>arr2;
for(int i=1;i<2001;i++){
sort(arr[i].begin(),arr[i].end());
reverse(arr[i].begin(),arr[i].end());
int rem=s/i;
for(auto j:arr[i]){
while(j.second&&rem){
arr2.emplace_back(i,j.first);
j.second--;
rem--;
}
if(rem==0)
break;
}
}
cout<<knapsack(arr2,s)<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
15 5
4 12 1
2 1 1
10 4 1
1 1 1
2 2 1
15
20 3
5000 15 1
100 1 3
50 1 4
5400
*/
Compilation message (stderr)
knapsack.cpp: In function 'll knapsack(std::vector<std::pair<int, int> >, int)':
knapsack.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=1;i<=arr.size();i++){
| ~^~~~~~~~~~~~
# | 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... |