A - Saturday

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

配点 : 100

問題文

ある日、学校へ行くのに疲れてしまった高橋くんは、土曜日まであと何日あるかを知りたくなりました。
その日は平日で、曜日を英語で表すと S だったことが分かっています。その日より後の直近の土曜日は何日後かを求めてください。

なお、月曜日、火曜日、水曜日、木曜日、金曜日はそれぞれ英語で Monday, Tuesday, Wednesday, Thursday, Friday です。

制約

  • SMonday, Tuesday, Wednesday, Thursday, Friday のいずれかである

入力

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

S

出力

答えを整数として出力せよ。


入力例 1

Wednesday

出力例 1

3

この日は水曜日なので、3 日後に土曜日になります。


入力例 2

Monday

出力例 2

5

Score : 100 points

Problem Statement

One day, tired from going to school, Takahashi wanted to know how many days there were until Saturday.
We know that the day was a weekday, and the name of the day of the week was S in English.
How many days were there until the first Saturday after that day (including Saturday but not the starting day)?

Constraints

  • S is Monday, Tuesday, Wednesday, Thursday, or Friday.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer as an integer.


Sample Input 1

Wednesday

Sample Output 1

3

It was Wednesday, so there were 3 days until the first Saturday after that day.


Sample Input 2

Monday

Sample Output 2

5
B - Order Something Else

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

配点 : 100

問題文

高橋君は、レストランで「AtCoder ドリンク」というドリンクを飲もうとしています。 AtCoder ドリンクは定価である P 円を払えば飲むことができます。

また、高橋君は割引券を持っており、それを使うと AtCoder ドリンクを定価より安い価格である Q 円で飲むことができますが、 その場合には AtCoder ドリンクの他に、N 品ある料理の中から 1 つを追加で注文しなければなりません。 i = 1, 2, \ldots, N について、i 番目の料理の価格は D_i 円です。

高橋君がドリンクを飲むため支払う合計金額の最小値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • 入力はすべて整数

入力

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

N P Q
D_1 D_2 \ldots D_N

出力

答えを出力せよ。


入力例 1

3 100 50
60 20 40

出力例 1

70

割引券を使用して 2 番目の料理を注文することで、ドリンク代 50 円と料理代 20 円の合計 70 円の支払いで AtCoder ドリンクを飲むことができ、支払う合計金額が最小となります。


入力例 2

3 100 50
60000 20000 40000

出力例 2

100

割引券を使用せず定価の 100 円で AtCoder ドリンクを飲むことで、支払う合計金額が最小となります。

Score : 100 points

Problem Statement

Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P yen.

He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, \ldots, N, the price of the i-th dish is D_i yen.

Print the minimum total amount of money that he must pay to get the drink.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • All input values are integers.

Input

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

N P Q
D_1 D_2 \ldots D_N

Output

Print the answer.


Sample Input 1

3 100 50
60 20 40

Sample Output 1

70

If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen for the dish, for a total of 70 yen, which is the minimum total payment needed.


Sample Input 2

3 100 50
60000 20000 40000

Sample Output 2

100

The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.

C - Sandwich Number

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

配点 : 200

問題文

英大文字と数字からなる文字列 S が与えられるので、S が以下の条件を満たすか判定してください。

  • S は次の文字または文字列をこの順番で連結して得られる。
    • 一文字の英大文字
    • 100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列
    • 一文字の英大文字

制約

  • S は英大文字と数字からなる
  • S の長さは 1 以上 10 以下

入力

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

S

出力

S が問題文中の条件を満たすなら Yes と、満たさないなら No と出力せよ。


入力例 1

Q142857Z

出力例 1

Yes

SQ142857Z をこの順に連結して得られます。
QZ は英大文字であり、142857100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列なので、S は条件を満たします。


入力例 2

AB912278C

出力例 2

No

AB は一文字の英大文字ではないため、S は条件を満たしません。


入力例 3

X900000

出力例 3

No

S の末尾の一文字が英大文字ではないため、S は条件を満たしません。


入力例 4

K012345K

出力例 4

No

