# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
873333 | Squar_Head | Knapsack (NOI18_knapsack) | C++14 | 0 ms | 0 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.
int main() {
int n, x;
cin >> n >> x;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) cin >> c[i];
auto cost = [&] (int i, int j, int k) {
return dp[i - 1][k] + (j - k) / a[i] * b[i];
};
for (int i = 1; i <= n; i++) {
for (int mod = 0; mod < a[i]; mod++) {
deque<int> dq;
for (int j = mod; j <= x; j += a[i]) {
while (dq.size() && cost(i, j, j) >= cost(i, j, dq.back())) dq.pop_back();
dq.push_back(j);
while (dq.size() && dq.front() < max(0ll, j - c[i] * a[i])) dq.pop_front();
dp[i][j] = cost(i, j, dq.front());
}
}
}
cout << dp[n][x];
return 0;
}