# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884467 | cabbit | Knapsack (NOI18_knapsack) | C++14 | 105 ms | 8784 KiB |
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 fi first
#define se second
#define sz(v) (int)(v).size()
using namespace std;
const int MXn = 100005, MXs = 2e3 + 6;
const long long oo = 1e18;
int S, N;
vector<int> obj[MXs];
int dp[MXs];
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
#define TASK "KNAPSACK"
if (ifstream(TASK".INP")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
cin >> S >> N;
for (int i = 1, v, w, k; i <= N; i++) {
cin >> v >> w >> k;
k = min(k, S / w);
int tmp = 0;
for (int t = 0; ; t++) {
if (tmp + (1 << t) <= k) {
tmp += (1 << t);
obj[w << t].emplace_back(v << t);
} else break;
}
if (k > tmp) obj[w * (k - tmp)].emplace_back(v * (k - tmp));
}
fill(dp + 1, dp + S + 1, -oo);
dp[0] = 0;
for (int w = 1; w <= S; w++) {
sort(obj[w].rbegin(), obj[w].rend());
for (int t = 0; t < min(S / w, sz(obj[w])); t++) {
for (int wei = S; wei >= w; wei--) {
dp[wei] = max(dp[wei], dp[wei - w] + obj[w][t]);
}
}
}
cout << *max_element(dp, dp + S + 1);
return 0;
}
// Revive! >:)
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... |