A - Streamer Takahashi

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

配信者の高橋君は L 時から R 時に配信をすることにしました。

高橋君には N 人のリスナーがおり、i 人目のリスナーは X_i 時から Y_i 時まで配信を見ることができます。

高橋君の配信を最初から最後まで見ることができるリスナーは何人いますか?

制約

  • 1\leq N\leq 100
  • 0\leq L\lt R\leq 23
  • 0\leq X_i\lt Y_i\leq 23
  • 入力は全て整数

入力

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

N L R
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

高橋君の配信を最初から最後まで見ることができるリスナーの数を出力せよ。


入力例 1

5 19 22
17 23
20 23
19 22
0 23
12 20

出力例 1

3

高橋君の配信を最初から最後まで見ることができるのは 1,3,4 人目のリスナーです。


入力例 2

3 12 13
0 1
0 1
0 1

出力例 2

0

高橋君の配信を最初から最後まで見ることができるリスナーはいません。


入力例 3

10 8 14
5 20
14 21
9 21
5 23
8 10
0 14
3 8
2 6
0 16
5 20

出力例 3

5

Score : 100 points

Problem Statement

Streamer Takahashi has decided to stream from L o'clock to R o'clock (using the 24-hour clock).

He has N listeners, and the i-th listener can watch the stream from X_i o'clock to Y_i o'clock.

How many listeners can watch Takahashi's stream from beginning to end?

Constraints

  • 1\leq N\leq 100
  • 0\leq L\lt R\leq 23
  • 0\leq X_i\lt Y_i\leq 23
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N L R
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Output the number of listeners who can watch Takahashi's stream from beginning to end.


Sample Input 1

5 19 22
17 23
20 23
19 22
0 23
12 20

Sample Output 1

3

The listeners who can watch Takahashi's stream from beginning to end are the 1st, 3rd, and 4th listeners.


Sample Input 2

3 12 13
0 1
0 1
0 1

Sample Output 2

0

No listeners can watch Takahashi's stream from beginning to end.


Sample Input 3

10 8 14
5 20
14 21
9 21
5 23
8 10
0 14
3 8
2 6
0 16
5 20

Sample Output 3

5
B - Leap Year

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

1583 以上 2023 以下の整数 Y が与えられます。

西暦 Y 年の日数を求めてください。

なお、制約の範囲内では西暦 Y 年の日数は以下の通りです。

  • Y が 4 の倍数でない年は 365 日

  • Y が 4 の倍数で、かつ 100 の倍数でない年は 366 日

  • Y が 100 の倍数で、かつ 400 の倍数でない年は 365 日

  • Y が 400 の倍数である年は 366 日

制約

  • Y は 1583 以上 2023 以下の整数

入力

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

Y

出力

西暦 Y 年の日数を整数として出力せよ。


入力例 1

2023

出力例 1

365

2023 は 4 の倍数でないので 365 日です。


入力例 2

1992

出力例 2

366

1992 は 4 の倍数で、かつ 100 の倍数でないので 366 日です。


入力例 3

1800

出力例 3

365

1800 は 100 の倍数で、かつ 400 の倍数でないので 365 日です。


入力例 4

1600

出力例 4

366

1600 は 400 の倍数なので 366 日です。

Score : 100 points

Problem Statement

You are given an integer Y between 1583 and 2023.

Find the number of days in the year Y of the Gregorian calendar.

Within the given range, the year Y has the following number of days:

  • if Y is not a multiple of 4, then 365 days;

  • if Y is a multiple of 4 but not a multiple of 100, then 366 days;

  • if Y is a multiple of 100 but not a multiple of 400, then 365 days;

  • if Y is a multiple of 400, then 366 days.

Constraints

  • Y is an integer between 1583 and 2023, inclusive.

Input

The input is given from Standard Input in the following format:

Y

Output

Print the number of days in the year Y as an integer.


Sample Input 1

2023

Sample Output 1

365

2023 is not a multiple of 4, so it has 365 days.


Sample Input 2

1992

Sample Output 2

366

1992 is a multiple of 4 but not a multiple of 100, so it has 366 days.


Sample Input 3

1800

Sample Output 3

365

1800 is a multiple of 100 but not a multiple of 400, so it has 365 days.


Sample Input 4

1600

Sample Output 4

366

1600 is a multiple of 400, so it has 366 days.

C - Glass and Mug

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

AtCoder 社はグラスとマグカップを販売しています。

高橋君は容量が G ml のグラスと、容量が M ml のマグカップを 1 つずつ持っています。
ここで、G,M は G<M をみたします。

最初、グラスとマグカップはいずれも空です。
以下の操作を K 回繰り返した後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか求めてください。

  • グラスが水で満たされているとき、すなわちグラスにちょうど G ml 入っているとき、グラスの水をすべて捨てる。
  • そうでなく、マグカップが空であるとき、マグカップを水で満たす。
  • 上のいずれでもないとき、マグカップが空になるかグラスが水で満たされるまで、マグカップからグラスに水を移す。

制約

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G,M,K は整数

入力

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

K G M

出力

操作を K 回行った後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか、この順に空白区切りで出力せよ。


入力例 1

5 300 500

出力例 1

200 500

操作は次の順で行われます。最初、グラスとマグカップはいずれも空です。

  • マグカップを水で満たす。グラスには 0 ml, マグカップには 500 ml 入った状態になる。
  • グラスが満たされるまでマグカップからグラスに水を移す。グラスには 300 ml, マグカップには 200 ml 入った状態になる。
  • グラスの水をすべて捨てる。グラスには 0 ml, マグカップには 200 ml 入った状態になる。
  • マグカップが空になるまでマグカップからグラスに水を移す。グラスには 200 ml, マグカップには 0 ml 入った状態になる。
  • マグカップを水で満たす。グラスには 200 ml, マグカップには 500 ml 入った状態になる。

よって、5 回の操作の後でグラスには 200 ml, マグカップには 500 ml 入った状態になります。 ゆえに、200, 500 を空白区切りでこの順に出力します。


入力例 2

5 100 200

出力例 2

0 0

Score : 200 points

Problem Statement

AtCoder Inc. sells glasses and mugs.

Takahashi has a glass with a capacity of G milliliters and a mug with a capacity of M milliliters.
Here, G<M.

Initially, both the glass and the mug are empty.
After performing the following operation K times, determine how many milliliters of water are in the glass and the mug, respectively.

  • When the glass is filled with water, that is, the glass contains exactly G milliliters of water, discard all the water from the glass.
  • Otherwise, if the mug is empty, fill the mug with water.
  • Otherwise, transfer water from the mug to the glass until the mug is empty or the glass is filled with water.

Constraints

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G, M, and K are integers.

Input

The input is given from Standard Input in the following format:

K G M

Output

Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K times.


Sample Input 1

5 300 500

Sample Output 1

200 500

The operation will be performed as follows. Initially, both the glass and the mug are empty.

  • Fill the mug with water. The glass has 0 milliliters, and the mug has 500 milliliters of water.
  • Transfer water from the mug to the glass until the glass is filled. The glass has 300 milliliters, and the mug has 200 milliliters of water.
  • Discard all the water from the glass. The glass has 0 milliliters, and the mug has 200 milliliters of water.
  • Transfer water from the mug to the glass until the mug is empty. The glass has 200 milliliters, and the mug has 0 milliliters of water.
  • Fill the mug with water. The glass has 200 milliliters, and the mug has 500 milliliters of water.

Thus, after five operations, the glass has 200 milliliters, and the mug has 500 milliliters of water. Hence, print 200 and 500 in this order, separated by a space.


Sample Input 2

5 100 200

Sample Output 2

0 0
D - Geometric Sequence

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A が等比数列であるか判定してください。

制約

  • 2\leq N\leq 100
  • 1\leq A_i\leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A が 等比数列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
3 6 12 24 48

出力例 1

Yes

A=(3,6,12,24,48) です。
A は初項 3, 公比 2, 項数 5 の等比数列となっています。
よって、Yes を出力します。


入力例 2

3
1 2 3

出力例 2

No

A=(1,2,3) です。
A_1:A_2=1:2\neq 2:3=A_2:A_3 より、A は等比数列ではありません。
よって、No を出力します。


入力例 3

2
10 8

出力例 3

Yes

A は初項 10, 公比 0.8, 項数 2 の等比数列となっています。
よって、Yes を出力します。

Score : 200 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\ldots,A_N) of positive integers.

Determine whether A is a geometric progression.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

If A is a geometric progression, print Yes; otherwise, print No.


Sample Input 1

5
3 6 12 24 48

Sample Output 1

Yes

A=(3,6,12,24,48).
A is a geometric progression with first term 3, common ratio 2, and five terms.
Therefore, print Yes.


Sample Input 2

3
1 2 3

Sample Output 2

No

