Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. vector<int> g[MAXN];
  2. int q[MAXN], dp[MAXN][2];
  3.  
  4. void calc(int v) {
  5.     int sum1 = q[v], sum2 = 0;
  6.     for (int i : g[v]) {
  7.         calc(i);
  8.         sum1 += dp[i][0];
  9.         sum2 += max(dp[i][0], dp[i][1]);
  10.     }
  11.     dp[v][1] = sum1;
  12.     dp[v][0] = sum2;
  13. }
  14.  
  15. signed main(void) {
  16. #ifdef LOCAL
  17.     freopen("a.in", "r", stdin);
  18.     freopen("a.out", "w", stdout);
  19. #else
  20.     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  21. #endif
  22.     int n, p, root;
  23.     cin >> n;
  24.     for (int i = 0; i < n; ++i) {
  25.         cin >> p >> q[i], p--;
  26.         if (p >= 0) {
  27.             g[p].push_back(i);
  28.         } else root = i;
  29.     }
  30.     calc(root);
  31.     cout << max(dp[root][0], dp[root][1]);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement