CF1578J.Just Kingdom

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Just Kingdom is ruled by a king and his nn lords, numbered 11 to nn . 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 nn ( 0n31050 \le n \le 3 \cdot 10^5 ). Each of the next nn lines describes one of the lords. The ii -th line contains two integers: oio_i ( 0oi<i0 \le o_i < i ) — the index of the overlord of the ii -th lord (with zero meaning the king is the overlord), and mim_i ( 1mi1061 \le m_i \le 10^6 ) — the amount of money the ii -th lord needs.

输出格式

Print nn integer numbers tit_i . The ii -th number should be the minimum integer amount of tax money the king has to receive for which the needs of the ii -th lord will be met.

输入输出样例

  • 输入#1

    5
    0 2
    1 2
    0 1
    1 1
    0 5

    输出#1

    11
    7
    3
    5
    11

说明/提示

In the sample input, if the king receives 55 units of tax money, he will split it equally between his vassals — the lords 11 , 33 , and 55 , with each receiving 53\frac{5}{3} of money.

Lord 11 will split the money equally between his vassals — 22 and 44 , with each receiving 56\frac{5}{6} . Lord 55 will keep the money (having no vassals). Lord 33 will keep 11 unit of money, and give the remaining 23\frac{2}{3} to the king. The king will then split the 23\frac{2}{3} between the vassals with unmet needs — 11 and 55 , passing 13\frac{1}{3} to each. Lord 55 will keep the extra cash (now having a total of 22 , still not enough to meet his needs). Lord 11 will split it equally between his vassals, and the extra 16\frac{1}{6} will be enough to meet the needs of lord 44 .

首页