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;
#define int long long
int n, s, v[100005], w[100005], k[100005], a[2005];
vector<vector<pair<int,int>>> sv(2001, vector<pair<int,int>>());
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> s >> n;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> k[i];
sv[w[i]].push_back({v[i], k[i]});
}
for(int i = 1; i <= s; i++){
sort(sv[i].begin(), sv[i].end(), greater<pair<int,int>>());
int st = 0;
for(int j = 1; j <= s/i; j++){
if(st >= sv[i].size())break;
for(int k = s; k >= i; k--){
a[k] = max(a[k], a[k - i] + sv[i][st].first);
}
sv[i][st].second -=1;
if(sv[i][st].second==0)st++;
}
}
cout << a[s];
// for(int i = 0; i <)
// for(int i = 1; i <= n; i++){
// for(int j = s; j >= w[i]; j--){
// // a[j] = min(a[j], a[j])
// for(int o = min(k[i], j/w[i]); o > 0; o--){
// a[j] = max(a[j], a[j-w[i]*o] + v[i]*o);
// }
// }
// }
// cout << a[s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:17:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if(st >= sv[i].size())break;
| ~~~^~~~~~~~~~~~~~~
# | 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... |