# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
942297 | TrendBattles | Knapsack (NOI18_knapsack) | C++14 | 101 ms | 5700 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.
//https://oj.uz/problem/view/NOI18_knapsack
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;
const lli inf = 0x3f3f3f3f3f3f3f3f;
void maximise(lli& x, lli y) {
if (x < y) x = y;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, S; cin >> S >> n;
vector <vector <int>> value(S + 1);
for (int i = 1; i <= n; ++i) {
int v, w, k; cin >> v >> w >> k;
int pow_2 = 1;
while (k >= pow_2) {
if ((lli) w * pow_2 <= S) {
value[w * pow_2].push_back(v * pow_2);
}
k -= pow_2;
pow_2 <<= 1;
}
if (k > 0 and (lli) w * k <= S) {
value[w * k].push_back(v * k);
}
}
vector <lli> dp(S + 1);
for (int i = 1; i <= S; ++i) {
sort(value[i].begin(), value[i].end(), greater <int> ());
if (value[i].size() > S / i) {
value[i].erase(value[i].begin() + S / i, value[i].end());
}
for (int v : value[i]) {
for (int j = S; j >= i; --j) {
maximise(dp[j], dp[j - i] + v);
}
}
}
cout << dp[S];
return 0;
}
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... |