Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<int> g[MAXN];
- int q[MAXN], dp[MAXN][2];
- void calc(int v) {
- int sum1 = q[v], sum2 = 0;
- for (int i : g[v]) {
- calc(i);
- sum1 += dp[i][0];
- sum2 += max(dp[i][0], dp[i][1]);
- }
- dp[v][1] = sum1;
- dp[v][0] = sum2;
- }
- signed main(void) {
- #ifdef LOCAL
- freopen("a.in", "r", stdin);
- freopen("a.out", "w", stdout);
- #else
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- #endif
- int n, p, root;
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> p >> q[i], p--;
- if (p >= 0) {
- g[p].push_back(i);
- } else root = i;
- }
- calc(root);
- cout << max(dp[root][0], dp[root][1]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement