#include<bits/stdc++.h>
using namespace std;
vector<stack<int>> snowmen;
int n;
long long res;
int main()
{
    stack<int> start;
    snowmen.push_back(start);
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int p, m;
        cin >> p >> m;
        stack<int> curr = snowmen[p];
        snowmen.push_back(curr);
        if(m == 0)
        {
            snowmen[snowmen.size() - 1].pop();
        }
        else
        {
            snowmen[snowmen.size() - 1].push(m);
        }
    }
    for(int i = 0; i < snowmen.size(); i++)
    {
        while(!snowmen[i].empty())
        {
            res += snowmen[i].top();
            snowmen[i].pop();
        }
    }
    cout << res;
    return 0;
}
