CF82C.General Mobilization

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

The Berland Kingdom is a set of nn cities connected with each other with n1n-1 railways. Each road connects exactly two different cities. The capital is located in city 11 . For each city there is a way to get from there to the capital by rail.

In the ii -th city there is a soldier division number ii , each division is characterized by a number of aia_{i} . It represents the priority, the smaller the number, the higher the priority of this division. All values of aia_{i} are different.

One day the Berland King Berl Great declared a general mobilization, and for that, each division should arrive in the capital. Every day from every city except the capital a train departs. So there are exactly n1n-1 departing trains each day. Each train moves toward the capital and finishes movement on the opposite endpoint of the railway on the next day. It has some finite capacity of cjc_{j} , expressed in the maximum number of divisions, which this train can transport in one go. Each train moves in the direction of reducing the distance to the capital. So each train passes exactly one railway moving from a city to the neighboring (where it stops) toward the capital.

In the first place among the divisions that are in the city, division with the smallest number of aia_{i} get on the train, then with the next smallest and so on, until either the train is full or all the divisions are be loaded. So it is possible for a division to stay in a city for a several days.

The duration of train's progress from one city to another is always equal to 11 day. All divisions start moving at the same time and end up in the capital, from where they don't go anywhere else any more. Each division moves along a simple path from its city to the capital, regardless of how much time this journey will take.

Your goal is to find for each division, in how many days it will arrive to the capital of Berland. The countdown begins from day 00 .

输入格式

The first line contains the single integer nn ( 1<=n<=50001<=n<=5000 ). It is the number of cities in Berland. The second line contains nn space-separated integers a1,a2,...,ana_{1},a_{2},...,a_{n} , where aia_{i} represents the priority of the division, located in the city number ii . All numbers a1,a2,...,ana_{1},a_{2},...,a_{n} are different ( 1<=ai<=1091<=a_{i}<=10^{9} ). Then n1n-1 lines contain the descriptions of the railway roads. Each description consists of three integers vj,uj,cjv_{j},u_{j},c_{j} , where vjv_{j} , uju_{j} are number of cities connected by the jj -th rail, and cjc_{j} stands for the maximum capacity of a train riding on this road ( 1<=vj,uj<=n,vjuj1<=v_{j},u_{j}<=n,v_{j}≠u_{j} , 1<=cj<=n1<=c_{j}<=n ).

输出格式

Print sequence t1,t2,...,tnt_{1},t_{2},...,t_{n} , where tit_{i} stands for the number of days it takes for the division of city ii to arrive to the capital. Separate numbers with spaces.

输入输出样例

  • 输入#1

    4
    40 10 30 20
    1 2 1
    2 3 1
    4 2 1
    

    输出#1

    0 1 3 2 
  • 输入#2

    5
    5 4 3 2 1
    1 2 1
    2 3 1
    2 4 1
    4 5 1
    

    输出#2

    0 1 4 2 3 
首页