A - A to Z String 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AN 個、BN 個、…、ZN 個この順に繋げて得られる文字列の先頭から X 番目の文字を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq X \leq N\times 26
  • 入力は全て整数

入力

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

N X

出力

答えを出力せよ。


入力例 1

1 3

出力例 1

C

得られる文字列は ABCDEFGHIJKLMNOPQRSTUVWXYZ です。先頭から 3 番目の文字は C です。


入力例 2

2 12

出力例 2

F

得られる文字列は AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ です。先頭から 12 番目の文字は F です。

Score : 100 points

Problem Statement

Find the X-th character from the beginning of the string that is obtained by concatenating these characters: N copies of A's, N copies of B's, …, and N copies of Z's, in this order.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq X \leq N\times 26
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the answer.


Sample Input 1

1 3

Sample Output 1

C

We obtain the string ABCDEFGHIJKLMNOPQRSTUVWXYZ, whose 3-rd character from the beginning is C.


Sample Input 2

2 12

Sample Output 2

F

We obtain the string AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ, whose 12-th character from the beginning is F.

B - 1D Pawn

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 個のマスが左右一列に並んでおり、左から順にマス 1、マス 2、…、マス N と番号づけられています。
また、K 個のコマがあり、最初左から i 番目のコマはマス A_i に置かれています。
これらに対して、Q 回の操作を行います。 i 回目の操作では次の操作を行います。

  • 左から L_i 番目のコマが一番右のマスにあるならば何も行わない。
  • そうでない時、左から L_i 番目のコマがあるマスの 1 つ右のマスにコマが無いならば、左から L_i 番目のコマを 1 つ右のマスに移動させる。 1 つ右のマスにコマがあるならば、何も行わない。

Q 回の操作が終了した後の状態について、i=1,2,\ldots,K に対して左から i 番目のコマがあるマスの番号を出力してください。

制約

  • 1\leq K\leq N\leq 200
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1\leq Q\leq 1000
  • 1\leq L_i\leq K
  • 入力はすべて整数

入力

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

N K Q
A_1 A_2 \ldots A_K
L_1 L_2 \ldots L_Q

出力

K 個の整数を空白区切りで一行に出力せよ。 ここで、i 個目の整数は、 Q 回の操作が終了した後の状態について、左から i 番目のコマの番号を表す。


入力例 1

5 3 5
1 3 4
3 3 1 1 2

出力例 1

2 4 5

最初、コマはマス 1, 3, 4 にあります。これに対して以下のように操作が行われます。

  • 左から 3 番目のコマはマス 4 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 3 番目のコマをマス 5 に動かします。 コマはマス 1, 3, 5 にある状態になります。
  • 左から 3 番目のコマはマス 5 にあります。 これは一番右のマスなので、何も行いません。 コマはマス 1, 3, 5 にある状態のままです。
  • 左から 1 番目のコマはマス 1 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 1 番目のコマをマス 2 に動かします。 コマはマス 2, 3, 5 にある状態になります。
  • 左から 1 番目のコマはマス 2 にあります。 これは一番右のマスでありませんが、その 1 つ右のマス(マス 3 )にコマが置かれているため、何も行いません。 コマはマス 2, 3, 5 にある状態のままです。
  • 左から 2 番目のコマはマス 3 にあります。 これは一番右のマスでなく、その右のマスにもコマが置かれていないため、左から 2 番目のコマをマス 4 に動かします。 コマはマス 2, 4, 5 にある状態になります。

よって、Q 回の操作が終わった後でコマはマス 2, 4, 5 に置かれているため、2,4,5 を空白区切りでこの順に出力します。


入力例 2

2 2 2
1 2
1 2

出力例 2

1 2

入力例 3

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

出力例 3

2 5 6 7 9 10

Score : 200 points

Problem Statement

