A - chmin

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
i=1,2,\dots,N の順に以下を行ってください。

  • もし A_i<X なら、 X=A_i に更新した上で 1 を出力する。
  • そうでないなら 0 を出力する。

制約

  • 入力は全て整数
  • 1 \le N,X,A_i \le 100

入力

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

N X
A_1 A_2 \dots A_N

出力

N 行出力せよ。
そのうち k 行目には、 i=k についての出力をせよ。


入力例 1

5 10
6 4 7 1 3

出力例 1

1
1
0
1
0
  • 最初、 X=10 です。
  • i=1 について、 A_1=6<X=10 なので、 X=6 に更新した上で 1 を出力します。
  • i=2 について、 A_2=4<X=6 なので、 X=4 に更新した上で 1 を出力します。
  • i=3 について、 A_3=7 \ge X=4 なので、 0 を出力します。
  • i=4 について、 A_4=1<X=4 なので、 X=1 に更新した上で 1 を出力します。
  • i=5 について、 A_5=3 \ge X=1 なので、 0 を出力します。

入力例 2

1 1
1

出力例 2

0

入力例 3

8 20
9 19 14 17 17 4 18 4

出力例 3

1
0
0
0
0
1
0
0

Score : 100 points

Problem Statement

You are given a length-N integer sequence A=(A_1,A_2,\dots,A_N) and an integer X.
For i=1,2,\dots,N in this order, do the following.

  • If A_i<X, update X=A_i and output 1.
  • Otherwise, output 0.

Constraints

  • All input values are integers.
  • 1 \le N,X,A_i \le 100

Input

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

N X
A_1 A_2 \dots A_N

Output

Output N lines.
The k-th line should contain the output for i=k.


Sample Input 1

5 10
6 4 7 1 3

Sample Output 1

1
1
0
1
0
  • Initially, X=10.
  • For i=1: since A_1=6<X=10, update X=6 and output 1.
  • For i=2: since A_2=4<X=6, update X=4 and output 1.
  • For i=3: since A_3=7 \ge X=4, output 0.
  • For i=4: since A_4=1<X=4, update X=1 and output 1.
  • For i=5: since A_5=3 \ge X=1, output 0.

Sample Input 2

1 1
1

Sample Output 2

0

Sample Input 3

8 20
9 19 14 17 17 4 18 4

Sample Output 3

1
0
0
0
0
1
0
0
B - Armor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

高橋君は鎧を着ています。

この鎧は威力(整数で表される攻撃の強さ)が D 以下の攻撃をすべて防ぎますが、威力が D より大きい攻撃は防ぎません。

この鎧は威力が A の攻撃を防ぎますか。

制約

  • 1 \leq A \leq 100
  • 1 \leq D \leq 100
  • 入力される値はすべて整数

入力

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

A D

出力

鎧が攻撃を防ぐなら Yes、防がないなら No と出力せよ。


入力例 1

4 5

出力例 1

Yes

この例では A = 4, D = 5 です。A = 4 は D = 5 以下であるため鎧は攻撃を防ぎ、答えは Yes です。


入力例 2

5 5

出力例 2

Yes

この例では A = 5, D = 5 です。A = 5 は D = 5 以下であるため鎧は攻撃を防ぎ、答えは Yes です。


入力例 3

6 5

出力例 3

No

この例では A = 6, D = 5 です。A = 6 は D = 5 以下でないため鎧は攻撃を防がず、答えは No です。

Score : 100 points

Problem Statement

Takahashi is wearing armor.

This armor blocks all attacks with power (strength represented as an integer) of D or less, but does not block attacks with power greater than D.

Does this armor block an attack with power A?

Constraints

  • 1 \leq A \leq 100
  • 1 \leq D \leq 100
  • All input values are integers.

Input

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

A D

Output

Output Yes if the armor blocks the attack, and No otherwise.


Sample Input 1

4 5

Sample Output 1

Yes

In this sample, A = 4 and D = 5. Since A = 4 is at most D = 5, the armor blocks the attack, so the answer is Yes.


Sample Input 2

5 5

Sample Output 2

Yes

In this sample, A = 5 and D = 5. Since A = 5 is at most D = 5, the armor blocks the attack, so the answer is Yes.


