#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
long long eff[200005];
long long dp[200005][2];
long long dfs(int src, int polarity) {
  long long &ret = dp[src][polarity];
  if (ret != -1) return ret;
  if (adj[src].size() == 0) return ret = eff[src] * polarity;
  long long sumE = 0;
  vector<long long> even(adj[src].size()), diff(adj[src].size());
  for (int i = 0; i < adj[src].size(); i++) {
    even[i] = dfs(adj[src][i], 0);
    sumE += even[i];
    diff[i] = dfs(adj[src][i], 1) - even[i];
  }
  sort(diff.begin(), diff.end());
  vector<long long> temp = diff;
  long long bestEvenFromChildren = sumE, bestOddFromChildren = sumE;
  while (temp.size() >= 2 && temp.back() + temp[temp.size() - 2] > 0) {
    bestEvenFromChildren += temp.back();
    temp.pop_back();
    bestEvenFromChildren += temp.back();
    temp.pop_back();
  }
  temp = diff;
  bestOddFromChildren += temp.back();
  temp.pop_back();
  while (temp.size() >= 2 && temp.back() + temp[temp.size() - 2] > 0) {
    bestOddFromChildren += temp.back();
    temp.pop_back();
    bestOddFromChildren += temp.back();
    temp.pop_back();
  }
  if (polarity == 0)
    return ret = bestEvenFromChildren;
  else
    return ret = max(bestEvenFromChildren + eff[src], bestOddFromChildren);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p, e;
    cin >> p >> eff[i];
    if (p != -1) adj[p].push_back(i);
  }
  cout << max(dfs(1, 0), dfs(1, 1)) << "\n";
  return 0;
}
