A - Weekly Records

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は N 週間の間、自分が歩いた歩数を記録しました。i 日目に歩いた歩数は A_i でした。

各週に高橋君が歩いた歩数の合計を求めてください。
より正確には、1 週目( 1 日目から 7 日目まで)の 7 日間の歩数の合計、2 週目( 8 日目から 14 日目まで)の 7 日間の歩数の合計、……をそれぞれ求めてください。

制約

  • 1 \leq N \leq 10
  • 0 \leq A_i \leq 10^5
  • 入力は整数

入力

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

N
A_1 A_2 \ldots A_{7N}

出力

i 週目に歩いた歩数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

出力例 1

28000 35000

高橋君は 1 週目に 1000+2000+3000+4000+5000+6000+7000=28000 歩あるき、2 週目に 2000+3000+4000+5000+6000+7000+8000=35000 歩あるきました。


入力例 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

出力例 2

314333 419427 335328

Score : 100 points

Problem Statement

Takahashi has recorded the number of steps he walked for N weeks. He walked A_i steps on the i-th day.

Find the total number of steps Takahashi walked each week.
More precisely, find the sum of the steps for the first week (the 1-st through 7-th day), the sum of the steps for the second week (the 8-th through 14-th day), and so on.

Constraints

  • 1 \leq N \leq 10
  • 0 \leq A_i \leq 10^5
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_{7N}

Output

Let B_i be the number of steps walked for the i-th week. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

Sample Output 1

28000 35000

For the first week, he walked 1000+2000+3000+4000+5000+6000+7000=28000 steps, and for the second week, he walked 2000+3000+4000+5000+6000+7000+8000=35000 steps.


Sample Input 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

Sample Output 2

314333 419427 335328
B - racecar

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。
1 以上 N 以下の 相異なる 整数 i,j であって、S_iS_j をこの順に連結した文字列が回文となるようなものが存在するか判定してください。

ただし、長さ M の文字列 T が回文であるとは、任意の 1\leq i\leq M について、Ti 文字目と (M+1-i) 文字目が一致していることをいいます。

制約

  • 2\leq N\leq 100
  • 1\leq \lvert S_i\rvert \leq 50
  • N は整数
  • S_i は英小文字のみからなる文字列
  • S_i はすべて異なる。

入力

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

N
S_1
S_2
\vdots
S_N

出力

問題文の条件をみたす i,j が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
ab
ccef
da
a
fe

出力例 1

Yes

(i,j)=(1,4) とすると、S_1=abS_4=a をこの順に連結した文字列は aba となり、 これは回文であるため条件をみたしています。
よって、Yes を出力します。

また、(i,j)=(5,2) としても、S_5=feS_2=ccef をこの順に連結した文字列は feccef となり、やはり条件をみたしています。


入力例 2

3
a
b
aba

出力例 2

No

S_1, S_2, S_3 のうち、どの相異なる 2 つの文字列を繋げても回文となりません。 よって、No を出力します。
問題文における i,j は相異なる必要があることに注意してください。


入力例 3

2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

出力例 3

Yes

Score : 200 points

Problem Statement

You are given N strings S_1,S_2,\ldots,S_N consisting of lowercase English letters.
Determine if there are distinct integers i and j between 1 and N, inclusive, such that the concatenation of S_i and S_j in this order is a palindrome.

A string T of length M is a palindrome if and only if the i-th character and the (M+1-i)-th character of T are the same for every 1\leq i\leq M.

Constraints

  • 2\leq N\leq 100
  • 1\leq \lvert S_i\rvert \leq 50
  • N is an integer.
  • S_i is a string consisting of lowercase English letters.
  • All S_i are distinct.

Input

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

N
S_1
S_2
\vdots
S_N

Output

If there are i and j that satisfy the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1

5
ab
ccef
da
a
fe

Sample Output 1

Yes

If we take (i,j)=(1,4), the concatenation of S_1=ab and S_4=a in this order is aba, which is a palindrome, satisfying the condition.
Thus, print Yes.

Here, we can also take (i,j)=(5,2), for which the concatenation of S_5=fe and S_2=ccef in this order is feccef, satisfying the condition.


