/*
Though leaves are many, the root is one;
Through all the lying days of my youth
I swayed my leaves and flowers in the sun,
Now may I wither into the truth.

- William Butler Yeats
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <cassert>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <iomanip>
#include <bitset>
//#include <windows.h>  //Should be deleted when using AtCoder&POJ
using namespace std;

#define ll long long
#define pii pair<int,int>
#define pll pair<int,ll>
#define qi ios::sync_with_stdio(0)
/**==Info==
*Program:1
*Problem:Usoperanto
*Date:2020-7-26
*Algorithm:My Random Sorting and DFS
*Status:RE13*/

bool debug=false;

vector<int> nei[1000005];
int par[1000005];
int n;
ll len[1000005];
 
//{size,cost}
pll dfs(int pos,int par){
	
	vector<int> v;
	int sz=len[pos];
	ll cost=0;
	for(int to:nei[pos]){
		pii val=dfs(to,pos);
		sz+=val.first;
		cost+=val.second;
		v.push_back(val.first);
	}
	
	sort(v.begin(),v.end());
	
	for(int i=0;i<v.size();i++){
		cost+=1ll*v[i]*(v.size()-i-1);
	}
	
	return make_pair(sz,cost);
}

struct Task{
	int pos;
	int sz;
	ll cost;
	int state;
	vector<int> v;
	
	Task(int pos){
		this->pos=pos;
		sz=len[pos];
		cost=0;
		state=-1;
	}
};

template<typename T> ostream& operator<<(ostream& o,vector<T> vec){
	o<<"{";
	for(int i=0;i<vec.size();i++){
		o<<vec[i]<<" ";
	}
	o<<"}";
	return o;
}

ostream& operator<<(ostream& os,Task t){
	os<<"Task["<<t.pos<<","<<t.sz<<","<<t.cost<<","<<t.state<<","<<t.v<<"]";
	return os;
}

#define curStk task.back()
//FUCK YOU AIZU OJ
pll solve(int pos){
	vector<Task> task;
	task.push_back(Task(pos));
	
	pll ret=make_pair(-1,-1);
	
	while(!task.empty()){
		
//		cout<<curStk<<endl;
		
		if(nei[curStk.pos].size()==0){
			ret=make_pair(curStk.sz,0);
			task.pop_back();
			continue;
		}
		
		if(curStk.state==-1){
			curStk.state=0; //state= finished the state-th call
//			cout<<"A:"<<curStk<<endl;
			
			Task nw=Task(nei[curStk.pos][0]);
			task.push_back(nw);
		}else{
			curStk.sz+=ret.first;
			curStk.cost+=ret.second;
			curStk.v.push_back(ret.first);
			curStk.state++;
			if(curStk.state==nei[curStk.pos].size()){
				sort(curStk.v.begin(),curStk.v.end());
				for(int i=0;i<curStk.v.size();i++){
					curStk.cost+=1ll*curStk.v[i]*(curStk.v.size()-i-1);
				}
				
				ret=make_pair(curStk.sz,curStk.cost);
				
				task.pop_back();
			}else{
				Task nw=Task(nei[curStk.pos][curStk.state]);
				task.push_back(nw);
			}
		}
	}
	
	return ret;
}

int main(int argc,char* argv[]){
	
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld%d",&len[i],&par[i]);
		if(par[i]!=-1){
			nei[par[i]].push_back(i);
		}
	}
	
	ll cost=0;
	for(int i=0;i<n;i++){
		if(par[i]==-1){
			pll ans=solve(i);
			cost+=ans.second;
		}
	}
	
	
	cout<<cost<<endl;
	return 0;
}




Measured memory_bytes: 118464