A - Anyway Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

整数 a, b, c, d が与えられるので、以下の指示に従って 2 行出力してください。

1 行目は (a + b) \times (c - d) の計算結果を整数として出力してください。
2 行目は入力にかかわらず Takahashi と出力してください。

制約

  • -100 \leq a, b, c, d \leq 100
  • a,b,c,d は整数

入力

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

a b c d 

出力

問題文の指示に従って 2 行出力せよ。


入力例 1

1 2 5 3

出力例 1

6
Takahashi

(1 + 2) \times(5 - 3) = 3 \times 2 = 6 です。よって 1 行目は 6 を出力します。
2 行目は Takahashi を出力します。1 文字目を小文字にしたりスペルを誤ったりすると誤答となるので注意してください。


入力例 2

10 -20 30 -40

出力例 2

-700
Takahashi

入出力に負の数が含まれる場合もあります。


入力例 3

100 100 100 -100

出力例 3

40000
Takahashi

Score : 100 points

Problem Statement

You are given integers a, b, c, and d. Print two lines as follows.

The first line should contain the result of calculating (a + b) \times (c - d) as an integer.
The second line should contain Takahashi, regardless of the input.

Constraints

  • -100 \leq a, b, c, d \leq 100
  • a, b, c, and d are integers.

Input

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

a b c d 

Output

Print two lines according to the Problem Statement.


Sample Input 1

1 2 5 3

Sample Output 1

6
Takahashi

We have (1 + 2) \times(5 - 3) = 3 \times 2 = 6, so the first line should contain 6.
The second line should contain Takahashi. Lowercasing the first character or incorrect spelling will not be accepted, so be careful.


Sample Input 2

10 -20 30 -40

Sample Output 2

-700
Takahashi

The input or output may contain negative numbers.


Sample Input 3

100 100 100 -100

Sample Output 3

40000
Takahashi
B - Past ABCs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

長さ 6 の文字列 S が与えられます。S の先頭 3 文字は ABC であり、末尾 3 文字は数字であることが保証されます。

Sが、このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称であるかどうか判定してください。

ただし、文字列 T が「このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称」であるとは、以下の 348 個の文字列のうちいずれかに等しいことと定めます。

ABC001, ABC002, \ldots, ABC314, ABC315, ABC317, ABC318, \ldots, ABC348, ABC349

特に ABC316 が含まれないことに注意してください。

制約

  • S は先頭 3 文字が ABC、末尾 3 文字が数字である長さ 6 の文字列

入力

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

S

出力

Sが、このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称であるなら Yes、そうでないなら No と出力せよ。


入力例 1

ABC349

出力例 1

Yes

ABC349 は先週AtCoder上で開催され終了したコンテストの略称です。


入力例 2

ABC350

出力例 2

No

ABC350 はこのコンテストです。まだ終了していません。


入力例 3

ABC316

出力例 3

No

ABC316 はAtCoder上で開催されていません。

Score: 100 points

Problem Statement

You are given a string S of length 6. It is guaranteed that the first three characters of S are ABC and the last three characters are digits.

Determine if S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest.

Here, a string T is "the abbreviation of a contest held and concluded on AtCoder before the start of this contest" if and only if it equals one of the following 348 strings:

ABC001, ABC002, \ldots, ABC314, ABC315, ABC317, ABC318, \ldots, ABC348, ABC349.

Note that ABC316 is not included.

Constraints

  • S is a string of length 6 where the first three characters are ABC and the last three characters are digits.

Input

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

S

Output

If S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest, print Yes; otherwise, print No.


Sample Input 1

ABC349

Sample Output 1

Yes

ABC349 is the abbreviation of a contest held and concluded on AtCoder last week.


Sample Input 2

ABC350

Sample Output 2

No

ABC350 is this contest, which has not concluded yet.


Sample Input 3

ABC316

Sample Output 3

No

ABC316 was not held on AtCoder.

C - Santa Claus 1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

縦 H 行横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。

マス (i,j) は S_{i,j} が # のとき通行不可能、. のとき通行可能であり家が建っていない、@ のとき通行可能であり家が建っていることを表します。

