#include <iostream>
#include <vector>
using namespace std;

struct snm
{
    int mass;
    vector<int> balls;
};

int main()
{
    int n;
    cin >> n;

    struct snm sm[n];

    int p,m,res=0;
    cin >> p >> m;//vinagi v nachaloto p shte e 0
    sm[p].mass=m;
    sm[p].balls.push_back(m);
    res+=m;

    for(int i = 1; i<n; i++)
    {
        cin >> p >> m;
        if(m>0)
        {
            sm[p].balls=sm[p-1].balls;
            sm[p].balls.push_back(m);

            sm[p].mass=sm[p-1].mass+m;
        }
        else //m==0
        {
            sm[p].balls=sm[p-1].balls;
            sm[p].balls.pop_back();
            sm[p].mass=sm[p-1].mass-sm[p-1].balls.back();
            cout << sm[p].mass << endl;
        }
        res+=sm[p].mass;
    }

    cout << res <<endl;
    return 0;
}
