Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 枚からなるカードの山があり、上から i 枚目のカードには整数 A_i が書かれています。
山の下から K 枚のカードを取り出し、順序を保ったまま山の一番上に乗せました。
この操作後の山の上から順に、カードに書かれた整数を出力してください。
制約
- 1 \leq K < N \leq 100
- 1 \leq A_i \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
操作後の山の上から i 枚目のカードに書かれた整数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。
入力例 1
5 3 1 2 3 4 5
出力例 1
3 4 5 1 2
最初、カードに書かれた整数は山の上から順に 1,2,3,4,5 です。
山の下から 3 枚のカードを取り出し、そのまま山の一番上に乗せたあと、カードに書かれた整数は山の上から順に 3,4,5,1,2 となります。
入力例 2
6 2 1 2 1 2 1 2
出力例 2
1 2 1 2 1 2
カードに書かれている整数は相異なるとは限りません。
Score : 100 points
Problem Statement
There is a stack of N cards, and the i-th card from the top has an integer A_i written on it.
You take K cards from the bottom of the stack and place them on top of the stack, maintaining their order.
Print the integers written on the cards from top to bottom after the operation.
Constraints
- 1 \leq K < N \leq 100
- 1 \leq A_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Let B_i be the integer written on the i-th card from the top of the stack after the operation. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.
Sample Input 1
5 3 1 2 3 4 5
Sample Output 1
3 4 5 1 2
Initially, the integers written on the cards are 1,2,3,4,5 from top to bottom.
After taking three cards from the bottom of the stack and placing them on top, the integers written on the cards become 3,4,5,1,2 from top to bottom.
Sample Input 2
6 2 1 2 1 2 1 2
Sample Output 2
1 2 1 2 1 2
The integers written on the cards are not necessarily distinct.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字からなる長さ 3 の文字列 S が与えられます。S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。
制約
- S は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。
入力例 1
ABC
出力例 1
No
S = ABC のとき、S は ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれとも等しくないので No を出力します。
入力例 2
FAC
出力例 2
Yes
入力例 3
XYX
出力例 3
No
Score : 100 points
Problem Statement
Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Constraints
- S is a length-3 string consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Sample Input 1
ABC
Sample Output 1
No
When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.
Sample Input 2
FAC
Sample Output 2
Yes
Sample Input 3
XYX
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 8 マス、横 8 マスの 64 マスからなるマス目があります。 上から i 行目 (1\leq i\leq8) 、左から j 列目 (1\leq j\leq8) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。
マスの状態は長さ 8 の文字列からなる長さ 8 の列 (S _ 1,S _ 2,S _ 3,\ldots,S _ 8) で表されます。
マス (i,j) (1\leq i\leq8,1\leq j\leq8) は、S _ i の j 文字目が . のとき空マスで、# のときコマが置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどちらかの条件を満たすコマを取ることができます。
- i 行目のマスに置かれている
- j 列目のマスに置かれている
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。

あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- S _ i は
.,#からなる長さ 8 の文字列 (1\leq i\leq 8)
入力
入力は以下の形式で標準入力から与えられる。
S _ 1 S _ 2 S _ 3 S _ 4 S _ 5 S _ 6 S _ 7 S _ 8
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
出力例 1
4
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。

よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスはマス (6,6), マス (6,7), マス (7,6), マス (7,7) の 4 マスです。
入力例 2
........ ........ ........ ........ ........ ........ ........ ........
出力例 2
64
コマがひとつも置かれていないこともあります。
入力例 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
出力例 3
4
Score : 200 points
Problem Statement
There is a grid of 64 squares with 8 rows and 8 columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq8) and j-th column from the left (1\leq j\leq8).
Each square is either empty or has a piece placed on it.
The state of the squares is represented by a sequence (S_1,S_2,S_3,\ldots,S_8) of 8 strings of length 8.
Square (i,j) (1\leq i\leq8,1\leq j\leq8) is empty if the j-th character of S_i is ., and has a piece if it is #.
You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.
A piece placed on square (i,j) can capture pieces that satisfy either of the following conditions:
- Placed on a square in row i
- Placed on a square in column j
For example, a piece placed on square (4,4) can capture pieces placed on the squares shown in blue in the following figure:

How many squares can you place your piece on?
Constraints
- Each S_i is a string of length 8 consisting of
.and#(1\leq i\leq 8).
Input
The input is given from Standard Input in the following format:
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
Sample Output 1
4
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece without it being captured on 4 squares: square (6,6), square (6,7), square (7,6), and square (7,7).
Sample Input 2
........ ........ ........ ........ ........ ........ ........ ........
Sample Output 2
64
There may be no pieces on the grid.
Sample Input 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
年に 1 度の節分には年齢と同じ数の豆を食べます。高橋君はそれ以外に豆を食べることはありません。
高橋君は、今年 ( 0 年後 ) の節分の時点で N 歳です。
高橋君が今年以降 (今年を含む) で累計 K 個以上の豆を食べたことになるのは、最短で何年後の節分ですか?
なお、高橋君は不死身なので、高橋君が今年以降で累計 K 個以上の豆を食べたという状況は必ず訪れます。
制約
- 入力は全て整数
- 1 \le N,K \le 10^8
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
答えを出力せよ。
入力例 1
4 43
出力例 1
6
- 0 年後の節分に高橋君は 4 個の豆を食べ、今年以降で累計で 4 個の豆を食べます。
- 1 年後の節分に高橋君は 5 個の豆を食べ、今年以降で累計で 9 個の豆を食べます。
- 2 年後の節分に高橋君は 6 個の豆を食べ、今年以降で累計で 15 個の豆を食べます。
- 3 年後の節分に高橋君は 7 個の豆を食べ、今年以降で累計で 22 個の豆を食べます。
- 4 年後の節分に高橋君は 8 個の豆を食べ、今年以降で累計で 30 個の豆を食べます。
- 5 年後の節分に高橋君は 9 個の豆を食べ、今年以降で累計で 39 個の豆を食べます。
- 6 年後の節分に高橋君は 10 個の豆を食べ、今年以降で累計で 49 個の豆を食べます。
6 年後の節分の時点で累計 43 個以上の豆を食べたことになるので、 6 と出力してください。
入力例 2
100000000 100000000
出力例 2
0
今年のみで食べた豆の個数が K 個以上に達することもあります。
入力例 3
1234 12345678
出力例 3
3886
Score : 200 points
Problem Statement
At the annual Setsubun festival, one eats the same number of beans as their age. Takahashi-kun does not eat beans at any other time.
He is N years old at this year's Setsubun (0 years later).
At the earliest, how many years later will he have eaten a total of K or more beans from this year onward (including this year)?
He is immortal, so he will eventually have eaten a total of K or more beans from this year onward.
Constraints
- All input values are integers.
- 1 \le N,K \le 10^8
Input
The input is given from Standard Input in the following format:
N K
Output
Print the answer.
Sample Input 1
4 43
Sample Output 1
6
- At Setsubun 0 years later, Takahashi-kun eats 4 beans, totaling 4 beans from this year onward.
- At Setsubun 1 year later, he eats 5 beans, totaling 9 beans from this year onward.
- At Setsubun 2 years later, he eats 6 beans, totaling 15 beans from this year onward.
- At Setsubun 3 years later, he eats 7 beans, totaling 22 beans from this year onward.
- At Setsubun 4 years later, he eats 8 beans, totaling 30 beans from this year onward.
- At Setsubun 5 years later, he eats 9 beans, totaling 39 beans from this year onward.
- At Setsubun 6 years later, he eats 10 beans, totaling 49 beans from this year onward.
At Setsubun 6 years later, he will have eaten a total of 43 or more beans, so output 6.
Sample Input 2
100000000 100000000
Sample Output 2
0
The number of beans eaten this year alone may reach K or more.
Sample Input 3
1234 12345678
Sample Output 3
3886
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
十進法ですべての桁の数字が 1 である整数をレピュニットと呼びます。レピュニットを小さい順に並べると 1,11,111,\ldots です。
ちょうど 3 つのレピュニットの和として表せる整数のうち N 番目に小さいものを求めてください。
制約
- N は 1 以上 333 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
5
出力例 1
113
ちょうど 3 つのレピュニットの和として表せる整数を小さい順に並べると 3,13,23,33,113,\ldots です。例えば 113 は 113=1+1+111 と表せます。
3 つのレピュニットは相異ならなくてもよいことに注意してください。
入力例 2
19
出力例 2
2333
入力例 3
333
出力例 3
112222222233
Score : 300 points
Problem Statement
A repunit is an integer whose digits are all 1 in decimal representation. The repunits in ascending order are 1, 11, 111, \ldots.
Find the N-th smallest integer that can be expressed as the sum of exactly three repunits.
Constraints
- N is an integer between 1 and 333, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
5
Sample Output 1
113
The integers that can be expressed as the sum of exactly three repunits are 3, 13, 23, 33, 113, \ldots in ascending order. For example, 113 can be expressed as 113 = 1 + 1 + 111.
Note that the three repunits do not have to be distinct.
Sample Input 2
19
Sample Output 2
2333
Sample Input 3
333
Sample Output 3
112222222233
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 次元平面上に点 1 から点 N までの N 個の点があります。点 i (1\le i\le N) の座標は (X_i,Y_i) です。ここで、X,Y はそれぞれ (1,2,\ldots,N) の順列であることが保証されます。
左下の頂点を (0,0) 、右上の頂点を (X_i,Y_i) とする x 軸に平行な辺と y 軸に平行な辺のみからなる長方形の内部(辺上を含まない)に点 1 から点 N までの N 個の点をどれも含まないような i の個数を求めてください。
制約
- 1\le N\le 3\times 10^5
- 1\le X_i,Y_i\le N
- X,Y はそれぞれ (1,2,\ldots,N) の順列
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを出力せよ。
入力例 1
3 2 1 1 3 3 2
出力例 1
2
i=1,2 の 2 つが条件を満たします。
i=3 の場合、左下を (0,0) 、右上を (3,2) とする x 軸に平行な辺を持つ長方形の内部(辺上を含まない)に点 1 を含みます。
入力例 2
5 1 1 4 2 2 3 5 5 3 4
出力例 2
1
入力例 3
7 3 4 6 1 5 5 2 7 7 2 1 3 4 6
出力例 3
2
Score : 300 points
Problem Statement
There are N points numbered 1 through N on a two-dimensional plane. Point i (1\le i\le N) has coordinates (X_i,Y_i). Here, it is guaranteed that X and Y are each permutations of (1,2,\ldots,N).
Find the number of values of i such that the interior (not including the boundary) of the rectangle with sides parallel to the x-axis and y-axis, with lower-left vertex (0,0) and upper-right vertex (X_i,Y_i), contains none of the N points numbered 1 through N.
Constraints
- 1\le N\le 3\times 10^5
- 1\le X_i,Y_i\le N
- X and Y are each permutations of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Output the answer.
Sample Input 1
3 2 1 1 3 3 2
Sample Output 1
2
The two values i=1,2 satisfy the condition.
For i=3, the interior (not including the boundary) of the rectangle with sides parallel to the x-axis, with lower-left vertex (0,0) and upper-right vertex (3,2), contains point 1.
Sample Input 2
5 1 1 4 2 2 3 5 5 3 4
Sample Output 2
1
Sample Input 3
7 3 4 6 1 5 5 2 7 7 2 1 3 4 6
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1 から N までの番号がついた N 枚のカードが一列に並んでいて、各 i\ (1\leq i < N) に対してカード i とカード i+1 が隣り合っています。 カード i の表には A_i が、裏には B_i が書かれており、最初全てのカードは表を向いています。
今から、N 枚のカードのうち好きな枚数 (0 枚でも良い) を選んで裏返すことを考えます。 裏返すカードの選び方は 2^N 通りありますが、そのうち以下の条件を満たすものの数を 998244353 で割った余りを求めてください。
- 選んだカードを裏返した後、どの隣り合う 2 枚のカードについても、向いている面に書かれた数が相異なる。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを整数として出力せよ。
入力例 1
3 1 2 4 2 3 4
出力例 1
4
裏返すカードの番号の集合を S とします。
例えば S=\{2,3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,2,4 となるため条件を満たします。
一方 S=\{3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,4,4 となり、カード 2 とカード 3 の数が一致するため条件を満たしません。
条件を満たす S は \{\},\{1\},\{2\},\{2,3\} の 4 通りです。
入力例 2
4 1 5 2 6 3 7 4 8
出力例 2
16
入力例 3
8 877914575 602436426 861648772 623690081 476190629 262703497 971407775 628894325 822804784 450968417 161735902 822804784 161735902 822804784 822804784 161735902
出力例 3
48
Score : 400 points
Problem Statement
N cards, numbered 1 through N, are arranged in a line. For each i\ (1\leq i < N), card i and card (i+1) are adjacent to each other. Card i has A_i written on its front, and B_i written on its back. Initially, all cards are face up.
Consider flipping zero or more cards chosen from the N cards. Among the 2^N ways to choose the cards to flip, find the number, modulo 998244353, of such ways that:
- when the chosen cards are flipped, for every pair of adjacent cards, the integers written on their face-up sides are different.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer as an integer.
Sample Input 1
3 1 2 4 2 3 4
Sample Output 1
4
Let S be the set of card numbers to flip.
For example, when S=\{2,3\} is chosen, the integers written on their visible sides are 1,2, and 4, from card 1 to card 3, so it satisfies the condition.
On the other hand, when S=\{3\} is chosen, the integers written on their visible sides are 1,4, and 4, from card 1 to card 3, where the integers on card 2 and card 3 are the same, violating the condition.
Four S satisfy the conditions: \{\},\{1\},\{2\}, and \{2,3\}.
Sample Input 2
4 1 5 2 6 3 7 4 8
Sample Output 2
16
Sample Input 3
8 877914575 602436426 861648772 623690081 476190629 262703497 971407775 628894325 822804784 450968417 161735902 822804784 161735902 822804784 822804784 161735902
Sample Output 3
48
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 頂点の有向グラフがあり、頂点には 1, 2, \ldots, N の番号が付いています。
辺の情報は N^2 個の文字 C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N} によって与えられます。ここで、C_{i, j} は英小文字あるいは - です。
C_{i, j} が英小文字のとき頂点 i から頂点 j へ向かう辺がちょうど 1 つ存在してラベル C_{i, j} が付いており、C_{i, j} が - のとき頂点 i から頂点 j へ向かう辺は存在しません。
1 \leq i, j \leq N を満たす各整数組 (i, j) について、以下の問題に対する答えを求めてください。
- 頂点 i から頂点 j に向かう(単純とは限らない)パスのうち、辺に付けられたラベルの文字を順に結合した文字列が回文となるようなものの中で最も短いものの長さを求めよ。ただし、そのようなパスがない場合は答えは -1 とせよ。
制約
- 1 \leq N \leq 100
- N は整数
- C_{i, j} は英小文字あるいは
-
入力
入力は以下の形式で標準入力から与えられる。
N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}
出力
整数組 (i, j) に対する答えを A_{i, j} として以下の形式で出力せよ。
A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}
入力例 1
4 ab-- --b- ---a c---
出力例 1
0 1 2 4 -1 0 1 -1 3 -1 0 1 1 -1 -1 0
例として (i, j) = (1, 4) の場合について説明します。
頂点 1 \to 1 \to 2 \to 3 \to 4 というパスにおける辺に付けられた文字のラベルを順に結合すると文字列 abba が得られます。これは回文であり、頂点 1 から頂点 4 へ向かう長さ 3 以下のパスであって辺に付けられたラベルの文字を順に結合した文字列が回文となるものは存在しないため (i, j) = (1, 4) に対する答えは 4 となります。
空文字列も回文であることに注意してください。
入力例 2
5 us--- -st-- --s-- u--s- ---ts
出力例 2
0 1 3 -1 -1 -1 0 1 -1 -1 -1 -1 0 -1 -1 1 3 -1 0 -1 -1 -1 5 1 0
Score : 450 points
Problem Statement
We have a directed graph with N vertices, numbered 1, 2, \ldots, N.
Information about the edges is given by N^2 characters C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N}. Here, each C_{i, j} is either a lowercase English letter or -.
If C_{i, j} is a lowercase English letter, then there is exactly one directed edge from vertex i to vertex j labeled C_{i, j}. If C_{i, j} is -, there is no edge from vertex i to vertex j.
For each integer pair (i, j) with 1 \leq i, j \leq N, answer the following question:
- Among all (not necessarily simple) paths from vertex i to vertex j whose concatenation of labels on the edges forms a palindrome, what is the length of the shortest such path? If there is no such path, the answer is -1.
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each C_{i, j} is either a lowercase English letter or
-.
Input
The input is given from Standard Input in the following format:
N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}
Output
Let A_{i, j} be the answer to the question for the pair (i, j). Print them in the following format:
A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}
Sample Input 1
4 ab-- --b- ---a c---
Sample Output 1
0 1 2 4 -1 0 1 -1 3 -1 0 1 1 -1 -1 0
For example, consider the case (i, j) = (1, 4).
By taking the path 1 \to 1 \to 2 \to 3 \to 4, and concatenating the labels on its edges in order, we get the string abba, which is a palindrome.
There is no path of length at most 3 from vertex 1 to vertex 4 whose concatenation of labels is a palindrome. Thus, the answer for (1, 4) is 4.
Note that the empty string is also a palindrome.
Sample Input 2
5 us--- -st-- --s-- u--s- ---ts
Sample Output 2
0 1 3 -1 -1 -1 0 1 -1 -1 -1 -1 0 -1 -1 1 3 -1 0 -1 -1 -1 5 1 0
Time Limit: 4 sec / Memory Limit: 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