#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:128000000")
int n;
vector<int> a;
vector<vector<int> > g;
vector<vector<vector<long long> > > dp;
inline void init() {
  scanf("%d", &n);
  a.resize(n);
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    int par;
    scanf("%d%d", &par, &a[i]);
    if (par == -1) continue;
    --par;
    g[par].push_back(i);
  }
  dp.resize(n);
  for (int i = 0; i < n; ++i) {
    dp[i].resize(2);
    for (int j = 0; j < 2; ++j) dp[i][j].assign((int)g[i].size(), -1LL);
  }
}
long long rec(int x, int y, int z) {
  int k = (int)g[x].size();
  if (z >= k) {
    if (y) return -2LL;
    return 0LL;
  }
  if (dp[x][y][z] != -1LL) return dp[x][y][z];
  long long res = rec(x, y, z + 1);
  long long cur1 = rec(g[x][z], 0, 0);
  long long cur2 = rec(x, (y ^ 1), z + 1);
  if (cur1 != -2LL && cur2 != -2LL) {
    res = max(res, (long long)a[g[x][z]] + cur1 + cur2);
  }
  long long cur3 = rec(g[x][z], 1, 0);
  if (cur3 != -2LL && cur2 != -2LL) {
    res = max(res, cur3 + cur2);
  }
  long long cur4 = rec(x, y, z + 1);
  if (cur1 != -2LL && cur4 != -2LL) {
    res = max(res, cur1 + cur4);
  }
  return dp[x][y][z] = res;
}
int main() {
  init();
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    long long cur = rec(i, 0, 0);
    if (cur == -2LL) continue;
    cur += (long long)a[i];
    res = max(res, cur);
  }
  for (int i = 0; i < n; ++i) {
    long long cur = rec(i, 1, 0);
    if (cur == -2LL) continue;
    res = max(res, cur);
  }
  cout << res << endl;
  return 0;
}
