题库练习 Just Kingdom
← 上一题 下一题 →

A14478 | Just Kingdom

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

The Just Kingdom is ruled by a king and his $n$ lords, numbered $1$ to $n$ . Each of the lords is a vassal of some overlord, who might be the king himself, or a different lord closer to the king. The king, and all his lords, are just and kind.

Each lord has certain needs, which can be expressed as a certain amount of money they need. However, if a lord, or the king, receives any money, they will first split it equally between all their vassals who still have unmet needs. Only if all the needs of all their vassals are met, they will take the money to fulfill their own needs. If there is any money left over, they will return the excess to their overlord (who follows the standard procedure for distributing money).

At the beginning of the year, the king receives a certain sum of tax money and proceeds to split it according to the rules above. If the amount of tax money is greater than the total needs of all the lords, the procedure guarantees everybody's needs will be fulfilled, and the excess money will be left with the king. However, if there is not enough money, some lords will not have their needs met.

For each lord, determine the minimum amount of tax money the king has to receive so that this lord's needs are met.

输入格式

The first line of the input contains the number of lords $n$ ( $0 \le n \le 3 \cdot 10^5$ ). Each of the next $n$ lines describes one of the lords. The $i$ -th line contains two integers: $o_i$ ( $0 \le o_i < i$ ) — the index of the overlord of the $i$ -th lord (with zero meaning the king is the overlord), and $m_i$ ( $1 \le m_i \le 10^6$ ) — the amount of money the $i$ -th lord needs.

输出格式

Print $n$ integer numbers $t_i$ . The $i$ -th number should be the minimum integer amount of tax money the king has to receive for which the needs of the $i$ -th lord will be met.

输入输出样例

输入 #1
5
0 2
1 2
0 1
1 1
0 5
输出 #1
11
7
3
5
11
C++ 编辑器
输入
输出