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 <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
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)); }
using namespace std;
#ifdef imtiyazrasool92
#include "algos/debug.h"
#else
#define dbg(...) 92
#endif
int dp[100000][2001];
void run_case() {
int S, N;
cin >> S >> N;
// vi,wi,ki
vector<array<int, 3>> A(N);
for (auto &[x, y, z] : A)
cin >> x >> y >> z;
for (int i = 0; i < N; i++)
for (int s = 0; s <= S; s++)
dp[i][s] = -1;
cout << y_combinator([&](auto dfs, int index, int weight)->int {
if (weight == 0)
return 0;
if (index >= N)
return 0;
if (dp[index][weight] == -1) {
int answer = 0;
for (int i = 0; i <= min(2000, A[index][2]); i++) {
if (weight - (i * A[index][1]) >= 0)
answer = max(answer, dfs(index + 1, weight - (i * A[index][1])) + A[index][0] * i);
}
dp[index][weight] = answer;
}
return dp[index][weight];
})(0, S);
}
int32_t main() {
ios::sync_with_stdio(false);
#ifndef imtiyazrasool92
cin.tie(nullptr);
#endif
int tests = 1;
// cin >> tests;
while (tests--) {
run_case();
cout << '\n';
}
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... |