There are N squares, indexed Square 1, Square 2, …, Square N, arranged in a row from left to right.
Also, there are K pieces. The i-th piece from the left is initially placed on Square A_i.
Now, we will perform Q operations against them. The i-th operation is as follows:

  • If the L_i-th piece from the left is on its rightmost square, do nothing.
  • Otherwise, move the L_i-th piece from the left one square right if there is no piece on the next square on the right; if there is, do nothing.

Print the index of the square on which the i-th piece from the left is after the Q operations have ended, for each i=1,2,\ldots,K.

Constraints

  • 1\leq K\leq N\leq 200
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1\leq Q\leq 1000
  • 1\leq L_i\leq K
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 \ldots A_K
L_1 L_2 \ldots L_Q

Output

Print K integers in one line, with spaces in between. The i-th of them should be the index of the square on which the i-th piece from the left is after the Q operations have ended.


Sample Input 1

5 3 5
1 3 4
3 3 1 1 2

Sample Output 1

2 4 5

At first, the pieces are on Squares 1, 3, and 4. The operations are performed against them as follows:

  • The 3-rd piece from the left is on Square 4. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 3-rd piece from the left to Square 5. Now, the pieces are on Squares 1, 3, and 5.
  • The 3-rd piece from the left is on Square 5. This is the rightmost square, so do nothing. The pieces are still on Squares 1, 3, and 5.
  • The 1-st piece from the left is on Square 1. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 1-st piece from the left to Square 2. Now, the pieces are on Squares 2, 3, and 5.
  • The 1-st piece from the left is on Square 2. This is not the rightmost square, but the next square on the right (Square 3) contains a piece, so do nothing. The pieces are still on Squares 2, 3, and 5.
  • The 2-nd piece from the left is on Square 3. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 2-nd piece from the left to Square 4; Now, the pieces are still on Squares 2, 4, and 5.

Thus, after the Q operations have ended, the pieces are on Squares 2, 4, and 5, so 2, 4, and 5 should be printed in this order, with spaces in between.


Sample Input 2

2 2 2
1 2
1 2

Sample Output 2

1 2

Sample Input 3

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

Sample Output 3

2 5 6 7 9 10
C - Robot Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、01 からなる長さ N の文字列 S によって表され、
Si 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。

ロボットである高橋君に対して実数 X を設定すると、 高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。

X が実数全体を動くとき、f(X) の最大値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • S01 からなる長さ N の文字列
  • 1\leq W_i\leq 10^9
  • N,W_i は整数

入力

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

N
S
W_1 W_2 \ldots W_N

出力

f(X) の最大値を整数で一行に出力せよ。


入力例 1

5
10101
60 45 30 40 80

出力例 1

4

X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。 よって、f(50)=4 です。

5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。


入力例 2

3
000
1 2 3

出力例 2

3

例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。


入力例 3

5
10101
60 50 50 50 60

出力例 3

4

例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。

Score : 300 points

Problem Statement

There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.

When Takahashi the robot is given a real number X, Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.

Find the maximum value of f(X) for all real values of X.

Constraints

  • 1\leq N\leq 2\times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1\leq W_i\leq 10^9
  • N and W_i are integers.

Input

Input is given from Standard Input in the following format:

N
S
W_1 W_2 \ldots W_N

Output

Print the maximum value of f(X) as an integer in a single line.


Sample Input 1

5
10101
60 45 30 40 80

Sample Output 1

4

When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged. Thus, f(50)=4.

This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.


Sample Input 2

3
000
1 2 3

Sample Output 2

3

For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.


Sample Input 3

5
10101
60 50 50 50 60

Sample Output 3

4

For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.

D - Jumping Takahashi 2

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君が住んでいる二次元平面上の街には N 個のジャンプ台があります。i 番目のジャンプ台は点 (x_i, y_i) にあり、ジャンプ台のパワーは P_i です。また高橋君のジャンプ力は S で表され、はじめ S=0 です。高橋君が訓練を 1 回行う度に S1 増えます。

高橋君は以下の条件を満たす場合に限り、i 番目のジャンプ台から j 番目のジャンプ台にジャンプすることができます。

  • P_iS\geq |x_i - x_j| +|y_i - y_j|