A=(1,2,3).
Since A_1 : A_2 = 1 : 2 \neq 2 : 3 = A_2 : A_3, A is not a geometric progression.
Therefore, print No.


Sample Input 3

2
10 8

Sample Output 3

Yes

A is a geometric progression with first term 10, common ratio 0.8, and two terms.
Therefore, print Yes.

E - Sake or Water

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

N 個のカップがあり、それぞれのカップには無色透明な液体が入っています。
具体的には、i 番目 (1\leq i\leq N) のカップには A_i ml の液体が入っています。
また、これらのうちちょうど K 個のカップには日本酒が入っており、それ以外には水が入っていることが分かっています。
ただし、どのカップに日本酒が入っているかについては分かっていません。

高橋君は(1 つ以上の)いくつかのカップを選んでそれらに入った液体をすべて飲むことができます。
どのカップに日本酒が入っているかによらず、高橋君が確実に X ml 以上の日本酒を飲むためには、最低何個のカップを選ぶ必要があるか求めてください。
そのような選び方が不可能である場合には -1 を出力してください。

制約

  • 1 \leq K \leq N \leq 3\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 3\times 10^{14}
  • 入力はすべて整数

入力

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

N K X
A_1 A_2 \ldots A_N

出力

条件をみたすために高橋君が選ぶ必要があるカップの個数の最小値を出力せよ。 そのような選び方が不可能である場合には -1 を出力せよ。


入力例 1

3 2 5
10 6 8

出力例 1

2

高橋君が 1 番目と 3 番目のカップを選んで飲んだ場合を考えます。

3 個のカップのうち 2 個に日本酒が入っているため、次の 3 通りが考えられます。

  • 1,2 番目のカップに日本酒が入っていた場合

高橋君は 10 ml の日本酒と 8 ml の水を飲むことになります。

  • 1,3 番目のカップに日本酒が入っていた場合

高橋君は 18 ml の日本酒を飲むことになります。

  • 2,3 番目のカップに日本酒が入っていた場合

高橋君は 8 ml の日本酒と 10 ml の水を飲むことになります。

よって、いずれの場合でも 5 ml 以上の日本酒を飲むことができます。
一方で、どのカップに日本酒が入っているか分かっていない状態で、1 つのみのカップを選んで条件をみたすようにすることは不可能です。

よって、2 を出力します。


入力例 2

2 1 8
6 10

出力例 2

-1

1 番目のカップに日本酒が入っていた場合、どのようにカップを選んでも 8 ml 以上の日本酒を飲むことは不可能です。
よって、-1 を出力します。


入力例 3

5 3 3000000000
1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

5

Score : 300 points

Problem Statement

There are N cups, each containing a colorless and transparent liquid.
Specifically, the i-th (1\leq i\leq N) cup contains A_i ml of liquid.
It is known that exactly K of these cups contain sake (rice wine), and the rest contain water.
However, it is not known which cups contain sake.

Takahashi can choose some (one or more) cups and drink all the liquid in them.
Find the minimum number of cups he needs to choose to ensure he drinks at least X ml of sake, regardless of which cups contain sake.
If such a choice is impossible, print -1.

Constraints

  • 1 \leq K \leq N \leq 3\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 3\times 10^{14}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K X
A_1 A_2 \ldots A_N

Output

Print the minimum number of cups Takahashi needs to choose to satisfy the condition. If such a choice is impossible, print -1.


Sample Input 1

3 2 5
10 6 8

Sample Output 1

2

Consider the case where Takahashi chooses the first and third cups and drinks them.

Two out of the three cups contain sake, so the following three cases are possible:

  • If the first and second cups contain sake

He drinks 10 ml of sake and 8 ml of water.

  • If the first and third cups contain sake

He drinks 18 ml of sake.

  • If the second and third cups contain sake

He drinks 8 ml of sake and 10 ml of water.

Thus, in all cases, he can drink at least 5 ml of sake.
On the other hand, it is impossible to satisfy the condition by choosing only one cup without knowing which cups contain sake.

Therefore, print 2.


Sample Input 2

2 1 8
6 10

Sample Output 2

-1

If the first cup contained sake, it is impossible to drink 8 ml or more of sake no matter which cups are chosen.
Therefore, print -1.


Sample Input 3

5 3 3000000000
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5
F - Sugoroku Destination

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

マス 1, マス 2,\ldots, マス N の N 個のマスが 1 列に並んでいます。 マス i には整数 A _ i (i\le A_ i\le N) が書かれています。

s=1,2,\ldots,N のそれぞれについて、以下の問題を解いてください。

  • はじめ、マス s に駒を置く。「駒が置かれているマスに書かれている整数を x として、駒をマス x に移動させる」という操作を 10 ^ {100} 回行った後、駒が置かれているマスの番号を出力する。

制約

  • 1\le N\le5\times10 ^ 5
  • i\le A _ i\le N\ (1\le i\le N)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N

出力

s=1,2,\ldots,N に対する答えを、この順に空白を区切りとして一行に出力せよ。


入力例 1

7
2 4 7 5 5 6 7

出力例 1

5 5 7 5 5 6 7

s=1 のとき、駒は以下の図のように移動します。

駒がマス 5 に置かれているとき、操作が行われても駒は移動しないため、s=1 のときの答えは 5 となります。


入力例 2

5
1 2 3 4 5

出力例 2

1 2 3 4 5

駒が一度も移動しないこともあります。


入力例 3

15
11 3 10 7 15 10 10 11 11 13 11 12 14 14 15

出力例 3

11 14 14 14 15 14 14 11 11 14 11 12 14 14 15

Score : 300 points

Problem Statement

There are N cells, cell 1, cell 2,\ldots, cell N, arranged in a line. Cell i has an integer A_i\ (i \le A_i \le N) written on it.

For each of s=1,2,\ldots,N, solve the following problem.

  • Initially, place a piece on cell s. After performing the operation "let x be the integer written on the cell where the piece is placed, and then move the piece to cell x" 10^{100} times, output the number of the cell where the piece is placed.

