Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 2e3;
- struct edge {
- int u, c;
- vector<int> dp;
- edge(int _u, int _c) : u(_u), c(_c) {
- dp = vector<int>(101, -1);
- }
- };
- int n;
- vector<edge> G[MAX];
- int R[MAX];
- int solve(int v, int e, int c) {
- if(e >= G[v].size()) return c >= R[v];
- if(G[v][e].dp[c] != -1) return G[v][e].dp[c];
- int ans = 0;
- for(int k = 0; k <= c and k <= G[v][e].c; k++)
- ans = max(ans, solve(G[v][e].u, 0, k) + solve(v, e + 1, c - k));
- return G[v][e].dp[c] = ans;
- }
- int32_t main() {
- cin >> n;
- for(int i = 1; i <= n; i++) {
- int p, r, c;
- cin >> p >> r >> c;
- G[p].push_back(edge(i, c));
- R[i] = r;
- }
- int ans = 0;
- for(int i = 0; i < G[0].size(); i++)
- ans += solve(G[0][i].u, 0, G[0][i].c);
- cout << ans << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment