A - Fruit Sorting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君は果物農家で、今日の収穫で N 個の果物を得ました。各果物 i の糖度を測定したところ、 S_i でした。

出荷の基準として、糖度が K 以上の果物のみを出荷することができます。糖度が K 未満の果物は規格外として出荷できません。

高橋君は、出荷できる果物(糖度が K 以上の果物)の糖度の平均値を知りたいと考えています。ただし、出荷できる果物が 1 つもない場合は、平均値を計算することができません。

出荷できる果物が 1 つ以上ある場合は、それらの糖度の平均値を出力してください。出荷できる果物が 1 つもない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq 10^9
  • 1 \leq S_i \leq 10^9
  • 入力はすべて整数である。

入力

N K
S_1 S_2 \ldots S_N
  • 1 行目には、果物の個数を表す N 、出荷基準の糖度を表す K が、スペース区切りで与えられる。
  • 2 行目には、各果物の糖度を表す S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。

出力

出荷できる果物(糖度が K 以上の果物)が 1 つ以上ある場合は、それらの糖度の平均値を出力せよ。出荷できる果物が 1 つもない場合は -1 を出力せよ。

なお、平均値を出力する場合、真の値との絶対誤差または相対誤差が 10^{-6} 以下であれば正解とする。


入力例 1

5 8
3 10 7 12 9

出力例 1

10.333333333333334

入力例 2

4 15
3 7 10 14

出力例 2

-1

入力例 3

10 50
45 60 55 30 70 48 52 49 80 51

出力例 3

61.333333333333336

入力例 4

20 500
120 600 450 800 300 550 499 501 750 200 630 480 520 999 100 500 350 680 490 510

出力例 4

640.0

入力例 5

1 1000000000
1000000000

出力例 5

1000000000.0

Score : 233 pts

Problem Statement

Takahashi is a fruit farmer and obtained N fruits from today's harvest. He measured the sugar content of each fruit i and found it to be S_i.

As a shipping criterion, only fruits with sugar content of K or higher can be shipped. Fruits with sugar content less than K are considered substandard and cannot be shipped.

Takahashi wants to know the average sugar content of the fruits that can be shipped (fruits with sugar content of K or higher). However, if there are no fruits that can be shipped, the average cannot be calculated.

If there is at least one fruit that can be shipped, output the average sugar content of those fruits. If there are no fruits that can be shipped, output -1.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq 10^9
  • 1 \leq S_i \leq 10^9
  • All input values are integers.

Input

N K
S_1 S_2 \ldots S_N
  • The first line contains N, the number of fruits, and K, the minimum sugar content for shipping, separated by a space.
  • The second line contains S_1, S_2, \ldots, S_N, the sugar content of each fruit, separated by spaces.

Output

If there is at least one fruit that can be shipped (fruits with sugar content of K or higher), output the average sugar content of those fruits. If there are no fruits that can be shipped, output -1.

When outputting the average, the answer will be considered correct if the absolute error or relative error from the true value is at most 10^{-6}.


Sample Input 1

5 8
3 10 7 12 9

Sample Output 1

10.333333333333334

Sample Input 2

4 15
3 7 10 14

Sample Output 2

-1

Sample Input 3

10 50
45 60 55 30 70 48 52 49 80 51

Sample Output 3

61.333333333333336

Sample Input 4

20 500
120 600 450 800 300 550 499 501 750 200 630 480 520 999 100 500 350 680 490 510

Sample Output 4

640.0

Sample Input 5

1 1000000000
1000000000

Sample Output 5

1000000000.0
B - Updating the Electronic Message Board

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333

問題文

横一列に N 個のマスが並んだ電光掲示板があります。各マスには英大文字 1 文字が表示されており、左から i 番目のマスに表示されている文字を S_i とすると、初期状態は長さ N の文字列 S = S_1 S_2 \dots S_N で表されます。

高橋君はこの掲示板に対して Q 回の 更新 を順に行います。k 回目の更新では、左から i_k 番目のマスの文字を英大文字 c_k に変更します(現在の文字と同じ文字に変更する場合もあります)。

