A - wwwvvvvvv

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

配点 : 100 点

問題文

v と w のみからなる文字列 S が与えられます。

S の中に、下に尖っている部分が何箇所あるかを出力してください(入出力例にある図もご参照ください)。

制約

  • S は v と w のみからなる文字列
  • S の長さは 1 以上 100 以下

入力

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

S

出力

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


入力例 1

vvwvw

出力例 1

7

vvwvw

上の画像のように、vvwvw という文字列には下に尖った部分が 7 箇所あります。


入力例 2

v

出力例 2

1

入力例 3

wwwvvvvvv

出力例 3

12

Score : 100 points

Problem Statement

You are given a string S consisting of v and w.

Print the number of "bottoms" in the string S (see the figure at Sample Input/Output).

Constraints

  • S is a string consisting of v and w.
  • The length of S is between 1 and 100, inclusive.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

vvwvw

Sample Output 1

7

vvwvw

The image above shows the seven "bottoms" in the string vvwvw.


Sample Input 2

v

Sample Output 2

1

Sample Input 3

wwwvvvvvv

Sample Output 3

12
B - 484558

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

配点 : 100 点

問題文

0123456789 に加えて 10,11,12,13,14,15 に対応する数字として ABCDEF を使う 16 進表記では、0 以上 255 以下の整数は 1 桁または 2 桁になります。
例えば、0 や 12 は 16 進表記では 0 や C と 1 桁になり、99 や 255 は 16 進表記では 63 や FF と 2 桁になります。

0 以上 255 以下の整数 N を、必要に応じて先頭に 0 を加えることでちょうど 2 桁の 16 進表記に変換してください。

注記

英大文字と英小文字は区別されます。特に、16 進表記の数字として ABCDEF の代わりに abcdef を使うことは出来ません。

制約

  • 0 \leq N \leq 255
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

99

出力例 1

63

99 は 16 進表記で 63 です。


入力例 2

12

出力例 2

0C

12 は 16 進表記で C です。
要求されているのはちょうど 2 桁の 16 進表記に変換することなので、C の先頭に 0 を加えた 0C が答えです。


入力例 3

0

出力例 3

00

入力例 4

255

出力例 4

FF

Score : 100 points

Problem Statement

In the hexadecimal system, where the digits ABCDEF corresponding to 10,11,12,13,14, and 15 are used in addition to 0123456789, every integer between 0 and 255 is represented as a 1- or 2-digit numeral.
For example, 0 and 12 are represented as 1-digit hexadecimal numerals 0 and C; 99 and 255 are represented as 2-digit hexadecimals 63 and FF.

Given an integer N between 0 and 255, convert it to an exactly two-digit hexadecimal numeral, prepending leading 0s if necessary.

Notes

The judge is case-sensitive. Specifically, you cannot use abcdef as hexadecimal digits instead of ABCDEF.

Constraints

  • 0 \leq N \leq 255
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

99

Sample Output 1

63

99 is represented as 63 in hexadecimal.


Sample Input 2

12

Sample Output 2

0C

12 is represented as C in hexadecimal.
Since we ask you to convert it to a two-digit hexadecimal numeral, the answer is 0C, where 0 is prepended to C.


Sample Input 3

0

Sample Output 3

00

Sample Input 4

255

Sample Output 4

FF
C - Longest Uncommon Prefix

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

配点 : 200 点

問題文

英小文字からなる長さ N の文字列 S が与えられます。 S の x 文字目 (1 \le x \le N) は S_x です。

i=1,2,\dots,N-1 それぞれについて、以下の条件を全て満たす最大の非負整数 l を求めてください。

  • l+i \le N である。
  • 全ての 1 \le k \le l を満たす整数 k について、 S_{k} \neq S_{k+i} を満たす。

なお、 l=0 は常に条件を満たすことに注意してください。

制約

  • N は 2 \le N \le 5000 を満たす整数
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

N-1 行にわたって出力せよ。そのうち x 行目 (1 \le x < N) には i=x とした場合の答えを整数として出力せよ。


入力例 1

6
abcbac

出力例 1

5
1
2
0
1

この入力では、 S= abcbac です。

  • i=1 のとき、 S_1 \neq S_2, S_2 \neq S_3, \dots ,S_5 \neq S_6 であるため、 最大値は l=5 です。
  • i=2 のとき、 S_1 \neq S_3 ですが S_2 = S_4 であるため、 最大値は l=1 です。
  • i=3 のとき、 S_1 \neq S_4, S_2 \neq S_5 ですが S_3 = S_6 であるため、 最大値は l=2 です。
  • i=4 のとき、 S_1 = S_5 であるため、 最大値は l=0 です。
  • i=5 のとき、 S_1 \neq S_6 であるため、 最大値は l=1 です。

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. The x-th (1 \le x \le N) character of S is S_x.

For each i=1,2,\dots,N-1, find the maximum non-negative integer l that satisfies all of the following conditions:

  • l+i \le N, and
  • for all integers k such that 1 \le k \le l, it holds that S_{k} \neq S_{k+i}.

Note that l=0 always satisfies the conditions.

Constraints

  • N is an integer such that 2 \le N \le 5000.
  • S is a string of length N consisting of lowercase English letters.

Input

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

N
S

Output

Print (N-1) lines. The x-th (1 \le x < N) line should contain the answer as an integer when i=x.


Sample Input 1

6
abcbac

Sample Output 1

5
1
2
0
1

In this input, S= abcbac.

  • When i=1, we have S_1 \neq S_2, S_2 \neq S_3, \dots, and S_5 \neq S_6, so the maximum value is l=5.
  • When i=2, we have S_1 \neq S_3 but S_2 = S_4, so the maximum value is l=1.
  • When i=3, we have S_1 \neq S_4 and S_2 \neq S_5 but S_3 = S_6, so the maximum value is l=2.
  • When i=4, we have S_1 = S_5, so the maximum value is l=0.
  • When i=5, we have S_1 \neq S_6, so the maximum value is l=1.
D - Climbing Takahashi

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

配点 : 200 点

問題文

N 個の台が一列に並んでおり、左から i 番目の台の高さは H_i です。

高橋君は最初、左端の台の上に立っています。

高橋君は高い所が好きなので、次のルールで可能な限り移動を繰り返します。

  • いま立っているのが右端の台ではなく、かつ、右隣にある台の高さが自分がいま立っている台より高いとき、右隣の台に移動する

最終的に高橋君が立っている台の高さを求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N
H_1 \ldots H_N

出力

答えを出力せよ。


入力例 1

5
1 5 10 4 2

出力例 1

10

最初、高橋君は左端にある高さ 1 の台に立っています。右隣の台の高さは 5 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 2 番目にある高さ 5 の台に立っています。右隣の台の高さは 10 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 3 番目にある高さ 10 の台に立っています。右隣の台の高さは 4 であり、いま立っている台より低いので、高橋君は移動をやめます。

よって、最終的に高橋君が立っている台の高さは 10 です。


入力例 2

3
100 1000 100000

出力例 2

100000

入力例 3

4
27 1828 1828 9242

出力例 3

1828

Score : 200 points

Problem Statement

There are N platforms arranged in a row. The height of the i-th platform from the left is H_i.

Takahashi is initially standing on the leftmost platform.

Since he likes heights, he will repeat the following move as long as possible.

  • If the platform he is standing on is not the rightmost one, and the next platform to the right has a height greater than that of the current platform, step onto the next platform.

Find the height of the final platform he will stand on.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H_1 \ldots H_N

Output

Print the answer.


Sample Input 1

5
1 5 10 4 2

Sample Output 1

10

Takahashi is initially standing on the leftmost platform, whose height is 1. The next platform to the right has a height of 5 and is higher than the current platform, so he steps onto it.

He is now standing on the 2-nd platform from the left, whose height is 5. The next platform to the right has a height of 10 and is higher than the current platform, so he steps onto it.

He is now standing on the 3-rd platform from the left, whose height is 10. The next platform to the right has a height of 4 and is lower than the current platform, so he stops moving.