Constraints

  • 1 \le N \le 5\times10^5
  • i \le A_i \le N\ (1 \le i \le N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Output the answers for s=1,2,\ldots,N in this order on a single line, separated by spaces.


Sample Input 1

7
2 4 7 5 5 6 7

Sample Output 1

5 5 7 5 5 6 7

For s=1, the piece moves as shown in the following figure.

When the piece is placed on cell 5, the operation does not move the piece, so the answer for s=1 is 5.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

It is possible that the piece never moves.


Sample Input 3

15
11 3 10 7 15 10 10 11 11 13 11 12 14 14 15

Sample Output 3

11 14 14 14 15 14 14 11 11 14 11 12 14 14 15
G - Squares in Circle

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

二次元座標平面上に 1\times 1 の正方形が無限に敷き詰められています。

ある正方形の中心を中心として、半径 R の円を描いたとき、円に完全に内包される正方形は何個あるでしょうか?

より厳密には、整数組 (i,j) であって、4 点 (i+0.5,j+0.5),(i+0.5,j-0.5),(i-0.5,j+0.5),(i-0.5,j-0.5) 全てが原点との距離が R 以下という条件を満たすものの個数を求めてください。

制約

  • 1\leq R\leq 10^{6}
  • 入力される数値は全て整数

入力

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

R

出力

答えを出力せよ。


入力例 1

2

出力例 1

5

正方形の中心が円の中心と一致するような正方形、及びその正方形に隣接する正方形 4 個の、合計 5 個の正方形が円に完全に内包されています。


入力例 2

4

出力例 2

37

入力例 3

26

出力例 3

2025

Score : 400 points

Problem Statement

On the two-dimensional coordinate plane, there is an infinite tiling of 1 \times 1 squares.

Consider drawing a circle of radius R centered at the center of one of these squares. How many of these squares are completely contained inside the circle?

More precisely, find the number of integer pairs (i,j) such that all four points (i+0.5,j+0.5), (i+0.5,j-0.5), (i-0.5,j+0.5), and (i-0.5,j-0.5) are at a distance of at most R from the origin.

Constraints

  • 1 \leq R \leq 10^{6}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

R

Output

Print the answer.


Sample Input 1

2

Sample Output 1

5

There are a total of five squares completely contained in the circle: the square whose center matches the circle’s center, plus the four squares adjacent to it.


Sample Input 2

4

Sample Output 2

37

Sample Input 3

26

Sample Output 3

2025
H - Modulo MST

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 475 点

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の重み付き単純連結無向グラフと正整数 K が与えられます。
辺 i\ (1\leq i\leq M) は頂点 u _ i​ と頂点 v _ i を結んでおり、重みは w _ i です。

このグラフの全域木 T に対して、T のコストを T に含まれる辺の重みの総和を K で割ったあまりで定めます。 このグラフの全域木のコストの最小値を求めてください。

制約

  • 2\leq N\leq8
  • N-1\leq M\leq\dfrac{N(N-1)}2
  • 1\leq K\leq10^{15}
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
  • 0\leq w _ i\lt K\ (1\leq i\leq M)
  • 与えられるグラフは単純かつ連結
  • 入力はすべて整数

入力

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

N M K
u _ 1 v _ 1 w _ 1
u _ 2 v _ 2 w _ 2
\vdots
u _ M v _ M w _ M

出力

答えを出力せよ。


入力例 1

5 6 328
1 2 99
1 3 102
2 3 86
2 4 94
2 5 95
3 4 81

出力例 1

33

与えられるグラフは次のようになります。

辺 1,3,5,6 の 4 本を含む全域木のコストは (99+86+81+95)\bmod{328}=361\bmod{328}=33 となります。 このグラフの全域木のコストはすべて 33 以上であるため、33 を出力してください。


入力例 2

6 5 998244353
1 2 337361568
1 6 450343304
2 3 61477244
2 5 745383438
4 5 727360840

出力例 2

325437688

このグラフのただ一つの全域木のコスト 325437688 を出力してください。


入力例 3

8 28 936294041850197
1 2 473294720906780
1 3 743030800139244
1 4 709363019414774
1 5 383643612490312
1 6 557102781022861
1 7 623179288538138
1 8 739618599410809
2 3 857687812294404
2 4 893923168139714
2 5 581822471860662
2 6 740549363586558
2 7 307226438833222
2 8 447399029952998
3 4 636318083622768
3 5 44548707643622
3 6 307262781240755
3 7 12070267388230
3 8 700247263184082
4 5 560567890325333
4 6 704726113717147
4 7 588263818615687
4 8 549007536393172
5 6 779230871080408
5 7 825982583786498
5 8 713928998174272
6 7 751331074538826
6 8 449873635430228
7 8 11298381761479

出力例 3

11360716373

入力や答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。

Score : 475 points

Problem Statement

You are given a weighted simple connected undirected graph with N vertices and M edges, where vertices are numbered 1 to N, and edges are numbered 1 to M. Additionally, a positive integer K is given.
Edge i\ (1\leq i\leq M) connects vertices u_i and v_i and has a weight of w_i.

For a spanning tree T of this graph, the cost of T is defined as the sum, modulo K, of the weights of the edges in T. Find the minimum cost of a spanning tree of this graph.

Constraints

  • 2\leq N\leq8
  • N-1\leq M\leq\dfrac{N(N-1)}2
  • 1\leq K\leq10^{15}
  • 1\leq u_i\lt v_i\leq N\ (1\leq i\leq M)
  • 0\leq w_i\lt K\ (1\leq i\leq M)
  • The given graph is simple and connected.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M K
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

Output

Print the answer.


Sample Input 1

5 6 328
1 2 99
1 3 102
2 3 86
2 4 94
2 5 95
3 4 81

Sample Output 1

33

The given graph is shown below:

The cost of the spanning tree containing edges 1,3,5,6 is (99+86+81+95)\bmod{328}=361\bmod{328}=33. The cost of every spanning tree of this graph is at least 33, so print 33.


Sample Input 2

6 5 998244353
1 2 337361568
1 6 450343304
2 3 61477244
2 5 745383438
4 5 727360840

Sample Output 2

325437688

Print the cost of the only spanning tree of this graph, which is 325437688.


Sample Input 3

8 28 936294041850197
1 2 473294720906780
1 3 743030800139244
1 4 709363019414774
1 5 383643612490312
1 6 557102781022861
1 7 623179288538138
1 8 739618599410809
2 3 857687812294404
2 4 893923168139714
2 5 581822471860662
2 6 740549363586558
2 7 307226438833222
2 8 447399029952998
3 4 636318083622768
3 5 44548707643622
3 6 307262781240755
3 7 12070267388230
3 8 700247263184082
4 5 560567890325333
4 6 704726113717147
4 7 588263818615687
4 8 549007536393172
5 6 779230871080408
5 7 825982583786498
5 8 713928998174272
6 7 751331074538826
6 8 449873635430228
7 8 11298381761479

Sample Output 3

11360716373

Note that the input and the answer may not fit into a 32\operatorname{bit} integer.

I - Expensive Expense

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

配点 : 500 点

問題文

AtCoder 王国は N 個の街と N-1 個の道路からなります。
街には 街 1, 街 2, \dots, 街 N と番号がついています。道路にも同様に 道路 1, 道路 2, \dots, 道路 N-1 と番号が付いています。 道路 i は街 A_i と B_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