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 task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define vii vector <ii>
#define vi vector <int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
void solve();
int32_t main() {
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc = 1; // cin >> tc;
FOR(i, 1, tc) {
solve();
}
}
const int N = 2e3+5;
int s, n, dp[N];
vii item;
vi weight[N];
void solve() {
cin >> s >> n;
FOR(i, 1, n) {
int v, w, k; cin >> v >> w >> k;
FOR(j, 0, 30) if(k >= (1<<j)) {
k -= (1<<j);
int nww = min(1<<j, s/w)*w,
nwv = min(1<<j, s/w)*v;
item.eb(nww, nwv);
}
item.eb(min(k,s/w)*w, min(k,s/w)*v);
}
for(auto [w, v]:item) {
weight[w].eb(v);
}
FOR(i, 1, s) {
sort(all(weight[i]), greater <int>());
}
FOR(w, 1, s) {
FOR(v, 0, sz(weight[w])-1) {
if(v > s/w) break;
FORD(j, s, w) {
dp[j] = max(dp[j], dp[j-w] + weight[w][v]);
}
}
}
cout << dp[s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:26:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |