이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int S, N;
cin >> S >> N;
vector<int> V(N + 1), W(N + 1), K(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> V[i] >> W[i] >> K[i];
}
const i64 inf = 1e18;
map<pair<int, int>, i64> memo;
auto solve = y_combinator([&](auto self, int sum, int n) -> i64 {
if (!n) {
return sum ? -inf : 0;
}
auto pr = make_pair(sum, n);
if (memo.count(pr)) {
return memo[pr];
}
i64 ret = 0;
for (int cnt = 0; sum - cnt * W[n] >= 0 && cnt <= K[n]; ++cnt) {
ret = max(ret, self(sum - cnt * W[n], n - 1) + cnt * V[n]);
}
return memo[pr] = ret;
});
i64 ans = 0;
for (int sum = 0; sum <= S; ++sum) {
ans = max(ans, solve(sum, N));
}
cout << ans << '\n';
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | 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... |