Sample Input 3

6 5

Sample Output 3

No

In this sample, A = 6 and D = 5. Since A = 6 is not at most D = 5, the armor does not block the attack, so the answer is No.

C - Base 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

0 と 1 からなる長さ 64 の数列 A=(A_0,A_1,\dots,A_{63}) が与えられます。

A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63} を求めてください。

制約

  • A_i は 0 または 1

入力

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

A_0 A_1 \dots A_{63}

出力

答えを整数として出力せよ。


入力例 1

1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

出力例 1

13

A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63} = 2^0 + 2^2 + 2^3 = 13 です。


入力例 2

1 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0

出力例 2

766067858140017173

Score : 200 points

Problem Statement

You are given a sequence A=(A_0,A_1,\dots,A_{63}) of length 64 consisting of 0 and 1.

Find A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63}.

Constraints

  • A_i is 0 or 1.

Input

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

A_0 A_1 \dots A_{63}

Output

Print the answer as an integer.


Sample Input 1

1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output 1

13

A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63} = 2^0 + 2^2 + 2^3 = 13.


Sample Input 2

1 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0

Sample Output 2

766067858140017173
D - Count Adjacent Cells

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

H 行 W 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。

マス (x_1, y_1) とマス (x_2, y_2) が辺で隣接するとは、|x_1 - x_2| + |y_1 - y_2| = 1 が成り立つことをいいます。

すべてのマスについて、そのマスに辺で隣接するマスの個数を求めてください。

制約

  • 1 \leq H,W \leq 50
  • 入力される値はすべて整数

入力

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

H W

出力

答えを以下の形式で出力せよ。

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

ここで、x_{i,j} はマス (i,j) に辺で隣接するマスの個数を表す。


入力例 1

4 5

出力例 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

マス (1, 5) に辺で隣接するマスは、マス (1, 4), (2, 5) の 2 個です。

マス (2, 3) に辺で隣接するマスは、マス (1, 3), (2, 2), (2, 4), (3, 3) の 4 個です。

マス (4, 2) に辺で隣接するマスは、マス (3, 2), (4, 1), (4, 3) の 3 個です。


入力例 2

1 1

出力例 2

0

マス (1,1) に辺で隣接するマスは存在しません。


入力例 3

12 8

出力例 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i, j).

Cells (x_1, y_1) and (x_2, y_2) are said to be edge-adjacent when |x_1 - x_2| + |y_1 - y_2| = 1.

For every cell, find the number of cells that are edge-adjacent to it.

Constraints

  • 1 \leq H, W \leq 50
  • All input values are integers.

Input

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

H W

Output

Output the answer in the following format:

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

Here, x_{i,j} represents the number of cells that are edge-adjacent to cell (i, j).


Sample Input 1

4 5

Sample Output 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

The cells edge-adjacent to cell (1, 5) are cells (1, 4), (2, 5), for a total of two cells.

The cells edge-adjacent to cell (2, 3) are cells (1, 3), (2, 2), (2, 4), (3, 3), for a total of four cells.

The cells edge-adjacent to cell (4, 2) are cells (3, 2), (4, 1), (4, 3), for a total of three cells.


Sample Input 2

1 1

Sample Output 2

0

There are no cells edge-adjacent to cell (1, 1).


Sample Input 3

12 8

Sample Output 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2
E - Select Mul

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。

例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。

  • 12 と 3
  • 21 と 3
  • 13 と 2
  • 31 と 2
  • 23 と 1
  • 32 と 1

なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1 と 01 の 2 つに分離することはできません。また上述の「正整数に分離する」という条件より、101 を 11 と 0 の 2 つに分離することもできません。

適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?

制約

  • N は 1 以上 10^9 以下の整数
  • N には 0 でない桁が 2 つ以上含まれる

入力

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

N

出力

分離後の 2 数の積の最大値を出力せよ。


入力例 1

123

出力例 1

63

問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。

  • 12 と 3
  • 21 と 3
  • 13 と 2
  • 31 と 2
  • 23 と 1
  • 32 と 1

積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。


入力例 2

1010

出力例 2

100

考えられる分離の仕方は以下の 2 通りです。

  • 100 と 1
  • 10 と 10