012345100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列ではないため、S は条件を満たしません。

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters and digits. Determine whether S satisfies the following condition.

  • S is a concatenation of the following characters and string in the order listed.
    • An uppercase English letter
    • A string of length 6 that is a decimal representation of an integer between 100000 and 999999, inclusive
    • An uppercase English letter

Constraints

  • S consists of uppercase English letters and digits.
  • The length of S is between 1 and 10, inclusive.

Input

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

S

Output

If S satisfies the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1

Q142857Z

Sample Output 1

Yes

S is a concatenation of Q, 142857, and Z in this order.
Q and Z are uppercase English letters, and 142857 is a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S satisfies the condition.


Sample Input 2

AB912278C

Sample Output 2

No

AB is not an uppercase English letter, so S does not satisfy the condition.


Sample Input 3

X900000

Sample Output 3

No

The last character of S is not an uppercase English letter, so S does not satisfy the condition.


Sample Input 4

K012345K

Sample Output 4

No

012345 is not a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S does not satisfy the condition.

D - Bombs

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

配点 : 200

問題文

R 行横 C 列の盤面があります。上から i 行目、左から j 列目のマスを (i,j) と表します。

(i,j) の現在の状態が文字 B_{i,j} として与えられます。 . は空きマス、# は壁があるマスを表し、 1, 2,\dots, 9 はそれぞれ威力 1,2,\dots,9 の爆弾があるマスを表します。

次の瞬間に、全ての爆弾が同時に爆発します。 爆弾が爆発すると、爆弾があるマスからのマンハッタン距離がその爆弾の威力以下であるような全てのマス(その爆弾があるマス自体を含む)が空きマスに変わります。 ここで、(r_1,c_1) から (r_2,c_2) までのマンハッタン距離は |r_1-r_2|+|c_1-c_2| です。

爆発後の盤面を出力してください。

制約

  • 1\leq R,C \leq 20
  • R,C は整数
  • B_{i,j}., #, 1, 2,\dots, 9 のいずれかである

入力

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

R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}

出力

爆発後の盤面を R 行で出力せよ。盤面の表し方は入力と同じ形式を用いること (RC を出力する必要はない)。


入力例 1

4 4
.1.#
###.
.#2.
#.##

出力例 1

...#
#...
....
#...

爆弾の効果範囲を表す図

  • (1,2) にある爆弾の爆発によって、上図の青いマスと紫のマスが空きマスに変わります。
  • (3,3) にある爆弾の爆発によって、上図の赤いマスと紫のマスが空きマスに変わります。

この例のように、爆弾が効果を及ぼす範囲に被りがあることもあります。


入力例 2

2 5
..#.#
###.#

出力例 2

..#.#
###.#

爆弾が 1 つもないこともあります。


入力例 3

2 3
11#
###

出力例 3

...
..#

入力例 4

4 6
#.#3#.
###.#.
##.###
#1..#.

出力例 4

......
#.....
#....#
....#.

Score : 200 points

Problem Statement

We have a board with R rows running horizontally and C columns running vertically. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

You are given characters B_{i,j} representing the current states of (i,j). . represents an empty square; # represents a square with a wall; 1, 2,\dots, 9 represent a square with a bomb of power 1,2,\dots,9, respectively.

At the next moment, all bombs will explode simultaneously. When a bomb explodes, every square whose Manhattan distance from the square with the bomb is not greater than the power of the bomb will turn into an empty square. Here, the Manhattan distance from (r_1,c_1) to (r_2,c_2) is |r_1-r_2|+|c_1-c_2|.

Print the board after the explosions.

Constraints

  • 1\leq R,C \leq 20
  • R and C are integers.
  • Each B_{i,j} is one of ., #, 1, 2, \dots, 9.

Input

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

R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}

Output

Print R lines representing the board after the explosions. Use the format used in the input (do not print R or C).


Sample Input 1

4 4
.1.#
###.
.#2.
#.##

Sample Output 1

...#
#...
....
#...

Figure representing the explosion ranges of bombs

  • The explosion of the bomb at (1,2) will turn the blue squares and purple squares in the above figure into empty squares.
  • The explosion of the bomb at (3,3) will turn the red squares and purple squares in the above figure into empty squares.