Thus, the height of the final platform Takahashi will stand on is 10.


Sample Input 2

3
100 1000 100000

Sample Output 2

100000

Sample Input 3

4
27 1828 1828 9242

Sample Output 3

1828
E - Cross

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

配点 : 300 点

問題文

縦 H マス横 W マスのグリッドがあります。グリッドの上から i 行目、左から j 列目のマスを (i, j) と呼びます。
グリッドの各マスには # と . のいずれかの文字が書かれています。(i, j) に書かれている文字を C[i][j] とします。また、整数 i, j が 1 \leq i \leq H と 1 \leq j \leq W の少なくとも一方を満たさない場合、 C[i][j] を . と定義します。

正整数 a, b, n が以下の条件を全て満たす時、(a,b) および (a+d,b+d),(a+d,b-d),(a-d,b+d),(a-d,b-d) (1 \leq d \leq n) の 4n + 1 マスを (a,b) を中心とするサイズ n のバツ印 と呼びます。

  • C[a][b] は # である。
  • 1 \leq d \leq n を満たす整数 d について、 C[a+d][b+d],C[a+d][b-d],C[a-d][b+d],C[a-d][b-d] はいずれも # である。
  • C[a+n+1][b+n+1],C[a+n+1][b-n-1],C[a-n-1][b+n+1],C[a-n-1][b-n-1] のうち少なくとも 1 つは . である。

例えば次の図で示された例では、(2, 2) を中心とするサイズ 1 のバツ印と (3, 7) を中心とするサイズ 2 のバツ印がグリッド上にあります。

image

グリッドにはいくつかのバツ印があります。バツ印を構成するマス以外に # は書かれていません。
また、異なるバツ印を構成するマス同士は頂点を共有しません。以下の 2 つのグリッドは異なるバツ印を構成するマス同士が頂点を共有している例で、このようなグリッドの状態は入力として与えられません。 例えば左のグリッドでは (3, 3) と (4, 4) が頂点を共有しているのが条件に反しています。

image2

N = \min(H, W) とします。また、サイズ n のバツ印の個数を S_n とします。S_1, S_2, \dots, S_N を計算してください。

制約

  • 3 \leq H, W \leq 100
  • C[i][j] は # または .
  • 異なるバツ印を構成するマス同士は頂点を共有しない
  • H, W は整数

入力

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

H W
C[1][1]C[1][2]\dots C[1][W]
C[2][1]C[2][2]\dots C[2][W]
\vdots
C[H][1]C[H][2]\dots C[H][W]

出力

S_1, S_2, \dots, S_N を空白区切りで出力せよ。


入力例 1

5 9
#.#.#...#
.#...#.#.
#.#...#..
.....#.#.
....#...#

出力例 1

1 1 0 0 0

問題文に書かれた説明の通り、(2, 2) を中心とするサイズ 1 のバツ印と (3, 7) を中心とするサイズ 2 のバツ印が書かれています。


入力例 2

3 3
...
...
...

出力例 2

0 0 0

バツ印が 1 個も書かれていない場合もあります。


入力例 3

3 16
#.#.....#.#..#.#
.#.......#....#.
#.#.....#.#..#.#

出力例 3

3 0 0

入力例 4

15 20
#.#..#.............#
.#....#....#.#....#.
#.#....#....#....#..
........#..#.#..#...
#.....#..#.....#....
.#...#....#...#..#.#
..#.#......#.#....#.
...#........#....#.#
..#.#......#.#......
.#...#....#...#.....
#.....#..#.....#....
........#.......#...
#.#....#....#.#..#..
.#....#......#....#.
#.#..#......#.#....#

出力例 4

5 0 1 0 0 0 1 0 0 0 0 0 0 0 0

Score : 300 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. We denote by (i, j) the cell at the i-th row from the top and j-th column from the left of the grid.
Each cell in the grid has a symbol # or . written on it. Let C[i][j] be the character written on (i, j). For integers i and j such that at least one of 1 \leq i \leq H and 1 \leq j \leq W is violated, we define C[i][j] to be ..

