#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int N, p, m, s = 0;
    cin>>N;
    vector<int> Vec[N + 1];
    Vec[0] = {};
    for(int i = 1; i <= N ; i++)
    {
        cin>>p;
        cin>>m;
        Vec[i].insert(Vec[i].end(), Vec[p].begin(), Vec[p].end());
        if(m > 0)
        {
            Vec[i].push_back(m);
        }
        else if(m == 0)
        {
            Vec[i].pop_back();
        }
        for( auto j = Vec[i].begin(); j < Vec[i].end(); j++)
        {
            s += *j;
        }
    }
    cout<<s;