最初、マス (X,Y) にサンタクロースがいます。サンタクロースは文字列 T に従って以下の行動を行います。

  • 文字列 T の長さを |T| とする。i=1,2,\ldots,|T| の順に以下のように移動する。
    • 現在サンタクロースがいるマスを (x,y) とする。
      • T_i が U かつマス (x-1,y) が通行可能ならマス (x-1,y) に移動する。
      • T_i が D かつマス (x+1,y) が通行可能ならマス (x+1,y) に移動する。
      • T_i が L かつマス (x,y-1) が通行可能ならマス (x,y-1) に移動する。
      • T_i が R かつマス (x,y+1) が通行可能ならマス (x,y+1) に移動する。
      • それ以外の場合、マス (x,y) に留まる。

行動を終えたあとにサンタクロースがいるマスと、行動により通過または到達した家の数を求めてください。ただし、同じ家を複数回通過または到達してもそれらは重複して数えません。

制約

  • 3 \leq H,W \leq 100
  • 1 \leq X \leq H
  • 1 \leq Y \leq W
  • 与えられる数値は全て整数である
  • S_{i,j} は #, ., @ のいずれか
  • 全ての 1 \leq i \leq H について S_{i,1},S_{i,W} は #
  • 全ての 1 \leq j \leq W について S_{1,j},S_{H,j} は #
  • S_{X,Y}= .
  • T は U, D, L, R のいずれかからなる長さ 1 以上 10^4 以下の文字列

入力

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

H W X Y
S_{1,1}S_{1,2}\ldots S_{1,W}
\dots
S_{H,1}S_{H,2}\ldots S_{H,W}
T

出力

行動を終えたあとサンタクロースがいるマスを (X,Y)、行動により通過または到達した家の数を C とするとき、X,Y,C をこの順に空白区切りで出力せよ。


入力例 1

5 5 3 4
#####
#...#
#.@.#
#..@#
#####
LLLDRUU

出力例 1

2 3 1

サンタクロースは以下のように行動します。

図

  • T_1= L なのでマス (3,4) からマス (3,3) に移動する。これにより家を通過する。
  • T_2= L なのでマス (3,3) からマス (3,2) に移動する。
  • T_3= L だがマス (3,1) は通行不可能なので、マス (3,2) に留まる。
  • T_4= D なのでマス (3,2) からマス (4,2) に移動する。
  • T_5= R なのでマス (4,2) からマス (4,3) に移動する。
  • T_6= U なのでマス (4,3) からマス (3,3) に移動する。これにより家を通過するが、この家はすでに通過したことがある家である。
  • T_7= U なのでマス (3,3) からマス (2,3) に移動する。

行動により通過または到達した家の数は 1 です。


入力例 2

6 13 4 6
#############
#@@@@@@@@@@@#
#@@@@@@@@@@@#
#@@@@.@@@@@@#
#@@@@@@@@@@@#
#############
UURUURLRLUUDDURDURRR

出力例 2

3 11 11

入力例 3

12 35 7 10
###################################
#.................................#
#..........@......................#
#......@................@.........#
#.............##............@.....#
#...##........##....##............#
#...##........##....##.......##...#
#....##......##......##....##.....#
#....##......##......##..##.......#
#.....#######.........###.........#
#.................................#
###################################
LRURRRUUDDULUDUUDLRLRDRRLULRRUDLDRU

出力例 3

4 14 1

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.

If S_{i,j} is #, the cell (i,j) is impassable; if it is ., the cell is passable and contains no house; if it is @, the cell is passable and contains a house.

Initially, Santa Claus is in cell (X,Y). He will act according to the string T as follows.

  • Let |T| be the length of the string T. For i=1,2,\ldots,|T|, he moves as follows.
    • Let (x,y) be the cell he is currently in.
      • If T_i is U and cell (x-1,y) is passable, move to cell (x-1,y).
      • If T_i is D and cell (x+1,y) is passable, move to cell (x+1,y).
      • If T_i is L and cell (x,y-1) is passable, move to cell (x,y-1).
      • If T_i is R and cell (x,y+1) is passable, move to cell (x,y+1).
      • Otherwise, stay in cell (x,y).