いずれの場合にも積は 100 となります。


入力例 3

998244353

出力例 3

939337176

Score : 300 points

Problem Statement

You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.

For example, for the integer 123, there are six ways to separate it, as follows:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.

What is the maximum possible product of the two resulting integers, obtained by the optimal separation?

Constraints

  • N is an integer between 1 and 10^9 (inclusive).
  • N contains two or more digits that are not 0.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible product of the two integers after separation.


Sample Input 1

123

Sample Output 1

63

As described in Problem Statement, there are six ways to separate it:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.


Sample Input 2

1010

Sample Output 2

100

There are two ways to separate it:

  • 100 and 1,
  • 10 and 10.

In either case, the product is 100.


Sample Input 3

998244353

Sample Output 3

939337176
F - Tallest at the Moment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

現在、会議室に N 人の高橋くんがいます。 i 番目 (1\le i\le N) の高橋くんの身長は H _ i であり、今から L _ i 分後に会議室を去ります。 一度会議室を去った高橋くんはそれ以降会議室に戻ることはありません。

Q 個のクエリが与えられるので、順に答えてください。 i 番目 (1\le i\le Q) のクエリでは整数 T _ i が与えられるので、今から T _ i+\dfrac12 分後に会議室にいる高橋くんの身長の最大値を答えてください。 この問題の制約のもとで、今から T _ i+\dfrac12 分後には会議室に 1 人以上の高橋くんがいることが保証されます。

制約

  • 1\le N\le3\times10 ^ 5
  • 1\le H _ i\le10 ^ 9\ (1\le i\le N)
  • 1\le L _ 1\le L _ 2\le\cdots\le L _ N\le10 ^ 9
  • 1\le Q\le3\times10 ^ 5
  • 0\le T _ i\lt L _ N\ (1\le i\le Q)
  • 入力はすべて整数

入力

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

N
H _ 1 L _ 1
H _ 2 L _ 2
\vdots
H _ N L _ N
Q
T _ 1 T _ 2 \ldots T _ Q

出力

Q 行にわたって出力せよ。 i 行目 (1\le i\le Q) には、i 番目のクエリに対する答えを出力せよ。


入力例 1

4
31 4
26 5
3 5
15 9
4
3 4 5 6

出力例 1

31
26
15
15

今から 3+\dfrac12 分後には、現在会議室にいる高橋くんは全員会議室にとどまっています。 よって、1 番目のクエリの答えは \lbrace31,26,3,15\rbrace の最大値である 31 です。

今から 5+\dfrac12 分後には、会議室には 4 番目の高橋くんだけがいます。 よって、3 番目のクエリの答えは \lbrace15\rbrace の最大値である 15 です。


入力例 2

10
587 138
772 155
755 404
519 408
529 432
169 586
114 632
249 656
329 972
299 984
14
443 801 824 276 399 314 300 510 311 580 498 930 359 5

出力例 2

329
329
329
755
755
755
755
329
755
329
329
329
755
772

Score : 300 points

Problem Statement

Currently, there are N Takahashi in a conference room. The i-th (1\le i\le N) Takahashi has a height of H _ i and will leave the room L _ i minutes from now. Once a Takahashi leaves the room, he never returns.

You are given Q queries, so answer them in order. For the i-th (1\le i\le Q) query, you are given an integer T _ i, so find the maximum height among the Takahashi who are in the room T _ i+\dfrac12 minutes from now. Under the constraints of this problem, it is guaranteed that at least one Takahashi will be in the room T _ i+\dfrac12 minutes from now.

Constraints

  • 1\le N\le3\times10 ^ 5
  • 1\le H _ i\le10 ^ 9\ (1\le i\le N)
  • 1\le L _ 1\le L _ 2\le\cdots\le L _ N\le10 ^ 9
  • 1\le Q\le3\times10 ^ 5
  • 0\le T _ i\lt L _ N\ (1\le i\le Q)
  • All input values are integers.

Input

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

N
H _ 1 L _ 1
H _ 2 L _ 2
\vdots
H _ N L _ N
Q
T _ 1 T _ 2 \ldots T _ Q

Output

Output Q lines. The i-th line (1\le i\le Q) should contain the answer to the i-th query.


Sample Input 1

