A - 注文の確認

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

配点 : 200

問題文

高橋君はレストランのホールスタッフとして働いています。今日は N 人のお客さんからそれぞれ 1 つずつ料理の注文を受けました。

i 番目 (1 \leq i \leq N) のお客さんが実際に注文した料理名は T_i ですが、高橋君がキッチンに伝えた料理名は S_i でした。聞き間違いや伝え間違いがあったかもしれないので、高橋君は確認することにしました。

実際の注文 T_i とキッチンへの伝達 S_i が異なるお客さん、すなわち注文を間違えて伝えてしまったお客さんの人数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • N は整数である。
  • T_i および S_i は英小文字からなる長さ 1 以上 20 以下の文字列である。

入力

N
T_1 S_1
T_2 S_2
\vdots
T_N S_N

1 行目にはお客さんの人数を表す整数 N が与えられる。続く N 行のうち i (1 \leq i \leq N) 番目の行には、i 番目のお客さんの実際の注文 T_i とキッチンへの伝達 S_i がスペース区切りで与えられる。

出力

注文を間違えて伝えてしまったお客さんの人数を 1 行で出力せよ。


入力例 1

4
ramen ramen
sushi susi
curry curry
pasta pizza

出力例 1

2

入力例 2

3
tea tea
cake cake
salad salad

出力例 2

0

入力例 3

10
ramen ramen
sushi sushi
pizza piza
burger burger
salad salad
omelet omelette
steak steak
pasta pasta
soup soap
juice juice

出力例 3

3

入力例 4

20
ramen ramen
sushi sushi
curry curry
pasta pasta
salad salsa
burger buger
steak steak
soup soup
juice joice
coffee coffee
tea tea
cake cake
icecream icecream
sandwich sandwitch
dumpling dumpling
noodle noodel
omelet omelet
pancake pancake
chicken chicken
fish dish

出力例 4

6

入力例 5

1
aaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb

出力例 5

1

Score : 200 pts

Problem Statement

Takahashi works as a floor staff member at a restaurant. Today, he received exactly one dish order from each of N customers.

The dish that the i-th (1 \leq i \leq N) customer actually ordered is T_i, but the dish name that Takahashi relayed to the kitchen was S_i. Since there may have been mishearings or miscommunications, Takahashi decided to verify the orders.

Find the number of customers for whom the actual order T_i and the relayed order S_i differ, that is, the number of customers whose orders were incorrectly communicated.

Constraints

  • 1 \leq N \leq 10^5
  • N is an integer.
  • T_i and S_i are strings of lowercase English letters with length between 1 and 20, inclusive.

Input

N
T_1 S_1
T_2 S_2
\vdots
T_N S_N

The first line contains an integer N representing the number of customers. The i-th (1 \leq i \leq N) of the following N lines contains the actual order T_i and the relayed order S_i for the i-th customer, separated by a space.

Output

Print the number of customers whose orders were incorrectly communicated, on a single line.


Sample Input 1

4
ramen ramen
sushi susi
curry curry
pasta pizza

Sample Output 1

2

Sample Input 2

3
tea tea
cake cake
salad salad

Sample Output 2

0

Sample Input 3

10
ramen ramen
sushi sushi
pizza piza
burger burger
salad salad
omelet omelette
steak steak
pasta pasta
soup soap
juice juice

Sample Output 3

3

Sample Input 4

20
ramen ramen
sushi sushi
curry curry
pasta pasta
salad salsa
burger buger
steak steak
soup soup
juice joice
coffee coffee
tea tea
cake cake
icecream icecream
sandwich sandwitch
dumpling dumpling
noodle noodel
omelet omelet
pancake pancake
chicken chicken
fish dish

Sample Output 4

6

Sample Input 5

1
aaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb

Sample Output 5

1
B - モンスター討伐

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

配点 : 333

問題文

高橋君はRPGゲームをプレイしています。ゲームのステージには N 体のモンスターが一列に並んでおり、左から順に番号 1, 2, \ldots, N が付けられています。モンスター i の強さは D_i で、倒すと報酬として V_i ゴールドを得られます。

高橋君は最初、攻撃力 0、所持ゴールド 0 の状態です。高橋君はモンスターを倒すとそのモンスターの力を吸収し、攻撃力が増加します。以下では、高橋君の現在の攻撃力を h で表します。

高橋君は左端からモンスター 1, 2, \ldots, N の順に、各モンスターをちょうど1回ずつ訪れます。

また、高橋君は「後回しスタック」と呼ばれる空のスタック(LIFO: 最後に追加した要素を最初に取り出すデータ構造)を持っています。スタックに最後に追加された要素(次に取り出される要素)のことを「一番上の要素」と呼びます。

高橋君がモンスター i を訪れたとき、以下のルールに従います。

  • もし D_i > h であれば、攻撃力が足りないためそのモンスターを後回しにします。後回しにしたモンスターは後回しスタックの一番上に追加されます。この場合、後回し解消処理は行いません。
  • もし D_i \leq h であれば、そのモンスターを倒します。倒すと報酬として V_i ゴールドを得て、攻撃力がそのモンスターの強さだけ増加します(h \leftarrow h + D_i)。モンスターを倒した直後、以下の「後回し解消処理」を行います。

後回し解消処理: 以下の操作を、終了条件を満たすまで繰り返します。

  1. 後回しスタックが空であれば、処理を終了します。
  2. スタックの一番上にあるモンスターの強さを確認します。
  • その強さが h 以下であれば、そのモンスターをスタックから取り除いて倒します。倒した際には通常と同様に報酬を得て、攻撃力がそのモンスターの強さだけ増加します。その後、手順 1 に戻ります。
  • その強さが h より大きければ、処理を終了します。

注意: 後回し解消処理では、常にスタックの一番上のみを確認します。一番上のモンスターを倒して取り除くと、その下にあったモンスターが新たに一番上になり、次の確認対象となります。スタックの途中にある要素を飛ばして確認することはありません。

すべてのモンスター 1, 2, \ldots, N を順に訪れ、それぞれについて上記のルールに従った処理を終えた後、最後にもう一度「後回し解消処理」を行います。

この一連の処理がすべて終了した時点で、後回しスタックにモンスターが残っている場合、それらのモンスターは倒せなかったものとして扱います。倒せなかったモンスターからは報酬を得られず、攻撃力の増加もありません。

最終的に高橋君が得られる報酬の合計ゴールド数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D_i \leq 10^9
  • 1 \leq V_i \leq 10^9
  • 入力はすべて整数である。

入力

N
D_1 V_1
D_2 V_2
\vdots
D_N V_N
  • 1 行目には、モンスターの数を表す整数 N が与えられる。
  • 2 行目から N + 1 行目では、各モンスターの情報が与えられる。
  • 1 + i 行目では、モンスター i の強さ D_i と報酬 V_i がスペース区切りで与えられる。

出力

高橋君が得られる報酬の合計ゴールド数を 1 行で出力せよ。


入力例 1

5
0 10
3 20
1 15
0 5
2 30

出力例 1

15

入力例 2

4
5 100
3 50
2 30
1 10

出力例 2

0

入力例 3

8
0 10
0 5
3 20
1 15
2 30
10 100
5 50
4 25

出力例 3

15

入力例 4

12
0 100
0 50
1 200
3 150
2 80
0 10
5 300
4 120
10 500
8 250
6 180
20 1000

出力例 4

160

入力例 5

1
0 1000000000

出力例 5

1000000000

Score : 333 pts

Problem Statement

Takahashi is playing an RPG game. On a game stage, N monsters are lined up in a row, numbered 1, 2, \ldots, N from left to right. Monster i has strength D_i, and defeating it yields a reward of V_i gold.

Takahashi starts with attack power 0 and 0 gold. When Takahashi defeats a monster, he absorbs that monster's power, increasing his attack power. Below, we denote Takahashi's current attack power as h.

Takahashi visits the monsters in order from the left: monster 1, 2, \ldots, N, visiting each monster exactly once.

Additionally, Takahashi has an empty stack called the "postpone stack" (LIFO: a data structure where the last element added is the first to be removed). The element that was added to the stack last (the next element to be removed) is called the "top element."