Find the cell where he is after completing all actions, and the number of distinct houses that he passed through or arrived at during his actions. If the same house is passed multiple times, it is only counted once.

Constraints

  • 3 \leq H,W \leq 100
  • 1 \leq X \leq H
  • 1 \leq Y \leq W
  • All given numbers are integers.
  • Each S_{i,j} is one of #, ., @.
  • S_{i,1} and S_{i,W} are # for every 1 \leq i \leq H.
  • S_{1,j} and S_{H,j} are # for every 1 \leq j \leq W.
  • S_{X,Y}= .
  • T is a string of length at least 1 and at most 10^4, consisting of U, D, L, R.

Input

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

H W X Y
S_{1,1}S_{1,2}\ldots S_{1,W}
\dots
S_{H,1}S_{H,2}\ldots S_{H,W}
T

Output

Let (X,Y) be the cell where he is after completing all actions, and C be the number of distinct houses he passed through or arrived at during his actions. Print X,Y,C in this order separated by spaces.


Sample Input 1

5 5 3 4
#####
#...#
#.@.#
#..@#
#####
LLLDRUU

Sample Output 1

2 3 1

Santa Claus behaves as follows:

Figure

  • T_1= L, so he moves from (3,4) to (3,3). A house is passed.
  • T_2= L, so he moves from (3,3) to (3,2).
  • T_3= L, but cell (3,1) is impassable, so he stays at (3,2).
  • T_4= D, so he moves from (3,2) to (4,2).
  • T_5= R, so he moves from (4,2) to (4,3).
  • T_6= U, so he moves from (4,3) to (3,3). A house is passed, but it has already been passed.
  • T_7= U, so he moves from (3,3) to (2,3).

The number of houses he passed or arrived during his actions is 1.


Sample Input 2

6 13 4 6
#############
#@@@@@@@@@@@#
#@@@@@@@@@@@#
#@@@@.@@@@@@#
#@@@@@@@@@@@#
#############
UURUURLRLUUDDURDURRR

Sample Output 2

3 11 11

Sample Input 3

12 35 7 10
###################################
#.................................#
#..........@......................#
#......@................@.........#
#.............##............@.....#
#...##........##....##............#
#...##........##....##.......##...#
#....##......##......##....##.....#
#....##......##......##..##.......#
#.....#######.........###.........#
#.................................#
###################################
LRURRRUUDDULUDUUDLRLRDRRLULRRUDLDRU

Sample Output 3

4 14 1
D - Ticket Counter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

AtCoder Land の入り口には 1 つのチケット売り場があり、来園客はこのチケット売り場の前に一列に並んで順にチケットを購入します。 チケットの購入手続きには一人当たり A 秒かかり、列の先頭の人がチケットを購入し終わると、(存在すれば)次の人がすぐさま購入手続きを開始します。

現在チケット売り場に並んでいる人はおらず、今から N 人の人が順にチケットを買いに来ます。 具体的には、i 番目の人は今から T_i 秒後にチケット売り場を訪れ、既に列が存在すればその最後尾に並び、存在しなければすぐさま購入手続きを開始します。 ここで、T_1< T_2< \dots < T_N です。

各 i\ (1\leq i\leq N) について、i 番目の人がチケットを購入し終わるのは今から何秒後か求めてください。

制約

  • 1\leq N \leq 100
  • 0\leq T_1< T_2< \dots < T_N\leq 10^6
  • 1\leq A\leq 10^6
  • 入力は全て整数

入力

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

N A
T_1 T_2 \dots T_N

出力

N 行出力せよ。 i\ (1\leq i \leq N) 行目には、i 番目の人がチケットを購入し終わるのは今から何秒後かを整数として出力せよ。


入力例 1

3 4
0 2 10

出力例 1

4
8
14

時系列順に以下のように物事が進行します。

  • 0 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 2 秒後:2 番目の人がチケット売り場を訪れ、1 番目の人の後ろに並ぶ。
  • 4 秒後:1 番目の人がチケットを購入し終え、2 番目の人が購入手続きを開始する。
  • 8 秒後:2 番目の人がチケットを購入し終える。
  • 10 秒後:3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 14 秒後:3 番目の人がチケットを購入し終える。