4
31 4
26 5
3 5
15 9
4
3 4 5 6

Sample Output 1

31
26
15
15

3+\dfrac12 minutes from now, all Takahashi currently in the room are still there. Thus, the answer to the first query is 31, the maximum of \lbrace31,26,3,15\rbrace.

5+\dfrac12 minutes from now, only the fourth Takahashi is in the room. Thus, the answer to the third query is 15, the maximum of \lbrace15\rbrace.


Sample Input 2

10
587 138
772 155
755 404
519 408
529 432
169 586
114 632
249 656
329 972
299 984
14
443 801 824 276 399 314 300 510 311 580 498 930 359 5

Sample Output 2

329
329
329
755
755
755
755
329
755
329
329
329
755
772
G - Kadomatsu Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
以下を全て満たす整数の 3 つ組 (i,j,k) がいくつあるか求めてください。

  • 1 \le i,j,k \le N
  • A_i : A_j : A_k = 7:5:3
  • \min(i,j,k) = j または \max(i,j,k) = j

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

10
3 10 7 10 7 6 7 6 5 14

出力例 1

7

条件を満たす整数の 3 つ組 (i,j,k) は以下の 7 個です。

  • (3,9,1)
    • A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
  • (5,9,1)
    • A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
  • (7,9,1)
    • A_i : A_j : A_k = 7:5:3 であり、 \max(i,j,k) = j です。
  • (10,2,6)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
  • (10,2,8)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
  • (10,4,6)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。
  • (10,4,8)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3 であり、 \min(i,j,k) = j です。

入力例 2

6
210 210 210 210 210 210

出力例 2

0

入力例 3

21
49 30 50 21 35 15 21 70 35 9 50 70 21 49 30 50 70 15 9 21 30

出力例 3

34

Score : 425 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the number of triples of integers (i,j,k) that satisfy all of the following:

  • 1 \le i,j,k \le N
  • A_i : A_j : A_k = 7:5:3
  • \min(i,j,k) = j or \max(i,j,k) = j.

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N
A_1 A_2 \dots A_N

Output

Output the answer.


Sample Input 1

10
3 10 7 10 7 6 7 6 5 14

Sample Output 1

7

The seven triples of integers (i,j,k) that satisfy the conditions are:

  • (3,9,1)
    • A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
  • (5,9,1)
    • A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
  • (7,9,1)
    • A_i : A_j : A_k = 7:5:3, and \max(i,j,k) = j.
  • (10,2,6)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
  • (10,2,8)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
  • (10,4,6)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.
  • (10,4,8)
    • A_i : A_j : A_k = 14:10:6 = 7:5:3, and \min(i,j,k) = j.

Sample Input 2

6
210 210 210 210 210 210

Sample Output 2

0

Sample Input 3

21
49 30 50 21 35 15 21 70 35 9 50 70 21 49 30 50 70 15 9 21 30

Sample Output 3

34
H - Forbidden Prefix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

文字列の多重集合 X,Y があります。はじめ、両方ともに空です。

Q 個のクエリが与えられるので、順に処理してください。i 番目のクエリでは、整数 T_i と文字列 S_i が与えられるので、T_i=1 ならば X に S_i を追加し、T_i=2 ならば Y に S_i を追加してください。

各クエリの処理後、以下の値を出力してください。

  • Y に含まれる文字列のうち、X のどの要素も接頭辞として持たないものの個数

制約

  • Q は 1 以上 2 \times 10^5 以下の整数
  • T_i \in \{1,2\}
  • S_i は長さ 1 以上 5 \times 10^5 以下の英小文字のみからなる文字列
  • \displaystyle \sum_{i=1}^Q |S_i| \leq 5 \times 10^5

入力

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

Q
T_1 S_1
T_2 S_2
\vdots
T_Q S_Q

出力

Q 行出力せよ。i 行目 (1 \leq i \leq Q) には、i 番目のクエリの処理後の答えを出力せよ。


入力例 1

4
1 at
2 watcoder
2 atcoder
1 wa

出力例 1

0
1
1
0