When Takahashi visits monster i, he follows these rules:

  • If D_i > h, his attack power is insufficient, so he postpones that monster. The postponed monster is added to the top of the postpone stack. In this case, the postpone resolution process is not performed.
  • If D_i \leq h, he defeats that monster. Upon defeating it, he receives a reward of V_i gold, and his attack power increases by that monster's strength (h \leftarrow h + D_i). Immediately after defeating the monster, the following "postpone resolution process" is performed.

Postpone resolution process: Repeat the following operations until the termination condition is met.

  1. If the postpone stack is empty, terminate the process.
  2. Check the strength of the monster on top of the stack.
  • If that strength is h or less, remove that monster from the stack and defeat it. Upon defeating it, receive the reward and increase attack power by that monster's strength, just as usual. Then, return to step 1.
  • If that strength is greater than h, terminate the process.

Note: In the postpone resolution process, only the top of the stack is ever checked. When the top monster is defeated and removed, the monster that was below it becomes the new top and is the next to be checked. Elements in the middle of the stack are never skipped over and checked.

After visiting all monsters 1, 2, \ldots, N in order and performing the above rules for each, one final "postpone resolution process" is performed.

After this entire sequence of operations is complete, if any monsters remain in the postpone stack, they are treated as undefeated. No reward is obtained from undefeated monsters, and no attack power increase occurs.

Determine the total gold Takahashi receives as reward.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D_i \leq 10^9
  • 1 \leq V_i \leq 10^9
  • All input values are integers.

Input

N
D_1 V_1
D_2 V_2
\vdots
D_N V_N
  • The first line contains an integer N, representing the number of monsters.
  • Lines 2 through N + 1 provide information about each monster.
  • Line 1 + i contains the strength D_i and reward V_i of monster i, separated by a space.

Output

Print the total gold Takahashi receives as reward in a single line.


Sample Input 1

5
0 10
3 20
1 15
0 5
2 30

Sample Output 1

15

Sample Input 2

4
5 100
3 50
2 30
1 10

Sample Output 2

0

Sample Input 3

8
0 10
0 5
3 20
1 15
2 30
10 100
5 50
4 25

Sample Output 3

15

Sample Input 4

12
0 100
0 50
1 200
3 150
2 80
0 10
5 300
4 120
10 500
8 250
6 180
20 1000

Sample Output 4

160

Sample Input 5

1
0 1000000000

Sample Output 5

1000000000
C - うわさの伝播

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

配点 : 366

問題文

N 人の人々がおり、それぞれ 1 から N までの番号が付けられています。人々の間には全部で M 本の一方向の連絡手段があり、i 番目の連絡手段では人 U_i から人 V_i へメッセージを送ることができます。

はじめ(日 0 の開始時点)では、人 S だけがあるうわさを知っています。うわさの伝播は次のルールに従って日ごとに進みます。

  • dd = 0, 1, 2, \ldots)の開始時点でうわさを知っている各人は、その人を送信元とするすべての連絡手段を通じて、うわさを一斉に送ります。送られたうわさは日 d+1 の開始時点で届き、届いた人はその時点からうわさを知っている状態になります。
  • d+1 の開始時点で新たにうわさを知った人が実際にうわさを送れるのは、日 d+1 以降です。
  • 一度うわさを知った人は、以降ずっとうわさを知っている状態のままです。

すべての人がうわさを知っている状態に最初になる日 d を求めてください。すなわち、日 d の開始時点ではじめてすべての人がうわさを知っている状態であるような最小の d を出力してください。日 0 の開始時点ですでにすべての人がうわさを知っている場合(N = 1 の場合など)は 0 を出力してください。どれだけ日数が経ってもすべての人にうわさが広まらない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 4 \times 10^5
  • 0 \leq M < 2 \times 10^5
  • 1 \leq S \leq N
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i(自己ループは存在しない)
  • 同じ順序対 (U_i, V_i) は高々 1 つしか存在しない
  • 入力はすべて整数である

入力

N M S
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、人数を表す N、連絡手段の数を表す M、最初にうわさを知っている人の番号を表す S が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目には、連絡手段の情報が与えられる。
  • 1 + i 行目では、i 番目の連絡手段が人 U_i から人 V_i へ向かうことを表す U_iV_i が、スペース区切りで与えられる。

出力