As seen in this sample, the explosion ranges of bombs may overlap.


Sample Input 2

2 5
..#.#
###.#

Sample Output 2

..#.#
###.#

There may be no bombs.


Sample Input 3

2 3
11#
###

Sample Output 3

...
..#

Sample Input 4

4 6
#.#3#.
###.#.
##.###
#1..#.

Sample Output 4

......
#.....
#....#
....#.
E - Flavors

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

配点 : 300

問題文

N カップのアイスクリームがあります。
i カップ目の味は F_i 、美味しさは S_i ( S_i は偶数 ) です。

あなたは、 N 個のカップの中から 2 つを選んで食べることにしました。
このときの満足度は次のように定義されます。

  • 食べたアイスクリームの美味しさを s,t ( 但し、 s \ge t ) とする。
    • 2 つのカップの味が異なるなら、満足度は \displaystyle s+t である。
    • そうでないなら、満足度は \displaystyle s + \frac{t}{2} である。

満足度として達成可能な最大値を求めてください。

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i は偶数

入力

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

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

出力

答えを整数として出力せよ。


入力例 1

4
1 4
2 10
2 8
3 6

出力例 1

16

2 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 2 カップ目の味は 2 、美味しさは 10 です。
  • 4 カップ目の味は 3 、美味しさは 6 です。
  • 両者の味は異なるので、満足度は 10+6=16 です。

以上より、満足度 16 を達成できます。
満足度を 16 より大きくすることはできません。


入力例 2

4
4 10
3 2
2 4
4 12

出力例 2

17

1 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 1 カップ目の味は 4 、美味しさは 10 です。
  • 4 カップ目の味は 4 、美味しさは 12 です。
  • 両者の味は同じなので、満足度は 12+\frac{10}{2}=17 です。

以上より、満足度 17 を達成できます。
満足度を 17 より大きくすることはできません。

Score : 300 points

Problem Statement

We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are F_i and S_i, respectively (S_i is an even number).

You will choose and eat two of the N cups.
Your satisfaction here is defined as follows.

  • Let s and t (s \ge t) be the deliciousness of the eaten cups.
    • If the two cups have different flavors, your satisfaction is \displaystyle s+t.
    • Otherwise, your satisfaction is \displaystyle s + \frac{t}{2}.

Find the maximum achievable satisfaction.

Constraints

  • All input values are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i is even.

Input

Input is given from Standard Input in the following format:

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

Output

Print the answer as an integer.


Sample Input 1

4
1 4
2 10
2 8
3 6

Sample Output 1

16

Consider eating the second and fourth cups.

  • The second cup has a flavor of 2 and deliciousness of 10.
  • The fourth cup has a flavor of 3 and deliciousness of 6.
  • Since they have different flavors, your satisfaction is 10+6=16.

Thus, you can achieve the satisfaction of 16.
You cannot achieve a satisfaction greater than 16.


Sample Input 2

4
4 10
3 2
2 4
4 12

Sample Output 2

17

Consider eating the first and fourth cups.

  • The first cup has a flavor of 4 and deliciousness of 10.
  • The fourth cup has a flavor of 4 and deliciousness of 12.
  • Since they have the same flavor, your satisfaction is 12+\frac{10}{2}=17.

Thus, you can achieve the satisfaction of 17.
You cannot achieve a satisfaction greater than 17.

F - X drawing

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

配点 : 300

問題文

上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。

高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。

  • \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
  • \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。

この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • 1 \leq P \leq Q \leq N
  • 1 \leq R \leq S \leq N
  • (Q-P+1)\times(S-R+1)\leq 3\times 10^5
  • 入力は全て整数である。

入力

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

N A B
P Q R S

出力

Q-P+1 行出力せよ。
各行は #. のみからなる長さ S-R+1 の文字列であり、 i 行目の文字列の j 番目の文字が # であることは (P+i-1,R+j-1) が黒く塗られていることを、 . であることは (P+i-1,R+j-1) が白く塗られていることをさす。


