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 f first
#define s second
using namespace std;
const int MAX = 2010;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int S,N; cin >> S >> N;
vector<pair<int,int>> mp[MAX];
for(int i = 0;i<N;++i){
int v,w,k; cin >> v >> w >> k;
mp[w].push_back({v,min(k,S/w)});
}
vector<int> dp(S+1,0);
for(int i = 1;i<=S;++i){
vector<pair<int,int>> vec = mp[i];
sort(vec.begin(),vec.end(),greater<pair<int,int>>());
for(int j = 0;j<vec.size();++j){
int v = vec[j].f, k = vec[j].s, cnt = 0;
while(k--){
for(int l = S;l>=i;l--){
dp[l] = max(dp[l],dp[l-i]+v);
}
++cnt;
if(cnt*i >= S) break;
}
if(cnt*i >= S) break;
}
}
cout << dp[S] << "\n";
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:19:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int j = 0;j<vec.size();++j){
| ~^~~~~~~~~~~
# | 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... |