Sample Input 2

3
a
b
aba

Sample Output 2

No

No two distinct strings among S_1, S_2, and S_3 form a palindrome when concatenated. Thus, print No.
Note that the i and j in the statement must be distinct.


Sample Input 3

2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 3

Yes
C - Ideal Sheet

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君は黒いマスと透明なマスからなるシート A,B1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。

シート A,B,X の大きさはそれぞれ縦 H_A マス \timesW_A マス、縦 H_B マス \timesW_B マス、縦 H_X マス \timesW_X マスです。
シート A の各マスは .# からなる長さ W_A の文字列 H_AA_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A)j 文字目 (1\leq j\leq W_A) が、 . のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、 # のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_BB_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_XX_1,X_2,\ldots,X_{H_X} によって表されます。

高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。

  1. シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
  2. シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。

このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。

  • 切り出されたシートはシート A,B黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
  • 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。

制約

  • 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
  • H_A,W_A,H_B,W_B,H_X,W_X は整数
  • A_i.# のみからなる長さ W_A の文字列
  • B_i.# のみからなる長さ W_B の文字列
  • X_i.# のみからなる長さ W_X の文字列
  • シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。

入力

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

出力

高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

まず、シート A をシート C に貼り付けると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。

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

これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。

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


入力例 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

出力例 2

No

シート AB を回転させて貼ってはいけないことに注意してください。


入力例 3

1 1
#
1 2
##
1 1
#

出力例 3

No

どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。 よって、No を出力します。


入力例 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

出力例 4

Yes

Score : 300 points

Problem Statement

Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.

The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.

Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.

  1. Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
  2. Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.

Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.

  • The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
  • The cut-out sheet coincides sheet X without rotating or flipping.

Constraints

  • 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
  • H_A, W_A, H_B, W_B, H_X, W_X are integers.
  • A_i is a string of length W_A consisting of . and #.
  • B_i is a string of length W_B consisting of . and #.
  • X_i is a string of length W_X consisting of . and #.
  • Sheets A, B, and X each contain at least one black square.

Input

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

Output

If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

First, paste sheet A onto sheet C, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.

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

This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.

Therefore, print Yes.


Sample Input 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

Sample Output 2

No

Note that sheets A and B may not be rotated or flipped when pasting them.


Sample Input 3

1 1
#
1 2
##
1 1
#

Sample Output 3

No

No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition. Therefore, print No.


Sample Input 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

Sample Output 4

Yes
D - Mismatched Parentheses

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

英小文字および (, ) からなる長さ N の文字列 S が与えられます。
以下の操作を可能な限り繰り返したあとの S を出力してください。

  • S の連続部分文字列であって、最初の文字が ( かつ 最後の文字が ) かつ 最初と最後以外に () も含まないものを自由に 1 つ選び削除する

なお、操作を可能な限り繰り返したあとの S は操作の手順によらず一意に定まることが証明できます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • N は整数である
  • S は英小文字および (, ) からなる長さ N の文字列である

入力

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

N
S

出力

答えを出力せよ。


入力例 1

8
a(b(d))c

出力例 1

ac

例えば、以下の手順により操作後の Sac となります。

  • S4 文字目から 6 文字目までからなる部分文字列 (d) を削除する。Sa(b)c となる。
  • S2 文字目から 4 文字目までからなる部分文字列 (b) を削除する。Sac となる。
  • これ以上操作を行うことはできない。

入力例 2

