Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int maxProfit(int n, vector<int>& present, vector<int>& future, vector<vector<int>>& hierarchy, int budget) {8 vector<vector<int>> adj(n);9 const auto& iter_dfs = [&]() {10 using RET = vector<unordered_map<int, int>>;11 RET ret(2);12 vector<tuple<int, int, int, shared_ptr<RET>, RET *>> stk = {{1, 0, -1, nullptr, &ret}};13 while (!empty(stk)) {14 const auto [step, u, i, new_ret, ret] = stk.back(); stk.pop_back();15 if (step == 1) {16 (*ret)[0][0] = (*ret)[1][0] = 0;17 stk.emplace_back(4, u, -1, nullptr, ret);18 stk.emplace_back(2, u, 0, nullptr, ret);19 } else if (step == 2) {20 if (i == size(adj[u])) {21 continue;22 }23 const auto& v = adj[u][i];24 stk.emplace_back(2, u, i + 1, nullptr, ret);25 const auto& new_ret = make_shared<RET>(2);26 stk.emplace_back(3, -1, -1, new_ret, ret);27 stk.emplace_back(1, v, -1, nullptr, new_ret.get());28 } else if (step == 3) {29 for (int i = 0; i < 2; ++i) {30 unordered_map<int, int> copy_dp((*ret)[i]);31 for (const auto& [j1, v1] : copy_dp) {32 for (const auto& [j2, v2] : (*new_ret)[i]) {33 if (j1 + j2 <= budget) {34 (*ret)[i][j1 + j2] = max((*ret)[i][j1 + j2], v1 + v2);35 }36 }37 }38 }39 } else if (step == 4) {40 RET new_ret(2);41 for (int i = 0; i < 2; ++i) {42 for (const auto& [j, v] : (*ret)[0]) {43 new_ret[i][j] = max(new_ret[i][j], v);44 }45 const int cost = present[u] >> i;46 if (cost > budget) {47 continue;48 }49 const int profit = future[u] - cost;50 for (const auto& [j, v] : (*ret)[1]) {51 if (j + cost <= budget) {52 new_ret[i][j + cost] = max(new_ret[i][j + cost], v + profit);53 }54 }55 }56 *ret = move(new_ret);57 }58 }59 int result = 0;60 for (const auto& [_, v] : ret[0]) {61 result = max(result, v);62 }63 return result;64 };65 66 for (const auto& h: hierarchy) {67 adj[h[0] - 1].emplace_back(h[1] - 1);68 }69 return iter_dfs();70 }71};72 73747576class Solution2 {77public:78 int maxProfit(int n, vector<int>& present, vector<int>& future, vector<vector<int>>& hierarchy, int budget) {79 vector<vector<int>> adj(n);80 const function<vector<unordered_map<int, int>> (int)> dfs = [&](int u) {81 vector<unordered_map<int, int>> dp(2);82 dp[0][0] = dp[1][0] = 0;83 for (const auto& v : adj[u]) {84 const auto& new_dp = dfs(v);85 for (int i = 0; i < 2; ++i) {86 unordered_map<int, int> copy_dp(dp[i]);87 for (const auto& [j1, v1] : copy_dp) {88 for (const auto& [j2, v2] : new_dp[i]) {89 if (j1 + j2 <= budget) {90 dp[i][j1 + j2] = max(dp[i][j1 + j2], v1 + v2);91 }92 }93 }94 }95 }96 vector<unordered_map<int, int>> result(2);97 for (int i = 0; i < 2; ++i) {98 for (const auto& [j, v] : dp[0]) {99 result[i][j] = max(result[i][j], v);100 }101 const int cost = present[u] >> i;102 if (cost > budget) {103 continue;104 }105 const int profit = future[u] - cost;106 for (const auto& [j, v] : dp[1]) {107 if (j + cost <= budget) {108 result[i][j + cost] = max(result[i][j + cost], v + profit);109 }110 }111 }112 return result; 113 };114 115 for (const auto& h: hierarchy) {116 adj[h[0] - 1].emplace_back(h[1] - 1);117 }118 const auto& ret = dfs(0);119 int result = 0;120 for (const auto& [_, v] : ret[0]) {121 result = max(result, v);122 }123 return result;124 }125};126