入力例 1

5 3 2
1 5 1 5

出力例 1

...#.
#.#..
.#...
#.#..
...#.

1 つめの操作で (2,1), (3,2), (4,3), (5,4)4 マスが、 2 つめの操作で (4,1), (3,2), (2,3), (1,4)4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。


入力例 2

5 3 3
4 5 2 5

出力例 2

#.#.
...#

操作によって、 (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5)9 マスが 黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。


入力例 3

1000000000000000000 999999999999999999 999999999999999999
999999999999999998 1000000000000000000 999999999999999998 1000000000000000000

出力例 3

#.#
.#.
#.#

入力が 32 bit 整数型に収まらないことがあることに注意してください。

Score : 300 points

Problem Statement

There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.

Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.

  • For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
  • For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.

In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • 1 \leq P \leq Q \leq N
  • 1 \leq R \leq S \leq N
  • (Q-P+1)\times(S-R+1)\leq 3\times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B
P Q R S

Output

Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and .. The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.


Sample Input 1

5 3 2
1 5 1 5

Sample Output 1

...#.
#.#..
.#...
#.#..
...#.

The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.


Sample Input 2

5 3 3
4 5 2 5

Sample Output 2

#.#.
...#

The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.


Sample Input 3

1000000000000000000 999999999999999999 999999999999999999
999999999999999998 1000000000000000000 999999999999999998 1000000000000000000

Sample Output 3

#.#
.#.
#.#

Note that the input may not fit into a 32-bit integer type.

G - Prefix K-th Max

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

配点 : 400

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N)、および正整数 K が与えられます。

i=K,K+1,\ldots,N について、以下を求めてください。

  • P の先頭 i 項のうち、K 番目に大きい値

制約

  • 1 \leq K \leq N \leq 5 \times 10^5
  • (P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の並び替えによって得られる
  • 入力はすべて整数

入力

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

N K
P_1 P_2 \ldots P_N

出力

i=K,K+1,\ldots,N についてこの順に、問題文中で問われている値を改行区切りで出力せよ。


入力例 1

3 2
1 2 3

出力例 1

1
2
  • P の先頭 2 項、すなわち (P_1,P_2)=(1,2) の中で K=2 番目に大きい値は 1 となります。
  • P の先頭 3 項、すなわち (P_1,P_2,P_3)=(1,2,3) の中で K=2 番目に大きい値は 2 となります。

入力例 2

11 5
3 7 2 5 11 6 1 9 8 10 4

出力例 2

2
3
3
5
6
7
7

Score : 400 points

Problem Statement

Given are a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) and a positive integer K.

For each i=K,K+1,\ldots,N, find the following.

  • The K-th greatest value among the first i terms of P.

