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