1 \le i < N を満たす整数 i に対して、左から i 番目のマスと i+1 番目のマスに表示されている文字が同じであるとき、この組を 一致ペア と呼びます。一致ペアの個数とは、この条件を満たす i の個数のことです。

各更新の直後について、一致ペアの個数を求めてください。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 10^5
  • S は長さ N の英大文字からなる文字列
  • 1 \leq i_k \leq N (1 \leq k \leq Q)
  • c_k は英大文字 (1 \leq k \leq Q)
  • N, Q, i_k は整数

入力

N Q
S
i_1 c_1
i_2 c_2
\vdots
i_Q c_Q
  • 1 行目には、マスの個数 N と更新回数 Q がスペース区切りで与えられる。
  • 2 行目には、初期状態の文字列 S が与えられる。Si 文字目が、左から i 番目のマスに表示されている文字を表す。
  • 3 行目以降の Q 行には、各更新の内容が与えられる。このうち k 行目(全体では 2 + k 行目)には、k 回目の更新で変更するマスの位置 i_k と変更後の文字 c_k がスペース区切りで与えられる。

出力

Q 行出力せよ。k 行目には、k 回目の更新直後における一致ペアの個数を出力せよ。


入力例 1

5 4
ABBCC
2 A
3 A
5 D
4 D

出力例 1

2
3
2
3

入力例 2

6 5
ABCDEF
1 A
2 A
6 E
5 E
3 A

出力例 2

0
1
2
2
3

入力例 3

12 8
AABCCDDEFGGH
3 A
4 A
5 A
8 D
9 D
10 D
11 D
12 D

出力例 3

5
5
6
7
8
8
9
10

入力例 4

20 12
ABBAACCDDDEEFFGGHHII
1 B
4 B
5 B
10 E
8 C
9 C
20 H
19 H
12 F
11 F
15 H
16 H

出力例 4

11
11
12
12
12
13
12
14
14
14
13
15

入力例 5

2 1
AA
1 B

出力例 5

0

Score : 333 pts

Problem Statement

There is an electronic display board with N cells arranged in a horizontal row. Each cell displays a single uppercase English letter. Let S_i denote the character displayed in the i-th cell from the left. The initial state is represented by a string S = S_1 S_2 \dots S_N of length N.

Takahashi performs Q updates on this display board in order. In the k-th update, he changes the character in the i_k-th cell from the left to the uppercase English letter c_k (the character may be changed to the same character as the current one).

For an integer i satisfying 1 \le i < N, if the characters displayed in the i-th cell and the (i+1)-th cell from the left are the same, this pair is called a matching pair. The number of matching pairs is the number of such i that satisfy this condition.

For each update, determine the number of matching pairs immediately after the update.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 10^5
  • S is a string of length N consisting of uppercase English letters
  • 1 \leq i_k \leq N (1 \leq k \leq Q)
  • c_k is an uppercase English letter (1 \leq k \leq Q)
  • N, Q, i_k are integers

Input

N Q
S
i_1 c_1
i_2 c_2
\vdots
i_Q c_Q
  • The first line contains the number of cells N and the number of updates Q, separated by a space.
  • The second line contains the initial string S. The i-th character of S represents the character displayed in the i-th cell from the left.
  • The following Q lines contain the details of each update. The k-th of these lines (the (2 + k)-th line overall) contains the position i_k of the cell to be changed in the k-th update and the new character c_k, separated by a space.

Output

Output Q lines. The k-th line should contain the number of matching pairs immediately after the k-th update.


Sample Input 1

5 4
ABBCC
2 A
3 A
5 D
4 D

Sample Output 1

2
3
2
3

Sample Input 2

6 5
ABCDEF
1 A
2 A
6 E
5 E
3 A

Sample Output 2

0
1
2
2
3

Sample Input 3

12 8
AABCCDDEFGGH
3 A
4 A
5 A
8 D
9 D
10 D
11 D
12 D

Sample Output 3

5
5
6
7
8
8
9
10