(4n+1) squares, consisting of (a, b) and (a+d,b+d),(a+d,b-d),(a-d,b+d),(a-d,b-d) (1 \leq d \leq n, 1 \leq n), are said to be a cross of size n centered at (a,b) if and only if all of the following conditions are satisfied:

  • C[a][b] is #.
  • C[a+d][b+d],C[a+d][b-d],C[a-d][b+d], and C[a-d][b-d] are all #, for all integers d such that 1 \leq d \leq n,
  • At least one of C[a+n+1][b+n+1],C[a+n+1][b-n-1],C[a-n-1][b+n+1], and C[a-n-1][b-n-1] is ..

For example, the grid in the following figure has a cross of size 1 centered at (2, 2) and another of size 2 centered at (3, 7).

image

The grid has some crosses. No # is written on the cells except for those comprising a cross.
Additionally, no two squares that comprise two different crosses share a corner. The two grids in the following figure are the examples of grids where two squares that comprise different crosses share a corner; such grids are not given as an input. For example, the left grid is invalid because (3, 3) and (4, 4) share a corner.

image2

Let N = \min(H, W), and S_n be the number of crosses of size n. Find S_1, S_2, \dots, S_N.

Constraints

  • 3 \leq H, W \leq 100
  • C[i][j] is # or ..
  • No two different squares that comprise two different crosses share a corner.
  • H and W are integers.

Input

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

H W
C[1][1]C[1][2]\dots C[1][W]
C[2][1]C[2][2]\dots C[2][W]
\vdots
C[H][1]C[H][2]\dots C[H][W]

Output

Print S_1, S_2, \dots, and S_N, separated by spaces.


Sample Input 1

5 9
#.#.#...#
.#...#.#.
#.#...#..
.....#.#.
....#...#

Sample Output 1

1 1 0 0 0

As described in the Problem Statement, there are a cross of size 1 centered at (2, 2) and another of size 2 centered at (3, 7).


Sample Input 2

3 3
...
...
...

Sample Output 2

0 0 0

There may be no cross.


Sample Input 3

3 16
#.#.....#.#..#.#
.#.......#....#.
#.#.....#.#..#.#

Sample Output 3

3 0 0

Sample Input 4

15 20
#.#..#.............#
.#....#....#.#....#.
#.#....#....#....#..
........#..#.#..#...
#.....#..#.....#....
.#...#....#...#..#.#
..#.#......#.#....#.
...#........#....#.#
..#.#......#.#......
.#...#....#...#.....
#.....#..#.....#....
........#.......#...
#.#....#....#.#..#..
.#....#......#....#.
#.#..#......#.#....#

Sample Output 4

5 0 1 0 0 0 1 0 0 0 0 0 0 0 0
F - Cash Register

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

配点 : 300 点

問題文

高橋君は、レジ打ちの仕事をしています。

レジの機械には 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 の 11 個のボタンがあります。 レジの機械には、はじめ 0 が表示されています。 ボタン 00 を押すと、表示されている数が 100 倍されます。 それ以外のボタンを押すと、表示されている数が 10 倍されたあとに、押されたボタンに書かれている数が加算されます。

高橋君は、レジに整数 S を表示させたいです。 レジに S が表示されている状態にするためには、少なくとも何回ボタンを押す必要があるか求めてください。

制約

  • 1\leq S\leq 10^{100000}
  • S は整数

入力

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

S

出力

答えを 1 行で出力せよ。


入力例 1

40004

出力例 1

4

例えば、次のように操作することでボタンを 4 回押して 40004 を表示させることができます。 はじめ、レジには 0 が表示されています。

  • ボタン 4 を押す。レジに表示されている数は 4 となる。
  • ボタン 00 を押す。レジに表示されている数は 400 となる。
  • ボタン 0 を押す。レジに表示されている数は 4000 となる。
  • ボタン 4 を押す。レジに表示されている数は 40004 となる。

3 回までボタンを押すことでレジに 40004 を表示させることはできないので、出力すべき値は 4 です。


