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;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define f first
#define ll long long
#define s second
#define pii pair<int,int>
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define is insert
const long long INF = 1e9;
const long long MOD = 1e9+7;
const int MAXN = 2e5;
//store test cases here
/*
15 5
4 12 1
2 1 1
10 4 1
1 1 1
2 2 1
20 3
5000 15 1
100 1 3
50 1 4
*/
int s,n;
//weight, value
vector<pii> ar;
//vallue, occurences
vector<pii> wg[MAXN];
//max value after considering first j items with total weight i
ll dp[2002][16000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>s>>n;
for(int i=0;i<n;i++){
int v,w,k;cin>>v>>w>>k;
wg[w].pb(mp(v,k));
}
for(int i=1;i<=s;i++){
sort(wg[i].begin(),wg[i].end());
reverse(wg[i].begin(),wg[i].end());
int ct=0;
int ix=0;
while(ct<s/i){
while(ix<wg[i].size()&&wg[i][ix].s==0){
ix++;
}
if(ix==wg[i].size()){
break;
}
ct++;
ar.pb(mp(i,wg[i][ix].f));
wg[i][ix].s--;
}
}
ll as=0;
for(int i=0;i<=ar.size();i++){
for(int j=0;j<=s;j++){
if(i){
dp[j][i]=max(dp[j][i],dp[j][i-1]);
}
if(i<ar.size()&&ar[i].f+j<=s){
dp[ar[i].f+j][i+1]=max(dp[ar[i].f+j][i+1],dp[j][i]+ar[i].s);
}
}
}
cout<<dp[s][ar.size()]<<'\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while(ix<wg[i].size()&&wg[i][ix].s==0){
| ~~^~~~~~~~~~~~~
knapsack.cpp:63: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]
63 | if(ix==wg[i].size()){
| ~~^~~~~~~~~~~~~~
knapsack.cpp:72: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]
72 | for(int i=0;i<=ar.size();i++){
| ~^~~~~~~~~~~
knapsack.cpp:77:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if(i<ar.size()&&ar[i].f+j<=s){
| ~^~~~~~~~~~
knapsack.cpp:71:8: warning: unused variable 'as' [-Wunused-variable]
71 | ll as=0;
| ^~
# | 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... |