Constraints

  • 1 \leq K \leq N \leq 5 \times 10^5
  • (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \ldots P_N

Output

For each i=K, K+1, \ldots, N, in this order, print the specified value in Problem Statement, separated by newlines.


Sample Input 1

3 2
1 2 3

Sample Output 1

1
2
  • The (K=) 2-nd greatest value among the first 2 terms of P, (P_1,P_2)=(1,2), is 1.
  • The (K=) 2-nd greatest value among the first 3 terms of P, (P_1,P_2,P_3)=(1,2,3), is 2.

Sample Input 2

11 5
3 7 2 5 11 6 1 9 8 10 4

Sample Output 2

2
3
3
5
6
7
7
H - Packing Potatoes

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

配点 : 500

問題文

ベルトコンベアに載って 10^{100} 個のじゃがいもが 1 個ずつ流れてきます。流れてくるじゃがいもの重さは長さ N の数列 W = (W_0, \dots, W_{N-1}) で表され、i \, (1 \leq i \leq 10^{100}) 番目に流れてくるじゃがいもの重さは W_{(i-1) \bmod N} です。ここで、(i-1) \bmod Ni - 1N で割った余りを表します。

高橋君は、まず空の箱を用意し、次のルールに従ってじゃがいもを順番に箱に詰めていきます。

  • じゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和が X 以上になったら、その箱には蓋をし、新たに空の箱を用意する。

Q 個のクエリが与えられます。i \, (1 \leq i \leq Q) 番目のクエリでは、正整数 K_i が与えられるので、K_i 番目に蓋をされた箱に入っているじゃがいもの個数を求めてください。問題の制約下で、蓋をされた箱が K_i 個以上存在することが証明できます。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq X \leq 10^9
  • 1 \leq W_i \leq 10^9 \, (0 \leq i \leq N - 1)
  • 1 \leq K_i \leq 10^{12} \, (1 \leq i \leq Q)
  • 入力は全て整数

入力

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

N Q X
W_0 W_1 \ldots W_{N-1}
K_1
\vdots
K_Q

出力

Q 行出力せよ。i \, (1 \leq i \leq Q) 行目には、i 番目のクエリへの答えを出力せよ。


入力例 1

3 2 5
3 4 1
1
2

出力例 1

2
3

2 つの箱に蓋をするまでの高橋くんの行動は以下の通りです。

  • 空の箱を用意する。
  • 1 番目のじゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和は 3 である。
  • 2 番目のじゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和は 3 + 4 = 7 であり、X = 5 以上になったのでこの箱には蓋をする。
  • 新たに空の箱を用意する。
  • 3 番目のじゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和は 1 である。
  • 4 番目のじゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和は 1 + 3 = 4 である。
  • 5 番目のじゃがいもを箱に入れる。箱に入っているじゃがいもの重さの総和は 1 + 3 + 4 = 8 であり、X = 5 以上になったのでこの箱には蓋をする。

1 番目に蓋をされた箱には 2 つのじゃがいもが入っており、2 番目に蓋をされた箱には 3 つのじゃがいもが入っています。


入力例 2

10 5 20
5 8 5 9 8 7 4 4 8 2
1
1000
1000000
1000000000
1000000000000

出力例 2

4
5
5
5
5

Score : 500 points

Problem Statement

10^{100} potatoes are coming from a conveyor belt one by one. The weights of the potatoes are described by a sequence W = (W_0, \dots, W_{N-1}) of length N: the weight of the i-th potato coming is W_{(i-1) \bmod N}, where (i-1) \bmod N denotes the remainder when i - 1 is divided by N.

Takahashi will prepare an empty box and then pack the potatoes in order, as follows.

  • Pack the incoming potato into the box. If the total weight of the potatoes in the box is now X or greater, seal that box and prepare a new empty box.

You are given Q queries. In the i-th query (1 \leq i \leq Q), given a positive integer K_i, find the number of potatoes in the K_i-th box to be sealed. It can be proved that, under the Constraints of the problem, there will be at least K_i sealed boxes.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq X \leq 10^9
  • 1 \leq W_i \leq 10^9 \, (0 \leq i \leq N - 1)
  • 1 \leq K_i \leq 10^{12} \, (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q X
W_0 W_1 \ldots W_{N-1}
K_1
\vdots
K_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.


Sample Input 1

3 2 5
3 4 1
1
2

Sample Output 1

2
3

Before sealing the 2-nd box, Takahashi will do the following:

  • Prepare an empty box.
  • Pack the 1-st potato into the box. Now, the total weight of potatoes in the box is 3.
  • Pack the 2-nd potato into the box. Now, the total weight of potatoes in the box is 3 + 4 = 7, which is not less than X = 5, so seal this box.
  • Prepare a new empty box.
  • Pack the 3-rd potato into the box. Now, the total weight of potatoes in the box is 1.
  • Pack the 4-th potato into the box. Now, the total weight of potatoes in the box is 1 + 3 = 4.
  • Pack the 5-th potato into the box. Now, the total weight of potatoes in the box is 1 + 3 + 4 = 8, which is not less than X = 5, so seal this box.

The 1-st box sealed contains 2 potatoes, and the 2-nd box sealed contains 3 potatoes.


Sample Input 2

10 5 20
5 8 5 9 8 7 4 4 8 2
1
1000
1000000
1000000000
1000000000000

Sample Output 2

4
5
5
5
5
I - Expensive Expense

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

配点 : 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