#include <bits/stdc++.h>   using namespace std;   long long bin(long long x, long long n) {     long long res = 1;     x %= 1000000007;     while (n) {       if (n & 1) res = (res * x) % 1000000007;       x = (x * x) % 1000000007;       n >>= 1;     }     return res;   }   const long long MXN = 2e5 + 1;   vector<long long> adj[MXN];   long long n;   long long a[MXN], val[MXN], dp[MXN][2];   void dfs(long long u, long long p) {     vector<long long> chk0, chk1;     vector<pair<long long, long long> > in;     for (auto i : adj[u]) {       if (i == p) continue;       dfs(i, u);       in.push_back({dp[i][0] - dp[i][1], i});     }     sort(in.begin(), in.end());     chk1.push_back(0);     for (long long i = 0; i < in.size(); i++) {       chk1.push_back(dp[in[i].second][1]);       chk0.push_back(dp[in[i].second][0]);     }     long long y = in.size();     chk0.push_back(0);     for (long long i = chk0.size(); i >= 2; i--) {       chk0[i - 2] += chk0[i - 1];     }     for (long long i = 1; i < chk1.size(); i++) {       chk1[i] += chk1[i - 1];     }     long long odd = 0, eve = 0;     for (long long i = 0; i <= y; i += 2) {       eve = max(eve, chk1[i] + chk0[i]);     }     for (long long i = 1; i <= y; i += 2) {       odd = max(odd, chk1[i] + chk0[i]);     }     dp[u][0] = eve;     dp[u][1] = max(val[u] + eve, odd);   }   void solve() {     cin >> n;     for (long long i = 1; i <= n; i++) {       long long x, y;       cin >> x >> y;       if (x != -1) adj[i].push_back(x), adj[x].push_back(i);       val[i] = y;     }     dfs(1, -1);     cout << dp[1][1] << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }   }   