Problem:
Usoperanto

Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy.
The academy is now in study to establish Strict Usoperanto, a variation of the language intended for formal documents.

In Usoperanto, each word can modify at most one other word, and modifiers are always put before modifiees.
For example, with a noun uso ("truth") modified by an adjective makka ("total"), people say makka uso, not uso makka.
On the other hand, there have been no rules about the order among multiple words modifying the same word,
so in case uso is modified by one more adjective beta ("obvious"),
people could say both makka beta uso and beta makka uso.

In Strict Usoperanto, the word order will be restricted according to modification costs.
Words in a phrase must be arranged so that the total modification cost is minimized.
Each pair of a modifier and a modifiee is assigned a cost equal to the number of letters between the two words;
the total modification cost is the sum of the costs over all modifier-modifiee pairs in the phrase.
For example, the pair of makka and uso in a phrase makka beta uso has the cost of 4 for beta (four letters).
As the pair of beta and uso has no words in between and thus the cost of zero, makka beta uso has the total modification cost of 4.
Similarly beta makka uso has the total modification cost of 5.
Applying the "minimum total modification cost" rule, makka beta uso is preferred to beta makka uso in Strict Usoperanto.

Your mission in this problem is to write a program that, given a set of words in a phrase, finds the correct word order in Strict Usoperanto and reports the total modification cost.

Input

The format of the input is as follows.

N
M0 L0
...
MN-1 LN-1

The first line contains an integer

N (1 ≤ N ≤ 106).
N is the number of words in a phrase.

Each of the following N lines contains two integers
Mi (1 ≤ Mi ≤ 10) and
Li (-1 ≤ Li ≤ N - 1, Li ≠ i)
describing the i-th word (0 ≤ i ≤ N-1).
Mi is the number of the letters in the word.
Li specifies the modification:
Li = -1 indicates it does not modify any word;
otherwise it modifies the Li-th word.

Note the first sample input below can be interpreted as the uso-beta-makka case.

Output

Print the total modification cost.

Sample Input 1

3
3 -1
4 0
5 0

Output for the Sample Input 1

4

Sample Input 2

3
10 -1
10 0
10 1

Output for the Sample Input 2

0

Sample Input 3

4
1 -1
1 0
1 1
1 0

Output for the Sample Input 3

1

Solution in C (judge verdict: Wrong Answer)

// AOJ 2456: Usoperanto
// 2018.4.17 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// マイナス値の入力に対応
//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0, c = getchar_unlocked();
	if (c == '-') { c = getchar_unlocked();
		do n = 10*n + (c & 0xf), c = getchar_unlocked(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = getchar_unlocked(); while (c >= '0');
	return n;
}

#define MAX 1000002
int  N;
int  m[MAX];
int  *base, bsz;
int  hi[MAX], *to[MAX];
long long s[MAX], a[MAX], dp[MAX];

int cmp(long long *a, long long *b)
{
	if (*a == *b) return 0;
	if (*a < *b) return 1;
	return -1;
}

void dfs(int u)
{
	int i, v, sz;

	sz = 0, dp[u] = 0, s[u] = m[u];
    for(i = 0; i < hi[u]; i++) {
        v = to[u][i];
        dfs(v);
        a[sz++] = s[v], s[u] += s[v], dp[u] += dp[v];
    }
	qsort(a, sz, sizeof(long long), cmp);
	for (i = 1; i < sz; i++) dp[u] += a[i]*i;
}

int main()
{
	int i, l;
	long long ans;
	int *memo;

	N = in();
	memo = malloc(N<<2);
	for (i = 0; i < N; i++) {
		m[i] = in(), memo[i] = l = in();
		if (l == -1) bsz++;
		else hi[l]++;
	}
	base = malloc(bsz<<2);
	for (i = 0; i < N; i++) if (hi[i]) to[i] = malloc(hi[i]<<2);
	bsz = 0, memset(hi, 0, N<<2);

	for (i = 0; i < N; i++) {
		l = memo[i];
		if (l == -1) base[bsz++] = i;
		else to[l][hi[l]++] = i;
	}

	ans = 0;
	for (i = 0; i < bsz; i++) {
		dfs(base[i]);
		ans += dp[base[i]];
	}
	printf("%lld\n", ans);
	return 0;
}