5
a(b)(

出力例 2

a(

入力例 3

2
()

出力例 3


操作後の S は空文字列になる可能性があります。


入力例 4

6
)))(((

出力例 4

)))(((

Score : 400 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters and the characters ( and ).
Print the string S after performing the following operation as many times as possible.

  • Choose and delete a contiguous substring of S that starts with (, ends with ), and does not contain ( or ) other than the first and last characters.

It can be proved that the string S after performing the operation as many times as possible is uniquely determined without depending on how it is performed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an integer.
  • S is a string of length N consisting of lowercase English letters and the characters ( and ).

Input

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

N
S

Output

Print the answer.


Sample Input 1

8
a(b(d))c

Sample Output 1

ac

Here is one possible procedure, after which S will be ac.

  • Delete the substring (d) formed by the fourth to sixth characters of S, making it a(b)c.
  • Delete the substring (b) formed by the second to fourth characters of S, making it ac.
  • The operation can no longer be performed.

Sample Input 2

5
a(b)(

Sample Output 2

a(

Sample Input 3

2
()

Sample Output 3


The string S after the procedure may be empty.


Sample Input 4

6
)))(((

Sample Output 4

)))(((
E - Distinct Adjacent

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 475

問題文

1 から N の番号がついた N 人の人が輪になってならんでいます。人 1 の右隣には人 2 が、人 2 の右隣には人 3 が、……、人 N の右隣には人 1 がいます。

N 人の人にそれぞれ 0 以上 M 未満の整数を 1 つずつ渡します。
M^N 通りの渡し方のうち、どの隣り合う 2 人が渡された数も異なるものの数を、998244353 で割ったあまりを求めてください。

制約

  • 2 \leq N,M \leq 10^6
  • N,M は整数である

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 3

出力例 1

6

1,2,3 に渡す整数がそれぞれ (0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0) のときの 6 通りです。


入力例 2

4 2

出力例 2

2

1,2,3,4 に渡す整数がそれぞれ (0,1,0,1),(1,0,1,0) のときの 2 通りです。


入力例 3

987654 456789

出力例 3

778634319

998244353 で割ったあまりを求めてください。

Score : 475 points

Problem Statement

There are N people numbered from 1 to N standing in a circle. Person 1 is to the right of person 2, person 2 is to the right of person 3, ..., and person N is to the right of person 1.

We will give each of the N people an integer between 0 and M-1, inclusive.
Among the M^N ways to distribute integers, find the number, modulo 998244353, of such ways that no two adjacent people have the same integer.

Constraints

  • 2 \leq N,M \leq 10^6
  • N and M are integers.

Input

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

N M

Output

Print the answer.


Sample Input 1

3 3

Sample Output 1

6

There are six desired ways, where the integers given to persons 1,2,3 are (0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0).


Sample Input 2

4 2

Sample Output 2

2

There are two desired ways, where the integers given to persons 1,2,3,4 are (0,1,0,1),(1,0,1,0).


Sample Input 3

987654 456789

Sample Output 3

778634319

Be sure to find the number modulo 998244353.

F - Virus 2

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 550

問題文

部屋 1, 部屋 2, \ldots, 部屋 N と番号づけられた N 個の部屋に人が 1 人ずつ住んでおり、 また、いくつかの相異なる 2 つの部屋の間は通路によって結ばれています。 通路は M 本あり、i 本目の通路は部屋 U_i と部屋 V_i を結んでおり、長さは W_i です。

ある日(これを 0 日目とします)の夜に、部屋 A_1,A_2,\ldots, A_K に住んでいる K 人がウイルスに(新しく)感染してしまいました。 さらにその後の D 日間で i 日目 (1\leq i\leq D) には次のように感染が広がりました。

(i-1) 日目の夜の時点で感染していた人は、i 日目の夜の時点でも感染していた。
そうでない人については、(i-1) 日目の夜の時点で感染していた人の住んでいる部屋のうちの少なくとも 1 つから 距離 X_i 以内の部屋に住んでいた時かつその時に限り、新しく感染した。 ここで、部屋 P,Q の間の距離は、部屋 P から 部屋 Q まで通路のみを使って移動する時に通る通路の長さの総和としてあり得る最小値として定義される。 ただし、部屋 P から 部屋 Q へ通路のみを使って移動する事ができない時、距離は 10^{100} とする。

i (1\leq i\leq N) について、部屋 i に住んでいる人がそれぞれ何日目の夜に(新しく)感染したか出力してください。ただし、D 日目の夜の時点で感染していない場合は -1 を出力してください。

制約

  • 1 \leq N\leq 3\times 10^5
  • 0 \leq M\leq 3\times 10^5
  • 1 \leq U_i < V_i\leq N
  • (U_i,V_i) はすべて異なる。
  • 1\leq W_i\leq 10^9
  • 1 \leq K\leq N
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1 \leq D\leq 3\times 10^5
  • 1\leq X_i\leq 10^9
  • 入力はすべて整数

入力

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

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
K
A_1 A_2 \ldots A_K
D
X_1 X_2 \ldots X_D

出力

N 行出力せよ。
i 行目 (1\leq i\leq N) には、部屋 i に住んでいる人が何日目の夜に(新しく)感染したか出力せよ。


入力例 1

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

出力例 1

0
1
1
2

次のように感染は広がります。

  • 0 日目の夜、部屋 1 に住んでいる人が感染する。
  • 部屋 1 と部屋 2,3,4 の間の距離はそれぞれ 2,3,5 である。よって、X_1=3 であるから、1 日目の夜、部屋 2,3 に住んでいる人が新しく感染する。
  • 部屋 3 と部屋 4 の間の距離は 2 である。よって、X_2=3 であるから、2 日目の夜、部屋 4 に住んでいる人も感染する。

よって、部屋 1,2,3,4 に住んでいる人はそれぞれ 0,1,1,2 日目に新しく感染したため、0,1,1,2 をこの順で各行に出力します。


入力例 2

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

出力例 2

0
1
2
-1
2
0
-1

入力例 3

5 1
1 2 5
2
1 3
3
3 7 5

出力例 3

0
2
0
-1
-1

どの 2 つの部屋の間も通路のみを使って移動できるとは限らないことに注意してください。

Score : 550 points

Problem Statement

There are N rooms numbered 1, 2, \ldots, N, each with one person living in it, and M corridors connecting two different rooms. The i-th corridor connects room U_i and room V_i with a length of W_i.

One day (we call this day 0), the K people living in rooms A_1, A_2, \ldots, A_K got (newly) infected with a virus. Furthermore, on the i-th of the following D days (1\leq i\leq D), the infection spread as follows.

People who were infected at the end of the night of day (i-1) remained infected at the end of the night of day i.
For those who were not infected, they were newly infected if and only if they were living in a room within a distance of X_i from at least one room where an infected person was living at the end of the night of day (i-1). Here, the distance between rooms P and Q is defined as the minimum possible sum of the lengths of the corridors when moving from room P to room Q using only corridors. If it is impossible to move from room P to room Q using only corridors, the distance is set to 10^{100}.

For each i (1\leq i\leq N), print the day on which the person living in room i was newly infected. If they were not infected by the end of the night of day D, print -1.

Constraints

  • 1 \leq N\leq 3\times 10^5
  • 0 \leq M\leq 3\times 10^5
  • 1 \leq U_i < V_i\leq N
  • All (U_i,V_i) are different.
  • 1\leq W_i\leq 10^9
  • 1 \leq K\leq N
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1 \leq D\leq 3\times 10^5
  • 1\leq X_i\leq 10^9
  • All input values are integers.

Input

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

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
K
A_1 A_2 \ldots A_K
D
X_1 X_2 \ldots X_D

Output

Print N lines.
The i-th line (1\leq i\leq N) should contain the day on which the person living in room i was newly infected.


Sample Input 1

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

Sample Output 1

0
1
1
2

The infection spreads as follows.

  • On the night of day 0, the person living in room 1 gets infected.
  • The distances between room 1 and rooms 2,3,4 are 2,3,5, respectively. Thus, since X_1=3, the people living in rooms 2 and 3 are newly infected on the night of day 1.
  • The distance between rooms 3 and 4 is 2. Thus, since X_2=3, the person living in room 4 also gets infected on the night of day 2.

Therefore, the people living in rooms 1,2,3,4 were newly infected on days 0,1,1,2, respectively, so print 0,1,1,2 in this order on separate lines.


Sample Input 2

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

Sample Output 2

0
1
2
-1
2
0
-1

Sample Input 3

5 1
1 2 5
2
1 3
3
3 7 5

Sample Output 3

0
2
0
-1
-1

Note that it is not always possible to move between any two rooms using only corridors.

G - Approximate Equalization

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

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

高橋君は次の 2 種類の操作のうち 1 つを選んで行う事を、何回でも (0 回でも良い) 繰り返し行う事ができます。

  • 1\leq i\leq N-1 をみたす整数 i を選び、A_i1 減らし、A_{i+1}1 増やす。
  • 1\leq i\leq N-1 をみたす整数 i を選び、A_i1 増やし、A_{i+1}1 減らす。

数列 A が次の条件をみたすようにするために必要な操作の回数の最小値を求めてください。

  • 任意の 1 以上 N 以下の整数の組 (i,j) に対して、\lvert A_i-A_j\rvert\leq 1 が成り立つ。

制約

  • 2 \leq N \leq 5000
  • \lvert A_i \rvert \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

数列 A が問題文の条件をみたすようにするために必要な操作の回数の最小値を一行に出力せよ。


入力例 1

3
2 7 6

出力例 1

4

次のようにして 4 回の操作で A が条件をみたすようにできます。

  • i=1 を選び、A_11 増やし、 A_21 減らす。A=(3,6,6) となる。
  • i=1 を選び、A_11 増やし、 A_21 減らす。A=(4,5,6) となる。
  • i=2 を選び、A_21 増やし、 A_31 減らす。A=(4,6,5) となる。
  • i=1 を選び、A_11 増やし、 A_21 減らす。A=(5,5,5) となる。

この時操作回数が最小であり、よって 4 を出力します。


入力例 2

3
-2 -5 -2

出力例 2

2

次のようにして 2 回の操作で A が条件をみたすようにできます。

  • i=1 を選び、A_11 減らし、 A_21 増やす。A=(-3,-4,-2) となる。
  • i=2 を選び、A_21 増やし、 A_31 減らす。A=(-3,-3,-3) となる。

この時操作回数が最小であり、よって 2 を出力します。


入力例 3

5
1 1 1 1 -7

出力例 3

13

うまく操作することで、13 回の操作の後で、 A=(0,0,-1,-1,-1) にでき、これは問題文の条件をみたします。 12 回以下の操作で条件をみたすようにすることはできないため、13 を出力します。

Score : 550 points

Problem Statement

You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).

Takahashi can perform the following two operations any number of times, possibly zero, in any order.

  • Choose an integer i such that 1\leq i\leq N-1, and decrease A_i by 1 and increase A_{i+1} by 1.
  • Choose an integer i such that 1\leq i\leq N-1, and increase A_i by 1 and decrease A_{i+1} by 1.

Find the minimum number of operations required to make the sequence A satisfy the following condition:

  • \lvert A_i-A_j\rvert\leq 1 for any pair (i,j) of integers between 1 and N, inclusive.

Constraints

  • 2 \leq N \leq 5000
  • \lvert A_i \rvert \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

Print a single line containing the minimum number of operations required to make the sequence A satisfy the condition in the problem statement.


Sample Input 1

3
2 7 6

Sample Output 1

4

You can make A satisfy the condition with four operations as follows.

  • Choose i=1, and increase A_1 by 1 and decrease A_2 by 1, making A=(3,6,6).
  • Choose i=1, and increase A_1 by 1 and decrease A_2 by 1, making A=(4,5,6).
  • Choose i=2, and increase A_2 by 1 and decrease A_3 by 1, making A=(4,6,5).
  • Choose i=1, and increase A_1 by 1 and decrease A_2 by 1, making A=(5,5,5).

This is the minimum number of operations required, so print 4.


Sample Input 2

3
-2 -5 -2

Sample Output 2

2

You can make A satisfy the condition with two operations as follows:

  • Choose i=1, and decrease A_1 by 1 and increase A_2 by 1, making A=(-3,-4,-2).
  • Choose i=2, and increase A_2 by 1 and decrease A_3 by 1, making A=(-3,-3,-3).

This is the minimum number of operations required, so print 2.


Sample Input 3

5
1 1 1 1 -7

Sample Output 3

13

By performing the operations appropriately, with 13 operations you can make A=(0,0,-1,-1,-1), which satisfies the condition in the problem statement. It is impossible to satisfy it with 12 or fewer operations, so print 13.

Ex - Marquee

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

英大文字および英小文字からなる長さ L の文字列 S が幅 W の電光掲示板に表示されており、S が右から左へ 1 文字分の幅ずつスクロールするように切り替わっています。

表示は、S の最後の文字が左端から消えると同時に S の最初の文字が右端から現れる、L+W-1 周期での繰り返しとなっています。

例えば W=5S= ABC のとき、電光掲示板の表示は

  • ABC..
  • BC...
  • C....
  • ....A
  • ...AB
  • ..ABC
  • .ABC.

7 つの状態を繰り返します。(. は文字が表示されていないことを表します)

より厳密には、各 k=0,\ldots,L+W-2 に対して、表示が次のようになっている相異なる状態が定まります。

  • xL+W-1 で割ったあまりを f(x) と表す。電光掲示板の左から (i+1) 番目の位置には、f(i+k)<L のとき Sf(i+k)+1 番目の文字が表示され、そうでないとき何も表示されていない。

英大文字, 英小文字, ., _ からなる長さ W の文字列 P が与えられます。
電光掲示板の L+W-1 種類の状態のうち、_ の箇所を除いて P と一致するものの個数を求めてください。
より厳密には、以下の条件を満たす状態の個数を求めてください。

  • 全ての i=1,\ldots,W に対して次のいずれかが成立する
    • Pi 文字目は _ である
    • 電光掲示板の左から i 番目に表示されている文字が Pi 文字目と等しい
    • 電光掲示板の左から i 番目には何も表示されておらず、かつ、Pi 文字目は . である

制約

  • 1 \leq L \leq W \leq 3\times 10^5
  • L,W は整数である
  • S は英大文字および英小文字のみからなる長さ L の文字列である
  • P は英大文字, 英小文字, ., _ のみからなる長さ W の文字列である

入力

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

L W
S
P

出力

答えを出力せよ。


入力例 1

3 5
ABC
..___

出力例 1

3

電光掲示板の表示が ....A, ...AB, ..ABC であるときの 3 状態が該当します。


入力例 2

11 15
abracadabra
__.._________ab

出力例 2

2

入力例 3

20 30
abaababbbabaabababba
__a____b_____a________________

出力例 3

2

入力例 4

1 1
a
_

出力例 4

1

Score : 600 points

Problem Statement

There is a length L string S consisting of uppercase and lowercase English letters displayed on an electronic bulletin board with a width of W. The string S scrolls from right to left by a width of one character at a time.

The display repeats a cycle of L+W-1 states, with the first character of S appearing from the right edge when the last character of S disappears from the left edge.

For example, when W=5 and S= ABC, the board displays the following seven states in a loop:

  • ABC..
  • BC...
  • C....
  • ....A
  • ...AB
  • ..ABC
  • .ABC.

(. represents a position where no character is displayed.)

More precisely, there are distinct states for k=0,\ldots,L+W-2 in which the display is as follows.

  • Let f(x) be the remainder when x is divided by L+W-1. The (i+1)-th position from the left of the board displays the (f(i+k)+1)-th character of S when f(i+k)<L, and nothing otherwise.

You are given a length W string P consisting of uppercase English letters, lowercase English letters, ., and _.
Find the number of states among the L+W-1 states of the board that coincide P except for the positions with _.
More precisely, find the number of states that satisfy the following condition.

  • For every i=1,\ldots,W, one of the following holds.
    • The i-th character of P is _.
    • The character displayed at the i-th position from the left of the board is equal to the i-th character of P.
    • Nothing is displayed at the i-th position from the left of the board, and the i-th character of P is ..

Constraints

  • 1 \leq L \leq W \leq 3\times 10^5
  • L and W are integers.
  • S is a string of length L consisting of uppercase and lowercase English letters.
  • P is a string of length W consisting of uppercase and lowercase English letters, ., and _.

Input

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

L W
S
P

Output

Print the answer.


Sample Input 1

3 5
ABC
..___

Sample Output 1

3

There are three desired states, where the board displays ....A, ...AB, ..ABC.


Sample Input 2

11 15
abracadabra
__.._________ab

Sample Output 2

2

Sample Input 3

20 30
abaababbbabaabababba
__a____b_____a________________

Sample Output 3

2

Sample Input 4

1 1
a
_

Sample Output 4

1