高橋君の目的は、適切に始点とするジャンプ台を決めることで、そのジャンプ台からどのジャンプ台にも何回かのジャンプで移動できるようにすることです。

目的を達成するためには高橋君は最低で何回訓練を行う必要があるでしょうか?

制約

  • 2 \leq N \leq 200
  • -10^9 \leq x_i,y_i \leq 10^9
  • 1 \leq P_i \leq 10^9
  • (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
  • 入力は全て整数

入力

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

N
x_1 y_1 P_1
\vdots
x_N y_N P_N

出力

答えを出力せよ。


入力例 1

4
-10 0 1
0 0 5
10 0 1
11 0 1

出力例 1

2

高橋君が 2 回訓練したとすると、 S=2 です。 このとき、2 番目のジャンプ台から全てのジャンプ台に移動することができます。

例えば、4 番目のジャンプ台へは以下の方法で移動ができます。

  • 2 番目のジャンプ台から 3 番目のジャンプ台へジャンプする。( P_2 S = 10, |x_2-x_3| + |y_2-y_3| = 10 であり、 P_2 S \geq |x_2-x_3| + |y_2-y_3| を満たす。)

  • 3 番目のジャンプ台から 4 番目のジャンプ台へジャンプする。( P_3 S = 2, |x_3-x_4| + |y_3-y_4| = 1 であり、 P_3 S \geq |x_3-x_4| + |y_3-y_4| を満たす。)


入力例 2

7
20 31 1
13 4 3
-10 -15 2
34 26 5
-2 39 4
0 -50 1
5 -20 2

出力例 2

18

Score : 400 points

Problem Statement

There are N trampolines on a two-dimensional planar town where Takahashi lives. The i-th trampoline is located at the point (x_i, y_i) and has a power of P_i. Takahashi's jumping ability is denoted by S. Initially, S=0. Every time Takahashi trains, S increases by 1.

Takahashi can jump from the i-th to the j-th trampoline if and only if:

  • P_iS\geq |x_i - x_j| +|y_i - y_j|.

Takahashi's objective is to become able to choose a starting trampoline such that he can reach any trampoline from the chosen one with some jumps.

At least how many times does he need to train to achieve his objective?

Constraints

  • 2 \leq N \leq 200
  • -10^9 \leq x_i,y_i \leq 10^9
  • 1 \leq P_i \leq 10^9
  • (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 P_1
\vdots
x_N y_N P_N

Output

Print the answer.


Sample Input 1

4
-10 0 1
0 0 5
10 0 1
11 0 1

Sample Output 1

2

If he trains twice, S=2, in which case he can reach any trampoline from the 2-nd one.

For example, he can reach the 4-th trampoline as follows.

  • Jump from the 2-nd to the 3-rd trampoline. (Since P_2 S = 10 and |x_2-x_3| + |y_2-y_3| = 10, it holds that P_2 S \geq |x_2-x_3| + |y_2-y_3|.)

  • Jump from the 3-rd to the 4-th trampoline. (Since P_3 S = 2 and |x_3-x_4| + |y_3-y_4| = 1, it holds that P_3 S \geq |x_3-x_4| + |y_3-y_4|.)


Sample Input 2

7
20 31 1
13 4 3
-10 -15 2
34 26 5
-2 39 4
0 -50 1
5 -20 2

Sample Output 2

18
E - Addition and Multiplication 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋君は整数 x を持っています。最初 x=0 です。

高橋君は以下の操作を好きな回数行えます。

  • 整数 i\ (1\leq i \leq 9) を選ぶ。 C_i 円払い、x10x + i で置き換える。

高橋君の予算は N 円です。操作で支払うお金の総和が予算を超過しないように操作を行うとき、最終的に得られる x の最大値を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq C_i \leq N
  • 入力は全て整数

入力

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

N
C_1 C_2 \ldots C_9

出力

答えを出力せよ。


入力例 1

5
5 4 3 3 2 5 3 5 3

出力例 1

95

例えば i = 9 とする操作、i=5 とする操作を順に行うことで、x は以下のように変化します。

0 \rightarrow 9 \rightarrow 95

操作により支払うお金の合計は C_9 + C_5 = 3 + 2 = 5 円であり、これは予算を超過しません。 予算を超過しないような操作の方法によって 96 以上の整数を作ることが不可能であることが証明できるので、答えは 95 です。


入力例 2

20
1 1 1 1 1 1 1 1 1

出力例 2

99999999999999999999

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

Score : 500 points

Problem Statement

Takahashi has an integer x. Initially, x=0.

Takahashi may do the following operation any number of times.

  • Choose an integer i\ (1\leq i \leq 9). Pay C_i yen (the currency in Japan) to replace x with 10x + i.

Takahashi has a budget of N yen. Find the maximum possible value of the final x resulting from operations without exceeding the budget.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq C_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 \ldots C_9

Output

Print the answer.


Sample Input 1

5
5 4 3 3 2 5 3 5 3

Sample Output 1

95

For example, the operations where i = 9 and i=5 in this order change x as:

0 \rightarrow 9 \rightarrow 95.

The amount of money required for these operations is C_9 + C_5 = 3 + 2 = 5 yen, which does not exceed the budget. Since we can prove that we cannot make an integer greater than or equal to 96 without exceeding the budget, the answer is 95.


Sample Input 2

20
1 1 1 1 1 1 1 1 1

Sample Output 2

99999999999999999999

Note that the answer may not fit into a 64-bit integer type.

F - Teleporter Setting

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の町と M 個のテレポーターがあり、 町は町 1, 町 2, \ldots, 町N と番号づけられています。
それぞれのテレポーターは 2 つの町を双方向に結んでおり、テレポーターを使用する事によってその 2 つの町の間を 1 分で移動することができます。

i 番目のテレポーターは町 U_i と町 V_i を双方向に結んでいますが、 いくつかのテレポーターについては結ぶ町の片方が決まっておらず、 U_i=0 のときそのテレポーターが結ぶ町の片方は町 V_i であるが、 もう片方が未定であることを意味します。

i=1,2,\ldots,N それぞれについて、次の問題を解いてください。

結ぶ町の片方が未定となっているテレポーターの結ぶ先をすべて町 i とする。 この時に町 1 から町 N まで移動するのに最小で何分かかるか求めよ。 町 1 から町 N までテレポーターのみを使って移動するのが不可能な場合は -1 を出力せよ。

制約

  • 2 \leq N \leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 0\leq U_i<V_i\leq N
  • i \neq j ならば (U_i,V_i)\neq (U_j,V_j)
  • 入力は全て整数

入力

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

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

N 個の整数を空白区切りで出力せよ。 ここで、k 番目の整数は i=k とした時の問題に対する答えである.


入力例 1

3 2
0 2
1 2

出力例 1

-1 -1 2

結ぶ先が未定となっているテレポーターの結び先を町 1 としたとき、
1 番目と 2 番目のテレポーターはともに町 1 と町 2 を結びます。 このとき、町 1 から町 3 への移動はできません。

結ぶ先が未定となっているテレポーターの結び先を町 2 としたとき、
1 番目のテレポーターは町 2 同士を、 2 番目のテレポーターは町 1 と町 2 を結びます。 このときもやはり、町 1 から町 3 への移動はできません。

結ぶ先が未定となっているテレポーターの結び先を町 3 としたとき、
1 番目のテレポーターは町 3 と町 2 を、 2 番目のテレポーターは町 1 と町 2 を結びます。
この時、次のようにして町 1 から町 32 分で移動できます。

  • 2 番目のテレポーターを使用し、町 1 から町 2 まで移動する。
  • 1 番目のテレポーターを使用し、町 2 から町 3 まで移動する。

よって、-1,-1,2 をこの順に出力します。

結ぶ先が未定となっているテレポーターの結び先によっては、 同じ町同士を結ぶテレポーターが存在する可能性や、 ある 2 つの町を結ぶテレポーターが複数存在する可能性がある事に注意してください。


入力例 2

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

出力例 2

3 3 3 3 2

Score : 500 points

Problem Statement

There are N towns numbered Town 1, Town 2, \ldots, Town N.
There are also M Teleporters, each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute.

The i-th Teleporter connects Town U_i and Town V_i bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; U_i=0 means that one of the towns the i-th Teleporter connects is Town V_i, but the other end is undetermined.

For i=1,2,\ldots,N, answer the following question.

When the Teleporters with undetermined ends are all determined to be connected to Town i, how many minutes is required at minimum to travel from Town 1 to Town N? If it is impossible to travel from Towns 1 to N using Teleporters only, print -1 instead.

Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 0\leq U_i<V_i\leq N
  • If i \neq j, then (U_i,V_i)\neq (U_j,V_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print N integers, with spaces in between. The k-th integer should be the answer to the question when i=k.


Sample Input 1

3 2
0 2
1 2

Sample Output 1

-1 -1 2

When the Teleporters with an undetermined end are all determined to be connected to Town 1,
the 1-st and the 2-nd Teleporters both connect Towns 1 and 2. Then, it is impossible to travel from Town 1 to Town 3.

When the Teleporters with an undetermined end are all determined to be connected to Town 2,
the 1-st Teleporter connects Town 2 and itself, and the 2-nd one connects Towns 1 and 2. Again, it is impossible to travel from Town 1 to Town 3.

When the Teleporters with an undetermined end are all determined to be connected to Town 3,
the 1-st Teleporter connects Town 3 and Town 2, and the 2-nd one connects Towns 1 and 2. In this case, we can travel from Town 1 to Town 3 in two minutes.

  • Use the 2-nd Teleporter to travel from Town 1 to Town 2.
  • Use the 1-st Teleporter to travel from Town 2 to Town 3.

Therefore, -1,-1, and 2 should be printed in this order.

Note that, depending on which town the Teleporters with an undetermined end are connected to, there may be a Teleporter that connects a town and itself, or multiple Teleporters that connect the same pair of towns.


Sample Input 2

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

Sample Output 2

3 3 3 3 2
G - Prefix Concatenation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

英小文字のみからなる 2 つの文字列 S,T が与えられます。

(相異なっても良い) S の接頭辞を k 個連結することで T と一致させられるような最小の正整数 k を求めてください。

すなわち、S1 文字目から i 文字目までを取り出した文字列を S_i としたときに、 k 個の 1 以上 |S| 以下の整数の組 (a_1,a_2,\ldots, a_k) によって、
T=S_{a_1}+S_{a_2}+\cdots +S_{a_k}(ここで + は文字列としての連結を表す)と書くことができるような 最小の正整数 k を求めてください。

T と一致させる事が不可能な場合は -1 を出力してください。

制約

  • 1 \leq |S| \leq 5\times 10^5
  • 1 \leq |T| \leq 5\times 10^5
  • S,T は英小文字のみからなる文字列

入力

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

S
T

出力

S の接頭辞を k 個連結することで T と一致させられるような最小の正整数 k を出力せよ。 T と一致させる事が不可能な場合は -1 を出力せよ。


入力例 1

aba
ababaab

出力例 1

3

T= ababaabab + aba + ab と書け、ab, aba はそれぞれ S= aba の接頭辞となっています。
ababaab2 個以下の aba の接頭辞の連結によって表す方法はないため、3 を出力します。


入力例 2

atcoder
ac

出力例 2

-1

TS の接頭辞の連結によって表す方法はないため、-1 を出力します。

Score : 600 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters.

Find the minimum positive integer k such that you can choose (not necessarily distinct) k prefixes of S so that their concatenation coincides with T.

In other words, find the minimum positive integer k such that there exists a k-tuple (a_1,a_2,\ldots, a_k) of integers between 1 and |S| such that
T=S_{a_1}+S_{a_2}+\cdots +S_{a_k}, where S_i denotes the substring of S from the 1-st through the i-th characters and + denotes the concatenation of strings.

If it is impossible to make it coincide with T, print -1 instead.

Constraints

  • 1 \leq |S| \leq 5\times 10^5
  • 1 \leq |T| \leq 5\times 10^5
  • S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum positive integer k such that you can choose k prefixes of S so that their concatenation coincides with T. It is impossible to make it coincide with T, print -1 instead.


Sample Input 1

aba
ababaab

Sample Output 1

3

T= ababaab can be written as ab + aba + ab, of which ab and aba are prefixes of S= aba.
Since it is unable to express ababaab with two or less prefixes of aba, print 3.


Sample Input 2

atcoder
ac

Sample Output 2

-1

Since it is impossible to express T as a concatenation of prefixes of S, print -1.

Ex - Dice Sum 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

6 面サイコロ専門店「さいころや」には、N 個のサイコロが売られています。 i 番目のサイコロに書かれている目は A_{i,1},A_{i,2},\ldots,A_{i,6} であり、価格は C_i です。

高橋君はこの中からちょうど K 個のサイコロを選んで購入します。

現在「さいころや」ではキャンペーンが行われており、購入した K 個のサイコロをそれぞれ一度ずつ振り、出た目の総和の二乗のお金を貰えます。なお、どの目が出るかは一様ランダムであり、各サイコロについて独立です。

買う K 個のサイコロを適切に決めることで、(キャンペーンで貰えるお金 )-( 購入した K 個のサイコロの価格の合計) の期待値を最大化し、最大化した際の期待値を \bmod 998244353 で求めてください。

期待値 \bmod 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 1 \leq N \leq 1000
  • 1 \leq K \leq N
  • 1 \leq C_i \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • 入力は全て整数

入力

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

N K
C_1 C_2 \ldots C_N
A_{1,1} A_{1,2} \ldots A_{1,6}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,6}

出力

答えを出力せよ。


入力例 1

3 2
1 2 3
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3

出力例 1

20

2 番目のサイコロと 3 番目のサイコロを買うことにすると、(キャンペーンで貰えるお金 )-( 購入した K 個のサイコロの価格の合計) の期待値は (2 + 3)^2 - (2 + 3) = 20 となります。これが期待値の最大値です。


入力例 2

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

出力例 2

1014

Score : 600 points

Problem Statement

The six-sided dice speciality shop "Saikoroya" sells N dice. The i-th die (singular of dice) has A_{i,1},A_{i,2},\ldots,A_{i,6} written on its each side, and has a price of C_i.

Takahashi is going to choose exactly K of them and buy them.

Currently, "Saikoroya" is conducting a promotion: Takahashi may roll each of the purchased dice once and claim money whose amount is equal to the square of the sum of the numbers shown by the dice. Here, each die shows one of the six numbers uniformly at random and independently.

Maximize the expected value of (the amount of money he claims) - (the sum of money he pays for the purchased K dice) by properly choosing K dice to buy. Print the maximized expected value modulo 998244353.

Definition of the expected value modulo 998244353

We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, the sought expected value can be expressed by an irreducible fraction \frac{y}{x} where x is indivisible by 998244353.

In this case, we can uniquely determine the integer z between 0 and 998244352 (inclusive) such that xz \equiv y \pmod{998244353}. Print such z.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq K \leq N
  • 1 \leq C_i \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
C_1 C_2 \ldots C_N
A_{1,1} A_{1,2} \ldots A_{1,6}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,6}

Output

Print the answer.


Sample Input 1

3 2
1 2 3
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3

Sample Output 1

20

If he buys the 2-nd and 3-rd dice, the expected value of (the amount of money he claims) - (the sum of money he pays for the purchased K dice) equals (2 + 3)^2 - (2 + 3) = 20, which is the maximum expected value.


Sample Input 2

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

Sample Output 2

1014