#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
long long K[N], L[N], n, a[N], sK[N], sL[N];
vector<int> g[N];
bool cmp(int i, int j) { return (K[i] - L[i]) > (K[j] - L[j]); }
void f(int i) {
  if (g[i].size() == 0) {
    K[i] = a[i];
    L[i] = 0;
    return;
  }
  int j;
  for (j = 0; j < g[i].size(); j++) f(g[i][j]);
  sort(g[i].begin(), g[i].end(), cmp);
  for (j = 0; j < g[i].size(); j++) sK[j + 1] = sK[j] + K[g[i][j]];
  for (j = 0; j < g[i].size(); j++) sL[j + 1] = sL[j] + L[g[i][j]];
  K[i] = L[i] = 0;
  for (j = 0; j < g[i].size(); j++)
    if (j % 2 == 0)
      K[i] = max(K[i], sK[j + 1] - sL[j + 1] + sL[g[i].size()]);
    else
      L[i] = max(L[i], sK[j + 1] - sL[j + 1] + sL[g[i].size()]);
  L[i] = max(L[i], sL[g[i].size()]);
  K[i] = max(K[i], L[i] + a[i]);
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &j, &a[i]);
    if (j != -1) g[j].push_back(i);
  }
  f(1);
  cout << max(K[1], L[1]) << endl;
}
