#include <iostream>
#include <vector>

using namespace std;

typedef struct snowman{
    int stoinost;
    int topka;
}sm;

int main()
{
    int n;
    cin >> n;
    int p, m;
    long long count = 0;
    vector<sm> v;


    for (int i = 0; i < n; i++)
    {
        sm sm_ = {0,0};
        v.push_back(sm_);
        cin >> p >> m;
        if (p == 1 && m == 0)
        {
            v.push_back(sm_);
            v[p].stoinost = 0;
            v[p].topka = 0;
            continue;
        }

        if (m == 0)
        {
          //  cout << "tuk" << endl;
            v[i].stoinost = v[p - 1].stoinost - v[p - 1].topka;
          //  cout << v[i].stoinost << endl;
            count += v[i].stoinost;
          //  cout << count << endl;
            continue;
        }
        if ((p - 1) >= 0)
        {
           // cout << "p - 1" << endl;
           // cout << m << endl;
            v[i].topka = m;
            v[i].stoinost = v[p - 1].stoinost + m;
            //cout << v[i].stoinost << endl;
        }

        else
        {
            //cout << "else" << endl;
           // cout << m << endl;
            v[i].topka = m;
            v[i].stoinost = m;
        }
        //cout << count << endl;
        count += v[i].stoinost;
       // cout << count << endl;
    }

    cout << count << endl;

    return 0;
}