Sample Input 4

20 12
ABBAACCDDDEEFFGGHHII
1 B
4 B
5 B
10 E
8 C
9 C
20 H
19 H
12 F
11 F
15 H
16 H

Sample Output 4

11
11
12
12
12
13
12
14
14
14
13
15

Sample Input 5

2 1
AA
1 B

Sample Output 5

0
C - Cargo Delivery Truck

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は運送会社の配車担当です。倉庫には N 個の荷物が番号 1 から N の順に一列に並んでおり、i 番目の荷物の重さは A_i です。

これらの荷物をちょうど M 台のトラックに積み分けて配送します。積み分けは、荷物の番号の順序を保ったまま、連続する番号の荷物からなるちょうど M 個のグループに分割して行います。具体的には、0 = L_0 < L_1 < L_2 < \cdots < L_M = N を満たす整数列 L_0, L_1, \ldots, L_M を選び、k 番目 (1 \leq k \leq M) のトラックには荷物 L_{k-1}+1, L_{k-1}+2, \ldots, L_k を積みます。L_0 < L_1 < \cdots < L_M は狭義不等号であるため、各トラックには少なくとも 1 個の荷物が含まれることに注意してください。

各トラックについて、積まれた荷物の重さの合計を「積載量」と呼びます。M 台のトラックのうち積載量が最も大きいトラックに負担が集中してしまうため、高橋君は積載量の最大値ができるだけ小さくなるように荷物を分けたいと考えています。

すべての分け方の中で、積載量の最大値の最小値を S とします。整数 K が与えられるので、S > K であるかどうかを判定してください。

  • どのように分けても積載量の最大値が K を超えてしまう(すなわち S > K)ならば Yes を出力してください。
  • 積載量の最大値を K 以下にできる分け方が存在する(すなわち S \leq K)ならば No を出力してください。

制約

  • 1 \leq M \leq N \leq 10^6
  • 1 \leq A_i \leq 10^9
  • 1 \leq K \leq 10^{15}
  • 入力はすべて整数である

入力

N M K
A_1 A_2 \ldots A_N
  • 1 行目には、荷物の個数を表す整数 N、トラックの台数を表す整数 M、判定の基準値を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各荷物の重さを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

S > K ならば Yes を、S \leq K ならば No1 行で出力せよ。


入力例 1

5 2 10
3 5 2 4 6

出力例 1

No

入力例 2

5 2 9
3 5 2 4 6

出力例 2

Yes

入力例 3

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

出力例 3

No

入力例 4

20 4 50
7 12 3 8 15 6 9 11 4 10 13 2 14 5 8 16 3 7 11 6

出力例 4

No

入力例 5

3 3 5
5 6 5

出力例 5

Yes

Score : 366 pts

Problem Statement

Takahashi is a dispatch manager at a shipping company. In the warehouse, N packages are lined up in a row, numbered from 1 to N, and the weight of the i-th package is A_i.

These packages are to be divided among exactly M trucks for delivery. The division is performed by partitioning the packages into exactly M groups of consecutively numbered packages, preserving the order of package numbers. Specifically, we choose an integer sequence L_0, L_1, \ldots, L_M satisfying 0 = L_0 < L_1 < L_2 < \cdots < L_M = N, and the k-th truck (1 \leq k \leq M) is loaded with packages L_{k-1}+1, L_{k-1}+2, \ldots, L_k. Note that since L_0 < L_1 < \cdots < L_M uses strict inequalities, each truck contains at least one package.

For each truck, the total weight of its loaded packages is called its "load". Since the truck with the largest load among the M trucks bears a disproportionate burden, Takahashi wants to divide the packages so that the maximum load is as small as possible.

Let S be the minimum possible value of the maximum load over all possible ways to divide the packages. Given an integer K, determine whether S > K.

  • If the maximum load exceeds K no matter how the packages are divided (i.e., S > K), output Yes.
  • If there exists a way to divide the packages such that the maximum load is at most K (i.e., S \leq K), output No.

