F - Expensive Expense 解説 /

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 500

問題文

AtCoder 王国は N 個の街と N-1 個の道路からなります。
街には 街 1, 街 2, \dots, 街 N と番号がついています。道路にも同様に 道路 1, 道路 2, \dots, 道路 N-1 と番号が付いています。 道路 i は街 A_iB_i を双方向に結んでいて、通過するときに C_i の通行料がかかります。すべての異なる街の組 (i, j) に対して、道路を経由して街 i から街 j に行くことができます。

今、列 D = (D_1, D_2, \dots, D_N) が与えられます。 D_i は街 i を観光するときにかかる費用です。 このとき、街 i から街 j への旅費 E_{i,j} を、(同じ道を 2 回以上使わずに街 i から街 j へ向かうときにかかる通行料の和) に D_{j} を足したものとして定めます。

  • 厳密に言い換えると、i - j 間の最短パスを i = p_0, p_1, \dots, p_{k-1}, p_k = j として、街 p_{l} と街 p_{l+1} を結ぶ道路の通行料を c_l と置いたときに E_{i,j} = D_j + \displaystyle\sum_{l=0}^{k-1} c_l と定義します。

すべての i に対して、街 i を始点として他の街へ旅行したときにありえる旅費の最大値を求めてください。

  • 厳密に言い換えると、すべての i に対して \max_{1 \leq j \leq N, j \neq i} E_{i,j} を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N-1)
  • 1 \leq B_i \leq N (1 \leq i \leq N-1)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 整数の組 (i,j)1 \leq i \lt j \leq N を満たすならば、街 i から街 j へいくつかの道路を通ることで移動できる。
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N

出力

N 行出力せよ。i 行目には \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j} を出力せよ。


入力例 1

3
1 2 2
2 3 3
1 2 3

出力例 1

8
6
6

すべての街の順序つき組 (i,j) に対して E_{i,j} を計算すると次のようになります。

  • E_{1,2} = 2 + 2 = 4
  • E_{1,3} = 5 + 3 = 8
  • E_{2,1} = 2 + 1 = 3
  • E_{2,3} = 3 + 3 = 6
  • E_{3,1} = 5 + 1 = 6
  • E_{3,2} = 3 + 2 = 5

入力例 2

6
1 2 3
1 3 1
1 4 4
1 5 1
1 6 5
9 2 6 5 3 100

出力例 2

105
108
106
109
106
14

入力例 3

6
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
1 2 3 4 5 6

出力例 3

5000000006
4000000006
3000000006
3000000001
4000000001
5000000001

Score : 500 points

Problem Statement

The Kingdom of AtCoder is composed of N towns and N-1 roads.
The towns are labeled as Town 1, Town 2, \dots, Town N. Similarly, the roads are labeled as Road 1, Road 2, \dots, Road N-1. Road i connects Towns A_i and B_i bidirectionally, and you have to pay the toll of C_i to go through it. For every pair of different towns (i, j), it is possible to go from Town A_i to Town B_j via the roads.

You are given a sequence D = (D_1, D_2, \dots, D_N), where D_i is the cost to do sightseeing in Town i. Let us define the travel cost E_{i,j} from Town i to Town j as the total toll incurred when going from Town i to Town j, plus D_{j}.

  • More formally, E_{i,j} is defined as D_j + \displaystyle\sum_{l=0}^{k-1} c_l, where the shortest path between i and j is i = p_0, p_1, \dots, p_{k-1}, p_k = j and the toll for the road connecting Towns p_{l} and p_{l+1} is c_l.

For every i, find the maximum possible travel cost when traveling from Town i to another town.

  • More formally, for every i, find \max_{1 \leq j \leq N, j \neq i} E_{i,j}.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N-1)
  • 1 \leq B_i \leq N (1 \leq i \leq N-1)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • It is possible to travel from Town i to Town j via some number of roads, for a pair of integers (i,j) such that 1 \leq i \lt j \leq N.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N

Output

Print N lines. The i-th line should contain \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j}.


Sample Input 1

3
1 2 2
2 3 3
1 2 3

Sample Output 1

8
6
6

The value of E_{i,j} for every ordered pair of towns (i,j) is as follows.

  • E_{1,2} = 2 + 2 = 4
  • E_{1,3} = 5 + 3 = 8
  • E_{2,1} = 2 + 1 = 3
  • E_{2,3} = 3 + 3 = 6
  • E_{3,1} = 5 + 1 = 6
  • E_{3,2} = 3 + 2 = 5

Sample Input 2

6
1 2 3
1 3 1
1 4 4
1 5 1
1 6 5
9 2 6 5 3 100

Sample Output 2

105
108
106
109
106
14

Sample Input 3

6
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
1 2 3 4 5 6

Sample Output 3

5000000006
4000000006
3000000006
3000000001
4000000001
5000000001