이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pli = pair<lint, int>;
const int MAX_N = 1e5 + 5, MAX_S = 2000 + 5;
int s, n;
lint v[MAX_N];
int w[MAX_N], k[MAX_N];
lint dp[2][MAX_S];
deque<pli> trans[MAX_S];
int main() {
// freopen("knapsack.in", "r", stdin);
cin >> s >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], s);
}
for (int i = n; i >= 1; i--) {
int parity = i % 2, opp_parity = (i + 1) % 2;
for (int c = 0; c <= s; c++) {
if (c < w[i]) {
while (trans[c].size())
trans[c].pop_back();
trans[c].push_back({dp[opp_parity][c], c});
dp[parity][c] = dp[opp_parity][c];
} else {
lint val = dp[opp_parity][c] - v[i] * (c / w[i]);
while (trans[c % w[i]].size() && trans[c % w[i]].back().first <= val)
trans[c % w[i]].pop_back();
trans[c % w[i]].push_back({val, c});
while (trans[c % w[i]].front().second < c - k[i] * w[i])
trans[c % w[i]].pop_front();
int new_c = trans[c % w[i]].front().second;
dp[parity][c] = dp[opp_parity][new_c] + v[i] * ((c - new_c) / w[i]);
}
// cout << i << " " << c << ": " << dp[parity][c] << endl;
}
}
cout << dp[1][s] << '\n';
}
# | 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... |