#include <bits/stdc++.h>
#define endl '\n'

int n, p, m;
unsigned long long ans;
std::pair<int, int> snowmen[100001];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;
    for (int i = 0; i < n; ++ i) {
        std::cin >> p >> m;
        if (m == 0) {
            ans += snowmen[snowmen[p].second].first;
            snowmen[i + 1] = {snowmen[snowmen[p].second].first, snowmen[p].second};
        } else {
            ans += snowmen[p].first + m;
            snowmen[i + 1] = {snowmen[p].first + m, p};
        }
    }
    std::cout << ans << endl;
    return 0;
}
