A - Edge Checker

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

下の画像で示す図において、a 番の点と b 番の点が線で直接結ばれているかを答えてください。

制約

  • 1 \leq a \lt b \leq 10
  • a, b は整数

入力

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

a b

出力

a 番の点と b 番の点が線で直接結ばれている場合は Yes を出力し、結ばれていない場合は No を出力せよ。
ジャッジは英大文字と英小文字を厳密に区別することに注意せよ。


入力例 1

4 5

出力例 1

Yes

問題文で示した図において、4 番の点と 5 番の点は線で直接結ばれています。 よって、Yes を出力します。


入力例 2

3 5

出力例 2

No

問題文で示した図において、3 番の点と 5 番の点は線で直接結ばれていません。 よって、No を出力します。


入力例 3

1 10

出力例 3

Yes

Score : 100 points

Problem Statement

In the figure shown in the image below, are the points numbered a and b directly connected by a line segment?

Constraints

  • 1 \leq a \lt b \leq 10
  • a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the points numbered a and b are directly connected by a line segment, print Yes; otherwise, print No.
The judge is case-sensitive: be sure to print uppercase and lowercase letters correctly.


Sample Input 1

4 5

Sample Output 1

Yes

In the figure shown in the Problem Statement, the points numbered 4 and 5 are directly connected by a line segment.
Thus, Yes should be printed.


Sample Input 2

3 5

Sample Output 2

No

In the figure shown in the Problem Statement, the points numbered 3 and 5 are not directly connected by a line segment.
Thus, No should be printed.


Sample Input 3

1 10

Sample Output 3

Yes
B - Count Distinct Integers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

長さ N の正整数列 a = (a_1, a_2, \dots, a_N) には何種類の整数が現れますか?

制約

  • 1 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

6
1 4 1 2 2 1

出力例 1

3

1, 2, 43 種類の整数が現れます。


入力例 2

1
1

出力例 2

1

入力例 3

11
3 1 4 1 5 9 2 6 5 3 5

出力例 3

7

Score : 200 points

Problem Statement

In a sequence of N positive integers a = (a_1, a_2, \dots, a_N), how many different integers are there?

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

6
1 4 1 2 2 1

Sample Output 1

3

There are three different integers: 1, 2, 4.


Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

11
3 1 4 1 5 9 2 6 5 3 5

Sample Output 3

7
C - Jumping Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君は数直線上の座標 0 の位置にいます。

これから高橋君は N 回のジャンプを行います。i \, (1 \leq i \leq N) 回目のジャンプでは、正の方向に a_i または b_i 移動します。

N 回のジャンプの後に座標 X の位置にいるようにすることはできますか?

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • 入力は全て整数

入力

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

N X
a_1 b_1
\vdots
a_N b_N

出力

N 回のジャンプの後に座標 X の位置にいるようにすることができるならば Yes と、そうでないなら No と出力せよ。


入力例 1

2 10
3 6
4 5

出力例 1

Yes

1 回目のジャンプでは b_1 (= 6) 移動し、2 回目のジャンプでは a_2 (= 4) 移動することで、座標 X (= 10) の位置にいるようにすることができます。


入力例 2

2 10
10 100
10 100

出力例 2

No

1 回目のジャンプの後に座標 X (= 10) の位置にいるようにすることはできますが、全てのジャンプの後に座標 X (= 10) の位置にいるようにすることはできません。


入力例 3

4 12
1 8
5 7
3 4
2 6

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi is standing at the coordinate 0 on a number line.

He will now perform N jumps. In the i-th jump (1 \leq i \leq N), he moves a_i or b_i in the positive direction.

Is it possible for him to be at the coordinate X after N jumps?

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • 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

If it is possible for Takahashi to be at the coordinate X after N jumps, print Yes; otherwise, print No.


Sample Input 1

2 10
3 6
4 5

Sample Output 1

Yes

By moving b_1 (= 6) in the first jump and a_2 (= 4) in the second jump, he can be at the coordinate X (= 10).


Sample Input 2

2 10
10 100
10 100

Sample Output 2

No

He can be at the coordinate X (= 10) after the first jump, but not after all jumps.


Sample Input 3

4 12
1 8
5 7
3 4
2 6

Sample Output 3

Yes
D - Strange Balls

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は 2 以上の整数が書かれた N 個のボールを持っており、これらを細長い筒の中に落としていきます。i \, (1 \leq i \leq N) 回目には、a_i が書かれたボールを落とします。

ボールは特殊な材質でできており、筒の中において k \, (k \geq 2) が書かれたボールが k 個連続すると、それら k 個のボールは全て消えてしまいます。

i \, (1 \leq i \leq N) について、i 個目のボールを筒の中に落とした後、筒の中に何個のボールがあるか求めてください。

制約

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

入力

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

N
a_1 \ldots a_N

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 個目のボールを筒の中に落とした後、筒の中にあるボールの個数を出力せよ。


入力例 1

5
3 2 3 2 2

出力例 1

1
2
3
4
3

筒の中は次のように変化します。

  • 1 個目のボールを落とす。筒の中にあるボールに書かれた整数は 3 である。
  • 2 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2 である。
  • 3 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3 である。
  • 4 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2 である。
  • 5 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2, 2 となるが、2 が書かれたボールが 2 個連続しているのでこれらは消え、下から順に 3, 2, 3 となる。


入力例 2

10
2 3 2 3 3 3 2 3 3 2

出力例 2

1
2
3
4
5
3
2
3
1
0

Score : 400 points

Problem Statement

Takahashi has N balls. Each ball has an integer not less than 2 written on it. He will insert them in a cylinder one by one. The integer written on the i-th ball is a_i.

The balls are made of special material. When k balls with k (k \geq 2) written on them line up in a row, all these k balls will disappear.

For each i (1 \leq i \leq N), find the number of balls after inserting the i-th ball.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the number of balls after inserting the i-th ball.


Sample Input 1

5
3 2 3 2 2

Sample Output 1

1
2
3
4
3

The content of the cylinder changes as follows.

  • After inserting the 1-st ball, the cylinder contains the ball with 3.
  • After inserting the 2-nd ball, the cylinder contains 3, 2 from bottom to top.
  • After inserting the 3-rd ball, the cylinder contains 3, 2, 3 from bottom to top.
  • After inserting the 4-th ball, the cylinder contains 3, 2, 3, 2 from bottom to top.
  • After inserting the 5-th ball, the cylinder momentarily has 3, 2, 3, 2, 2 from bottom to top. The two consecutive balls with 2 disappear, and the cylinder eventually contains 3, 2, 3 from bottom to top.


Sample Input 2

10
2 3 2 3 3 3 2 3 3 2

Sample Output 2

1
2
3
4
5
3
2
3
1
0
E - Ranges on Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の根付き木が与えられます。頂点 1 が根です。
i = 1, 2, \ldots, N-1 について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

i = 1, 2, \ldots, N について、頂点 i を根とする部分木に含まれる頂点全体からなる集合を S_i で表します。(各頂点は自身を根とする部分木に含まれます。すなわち、i \in S_i です。)

また、整数 l, r について、l 以上 r 以下の整数全体からなる集合を [l, r] で表します。 すなわち、[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace です。

整数の 2 つ組を N 個並べた列 \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) であって以下の条件を満たすものを考えます。

  • 1 \leq i \leq N を満たすすべての整数 i について、1 \leq L_i \leq R_i
  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について次が成り立つ
    • S_i \subseteq S_j ならば、[L_i, R_i] \subseteq [L_j, R_j]
    • S_i \cap S_j = \emptyset ならば、[L_i, R_i] \cap [L_j, R_j] = \emptyset

そのような \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) が少なくとも 1 つ存在することが示せます。 それらのうち、登場する整数の最大値 \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace が最小のものを 1 つ出力してください。(複数ある場合はどれを出力しても正解となります。)

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 入力はすべて整数
  • 与えられるグラフは木である

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

下記の形式で N 行出力せよ。すなわち、i = 1, 2, \ldots, N について、i 行目に L_iR_i を空白区切りで出力せよ。

L_1 R_1
L_2 R_2
\vdots
L_N R_N

入力例 1

3
2 1
3 1

出力例 1

1 2
2 2
1 1

(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) が問題文中の条件を満たします。
実際、[L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset が成り立ちます。
また、\max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 であり、これが最小です。


入力例 2

5
3 4
5 4
1 2
1 4

出力例 2

1 3
3 3
2 2
1 2
1 1

入力例 3

5
4 5
3 2
5 2
3 1

出力例 3

1 1
1 1
1 1
1 1
1 1

Score : 500 points

Problem Statement

You are given a rooted tree with N vertices. The root is Vertex 1.
For each i = 1, 2, \ldots, N-1, the i-th edge connects Vertex u_i and Vertex v_i.

For each i = 1, 2, \ldots, N, let S_i denote the set of all vertices in the subtree rooted at Vertex i. (Each vertex is in the subtree rooted at itself, that is, i \in S_i.)

Additionally, for integers l and r, let [l, r] denote the set of all integers between l and r, that is, [l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace.

Consider a sequence of N pairs of integers \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) that satisfies the conditions below.

  • 1 \leq L_i \leq R_i for every integer i such that 1 \leq i \leq N.
  • The following holds for every pair of integers (i, j) such that 1 \leq i, j \leq N.
    • [L_i, R_i] \subseteq [L_j, R_j] if S_i \subseteq S_j
    • [L_i, R_i] \cap [L_j, R_j] = \emptyset if S_i \cap S_j = \emptyset

It can be shown that there is at least one sequence \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big). Among those sequences, print one that minimizes \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace, the maximum integer used. (If there are multiple such sequences, you may print any of them.)

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print N lines in the format below. That is, for each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i separated by a space.

L_1 R_1
L_2 R_2
\vdots
L_N R_N

Sample Input 1

3
2 1
3 1

Sample Output 1

1 2
2 2
1 1

(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) satisfies the conditions.
Indeed, we have [L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset.
Additionally, \max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 is the minimum possible value.


Sample Input 2

5
3 4
5 4
1 2
1 4

Sample Output 2

1 3
3 3
2 2
1 2
1 1

Sample Input 3

5
4 5
3 2
5 2
3 1

Sample Output 3

1 1
1 1
1 1
1 1
1 1
F - Sum Sum Max

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ M の整数列 A, B, C があります。

C は整数 x_1, \dots, x_N, y_1, \dots, y_N によって表されます。C の先頭 y_1 項は x_1 であり、続く y_2 項は x_2 であり、\ldots、末尾の y_N 項は x_N です。

BB_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M) によって定められます。

AA_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M) によって定められます。

A_1, \dots, A_M の最大値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 つのファイルに含まれるテストケースについて、N の総和は 2 \times 10^5 以下
  • 1 \leq M \leq 10^9
  • |x_i| \leq 4 \, (1 \leq i \leq N)
  • y_i \gt 0 \, (1 \leq i \leq N)
  • \sum_{k = 1}^N y_k = M
  • 入力は全て整数

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N M
x_1 y_1
\vdots
x_N y_N

出力

T 行出力せよ。i \, (1 \leq i \leq T) 行目には、i 個目のテストケースに対する答えを出力せよ。


入力例 1

3
3 7
-1 2
2 3
-3 2
10 472
-4 12
1 29
2 77
-1 86
0 51
3 81
3 17
-2 31
-4 65
4 23
1 1000000000
4 1000000000

出力例 1

4
53910
2000000002000000000

1 つ目のテストケースにおいて、

  • C = (-1, -1, 2, 2, 2, -3, -3)
  • B = (-1, -2, 0, 2, 4, 1, -2)
  • A = (-1, -3, -3, -1, 3, 4, 2)

であるので、A_1, \dots, A_M の最大値は 4 です。

Score : 500 points

Problem Statement

There are integer sequences A, B, C of length M each.

C is represented by integers x_1, \dots, x_N, y_1, \dots, y_N. The first y_1 terms of C are x_1, the subsequent y_2 terms are x_2, \ldots, the last y_N terms are x_N.

B is defined by B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M).

A is defined by A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M).

Find the maximum value among A_1, \dots, A_M.