入力例 2

3 3
1 4 7

出力例 2

4
7
10

時系列順に以下のように物事が進行します。

  • 1 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 4 秒後:1 番目の人がチケットを購入し終えると同時に、2 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 7 秒後:2 番目の人がチケットを購入し終えると同時に、3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 10 秒後:3 番目の人がチケットを購入し終える。

入力例 3

10 50000
120190 165111 196897 456895 540000 552614 561627 743796 757613 991216

出力例 3

170190
220190
270190
506895
590000
640000
690000
793796
843796
1041216

Score : 200 points

Problem Statement

At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds per person. Once the person at the front of the line finishes purchasing their ticket, the next person (if any) immediately starts their purchasing process.

Currently, there is no one in line at the ticket booth, and N people will come to buy tickets one after another. Specifically, the i-th person will arrive at the ticket booth T_i seconds from now. If there is already a line, they will join the end of it; if not, they will start the purchasing process immediately. Here, T_1 < T_2 < \dots < T_N.

For each i\ (1 \leq i \leq N), determine how many seconds from now the i-th person will finish purchasing their ticket.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq T_1 < T_2 < \dots < T_N \leq 10^6
  • 1 \leq A \leq 10^6
  • All input values are integers.

Input

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

N A
T_1 T_2 \dots T_N

Output

Print N lines. The i-th line should contain the number of seconds from now that the i-th person will finish purchasing their ticket.


Sample Input 1

3 4
0 2 10

Sample Output 1

4
8
14

The events proceed in the following order:

  • At 0 seconds: The 1st person arrives at the ticket booth and starts the purchasing process.
  • At 2 seconds: The 2nd person arrives at the ticket booth and joins the line behind the 1st person.
  • At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person starts the purchasing process.
  • At 8 seconds: The 2nd person finishes purchasing their ticket.
  • At 10 seconds: The 3rd person arrives at the ticket booth and starts the purchasing process.
  • At 14 seconds: The 3rd person finishes purchasing their ticket.

Sample Input 2

3 3
1 4 7

Sample Output 2

4
7
10

The events proceed in the following order:

  • At 1 second: The 1st person arrives at the ticket booth and starts the purchasing process.
  • At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person arrives at the ticket booth and starts the purchasing process.
  • At 7 seconds: The 2nd person finishes purchasing their ticket, and the 3rd person arrives at the ticket booth and starts the purchasing process.
  • At 10 seconds: The 3rd person finishes purchasing their ticket.

Sample Input 3

10 50000
120190 165111 196897 456895 540000 552614 561627 743796 757613 991216

Sample Output 3

170190
220190
270190
506895
590000
640000
690000
793796
843796
1041216
E - Understory

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋くんは、庭にある木の本数を管理しています。はじめ、庭に木は 1 本もありません。

Q 個のクエリが順に与えられます。クエリは次の 2 種類のいずれかです。各クエリを処理した直後に庭にある木の本数を出力してください。

  • 1 h : 庭に高さ h の木を新たに 1 本追加する。
  • 2 h : いま庭にある木のうち、高さが h 以下の木をすべて削除する。

制約

  • 1 \le Q \le 3 \times 10^5
  • 1 \le h \le 10^9
  • 入力される値は全て整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

i 番目のクエリを表す \text{query}_i は以下の 2 種類のいずれかである。

1 h
2 h

出力

Q 行出力せよ。

i 行目には i 番目のクエリを処理した直後の庭にある木の本数を出力せよ。


入力例 1

5
1 5
1 7
1 8
2 7
1 3

出力例 1

1
2
3
1
2

以下のように木の本数は変化します。

  • 高さ 5 の木が追加される。庭にある木は高さ 5 の 1 本である。
  • 高さ 7 の木が追加される。庭にある木は高さ 5, 7 の 2 本である。
  • 高さ 8 の木が追加される。庭にある木は高さ 5, 7, 8 の 3 本である。
  • 高さ 7 以下の木が削除される。庭にある木は高さ 8 の 1 本である。
  • 高さ 3 の木が追加される。庭にある木は高さ 8, 3 の 2 本である。

