#include <bits/stdc++.h>
using namespace std;
vector<vector<long long> > G;
vector<vector<long long> > DP;
vector<vector<bool> > calculat;
vector<long long> a;
long long millor(int pos, int p) {
  if (calculat[pos][p]) return DP[pos][p];
  calculat[pos][p] = true;
  long long sum = 0;
  long long worst = 1000000000000000000LL;
  if (p == 1) {
    sum = a[pos];
    worst = a[pos];
  }
  for (int i = 0; i < G[pos].size(); i++) {
    long long imp = millor(G[pos][i], 1);
    long long par = millor(G[pos][i], 0);
    sum += max(imp, par);
    worst = min(worst, abs(imp - par));
  }
  long long paritat = (1 + G[pos].size()) % 2;
  if (p == 0) paritat ^= 1;
  if (paritat == p)
    return DP[pos][p] = sum;
  else
    return DP[pos][p] = sum - worst;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (cin >> n) {
    DP = vector<vector<long long> >(n, vector<long long>(2, -1));
    calculat = vector<vector<bool> >(n, vector<bool>(2, false));
    G = vector<vector<long long> >(n);
    a = vector<long long>(n);
    int jefe;
    for (int i = 0; i < n; i++) {
      cin >> jefe;
      --jefe;
      if (jefe != -2) G[jefe].push_back(i);
      cin >> a[i];
    }
    cout << max(millor(0, 1), millor(0, 0)) << endl;
  }
}