You will be given T test cases to solve.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • The sum of N in a single file is at most 2 \times 10^5.
  • 1 \leq M \leq 10^9
  • |x_i| \leq 4 \, (1 \leq i \leq N)
  • y_i \gt 0 \, (1 \leq i \leq N)
  • \sum_{k = 1}^N y_k = M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is in the following format:

N M
x_1 y_1
\vdots
x_N y_N

Output

Print T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.


Sample Input 1

3
3 7
-1 2
2 3
-3 2
10 472
-4 12
1 29
2 77
-1 86
0 51
3 81
3 17
-2 31
-4 65
4 23
1 1000000000
4 1000000000

Sample Output 1

4
53910
2000000002000000000

In the first test case, we have:

  • C = (-1, -1, 2, 2, 2, -3, -3)
  • B = (-1, -2, 0, 2, 4, 1, -2)
  • A = (-1, -3, -3, -1, 3, 4, 2)

Thus, the maximum value among A_1, \dots, A_M is 4.

G - Teleporting Takahashi

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は無限に広がる三次元グリッドのマス (0, 0, 0) にいます。

高橋君は瞬間移動によってマスからマスへ移動する能力を持っています。 マス (x, y, z) にいるとき、瞬間移動を 1 回行うと (x+1, y, z), (x-1, y, z), (x, y+1, z), (x, y-1, z), (x, y, z+1), (x, y, z-1) のいずれかのマスに移動します。(マス (x, y, z) にとどまることは出来ないことに注意してください。)

ちょうど N 回の瞬間移動を行った後にマス (X, Y, Z) にいるような高橋君の移動経路が何通りあるかを求めてください。

すなわち、整数の 3 つ組を N+1 個並べた列 \big( (x_0, y_0, z_0), (x_1, y_1, z_1), (x_2, y_2, z_2), \ldots, (x_N, y_N, z_N)\big) であって、下記の 3 つの条件をすべて満たすものの個数を求めてください。

  • (x_0, y_0, z_0) = (0, 0, 0)
  • (x_N, y_N, z_N) = (X, Y, Z)
  • i = 1, 2, \ldots, N について、|x_i-x_{i-1}| + |y_i-y_{i-1}| + |z_i-z_{i-1}| = 1

ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを出力してください。

制約

  • 1 \leq N \leq 10^7
  • -10^7 \leq X, Y, Z \leq 10^7
  • N, X, Y, Z は整数

入力

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

N X Y Z

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

3 2 0 -1

出力例 1

3

ちょうど 3 回の瞬間移動を行った後にマス (2, 0, -1) にいるような高橋君の移動経路は、下記の 3 通り存在します。

  • (0, 0, 0) \rightarrow (1, 0, 0) \rightarrow (2, 0, 0) \rightarrow(2, 0, -1)
  • (0, 0, 0) \rightarrow (1, 0, 0) \rightarrow (1, 0, -1) \rightarrow(2, 0, -1)
  • (0, 0, 0) \rightarrow (0, 0, -1) \rightarrow (1, 0, -1) \rightarrow(2, 0, -1)

入力例 2

1 0 0 0

出力例 2

0

ちょうど N 回の瞬間移動を行わなければならないことと、瞬間移動の際には移動せずにその場にとどまることは出来ないことに注意してください。


入力例 3

314 15 92 65

出力例 3

106580952

答えを 998244353 で割った余りを出力することに注意してください。

Score : 600 points

Problem Statement

Takahashi is in the square (0, 0, 0) in an infinite three-dimensional grid.

He can teleport between squares. From the square (x, y, z), he can move to (x+1, y, z), (x-1, y, z), (x, y+1, z), (x, y-1, z), (x, y, z+1), or (x, y, z-1) in one teleport. (Note that he cannot stay in the square (x, y, z).)

Find the number of routes ending in the square (X, Y, Z) after exactly N teleports.

In other words, find the number of sequences of N+1 triples of integers \big( (x_0, y_0, z_0), (x_1, y_1, z_1), (x_2, y_2, z_2), \ldots, (x_N, y_N, z_N)\big) that satisfy all three conditions below.

  • (x_0, y_0, z_0) = (0, 0, 0).
  • (x_N, y_N, z_N) = (X, Y, Z).
  • |x_i-x_{i-1}| + |y_i-y_{i-1}| + |z_i-z_{i-1}| = 1 for each i = 1, 2, \ldots, N.