入力例 2

12
2 256601193
1 85138616
1 202564041
2 276477192
1 55551662
1 170271057
2 754166580
1 854388209
1 772036624
2 651124113
1 301137866
2 290875185

出力例 2

0
1
2
0
1
2
0
1
2
2
3
3

Score : 300 points

Problem Statement

Takahashi is managing the number of trees in his garden. Initially, there are no trees in the garden.

Q queries are given in order. Each query is one of the following two types. Immediately after processing each query, output the number of trees currently in the garden.

  • 1 h : Add one new tree of height h to the garden.
  • 2 h : Remove all trees currently in the garden whose height is at most h.

Constraints

  • 1 \le Q \le 3 \times 10^5
  • 1 \le h \le 10^9
  • All input values are integers.

Input

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

\text{query}_i, representing the i-th query, is one of the following two types:

1 h
2 h

Output

Output Q lines.

The i-th line should contain the number of trees in the garden immediately after processing the i-th query.


Sample Input 1

5
1 5
1 7
1 8
2 7
1 3

Sample Output 1

1
2
3
1
2

The number of trees changes as follows.

  • A tree of height 5 is added. The garden contains one tree of height 5.
  • A tree of height 7 is added. The garden contains two trees of heights 5, 7.
  • A tree of height 8 is added. The garden contains three trees of heights 5, 7, 8.
  • Trees of height at most 7 are removed. The garden contains one tree of height 8.
  • A tree of height 3 is added. The garden contains two trees of heights 8, 3.

Sample Input 2

12
2 256601193
1 85138616
1 202564041
2 276477192
1 55551662
1 170271057
2 754166580
1 854388209
1 772036624
2 651124113
1 301137866
2 290875185

Sample Output 2

0
1
2
0
1
2
0
1
2
2
3
3
F - Happy New Year!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

10 進法で表記したときに 0,2 のみからなる正整数のうち、 K 番目に小さいものを求めてください。

制約

  • K は 1 以上 10^{18} 以下の整数

入力

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

K

出力

答えを整数として出力せよ。
ただし、たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要がある。たとえば、 2.34e+22 のような指数表記や、 0523 のような先頭に不要な 0 を付けたような表記は許されない。


入力例 1

3

出力例 1

22

10 進法で表記した時に 0,2 のみからなる正整数を小さい方から並べると、 2,20,22,\dots となります。
このうち K=3 番目である 22 を出力してください。


入力例 2

11

出力例 2

2022

入力例 3

923423423420220108

出力例 3

220022020000202020002022022000002020002222002200002022002200

たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要があることに注意してください。

Score : 300 points

Problem Statement

Among the positive integers that consist of 0's and 2's when written in base 10, find the K-th smallest integer.

Constraints

  • K is an integer between 1 and 10^{18} (inclusive).

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer as an integer.
Here, the exact value must be printed as an integer, even if it is big. Exponential notations such as 2.34e+22, for example, or unnecessary leading zeros such as 0523 are not allowed.


Sample Input 1

3

Sample Output 1

22

The positive integers that consist of 0's and 2's when written in base 10 are 2,20,22,\dots in ascending order.
The (K=) 3-rd of them, which is 22, should be printed.


Sample Input 2

11

Sample Output 2

2022

Sample Input 3

923423423420220108

Sample Output 3

220022020000202020002022022000002020002222002200002022002200

Note that the exact value of the answer must be printed as an integer, even if it is big.

G - Shortest Path 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

N 頂点 M 辺の単純連結無向グラフが与えられます。頂点 i\,(1\leq i \leq N) は重み A_i を持ちます。また、辺 j\,(1\leq j \leq M) は頂点 U_j,V_j を双方向に結び、重み B_j を持ちます。

このグラフ上のパスの重みを、パス上に現れる頂点の重みと辺の重みの総和と定義します。

各 i=2,3,\dots,N について、以下の問題を解いてください。

  • 頂点 1 から頂点 i までのパスであって、重みが最小となるものの重みを求めよ。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • i\neq j なら (U_i,V_i) \neq (U_j,V_j)
  • グラフは連結である
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