すべての人がうわさを知っている状態になる最も早い日 d1 行で出力せよ。ただし、すべての人にうわさが広まらない場合は -1 を出力せよ。


入力例 1

4 4 1
1 2
1 3
2 4
3 4

出力例 1

2

入力例 2

5 4 2
2 3
3 4
4 2
1 5

出力例 2

-1

入力例 3

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

出力例 3

4

入力例 4

15 22 10
10 1
10 2
10 3
1 4
1 5
2 5
2 6
3 6
3 7
4 8
5 8
5 9
6 9
6 11
7 11
8 12
8 9
9 13
11 14
12 15
13 15
14 15

出力例 4

5

入力例 5

1 0 1

出力例 5

0

Score : 366 pts

Problem Statement

There are N people, numbered from 1 to N. There are a total of M one-way communication channels among the people. The i-th communication channel allows person U_i to send a message to person V_i.

Initially (at the start of day 0), only person S knows a certain rumor. The spread of the rumor proceeds day by day according to the following rules:

  • Each person who knows the rumor at the start of day d (d = 0, 1, 2, \ldots) simultaneously sends the rumor through all communication channels originating from that person. The sent rumors arrive at the start of day d+1, and the recipients become aware of the rumor from that point onward.
  • A person who newly learns the rumor at the start of day d+1 can only actually send the rumor from day d+1 onward.
  • Once a person learns the rumor, they remain aware of it permanently.

Find the day d when all people first become aware of the rumor. That is, output the smallest d such that at the start of day d, all people know the rumor for the first time. If all people already know the rumor at the start of day 0 (such as when N = 1), output 0. If the rumor cannot reach all people no matter how many days pass, output -1.

Constraints

  • 1 \leq N \leq 4 \times 10^5
  • 0 \leq M < 2 \times 10^5
  • 1 \leq S \leq N
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i (no self-loops exist)
  • Each ordered pair (U_i, V_i) appears at most once
  • All input values are integers

Input

N M S
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains N representing the number of people, M representing the number of communication channels, and S representing the number of the person who initially knows the rumor, separated by spaces.
  • Lines 2 through M + 1 contain the communication channel information.
  • The (1 + i)-th line contains U_i and V_i, separated by spaces, indicating that the i-th communication channel goes from person U_i to person V_i.

Output

Output in a single line the earliest day d when all people become aware of the rumor. However, if the rumor cannot spread to all people, output -1.


Sample Input 1

4 4 1
1 2
1 3
2 4
3 4

Sample Output 1

2

Sample Input 2

5 4 2
2 3
3 4
4 2
1 5

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

4

Sample Input 4

15 22 10
10 1
10 2
10 3
1 4
1 5
2 5
2 6
3 6
3 7
4 8
5 8
5 9
6 9
6 11
7 11
8 12
8 9
9 13
11 14
12 15
13 15
14 15

Sample Output 4

5

Sample Input 5

1 0 1

Sample Output 5

0
D - 本棚の整理

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

配点 : 400

問題文

高橋君は本棚に N 冊の本を左から一列に並べています。左から i 番目の本のページ数は A_i であり、この本を本棚から取り除いて古本屋に引き取ってもらうには、高橋君が手数料 C_i を支払う必要があります。本棚に残す本には手数料はかかりません。

高橋君は本棚の見栄えを良くするため、いくつかの本を取り除き、残った本のページ数が左から右に向かって狭義単調増加になるようにしたいと考えています。すなわち、残った本を左から順に見たとき、どの隣り合う 2 冊についても左の本のページ数が右の本のページ数より真に小さくなるようにします。なお、本を取り除くだけで並べ替えは行わないため、残った本の相対的な並び順は元の並び順と同じです。

取り除く冊数は 0 冊以上 N 冊以下の任意の冊数でよいものとします。残った本が 0 冊または 1 冊の場合も狭義単調増加の条件は満たされるものとします。

残った本のページ数が左から順に見て狭義単調増加となるように取り除く本の集合を選ぶとき、取り除いた本の手数料の合計の最小値を求めてください。

制約

  • 1 \leq N \leq 5000
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 入力はすべて整数である。

入力

