#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print_range(const T* begin, const T* end) {}
template <typename T>
inline int sign(const T& a) {
  return a < 0 ? -1 : a > 0;
}
void task();
int main() {
  ios_base::sync_with_stdio(false);
  task();
  return 0;
}
int n;
vector<int> g[int(2e5) + 10];
long long w[int(2e5) + 10];
long long sum[int(2e5) + 10];
long long min_head[int(2e5) + 10];
long long ans = 0;
bool cmp(const int& a, const int& b) { return sum[a] > sum[b]; }
bool cmp_head(const int& a, const int& b) { return min_head[a] < min_head[b]; }
void go(int v) {
  for (int i = 0; i < (int)g[v].size(); ++i) {
    int to = g[v][i];
    go(to);
  }
  sort(g[v].begin(), g[v].end(), cmp);
  long long cur = w[v];
  if (g[v].size() % 2 == 0) {
    sum[v] = cur;
    min_head[v] = cur;
    for (auto to : g[v]) sum[v] += sum[to];
  } else {
    long long tmp1 = 0;
    for (int i = 0; i < g[v].size() - 1; i++) {
      tmp1 += sum[g[v][i]];
    }
    tmp1 += cur;
    long long tmp2 = 0;
    sort(g[v].begin(), g[v].end(), cmp_head);
    for (auto to : g[v]) {
      tmp2 += sum[to];
    }
    long long tmp3 = tmp2;
    tmp2 -= min_head[g[v][0]];
    tmp2 += cur;
    tmp2 = max(tmp1, tmp2);
    if (tmp2 > tmp3) {
      sum[v] = tmp2;
      min_head[v] = cur;
    } else {
      sum[v] = tmp3;
      min_head[v] = min_head[g[v][0]];
    }
  }
}
void task() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  int root = 0;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    if (a >= 0)
      g[a].push_back(i);
    else
      root = i;
    w[i] = b;
  }
  go(root);
  cout << sum[root];
}