Constraints

  • 1 \leq M \leq N \leq 10^6
  • 1 \leq A_i \leq 10^9
  • 1 \leq K \leq 10^{15}
  • All input values are integers.

Input

N M K
A_1 A_2 \ldots A_N
  • The first line contains the integer N representing the number of packages, the integer M representing the number of trucks, and the integer K representing the threshold value for the determination, separated by spaces.
  • The second line contains the integers A_1, A_2, \ldots, A_N representing the weight of each package, separated by spaces.

Output

If S > K, output Yes; if S \leq K, output No, on a single line.


Sample Input 1

5 2 10
3 5 2 4 6

Sample Output 1

No

Sample Input 2

5 2 9
3 5 2 4 6

Sample Output 2

Yes

Sample Input 3

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

Sample Output 3

No

Sample Input 4

20 4 50
7 12 3 8 15 6 9 11 4 10 13 2 14 5 8 16 3 7 11 6

Sample Output 4

No

Sample Input 5

3 3 5
5 6 5

Sample Output 5

Yes
D - Construction of a Communication Network

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は通信会社のネットワーク設計担当者です。N 個の拠点があり、それらを結ぶ M 本の通信ケーブルの敷設候補があります。各候補 i は拠点 u_i と拠点 v_i を双方向に結び、敷設コストは c_i です。

高橋君はすべての拠点間で(直接または中継によって)通信可能なネットワークを構築しなければなりません。ただし、ネットワーク全体の評価値は単なる総コストではなく、以下のように定義される「負荷指数」で測られます。

敷設するケーブルの集合を S としたとき、負荷指数は次のように定義されます。

\text{負荷指数} = \left( \sum_{i \in S} c_i \right) + K \times \max_{i \in S} c_i

ここで K は会社の方針で定められた非負整数の係数です。負荷指数は、総敷設コストに加えて、最もコストの高いケーブルのコストに K を掛けたペナルティが加算されることを意味します。これは、極端にコストの高いケーブルを含むことによる保守・運用上のリスクを反映しています。

高橋君は、すべての拠点間を通信可能にするケーブルの集合 S のうち、負荷指数を最小化したいと考えています。負荷指数の最小値を求めてください。

なお、すべての拠点間を通信可能にするケーブルの集合が必ず存在することが保証されます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N - 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^6
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • 1 \leq c_i \leq 10^6
  • 入力はすべて整数である
  • すべての拠点間を通信可能にするケーブルの集合が存在する

入力

N M K
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
  • 1 行目には、拠点の数を表す N 、ケーブル候補の数を表す M 、係数を表す K が、スペース区切りで与えられる。
  • 2 行目から M 行では、各ケーブル候補の情報が与えられる。
  • 1 + i 行目では、ケーブル候補 i が結ぶ 2 つの拠点 u_i , v_i と敷設コスト c_i が、スペース区切りで与えられる。

出力

負荷指数の最小値を 1 行で出力せよ。


入力例 1

4 5 2
1 2 3
2 3 4
3 4 5
1 4 10
1 3 6

出力例 1

22

入力例 2

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

出力例 2

7

入力例 3

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

出力例 3

55

入力例 4

12 20 100
1 2 15
2 3 18
3 4 14
4 5 16
5 6 20
6 7 13
7 8 17
8 9 19
9 10 12
10 11 21
11 12 11
1 6 50
2 7 45
3 8 40
4 9 35
5 10 30
6 11 25
7 12 22
1 12 100
3 10 28

出力例 4

2276

入力例 5

2 1 1000000
1 2 1000000

出力例 5

1000001000000

Score : 400 pts

Problem Statement

Takahashi is a network design engineer at a telecommunications company. There are N bases, and there are M candidate communication cables to connect them. Each candidate i bidirectionally connects base u_i and base v_i with an installation cost of c_i.

Takahashi must construct a network where all bases can communicate with each other (either directly or via relays). However, the overall evaluation of the network is not measured simply by the total cost, but by a "load index" defined as follows.

Let S be the set of cables to be installed. The load index is defined as:

\text{Load Index} = \left( \sum_{i \in S} c_i \right) + K \times \max_{i \in S} c_i

Here, K is a non-negative integer coefficient determined by company policy. The load index means that, in addition to the total installation cost, a penalty is added by multiplying the cost of the most expensive cable by K. This reflects the maintenance and operational risks of including extremely expensive cables.

Takahashi wants to find a set of cables S that makes all bases connected while minimizing the load index. Find the minimum possible load index.

Note that it is guaranteed that there always exists a set of cables that makes all bases connected.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N - 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^6
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • 1 \leq c_i \leq 10^6
  • All input values are integers.
  • There exists at least one set of cables that makes all bases connected.

Input

N M K
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
  • The 1st line contains the number of bases N, the number of candidate cables M, and the coefficient K, separated by spaces.
  • The next M lines provide information about each candidate cable.
  • The (1 + i)-th line contains the two bases u_i and v_i connected by candidate cable i, and its installation cost c_i, separated by spaces.

Output

Print the minimum load index in a single line.


Sample Input 1

4 5 2
1 2 3
2 3 4
3 4 5
1 4 10
1 3 6

Sample Output 1

22

Sample Input 2

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

Sample Output 2

7

Sample Input 3

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

Sample Output 3

55

Sample Input 4

12 20 100
1 2 15
2 3 18
3 4 14
4 5 16
5 6 20
6 7 13
7 8 17
8 9 19
9 10 12
10 11 21
11 12 11
1 6 50
2 7 45
3 8 40
4 9 35
5 10 30
6 11 25
7 12 22
1 12 100
3 10 28

Sample Output 4

2276

Sample Input 5

2 1 1000000
1 2 1000000

Sample Output 5

1000001000000
E - Joining of DNA Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433

問題文

高橋君はバイオインフォマティクスの研究をしています。ある日、2つのDNA断片を接合する作業を行うことになりました。

研究で扱うDNA断片は、塩基配列を 01 の2値で符号化した文字列として表されています。断片 S は長さ L の文字列、断片 T は長さ M の文字列です( L \leq M )。高橋君は、断片 S を断片 T左側に付けるか右側に付けるかして接合したいと考えています。接合の際、2つの断片の端同士が重なるように配置できますが、重なった部分の配列は完全に一致していなければなりません。なお、重なりのない単純な連結は許されず、必ず 1 文字以上の重なりが必要です。

より正確に述べます。

左側接合(接合後の並びが S, T の順になるもの):ある正の整数 k1 \le k \le L )を選びます。S の末尾 k 文字と T の先頭 k 文字が一致するとき、重なり幅 k で左側接合ができます。接合後の文字列は、S 全体に続けて T の先頭 k 文字を除いた残りを並べたもの(equivalently、S の先頭 L - k 文字に続けて T 全体を並べたもの)となり、全体の長さは L + M - k です。

右側接合(接合後の並びが T, S の順になるもの):ある正の整数 k1 \le k \le L )を選びます。T の末尾 k 文字と S の先頭 k 文字が一致するとき、重なり幅 k で右側接合ができます。接合後の文字列は、T 全体に続けて S の先頭 k 文字を除いた残りを並べたもの(equivalently、T 全体に続けて S の末尾 L - k 文字を並べたもの)となり、全体の長さは L + M - k です。

いずれの場合も k = LS 全体が T の端と重なる場合)も許されます。L \leq M であるため、k \le L の範囲では T 側にも k 文字以上が常に存在することに注意してください。

重なり幅が大きいほど接合後の全体の長さが短くなり、解析が効率的になります。高橋君は、左側接合・右側接合のいずれかを選んで、重なり幅を最大化したいと考えています。

左側接合で実現可能な最大の重なり幅と、右側接合で実現可能な最大の重なり幅のうち、大きい方の値を求めてください。どちらの接合方法でも重なり幅 1 以上の接合が不可能な場合(すなわち、左側接合でも右側接合でも配列が一致する重なりが存在しない場合)は、0 を出力してください。

