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> V, W;
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){
V.pb(pow2 * v);
W.pb(pow2 * w);
k -= pow2, pow2 *= 2;
}
if(k > 0 && k * w <= s){
V.pb(k * v);
W.pb(k * w);
}
}
int ans = 0, sum = 0;
for(int i = 0; i < W.size(); i++){
for(int j = min(s, sum); j >= 0; j--){
dp[j + W[i]] = max(dp[j + W[i]], dp[j] + V[i]);
ans = max(ans, dp[j]);
sum += W[i];
}
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:31: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]
31 | 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... |