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;
using i64 = long long;
#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define pii pair<int, int>
#define fi first
#define se second
#define sz(x) (int)(x).size()
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int s, n, w, v, k, at = 1, copies, type_at, profit, cur_used;
cin >> s >> n;
map<int, vector<pii>> by_w;
for(int i = 0; i < n; i++){
cin >> w >> v >> k;
by_w[w].emplace_back(v, k);
}
vvi best(sz(by_w) + 1, vi(n + 1, INT32_MIN)); // best[i][j] -> using j weight and the first i weight types
best[0][0] = 0;
for(auto &[weight, items] : by_w){
sort(items.begin(), items.end(), greater<pii>());
for(int i = 0; i <= n; i++){
best[at][i] = best[at - 1][i];
while((copies + 1) * weight <= i && type_at < sz(items)){
copies++;
profit += items[type_at].fi;
if(best[at - 1][i - copies * weight] != INT32_MIN)
best[at][i] = max(best[at][i], best[at - 1][i - copies * weight] + profit);
cur_used++;
if(cur_used == items[type_at].se){
cur_used = 0;
type_at++;
}
}
}
at++;
}
cout << *max_element(best.back().begin(), best.back().end());
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:13: warning: 'cur_used' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | cur_used++;
| ~~~~~~~~^~
knapsack.cpp:31:12: warning: 'profit' may be used uninitialized in this function [-Wmaybe-uninitialized]
31 | profit += items[type_at].fi;
| ^
knapsack.cpp:37:13: warning: 'type_at' may be used uninitialized in this function [-Wmaybe-uninitialized]
37 | type_at++;
| ~~~~~~~^~
knapsack.cpp:29:18: warning: 'copies' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | while((copies + 1) * weight <= i && type_at < sz(items)){
| ~~~~~~~~^~~~
# | 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... |