N
A_1 A_2 \ldots A_N
C_1 C_2 \ldots C_N
  • 1 行目には、本の冊数を表す整数 N が与えられる。
  • 2 行目には、各本のページ数を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。ここで A_i は左から i 番目の本のページ数である。
  • 3 行目には、各本を取り除く際の手数料を表す N 個の整数 C_1, C_2, \ldots, C_N がスペース区切りで与えられる。ここで C_i は左から i 番目の本を取り除く際に高橋君が支払う手数料である。

出力

残った本のページ数が狭義単調増加となるように取り除く本を選んだとき、取り除いた本の手数料の合計の最小値を 1 行で出力せよ。


入力例 1

5
3 1 4 1 5
10 20 30 40 50

出力例 1

50

入力例 2

4
5 5 5 5
3 1 4 2

出力例 2

6

入力例 3

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

出力例 3

31

入力例 4

20
15 8 23 42 11 37 5 29 44 18 3 50 33 21 46 9 38 27 41 12
7 12 3 9 15 6 20 4 11 8 18 2 14 10 5 16 1 13 7 19

出力例 4

135

入力例 5

1
1000000000
1000000000

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi has N books arranged in a row from left to right on his bookshelf. The i-th book from the left has A_i pages, and to remove this book from the bookshelf and have it taken by a used bookstore, Takahashi must pay a handling fee of C_i. No fee is charged for books that remain on the bookshelf.

To improve the appearance of his bookshelf, Takahashi wants to remove some books so that the page counts of the remaining books are strictly monotonically increasing from left to right. That is, when looking at the remaining books from left to right, for any two adjacent books, the page count of the left book must be strictly less than the page count of the right book. Note that since books are only removed and not rearranged, the relative order of the remaining books is the same as the original order.

The number of books removed may be any number from 0 to N, inclusive. The strictly monotonically increasing condition is considered satisfied when 0 or 1 books remain.

Find the minimum total handling fee of the removed books when choosing which books to remove so that the page counts of the remaining books are strictly monotonically increasing from left to right.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • All input values are integers.

Input

N
A_1 A_2 \ldots A_N
C_1 C_2 \ldots C_N
  • The first line contains an integer N representing the number of books.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the page counts of each book. Here, A_i is the page count of the i-th book from the left.
  • The third line contains N integers C_1, C_2, \ldots, C_N separated by spaces, representing the handling fees for removing each book. Here, C_i is the handling fee Takahashi must pay to remove the i-th book from the left.

Output

Print in one line the minimum total handling fee of the removed books when choosing which books to remove so that the page counts of the remaining books are strictly monotonically increasing.


Sample Input 1

5
3 1 4 1 5
10 20 30 40 50

Sample Output 1

50

Sample Input 2

4
5 5 5 5
3 1 4 2

Sample Output 2

6

Sample Input 3

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

Sample Output 3

31

Sample Input 4

20
15 8 23 42 11 37 5 29 44 18 3 50 33 21 46 9 38 27 41 12
7 12 3 9 15 6 20 4 11 8 18 2 14 10 5 16 1 13 7 19

Sample Output 4

135

Sample Input 5

1
1000000000
1000000000

Sample Output 5

0
E - トーナメント分割の均衡グループ

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

配点 : 466

問題文

2^N 人の生徒が左から右に一列に並んでおり、各生徒は赤い帽子または白い帽子をかぶっています。生徒の帽子の色は、長さ 2^N の文字列 S で表されます。左から i 番目の生徒について、Si 文字目が 0 なら赤い帽子、1 なら白い帽子をかぶっています。

高橋君は体育祭の準備として、次の操作を高々 1行うことができます。

  • ある整数 l1 \leq l \leq 2^N - K + 1)を選び、左から l 番目から l+K-1 番目までの連続する K 人の生徒を並び替える。並び替えの結果、区間内の赤い帽子の生徒がすべて区間の左側に、白い帽子の生徒がすべて区間の右側に連続して配置される。区間外の生徒の位置は変わらない。

操作後(または操作を行わない場合はそのまま)、生徒たちは次のルールで再帰的にグループ分けされます。

  1. 最初に、左から 1 番目から 2^N 番目までの全生徒を 1 つのグループとする。
  2. 人数が 2 以上のグループは、左から数えて前半の生徒からなるグループと、後半の生徒からなるグループの 2 つに分割する。
  3. 各グループの人数が 1 になるまでこの分割を繰り返す。

