4125: General Mobilization

内存限制:256 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:3

题目描述

C. General Mobilization
C、 一般动员

Berland王国是一个由n个城市组成的集合,通过n-1条铁路相互连接。每条路正好连接两个不同的城市。首都位于城市1。每个城市都有一条从那里乘火车到首都的路。
在第i个城市有一个士兵师编号i,每个师都有一个ai编号。它代表优先级,编号越小,该师的优先级越高。ai的所有值都不同。
一天,柏林国王贝尔大帝宣布进行总动员,为此,每个师都应抵达首都。除了首都,每天都有火车从各个城市出发。因此,每天正好有n-1趟发车列车。每列火车都向首都方向移动,并在第二天在铁路的另一端完成移动。它有一些有限的cj容量,以最大分区数表示,这列火车可以一次性运输。每列火车都朝着缩短首都距离的方向行驶。因此,每列火车只经过一条铁路,从一个城市开往邻近的(停靠的)首都。
在城市中的分区中,排名第一的是ai数量最少的分区,然后是第二个最小的分区,依此类推,直到列车满载或所有分区都装载完毕。因此,一个师有可能在一个城市呆几天。
火车从一个城市开往另一个城市的持续时间总是等于1天。所有部门同时开始行动,最终抵达首都,不再前往其他地方。每个部门都沿着一条简单的路径从其城市到首都,无论这段旅程需要多长时间。
你的目标是找到每一个分区,它将在多少天内到达首都柏林。倒计时从第0天开始。

The Berland Kingdom is a set of n cities connected with each other with n-1 railways. Each road connects exactly two different cities. The capital is located in city 1. For each city there is a way to get from there to the capital by rail.
In the i-th city there is a soldier division number i, each division is characterized by a number of ai. It represents the priority, the smaller the number, the higher the priority of this division. All values of ai 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 n-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 cj, 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 ai 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 1 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 0.
Input
The first line contains the single integer n (1≤n≤5000). It is the number of cities in Berland. The second line contains n space-separated integers a1,a2,...,an, where ai represents the priority of the division, located in the city number i. All numbers a1,a2,...,an are different (1≤ai≤109). Then n-1 lines contain the descriptions of the railway roads. Each description consists of three integers vj,uj,cj, where vj, uj are number of cities connected by the j-th rail, and cj stands for the maximum capacity of a train riding on this road (1≤vj,ujn,vjuj, 1≤cjn).
Output
Print sequence t1,t2,...,tn, where ti stands for the number of days it takes for the division of city i to arrive to the capital. Separate numbers with spaces.
Examples
Input
4
40 10 30 20
1 2 1
2 3 1
4 2 1

Output
0 1 3 2 
Input
5
5 4 3 2 1
1 2 1
2 3 1
2 4 1
4 5 1
Output
0 1 4 2 3 

输入格式

第一行包含单个整数n(1≤n≤5000)。这是柏林的城市数量。第二行包含n个空格分隔的整数a1,a2,。。。,an,其中ai表示分区的优先级,位于城市编号i中。所有编号a1,a2,。。。,an不同(1≤ai≤109)。然后,n-1行包含铁路道路的描述。每个描述由三个整数vj、uj、cj组成,其中vj、u j是通过第j条铁路连接的城市数量,cj代表在这条道路上行驶的列车的最大容量(1≤vj,uj≤n,vj≠uj,1≤cj≤n)。

输出格式

打印顺序t1、t2、…、,。。。,tn,其中ti代表城市i的分区到达首都所需的天数。用空格分隔数字。

输入样例 复制

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

输出样例 复制

0 1 3 2