入力例 2

1355506027

出力例 2

10

入力例 3

10888869450418352160768000001

出力例 3

27

S は 64\operatorname{bit} 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi is a cashier.

There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 0. Whenever he types the key 00, the displayed number is multiplied by 100; whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.

Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?

Constraints

  • 1\leq S\leq 10^{100000}
  • S is an integer.

Input

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

S

Output

Print the answer in a line.


Sample Input 1

40004

Sample Output 1

4

For example, the following four keystrokes make the cash register display 40004. Initially, the cash register displays 0.

  • Type the key 4. It now displays 4.
  • Type the key 00. It now displays 400.
  • Type the key 0. It now displays 4000.
  • Type the key 4. It now displays 40004.

He cannot make it display 40004 with three or fewer keystrokes, so 4 should be printed.


Sample Input 2

1355506027

Sample Output 2

10

Sample Input 3

10888869450418352160768000001

Sample Output 3

27

Note that S may not fit into a 64-\operatorname{bit} integer type.

G - Trophy

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

配点 : 400 点

問題文

N 個のステージからなるゲームがあり、i \, (1 \leq i \leq N) 番目のステージは A_i 分間のストーリー映像と B_i 分間のゲームプレイによって構成されます。

初めて i 番目のステージをクリアするためにはストーリー映像の視聴とゲームプレイを両方行う必要がありますが、二回目以降はストーリー映像をスキップすることができるので、ゲームプレイのみでクリアすることができます。

初めから遊べるのは 1 番目のステージのみですが、i \, (1 \leq i \leq N - 1) 番目のステージをクリアすることにより、i+1 番目のステージも遊べるようになります。

合計 X 回ステージをクリアするために必要な時間の最小値を求めてください。ただし、同じステージを複数回クリアしたとしても、全てクリア回数に数えられます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10^9
  • 入力は全て整数

入力

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

N X
A_1 B_1
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

3 4
3 4
2 3
4 2

出力例 1

18

例えば、次のようにして 18 分で 4 回クリアすることができます。

  • ステージ 1 をクリアする。A_1 + B_1 = 7 分かかる。
  • ステージ 2 をクリアする。A_2 + B_2 = 5 分かかる。
  • ステージ 2 を再びクリアする。B_2 = 3 分かかる。
  • ステージ 2 を再びクリアする。B_2 = 3 分かかる。

17 分以内に 4 回クリアすることはできません。


入力例 2

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

出力例 2

1000000076

Score : 400 points

Problem Statement

We have a video game consisting of N stages. The i-th stage (1 \leq i \leq N) is composed of a movie lasting A_i minutes and gameplay lasting B_i minutes.

To clear the i-th stage for the first time, one must watch the movie and do the gameplay for that stage. For the second and subsequent times, one may skip the movie and do just the gameplay.

In the beginning, only the 1-st stage is unlocked, and clearing the i-th stage (1 \leq i \leq N - 1) unlocks the (i+1)-th stage.

Find the shortest time needed to clear a stage X times in total. Here, if the same stage is cleared multiple times, all of them count.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 B_1
\vdots
A_N B_N

Output

Print the answer.


Sample Input 1

3 4
3 4
2 3
4 2

Sample Output 1

18

Here is one way to clear a stage 4 times in 18 minutes:

  • Clear Stage 1. It takes A_1 + B_1 = 7 minutes.
  • Clear Stage 2. It takes A_2 + B_2 = 5 minutes.
  • Clear Stage 2 again. It takes B_2= 3 minutes.
  • Clear Stage 2 again. It takes B_2= 3 minutes.

It is impossible to clear a stage 4 times within 17 minutes.


Sample Input 2

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

Sample Output 2

1000000076
H - Crystal Switches

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

配点 : 500 点

問題文

N 個の頂点と M 本の辺からなる無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺であり、 a_i = 1 ならばはじめは通行可能、a_i = 0 ならばはじめは通行不能です。 また、頂点 s_1 、頂点 s_2 、\ldots 、頂点 s_K の K 個の頂点にはスイッチがあります。