この過程で現れるすべてのグループ(最初の全体グループ、途中の各段階のグループ、最終的な 1 人のグループをすべて含む、合計 2^{N+1} - 1 個)について考えます。あるグループにおいて、グループ内の赤い帽子の生徒の人数と白い帽子の生徒の人数が等しいとき、そのグループを「均衡グループ」と呼びます。なお、1 人のグループはこの条件を満たさないため、均衡グループにはなりません。

高橋君が操作の有無および区間の位置を最適に選んだとき、均衡グループの個数としてあり得る最大値を求めてください。

制約

  • 1 \leq N
  • 2^N \leq 10^6
  • 1 \leq K \leq 2^N
  • S01 からなる長さ 2^N の文字列
  • N, K は整数

入力

N K
S

1 行目には、生徒数のパラメータを表す整数 N と操作で並び替える区間の長さを表す整数 K がスペース区切りで与えられます。2 行目には、各生徒の帽子の色を表す長さ 2^N の文字列 S が与えられます。

出力

均衡グループの個数の最大値を 1 つの整数として出力してください。


入力例 1

2 2
0110

出力例 1

3

入力例 2

3 3
01011001

出力例 2

7

入力例 3

5 7
00110101111000010100111011001010

出力例 3

19

入力例 4

8 64
0100110010110001111001011010100011110000101010100101001111001100000111010010110111001000101101111010101000001111111100000101010101100110011010010101101011000011100100111011010001001011011110000011001111001100001111000011110011001010010101100011101010010111

出力例 4

146

入力例 5

1 1
01

出力例 5

1

Score : 466 pts

Problem Statement

2^N students are standing in a row from left to right, and each student is wearing either a red hat or a white hat. The hat colors of the students are represented by a string S of length 2^N. For the i-th student from the left, if the i-th character of S is 0, they are wearing a red hat, and if it is 1, they are wearing a white hat.

Takahashi, in preparation for the sports festival, can perform the following operation at most once:

  • Choose an integer l (1 \leq l \leq 2^N - K + 1), and rearrange the K consecutive students from the l-th to the (l+K-1)-th from the left. As a result of the rearrangement, all students with red hats within the interval are placed consecutively on the left side of the interval, and all students with white hats are placed consecutively on the right side of the interval. The positions of students outside the interval do not change.

After the operation (or as-is if no operation is performed), the students are recursively divided into groups according to the following rules:

  1. Initially, all students from the 1-st to the 2^N-th from the left form one group.
  2. A group with 2 or more people is split into two groups: one consisting of the first half of students (counted from the left) and one consisting of the second half.
  3. This splitting is repeated until each group contains exactly 1 person.

Consider all groups that appear during this process (including the initial whole group, groups at each intermediate stage, and the final single-person groups — a total of 2^{N+1} - 1 groups). A group is called a "balanced group" if the number of students with red hats and the number of students with white hats within the group are equal. Note that a single-person group cannot satisfy this condition, so it is never a balanced group.

Find the maximum possible number of balanced groups when Takahashi optimally chooses whether to perform the operation and the position of the interval.

Constraints

  • 1 \leq N
  • 2^N \leq 10^6
  • 1 \leq K \leq 2^N
  • S is a string of length 2^N consisting of 0 and 1
  • N, K are integers

Input

N K
S

The first line contains the integer N, which represents the parameter for the number of students, and the integer K, which represents the length of the interval to be rearranged in the operation, separated by a space. The second line contains the string S of length 2^N representing the hat color of each student.

Output

Output the maximum number of balanced groups as a single integer.


Sample Input 1

2 2
0110

Sample Output 1

3

Sample Input 2

3 3
01011001

Sample Output 2

7

Sample Input 3

5 7
00110101111000010100111011001010

Sample Output 3

19

Sample Input 4

8 64
0100110010110001111001011010100011110000101010100101001111001100000111010010110111001000101101111010101000001111111100000101010101100110011010010101101011000011100100111011010001001011011110000011001111001100001111000011110011001010010101100011101010010111

Sample Output 4

146

Sample Input 5

1 1
01

Sample Output 5

1