i=1,2,3,4 番目のクエリの処理後の答えはそれぞれ以下のようになります。

  • i=1: Y は空なので、求める個数は 0 です。
  • i=2: watcoder は X のどの要素も接頭辞として持たないので、求める個数は 1 です。
  • i=3: watcoder は X のどの要素も接頭辞として持たず、atcoder は at を接頭辞として持つので、求める個数は 1 個です。
  • i=4: watcoder は wa を、atcoder は at を接頭辞として持つので、求める個数は 0 個です。

入力例 2

10
1 w
1 avko
2 atcoder
1 bzginn
2 beginner
1 atco
2 contest
1 ntxcdg
1 atc
1 contest

出力例 2

0
0
1
1
2
1
2
2
2
1

Score : 500 points

Problem Statement

There are two multisets of strings, X and Y, both initially empty.

You are given Q queries to process in order. In the i-th query, you receive an integer T_i and a string S_i. If T_i=1, insert S_i into X; if T_i=2, insert S_i into Y.

After processing each query, print this value:

  • the number of strings in Y that have no element of X as a prefix.

Constraints

  • Q is an integer between 1 and 2 \times 10^5, inclusive.
  • T_i \in \{1,2\}
  • Each S_i is a string of length between 1 and 5\times 10^5, inclusive, consisting of lowercase English letters.
  • \displaystyle \sum_{i=1}^Q |S_i| \leq 5 \times 10^5

Input

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

Q
T_1\ S_1
T_2\ S_2
\vdots
T_Q\ S_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the count after processing the i-th query.


Sample Input 1

4
1 at
2 watcoder
2 atcoder
1 wa

Sample Output 1

0
1
1
0

The counts after processing the queries for i=1,2,3,4 are as follows.

  • i=1: Y is empty, so the count is 0.
  • i=2: watcoder has no element of X as a prefix, so the count is 1.
  • i=3: watcoder has no element of X as a prefix, while atcoder has at as a prefix, so the count is 1.
  • i=4: watcoder has wa as a prefix, and atcoder has at as a prefix, so the count is 0.

Sample Input 2

10
1 w
1 avko
2 atcoder
1 bzginn
2 beginner
1 atco
2 contest
1 ntxcdg
1 atc
1 contest

Sample Output 2

0
0
1
1
2
1
2
2
2
1
I - -1, +1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

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

あなたは以下の操作を A に対して 0 回以上好きな回数行うことができます:

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

A を狭義単調増加列にするために必要な操作回数の最小値を求めてください。

ただし、答えは 2^{63} 未満になることが証明できます。

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

制約

  • 1\le T\le 3\times 10^5
  • 1\le N\le 2\times 10^5
  • 0\le A_i\le 10^9
  • 全てのテストケースにおける N の総和は 6\times 10^5 以下
  • 入力される値は全て整数

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。

N
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

4
3
0 1 0
4
4 6 3 5
7
1 2 3 4 5 6 7
10
11 9 1 3 17 19 10 19 17 3

出力例 1

3
5
0
78

1 番目のテストケースについて考えます。

以下のように操作することで 3 回の操作で A を狭義単調増加列にすることができます:

  • i=1 を選ぶ。A=(-1,2,0) となる。
  • i=2 を選ぶ。A=(-1,1,1) となる。
  • i=2 を選ぶ。A=(-1,0,2) となる。

3 回未満の操作で A を狭義単調増加列にすることはできないので、3 を出力してください。

Score : 525 points

Problem Statement

You are given a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) of length N.

You can perform the following operation on A zero or more times:

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

Find the minimum number of operations required to make A strictly increasing.

It can be proved that the answer is less than 2^{63}.

You are given T test cases; solve each.

Constraints

  • 1 \le T \le 3 \times 10^5
  • 1 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • The sum of N across all test cases is at most 6 \times 10^5.
  • All input values are integers.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

4
3
0 1 0
4
4 6 3 5
7
1 2 3 4 5 6 7
10
11 9 1 3 17 19 10 19 17 3

Sample Output 1

3
5
0
78

Consider the first test case.

By performing the following operations, A can be made strictly increasing in three operations:

  • Choose i=1. A becomes (-1, 2, 0).
  • Choose i=2. A becomes (-1, 1, 1).
  • Choose i=2. A becomes (-1, 0, 2).

It is impossible to make A strictly increasing in fewer than three operations, so output 3.