高橋君は、はじめ頂点 1 におり、「下記の移動とスイッチを押すの 2 つの行動のどちらかを行うこと」を好きなだけ繰り返します。

  • 移動 : いまいる頂点と通行可能な辺を介して隣接する頂点を 1 つ選び、その頂点に移動する。
  • スイッチを押す : いまいる頂点にスイッチがあるならば、そのスイッチを押す。その結果、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。すなわち、通行可能である辺は通行不能に、通行不能である辺は通行可能に変化する。

高橋君が頂点 N に到達することが可能かどうかを判定し、可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • a_i \in \lbrace 0, 1\rbrace
  • 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
  • 入力はすべて整数

入力

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

N M K
u_1 v_1 a_1
u_2 v_2 a_2
\vdots
u_M v_M a_M
s_1 s_2 \ldots s_K

出力

高橋君が頂点 N に到達することが不可能な場合は -1 を、 可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力せよ。


入力例 1

5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4

出力例 1

5

高橋君は、下記の手順で行動することで頂点 N に到達できます。

  • 頂点 1 から頂点 2 に移動する。
  • 頂点 2 から頂点 3 に移動する。
  • 頂点 3 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。
  • 頂点 3 から頂点 1 に移動する。
  • 頂点 1 から頂点 4 に移動する。
  • 頂点 4 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が再び反転する。
  • 頂点 4 から頂点 5 に移動する。

この手順における移動の回数は 5 回であり、これが考えられる最小の回数です。


入力例 2

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

出力例 2

-1

与えられるグラフは、連結でないことや、多重辺を含むことがあります。 この入力例では、高橋君はどのように行動しても頂点 N に到達することはできないので、-1 を出力します。

Score : 500 points

Problem Statement

You are given an undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertex u_i and v_i that is initially passable if a_i = 1 and initially impassable if a_i = 0. Additionally, there are switches on K of the vertices: vertex s_1, vertex s_2, \ldots, vertex s_K.

Takahashi is initially on vertex 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.

  • Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
  • Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.

Determine whether Takahashi can reach vertex N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • a_i \in \lbrace 0, 1\rbrace
  • 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
  • All values in the input are integers.

Input

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

N M K
u_1 v_1 a_1
u_2 v_2 a_2
\vdots
u_M v_M a_M
s_1 s_2 \ldots s_K

Output

If Takahashi cannot reach vertex N, print -1; if he can, print the minimum possible number of times he performs Move before reaching vertex N.


Sample Input 1

5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4

Sample Output 1

5

Takahashi can reach vertex N as follows.

  • Move from vertex 1 to vertex 2.
  • Move from vertex 2 to vertex 3.
  • Hit the switch on vertex 3. This inverts the passability of every edge in the graph.
  • Move from vertex 3 to vertex 1.
  • Move from vertex 1 to vertex 4.
  • Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
  • Move from vertex 4 to vertex 5.

Here, Move is performed five times, which is the minimum possible number.


Sample Input 2

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

Sample Output 2

-1

The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N, so you should print -1.

I - Predilection

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

配点 : 500 点

問題文

長さ N の数列 A が与えられます。 数列の長さが 2 以上のとき、隣接する二つの値を選び、それらを削除し、それらが元にあった位置にそれらの和を挿入するという操作を好きなだけ行えます。 0 回以上の操作の後の数列として考えられるものは何通りあるか求め、998244353 で割ったあまりを出力してください。

制約

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

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

3
1 -1 1

出力例 1

4

0 回以上の操作の後の数列として考えられるのは以下の 4 通りです。

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

入力例 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

出力例 2

321

Score : 500 points

Problem Statement

Given is a sequence A of length N. You can do this operation any number of times: when the length of the sequence is at least 2, choose two adjacent values, delete them, and insert their sum where they were. How many sequences can result from zero or more operations? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • |A_i| \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

3
1 -1 1

Sample Output 1

4

The following four sequences can result from zero or more operations.

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

Sample Input 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

Sample Output 2

321