制約

  • 1 \leq L \leq 5 \times 10^5
  • 1 \leq M \leq 5 \times 10^5
  • L \leq M
  • S は長さ L の文字列であり、01 のみからなる。
  • T は長さ M の文字列であり、01 のみからなる。

入力

L M
S
T
  • 1 行目には、断片 S の長さ L と、断片 T の長さ M が、スペース区切りで与えられる。
  • 2 行目には、断片 S を表す長さ L の文字列が与えられる。S01 のみからなる。
  • 3 行目には、断片 T を表す長さ M の文字列が与えられる。T01 のみからなる。

出力

左側接合および右側接合で実現可能な最大の重なり幅のうち、大きい方の値を 1 行で出力せよ。どちらの接合方法でも重なり幅 1 以上の接合が不可能な場合は 0 を出力せよ。


入力例 1

3 5
101
01101

出力例 1

3

入力例 2

1 4
0
1111

出力例 2

0

入力例 3

12 20
110010101011
01010110011101011010

出力例 3

7

入力例 4

50 80
01001101010100110101010011010101001101010100110101
01001101010100110101010011010111111000001111100000111110000011111000001111100000

出力例 4

30

入力例 5

1 1
1
1

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is conducting research in bioinformatics. One day, he needs to join two DNA fragments together.

The DNA fragments used in the research are represented as strings where the base sequences are encoded using the two values 0 and 1. Fragment S is a string of length L, and fragment T is a string of length M (L \leq M). Takahashi wants to join fragment S to either the left side or the right side of fragment T. During joining, the ends of the two fragments can be placed so that they overlap, but the sequences in the overlapping portion must match exactly. Simple concatenation without overlap is not allowed; an overlap of at least 1 character is required.

More precisely:

Left joining (the resulting order is S, T): Choose a positive integer k (1 \le k \le L). When the last k characters of S match the first k characters of T, a left join with overlap width k is possible. The resulting string is formed by S in its entirety followed by the remaining part of T after removing its first k characters (equivalently, the first L - k characters of S followed by T in its entirety), and has total length L + M - k.

Right joining (the resulting order is T, S): Choose a positive integer k (1 \le k \le L). When the last k characters of T match the first k characters of S, a right join with overlap width k is possible. The resulting string is formed by T in its entirety followed by the remaining part of S after removing its first k characters (equivalently, T in its entirety followed by the last L - k characters of S), and has total length L + M - k.

In both cases, k = L (where the entirety of S overlaps with an end of T) is also permitted. Note that since L \leq M, for any k \le L, T always has at least k characters available.

The larger the overlap width, the shorter the total length after joining, making analysis more efficient. Takahashi wants to choose either left joining or right joining to maximize the overlap width.

Find the larger of the maximum achievable overlap width for left joining and the maximum achievable overlap width for right joining. If joining with an overlap width of 1 or more is impossible for both joining methods (i.e., there is no overlap where the sequences match for either left joining or right joining), output 0.

Constraints

  • 1 \leq L \leq 5 \times 10^5
  • 1 \leq M \leq 5 \times 10^5
  • L \leq M
  • S is a string of length L consisting only of 0 and 1.
  • T is a string of length M consisting only of 0 and 1.

Input

L M
S
T
  • The first line contains the length L of fragment S and the length M of fragment T, separated by a space.
  • The second line contains a string of length L representing fragment S. S consists only of 0 and 1.
  • The third line contains a string of length M representing fragment T. T consists only of 0 and 1.

Output

Output in a single line the larger of the maximum achievable overlap widths for left joining and right joining. If joining with an overlap width of 1 or more is impossible for both joining methods, output 0.


Sample Input 1

3 5
101
01101

Sample Output 1

3

Sample Input 2

1 4
0
1111

Sample Output 2

0

Sample Input 3

12 20
110010101011
01010110011101011010

Sample Output 3

7

Sample Input 4

50 80
01001101010100110101010011010101001101010100110101
01001101010100110101010011010111111000001111100000111110000011111000001111100000

Sample Output 4

30

Sample Input 5

1 1
1
1

Sample Output 5

1