- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 54 lines of C++ from the credited upstream file abc402_e.cpp.
- The implementation visibly relies on sequence storage, cached states.
- 2 loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <algorithm>2#include <functional>3#include <iomanip>4#include <iostream>5#include <vector>6 7using namespace std;8 9struct State {10 int solved;11 int money;12};13 14int main() {15 int n, x;16 cin >> n >> x;17 18 vector<int> s(n), c(n), p(n);19 for (int i = 0; i < n; i++) {20 cin >> s[i] >> c[i] >> p[i];21 }22 23 vector<vector<double>> dp(1 << n, vector<double>(x + 1, -1));24 25 function<double(int, int)> solve = [&](int solved, int money) {26 if (dp[solved][money] >= 0) return dp[solved][money];27 28 double ret = 0;29 30 for (int i = 0; i < n; i++) {31 if (solved & (1 << i)) continue;32 33 if (money < c[i]) continue;34 35 double pp = p[i] / 100.0;36 37 double ev_success = solve(solved | (1 << i), money - c[i]) + s[i];38 double ev_failure = solve(solved, money - c[i]);39 40 double ev = pp * ev_success + (1 - pp) * ev_failure;41 42 ret = max(ret, ev);43 }44 45 return dp[solved][money] = ret;46 };47 48 double ans = solve(0, x);49 50 cout << fixed << setprecision(15) << ans << endl;51 52 return 0;53}54