matheus_monteiro

Juice

Aug 17th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 2e3;
  4.  
  5. struct edge {
  6.     int u, c;
  7.     vector<int> dp;
  8.     edge(int _u, int _c) : u(_u), c(_c) {
  9.         dp = vector<int>(101, -1);
  10.     }
  11. };
  12.  
  13. int n;
  14. vector<edge> G[MAX];
  15. int R[MAX];
  16.  
  17. int solve(int v, int e, int c) {
  18.     if(e >= G[v].size()) return c >= R[v];
  19.     if(G[v][e].dp[c] != -1) return G[v][e].dp[c];
  20.     int ans = 0;
  21.     for(int k = 0; k <= c and k <= G[v][e].c; k++)
  22.         ans = max(ans, solve(G[v][e].u, 0, k) + solve(v, e + 1, c - k));
  23.     return G[v][e].dp[c] = ans;
  24. }
  25.  
  26. int32_t main() {
  27.  
  28.     cin >> n;
  29.     for(int i = 1; i <= n; i++) {
  30.         int p, r, c;
  31.         cin >> p >> r >> c;
  32.         G[p].push_back(edge(i, c));
  33.         R[i] = r;
  34.     }
  35.     int ans = 0;
  36.     for(int i = 0; i < G[0].size(); i++)
  37.         ans += solve(G[0][i].u, 0, G[0][i].c);
  38.     cout << ans << endl;
  39.  
  40.     return 0;
  41. }
Add Comment
Please, Sign In to add comment