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>
#define ll long long
#define ull unsigned long long
#define bip(i) __builtin_popcount(i)
#define gb(i,j) ((i>>j)&1)
#define se second
#define fi first
using namespace std;
const int N=2005;
int n , S;
ll f[2][N] , res;
vector<pair<int,int>>vec[N] , prc;
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> S >> n;
for( int i=1 ; i<=n ; i++ ){
int v , w , fre;
cin >> v >> w >> fre;
vec[w].push_back({v , fre});
}
prc.push_back({0,0});
for( int i=1 ; i<=S ; i++ ) // weight
if(vec[i].size()){
sort(vec[i].begin() , vec[i].end() , greater<pair<int,int>>() );
for( auto [val , fr]: vec[i] ){
int tmp=0 , ch=0;
for( int k=1 ; k<=fr ; k++ ){
tmp+=i;
if(tmp<=S)
prc.push_back({val , i});
else{
ch=1;
break;
}
}
if(ch)break;
}
}
for( int i=1 ; i<prc.size() ; i++ ){
int val = prc[i].fi , wei = prc[i].se;
for( int j=S ; j>=0 ; j-- )f[i%2][j]=f[(i-1)%2][j]; // truong hop ko chon i
for( int j=S ; j>=0 ; j-- )if(j>=wei){
f[i%2][j] = max( f[i%2][j] , f[(i-1)%2][j-wei]+val ); // truong hop chon i
res = max( res , f[i%2][j] );
}else break;
}
cout << res;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:42: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]
42 | for( int i=1 ; i<prc.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... |