#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> g[N];
int n, a[N];
long long dp[N][3], sol;
void dfs(int v) {
  long long dp_odd[(int)(g[v].size()) + 5];
  long long dp_even[(int)(g[v].size()) + 5];
  dp_odd[0] = 0;
  dp_even[0] = 0;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    dfs(u);
    if (i == 0) {
      dp_odd[0] = max(dp[u][0], dp[u][2]);
      dp_even[0] = dp[u][1];
    } else {
      dp_even[i] = max(dp_even[i - 1], dp_odd[i - 1] + max(dp[u][0], dp[u][2]));
      dp_even[i] = max(dp_even[i], dp_even[i - 1] + dp[u][1]);
      dp_odd[i] = max(dp_odd[i - 1], dp_even[i - 1] + max(dp[u][0], dp[u][2]));
      dp_odd[i] = max(dp_odd[i], dp_odd[i - 1] + dp[u][1]);
    }
  }
  if (g[v].size() == 0) {
    dp[v][0] = 1ll * a[v];
    dp[v][1] = 0;
    dp[v][2] = 0;
  } else {
    dp[v][0] = dp_even[g[v].size() - 1] + 1ll * a[v];
    dp[v][1] = dp_even[g[v].size() - 1];
    dp[v][2] = dp_odd[g[v].size() - 1];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int v;
    scanf("%d%d", &v, &a[i]);
    if (v != -1) g[v].push_back(i);
  }
  dfs(1);
  printf("%lld", max(dp[1][0], max(dp[1][1], dp[1][2])));
  return 0;
}
