# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1020198 | ArthuroWich | Knapsack (NOI18_knapsack) | C++17 | 81 ms | 5212 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>
using namespace std;
#define int long long int
void solve() {
int s, n;
cin >> s >> n;
vector<int> dp(s+1, -1);
dp[0] = 0;
set<int> weights;
map<int, vector<pair<int, int>>> items;
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
weights.insert(w);
items[w].push_back({v, k});
}
for (int w : weights) {
sort(items[w].rbegin(), items[w].rend());
int total = 0;
for (int i = 0; i < items[w].size() && total <= 2000; i++) {
int l;
auto [v, k] = items[w][i];
total += k*w;
while(k > 0) {
l = log2(k);
if (l != 0) {
k -= (1<<l)-1;
} else {
k--;
l++;
}
for (int x = 0; x < l; x++) {
if ((1LL<<x)*w > 2000) {
break;
}
for (int j = s; j >= 0; j--) {
if (j-(1LL<<x)*w < 0) {
break;
}
if (dp[j-(1LL<<x)*w] != -1) {
dp[j] = max(dp[j], dp[j-(1LL<<x)*w]+(1LL<<x)*v);
}
}
}
}
}
}
int ans = 0;
for (int i : dp) {
ans = max(ans, i);
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
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... |