# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1265791 | mingga | Knapsack (NOI18_knapsack) | C++20 | 28 ms | 1608 KiB |
// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
int dp[2005];
int n, s;
vector<pair<int, int>> d[2005];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
vector<int> W, V;
cin >> s >> n;
for(int i = 1; i <= n; i++) {
int v, w, k; cin >> v >> w >> k;
k = min(k, s / w);
d[w].pb({-v, k});
}
for(int i = 1; i <= s; i++) {
sort(all(d[i]));
int lim = s / i;
for(auto [v, k] : d[i]) {
if(lim == 0) break;
v = -v;
k = min(lim, k);
lim -= k;
int cnt = 1;
while(1) {
if(k < cnt) break;
W.pb(cnt * i);
V.pb(v * cnt);
k -= cnt;
cnt *= 2;
}
if(k > 0) {
W.pb(k * i);
V.pb(k * v);
}
}
}
for(int i = 0; i < sz(W); i++) {
for(int j = s; j >= W[i]; j--) {
dp[j] = max(dp[j], dp[j - W[i]] + V[i]);
}
}
cout << * max_element(dp, dp + s + 1) << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
# | 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... |