#include <iostream>
#include <vector>

int mas[1000009];
std::vector <int> v[1000009];

int main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int n;
    std::cin>>n;
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        int a, b;
        std::cin>>a>>b;
        if (b != 0)
        {
            for (int j = 0; j < v[a].size(); ++j)
            {
                v[i].push_back(v[a][j]);
            }
            v[i].push_back(b);
            mas[i] = mas[a] + b;
        }
        else
        {
            for (int j = 0; j < v[a].size() - 1; ++j)
            {
                v[i].push_back(v[a][j]);
            }
            mas[i] = mas[a] - v[a].back();
        }
        /*std::cout<<i<<' '<<mas[i]<<'\n';
        for (int j = 0; j < v[i].size(); ++j)
        {
            std::cout<<v[i][j]<<' ';
        }
        std::cout<<'\n';*/
        ans+=mas[i];
    }
    std::cout<<ans<<'\n';


    return 0;
}