Since the number can be enormous, print it modulo 998244353.

Constraints

  • 1 \leq N \leq 10^7
  • -10^7 \leq X, Y, Z \leq 10^7
  • N, X, Y, and Z are integers.

Input

Input is given from Standard Input in the following format:

N X Y Z

Output

Print the number modulo 998244353.


Sample Input 1

3 2 0 -1

Sample Output 1

3

There are three routes ending in the square (2, 0, -1) after exactly 3 teleports:

  • (0, 0, 0) \rightarrow (1, 0, 0) \rightarrow (2, 0, 0) \rightarrow(2, 0, -1)
  • (0, 0, 0) \rightarrow (1, 0, 0) \rightarrow (1, 0, -1) \rightarrow(2, 0, -1)
  • (0, 0, 0) \rightarrow (0, 0, -1) \rightarrow (1, 0, -1) \rightarrow(2, 0, -1)

Sample Input 2

1 0 0 0

Sample Output 2

0

Note that exactly N teleports should be performed, and they do not allow him to stay in the same position.


Sample Input 3

314 15 92 65

Sample Output 3

106580952

Be sure to print the number modulo 998244353.

Ex - Sequence of Substrings

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

01 のみからなる長さ N の文字列 S = s_1 s_2 \ldots s_N が与えられます。

整数の 2 つ組を K 個並べた列 \big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big) であって以下の 3 つの条件をすべて満たすものが存在するような最大の整数 K を出力してください。

  • i = 1, 2, \ldots, K について、1 \leq L_i \leq R_i \leq N
  • i = 1, 2, \ldots, K-1 について、R_i \lt L_{i+1}
  • i = 1, 2, \ldots, K-1 について、文字列 s_{L_i}s_{L_i+1} \ldots s_{R_i} は文字列 s_{L_{i+1}}s_{L_{i+1}+1}\ldots s_{R_{i+1}} より辞書順で真に小さい

制約

  • 1 \leq N \leq 2.5 \times 10^4
  • N は整数
  • S01 のみからなる長さ N の文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

7
0101010

出力例 1

3

K = 3 のとき、例えば (L_1, R_1) = (1, 1), (L_2, R_2) = (3, 5), (L_3, R_3) = (6, 7) が問題文中の条件を満たします。 実際、s_1 = 0s_3s_4s_5 = 010 より辞書順で真に小さく、s_3s_4s_5 = 010s_6s_7 = 10 より辞書順で真に小さいです。
K \geq 4 のときは、問題文中の条件を満たす \big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big) は存在しません。


入力例 2

30
000011001110101001011110001001

出力例 2

9

Score : 600 points

Problem Statement

You are given a string S = s_1 s_2 \ldots s_N of length N consisting of 0's and 1's.

Find the maximum integer K such that there is a sequence of K pairs of integers \big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big) that satisfy all three conditions below.

  • 1 \leq L_i \leq R_i \leq N for each i = 1, 2, \ldots, K.
  • R_i \lt L_{i+1} for i = 1, 2, \ldots, K-1.
  • The string s_{L_i}s_{L_i+1} \ldots s_{R_i} is strictly lexicographically smaller than the string s_{L_{i+1}}s_{L_{i+1}+1}\ldots s_{R_{i+1}}.

Constraints

  • 1 \leq N \leq 2.5 \times 10^4
  • N is an integer.
  • S is a string of length N consisting of 0's and 1's.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the answer.


Sample Input 1

7
0101010

Sample Output 1

3

For K = 3, one sequence satisfying the conditition is (L_1, R_1) = (1, 1), (L_2, R_2) = (3, 5), (L_3, R_3) = (6, 7). Indeed, s_1 = 0 is strictly lexicographically smaller than s_3s_4s_5 = 010, and s_3s_4s_5 = 010 is strictly lexicographically smaller than s_6s_7 = 10.
For K \geq 4, there is no sequence \big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big) satisfying the condition.


Sample Input 2

30
000011001110101001011110001001

Sample Output 2

9