出力

i=2,3,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。


入力例 1

3 3
1 2 3
1 2 1
1 3 6
2 3 2

出力例 1

4 9

頂点 1 から頂点 2 へのパスを考えます。 パス 1 \to 2 の重みは A_1+B_1+A_2=1+1+2=4、パス 1 \to 3 \to 2 の重みは A_1+B_2+A_3+B_3+A_2=1+6+3+2+2=14 であり、最小の重みは 4 です。

頂点 1 から頂点 3 へのパスを考えます。 パス 1 \to 3 の重みは A_1+B_2+A_3=1+6+3=10、パス 1 \to 2 \to 3 の重みは A_1+B_1+A_2+B_3+A_3=1+1+2+2+3=9 であり、最小の重みは 9 です。


入力例 2

2 1
0 1
1 2 3

出力例 2

4

入力例 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

出力例 3

2832044198 2824130042 4696218483 2805069468

答えが 32-bit 整数に収まらないことがあることに注意してください。

Score : 425 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. Each vertex i\,(1\leq i \leq N) has a weight A_i. Each edge j\,(1\leq j \leq M) connects vertices U_j and V_j bidirectionally and has a weight B_j.

The weight of a path in this graph is defined as the sum of the weights of the vertices and edges that appear on the path.

For each i=2,3,\dots,N, solve the following problem:

  • Find the minimum weight of a path from vertex 1 to vertex i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • (U_i, V_i) \neq (U_j, V_j) if i \neq j.
  • The graph is connected.
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

Output

Print the answers for i=2,3,\dots,N in a single line, separated by spaces.


Sample Input 1

3 3
1 2 3
1 2 1
1 3 6
2 3 2

Sample Output 1

4 9

Consider the paths from vertex 1 to vertex 2. The weight of the path 1 \to 2 is A_1 + B_1 + A_2 = 1 + 1 + 2 = 4, and the weight of the path 1 \to 3 \to 2 is A_1 + B_2 + A_3 + B_3 + A_2 = 1 + 6 + 3 + 2 + 2 = 14. The minimum weight is 4.

Consider the paths from vertex 1 to vertex 3. The weight of the path 1 \to 3 is A_1 + B_2 + A_3 = 1 + 6 + 3 = 10, and the weight of the path 1 \to 2 \to 3 is A_1 + B_1 + A_2 + B_3 + A_3 = 1 + 1 + 2 + 2 + 3 = 9. The minimum weight is 9.


Sample Input 2

2 1
0 1
1 2 3

Sample Output 2

4

Sample Input 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

Sample Output 3

2832044198 2824130042 4696218483 2805069468

Note that the answers may not fit in a 32-bit integer.

H - Safety Journey

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。

いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_i と A_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。

都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K) と (B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。

制約

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • (U_i, V_i) は全て互いに相異なる。
  • 入力は全て整数である。

入力

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

N M K
U_1 V_1
:
U_M V_M

出力

答えを出力せよ。


入力例 1

3 1 4
2 3

出力例 1

4

次のような 4 種類の旅が存在します。

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

これ以外に条件をみたすようなものは無いため、 4 を出力します。


入力例 2

3 3 3
1 2
1 3
2 3

出力例 2

0

使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。


入力例 3

5 3 100
1 2
4 5
2 3

出力例 3

428417047

Score : 500 points

Problem Statement

The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.

Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.

Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.

Constraints

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • All pairs (U_i, V_i) are pairwise distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
U_1 V_1
:
U_M V_M

Output

Print the answer.


Sample Input 1

3 1 4
2 3

Sample Output 1

4

There are four different trips as follows.

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

No other trip is valid, so we should print 4.


Sample Input 2

3 3 3
1 2
1 3
2 3

Sample Output 2

0

No road remains usable, so there is no valid trip.


Sample Input 3

5 3 100
1 2
4 5
2 3

Sample Output 3

428417047
I - Skate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

H 行 W 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

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

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

出力例 1

4

図は、(s_x,s_y) を S で、(g_x,g_y) を G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.