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 int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define task "1e5 + 5"
using namespace std;
const int N = 1e5 + 5;
int s, n, dp[2005];
vector<int> a[2005], W, V;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
cin >> s >> n;
for(int i = 1; i <= n; i++){
int v, w, k; cin >> v >> w >> k;
int pow2 = 1;
while(k >= pow2 && pow2 * w <= s){
a[pow2 * w].pb(pow2 * v);
k -= pow2, pow2 *= 2;
}
if(k > 0 && k * w <= s){
a[k * w].pb(k * v);
}
}
for(int i = 1; i <= s; i++){
sort(a[i].begin(), a[i].end(), [](int x, int y){return x > y;});
while(!a[i].empty() && s / i < a[i].size()) a[i].pop_back();
for(int x : a[i]){
W.pb(i);
V.pb(x);
}
}
int ans = 0;
for(int i = 0; i < W.size(); i++){
for(int j = s; j >= W[i]; j--){
dp[j] = max(dp[j], dp[j - W[i]] + V[i]);
ans = max(ans, dp[j]);
}
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:30:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | while(!a[i].empty() && s / i < a[i].size()) a[i].pop_back();
| ~~~~~~^~~~~~~~~~~~~
knapsack.cpp:37:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0; i < W.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... |