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 > p83 e p199
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("03")
typedef long long ll;
const int MAXS = 2000;
const int MAXLOG = 12;
ll dp[2][MAXS+1];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int S, n; cin >> S >> n;
vector<ll> val(n);
vector<int> w(n);
vector<int> K(n);
vector<pair<int, ll>> v; // peso, valor
v.reserve(n*MAXLOG);
int tmpn=0;
map<int, vector<pair<ll,int>>> by_w;
for (int i=0; i<n; i++) {
cin >> val[i] >> w[i] >> K[i];
K[i] = min(K[i], (S/w[i]));
by_w[w[i]].push_back({-val[i], i});
}
for (pair<int,vector<pair<ll,int>>> eq_w : by_w) {
int limit = S / eq_w.first;
sort(eq_w.second.begin(), eq_w.second.end());
for (pair<int,int> idx : eq_w.second) {
int i = idx.second;
K[i] = min(K[i], limit);
limit -= K[i];
for (int tmp=0; tmp<K[i]; tmp++) {
v.push_back({w[i], val[i]});
tmpn++;
}
// if (K[i] == 0) continue;
// bool fi = true;
// int other = K[i];
// for (int b=MAXLOG; b>=0; b--) {
// if (fi and ((1 << b) & K[i]) != 0) {
// fi = false;
// other -= ((1 << b) - 1);
// } else if (!fi) {
// tmpn++;
// v.push_back({(1 << b) * w[i], (1LL << b) * val[i]});
// }
// }
// tmpn++;
// v.push_back({other * w[i], other * val[i]});
}
}
for (int i=1; i<=tmpn; i++) {
for (int P=1; P<v[i-1].first; P++) {
dp[i&1][P] = max(dp[(i&1)^1][P], dp[i&1][P-1]);
}
for (int P=v[i-1].first; P<=S; P++) {
dp[i&1][P] = max({
dp[(i&1)^1][P-v[i-1].first] + v[i-1].second,
dp[(i&1)^1][P],
dp[i&1][P-1]
});
}
}
cout << dp[tmpn&1][S] << endl;
return 0;
}
# | 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... |