A - Sort Left and Right

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。

以下の操作を 0 回以上行うことで、 i=1,2,\dots,N に対し P_i=i が成り立つようにしたいです。

  • 1 \leq k \leq N を満たす整数 k を選ぶ。 まず 2 \leq k ならば P1 項目から k-1 項目を昇順に並び替える。その後、k \leq N-1 ならば Pk+1 項目から N 項目を昇順に並び替える。

この問題の制約の下ではどのような P に対しても有限回の操作で i=1,2,\dots,N に対し P_i=i が成り立つようにできることが証明できます。必要な最小の操作回数を求めてください。

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

制約

  • 1 \leq T \leq 10^5
  • 3 \leq N \leq 2 \times 10^5
  • P(1,2,\dots,N) の順列
  • 入力される値はすべて整数
  • 1 つの入力に含まれるテストケースについて、 N の総和は 2 \times 10^5 以下

入力

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

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

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

N
P_1 P_2 \dots P_N

出力

T 行出力せよ。i 行目には i 番目のテストケースについて答えを出力せよ。


入力例 1

3
5
2 1 3 5 4
3
1 2 3
7
3 2 1 7 5 6 4

出力例 1

1
0
2

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

  • k=1 として操作を行うと、 P(2,1,3,4,5) になります。

  • k=2 として操作を行うと、 P(2,1,3,4,5) になります。

  • k=3 として操作を行うと、 P(1,2,3,4,5) になります。

  • k=4 として操作を行うと、 P(1,2,3,5,4) になります。

  • k=5 として操作を行うと、 P(1,2,3,5,4) になります。

特に k=3 として操作を行った場合、操作後 i=1,2,\dots,5 に対し P_i=i が成り立ちます。よって必要な最小の操作回数は 1 です。

3 番目のテストケースについて、 k=4 として操作を行った後、 k=3 として操作を行うと P(3,2,1,7,5,6,4)\rightarrow (1,2,3,7,4,5,6) \rightarrow (1,2,3,4,5,6,7) と変化します。

Score : 300 points

Problem Statement

You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).

You want to satisfy P_i=i for all i=1,2,\dots,N by performing the following operation zero or more times:

  • Choose an integer k such that 1 \leq k \leq N. If k \geq 2, sort the 1-st through (k-1)-th terms of P in ascending order. Then, if k \leq N-1, sort the (k+1)-th through N-th terms of P in ascending order.

It can be proved that under the constraints of this problem, it is possible to satisfy P_i=i for all i=1,2,\dots,N with a finite number of operations for any P. Find the minimum number of operations required.

You have T test cases to solve.

Constraints

  • 1 \leq T \leq 10^5
  • 3 \leq N \leq 2 \times 10^5
  • P is a permutation of (1,2,\dots,N).
  • All input values are integers.
  • The sum of N across the test cases in a single input is at most 2 \times 10^5.

Input

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

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

Each case is given in the following format:

N
P_1 P_2 \dots P_N

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
5
2 1 3 5 4
3
1 2 3
7
3 2 1 7 5 6 4

Sample Output 1

1
0
2

For the first test case,

  • Performing the operation with k=1 results in P becoming (2,1,3,4,5).

  • Performing the operation with k=2 results in P becoming (2,1,3,4,5).

  • Performing the operation with k=3 results in P becoming (1,2,3,4,5).

  • Performing the operation with k=4 results in P becoming (1,2,3,5,4).

  • Performing the operation with k=5 results in P becoming (1,2,3,5,4).

Specifically, performing the operation with k=3 results in P satisfying P_i=i for all i=1,2,\dots,5. Therefore, the minimum number of operations required is 1.

For the third test case, performing the operation with k=4 followed by k=3 results in P changing as (3,2,1,7,5,6,4) \rightarrow (1,2,3,7,4,5,6) \rightarrow (1,2,3,4,5,6,7).

B - Annoying String Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

英小文字からなる文字列 S,T および 0, 1 からなる文字列 X に対し、英小文字からなる文字列 f(S,T,X) を以下のように定めます。

  • 空文字列に対し、 i=1,2,\dots,|X| の順に、 Xi 文字目が 0 なら S を、 1 なら T を末尾に結合することで得られる文字列

英小文字からなる文字列 S および 0, 1 からなる文字列 X,Y が与えられます。

英小文字からなる文字列 T (空文字列でもよい)であって、 f(S,T,X)=f(S,T,Y) が成り立つようなものが存在するか判定してください。

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

制約

  • 1 \leq t \leq 5 \times 10^5
  • 1 \leq |S| \leq 5\times 10^5
  • 1 \leq |X|,|Y| \leq 5\times 10^5
  • S は英小文字からなる文字列
  • X,Y0, 1 からなる文字列
  • 1 つの入力に含まれるテストケースについて、 |S| の総和は 5 \times 10^5 以下
  • 1 つの入力に含まれるテストケースについて、 |X| の総和は 5 \times 10^5 以下
  • 1 つの入力に含まれるテストケースについて、 |Y| の総和は 5 \times 10^5 以下

入力

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

t
\mathrm{case}_1
\vdots
\mathrm{case}_t

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

S
X
Y

出力

t 行出力せよ。 i 行目には i 番目のテストケースについて、条件を満たす T が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

3
araara
01
111
araaaa
100100
0010111
abacabac
0
1111

出力例 1

Yes
No
No

以下、文字列の結合を + を用いて表します。

1 番目のテストケースについて、 T=ara とすると f(S,T,X)=S+T=araaraara , f(S,T,Y)=T+T+T=araaraara となるため、 f(S,T,X)=f(S,T,Y) が成り立ちます。

2,3 番目のテストケースについて、条件を満たす T は存在しません。


入力例 2

2
empty
10101
00
empty
11111
111

出力例 2

Yes
Yes

T は空文字列であっても構いません。

Score : 600 points

Problem Statement

For strings S and T consisting of lowercase English letters, and a string X consisting of 0 and 1, define the string f(S,T,X) consisting of lowercase English letters as follows:

  • Starting with an empty string, for each i=1,2,\dots,|X|, append S to the end if the i-th character of X is 0, and append T to the end if it is 1.

You are given a string S consisting of lowercase English letters, and strings X and Y consisting of 0 and 1.

Determine if there exists a string T (which can be empty) such that f(S,T,X)=f(S,T,Y).

You have t test cases to solve.

Constraints

  • 1 \leq t \leq 5 \times 10^5
  • 1 \leq |S| \leq 5\times 10^5
  • 1 \leq |X|,|Y| \leq 5\times 10^5
  • S is a string consisting of lowercase English letters.
  • X and Y are strings consisting of 0 and 1.
  • The sum of |S| across all test cases in a single input is at most 5 \times 10^5.
  • The sum of |X| across all test cases in a single input is at most 5 \times 10^5.
  • The sum of |Y| across all test cases in a single input is at most 5 \times 10^5.

Input

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

t
\mathrm{case}_1
\vdots
\mathrm{case}_t

Each case is given in the following format:

S
X
Y

Output

Print t lines. The i-th line should contain Yes if there exists a T that satisfies the condition for the i-th test case, and No otherwise.


Sample Input 1

3
araara
01
111
araaaa
100100
0010111
abacabac
0
1111

Sample Output 1

Yes
No
No

Below, string concatenation is represented using +.

For the 1st test case, if T=ara, then f(S,T,X)=S+T=araaraara and f(S,T,Y)=T+T+T=araaraara, so f(S,T,X)=f(S,T,Y).

For the 2nd and 3rd test cases, there is no T that satisfies the condition.


Sample Input 2

2
empty
10101
00
empty
11111
111

Sample Output 2

Yes
Yes

T can be empty.

C - Row and Column Order

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N), Q=(Q_1,Q_2,\dots,Q_N) が与えられます。

NN 列からなるマス目の各マスに文字 0, 1 のいずれかを書き込み、以下の条件がすべて成り立つようにしてください。

  • i 行目のマスに書かれている文字を、 1,2,\dots,N 列目の順につなげて得られる文字列を S_i としたとき、辞書順で S_{P_1} < S_{P_2} < \dots < S_{P_N} が成り立つ
  • i 列目のマスに書かれている文字を、 1,2,\dots,N 行目の順につなげて得られる文字列を T_i としたとき、辞書順で T_{Q_1} < T_{Q_2} < \dots < T_{Q_N} が成り立つ

なお、どのような P,Q に対しても、条件をすべて満たす書き込み方が 1 つ以上あることが証明できます。

辞書順で X < Y が成り立つとは?

文字列 X=X_1X_2\dots X_{|X|}Y = Y_1Y_2\dots Y_{|Y|} について、辞書順で X < Y が成り立つとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|X|, |Y| はそれぞれ X, Y の長さを表します。

  1. |X| \lt |Y| かつ X_1X_2\ldots X_{|X|} = Y_1Y_2\ldots Y_{|X|}
  2. ある整数 1 \leq i \leq \min\lbrace |X|, |Y| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • X_1X_2\ldots X_{i-1} = Y_1Y_2\ldots Y_{i-1}
    • X_iY_i より小さい。

制約

  • 2 \leq N \leq 500
  • P,Q(1,2,\dots,N) の順列
  • 入力はすべて整数

入力

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

N
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

出力

ij 列 に書き込む文字を A_{ij} として、条件を満たす書き込み方を以下の形式で出力せよ。

A_{11}A_{12}\dots A_{1N}
\vdots
A_{N1}A_{N2}\dots A_{NN}

条件を満たす書き込み方が複数存在する場合は、いずれを出力しても正解となる。


入力例 1

3
1 2 3
2 1 3

出力例 1

001
101
110

この入出力例の場合、 S_1=001 , S_2=101 , S_3=110 であり、 T_1=011 , T_2=001 , T_3=110 です。よって S_1 < S_2 < S_3 かつ T_2 < T_1 < T_3 が成り立ち、条件を満たします。


入力例 2

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

出力例 2

010001111110101
001000000101001
010001001100010
010000011110010
010011101101101
100101110100000
111100011001000
000001001100000
100011011000101
000111101011110
101010101010101
011010101011110
010011000010011
100110010110101
000101101100100

Score : 600 points

Problem Statement

You are given two permutations P=(P_1,P_2,\dots,P_N) and Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N).

Write one of the characters 0 and 1 in each cell of an N-by-N grid so that all of the following conditions are satisfied:

  • Let S_i be the string obtained by concatenating the characters in the i-th row from the 1-st to the N-th column. Then, S_{P_1} < S_{P_2} < \dots < S_{P_N} in lexicographical order.
  • Let T_i be the string obtained by concatenating the characters in the i-th column from the 1-st to the N-th row. Then, T_{Q_1} < T_{Q_2} < \dots < T_{Q_N} in lexicographical order.

It can be proved that for any P and Q, there is at least one way to write the characters that satisfies all the conditions.

What does "X < Y in lexicographical order" mean?

For strings X=X_1X_2\dots X_{|X|} and Y = Y_1Y_2\dots Y_{|Y|}, "X < Y in lexicographical order" means that 1. or 2. below holds. Here, |X| and |Y| denote the lengths of X and Y, respectively.

  1. |X| \lt |Y| and X_1X_2\ldots X_{|X|} = Y_1Y_2\ldots Y_{|X|}.
  2. There exists an integer 1 \leq i \leq \min\lbrace |X|, |Y| \rbrace such that both of the following are true:
    • X_1X_2\ldots X_{i-1} = Y_1Y_2\ldots Y_{i-1}
    • X_i is less than Y_i.

Constraints

  • 2 \leq N \leq 500
  • P and Q are permutations of (1,2,\dots,N).
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

Output

Print a way to fill the grid that satisfies the conditions in the following format, where A_{ij} is the character written at the i-th row and j-th column:

A_{11}A_{12}\dots A_{1N}
\vdots
A_{N1}A_{N2}\dots A_{NN}

If there are multiple ways to satisfy the conditions, any of them will be accepted.


Sample Input 1

3
1 2 3
2 1 3

Sample Output 1

001
101
110

In this sample, S_1=001, S_2=101, S_3=110, and T_1=011, T_2=001, T_3=110. Therefore, S_1 < S_2 < S_3 and T_2 < T_1 < T_3 hold, satisfying the conditions.


Sample Input 2

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

Sample Output 2

010001111110101
001000000101001
010001001100010
010000011110010
010011101101101
100101110100000
111100011001000
000001001100000
100011011000101
000111101011110
101010101010101
011010101011110
010011000010011
100110010110101
000101101100100
D - Prefix Bubble Sort

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。

この順列に対して以下のような操作 k\ (k=2,3,\dots,N) を考えます。

  • 操作 k : i=1,2,\dots,k-1 の順に「 P_i > P_{i+1} ならば Pi,i+1 項目の値を入れ替える」を行う。

長さ M広義単調増加数列 A=(A_1,A_2\dots,A_M)\ (2 \leq A_i \leq N) が与えられます。

i=1,2,\dots,M について、 P に対し操作 A_1,A_2,\dots,A_i をこの順に適用した後の P の転倒数を求めてください。

数列の転倒数とは 長さ n の数列 x=(x_1,x_2,\dots,x_n) の転倒数とは、 整数の組 (i,j)\ (1\leq i < j \leq n) であって、 x_i > x_j を満たすものの個数です。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq A_i \leq N
  • P(1,2,\dots,N) の順列
  • i=1,2,\dots,M-1 に対して A_i \leq A_{i+1} が成り立つ
  • 入力される値はすべて整数

入力

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

N
P_1 P_2 \dots P_N
M
A_1 A_2 \dots A_M

出力

M 行出力してください。 k 行目には i=k に対する問題の答えを出力してください。


入力例 1

6
3 2 4 1 6 5
2
4 6

出力例 1

3
1

まず最初に操作 4 が行われます。操作 4 の過程で P(3,2,4,1,6,5)\rightarrow (2,3,4,1,6,5)\rightarrow (2,3,4,1,6,5) \rightarrow (2,3,1,4,6,5) と変化します。操作 4 が行われた後の P の転倒数は 3 です。

続けて操作 6 が行われると P は最終的に (2,1,3,4,5,6) に変化し、転倒数は 1 になります。


入力例 2

20
12 14 16 8 7 15 19 6 18 5 13 9 10 17 4 1 11 20 2 3
15
3 4 6 8 8 9 10 12 13 15 18 18 19 19 20

出力例 2

117
116
113
110
108
105
103
99
94
87
79
72
65
58
51

Score : 700 points

Problem Statement

You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).

Consider the following operations k\ (k=2,3,\dots,N) on this permutation.

  • Operation k: For i=1,2,\dots,k-1 in this order, if P_i > P_{i+1}, swap the values of the i-th and (i+1)-th elements of P.

You are also given a non-decreasing sequence A=(A_1,A_2,\dots,A_M)\ (2 \leq A_i \leq N) of length M.

For each i=1,2,\dots,M, find the inversion number of P after applying the operations A_1, A_2, \dots, A_i in this order.

What is the inversion number of a sequence? The inversion number of a sequence x=(x_1,x_2,\dots,x_n) of length n is the number of pairs of integers (i,j)\ (1\leq i < j \leq n) such that x_i > x_j.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq A_i \leq N
  • P is a permutation of (1,2,\dots,N).
  • A_i \leq A_{i+1} for i=1,2,\dots,M-1.
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N
M
A_1 A_2 \dots A_M

Output

Print M lines. The k-th line should contain the answer to the problem for i=k.


Sample Input 1

6
3 2 4 1 6 5
2
4 6

Sample Output 1

3
1

First, operation 4 is performed. During this, P changes as follows: (3,2,4,1,6,5) \rightarrow (2,3,4,1,6,5) \rightarrow (2,3,4,1,6,5) \rightarrow (2,3,1,4,6,5). The inversion number of P afterward is 3.

Next, operation 6 is performed, where P eventually becomes (2,1,3,4,5,6), whose inversion number is 1.


Sample Input 2

20
12 14 16 8 7 15 19 6 18 5 13 9 10 17 4 1 11 20 2 3
15
3 4 6 8 8 9 10 12 13 15 18 18 19 19 20

Sample Output 2

117
116
113
110
108
105
103
99
94
87
79
72
65
58
51
E - Min and Max at the edge

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

各頂点に番号が付いた無向グラフのうち、以下の条件を満たす全域木 T が取れるグラフを良いグラフといいます。なお、 2 頂点 u,v\ (u < v) を結ぶ辺を辺 (u,v) と表記します。

  • グラフのすべての辺 (u,v)\ (u < v) に対し、 T 上で頂点 u,v を結ぶただ 1 つの単純パスについて、単純パス上の頂点の番号の最小値、最大値はそれぞれ u,v である。

1 から N までの番号が付いた N 頂点からなる単純連結無向グラフ G があります。グラフ G には辺が M 本あり、 i 番目の辺は頂点 A_i,B_i\ (A_i < B_i) を結びます。

i=1,2,\dots,M に対し、 G から i 番目の辺を取り除いて得られるグラフが良いグラフであるか判定してください。

制約

  • 2\leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i < B_i \leq N
  • 入力される値はすべて整数
  • 与えられるグラフは単純連結無向グラフ

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

M 行出力せよ。i 行目には G から i 番目の辺を取り除いて得られるグラフ良いグラフである場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

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

出力例 1

No
No
No
No
Yes
No
No
Yes
Yes

(4,6) が削除された場合について、辺 (1,3),(2,5),(3,4),(3,5),(5,6) からなる全域木を考えてみます。例えば辺 (3,6) に対し、頂点 3,6 を結ぶ単純パスは頂点 3,5,6 をこの順にたどるパスであり、単純パス上の頂点の番号の最小値、最大値はそれぞれ 3,6 であるため条件を満たします。その他の辺についても同じように確認すると、この全域木は条件を満たしていることが分かります。よって答えは Yes となります。

一方辺 (1,5) が削除された場合について、同じ全域木が条件を満たすか考えてみます。辺 (4,6) に対し、頂点 4,6 を結ぶ単純パスは頂点 4,3,5,6 をこの順にたどるパスであり、単純パス上の頂点の番号の最小値、最大値はそれぞれ 3,6 であるため条件を満たしません。その他の全域木についても条件を満たさないことが示せるため、答えは No となります。


入力例 2

5 4
1 2
2 3
3 4
4 5

出力例 2

No
No
No
No

辺の除去によりグラフが連結でなくなることもあります。


入力例 3

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

出力例 3

No
No
No
Yes
Yes
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No

Score : 1000 points

Problem Statement

An undirected graph with numbered vertices is called a good graph if it has a spanning tree T that satisfies the following condition. Here, an edge connecting two vertices u and v (u < v) is denoted as edge (u,v).

  • For every edge (u,v) (u < v) in the graph, the minimum and maximum vertex numbers on the unique simple path connecting vertices u and v in T are u and v, respectively.

You are given a simple connected undirected graph G with N vertices numbered from 1 to N. The graph G has M edges, and the i-th edge connects vertices A_i and B_i (A_i < B_i).

For each i=1,2,\dots,M, determine whether the graph obtained by removing the i-th edge from G is a good graph.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i < B_i \leq N
  • All input values are integers.
  • The given graph is a simple connected undirected graph.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Print M lines. The i-th line should contain Yes if the graph obtained by removing the i-th edge from G is a good graph, and No otherwise.


Sample Input 1

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

Sample Output 1

No
No
No
No
Yes
No
No
Yes
Yes

Consider the case where edge (4,6) is removed. A spanning tree formed by edges (1,3),(2,5),(3,4),(3,5),(5,6) satisfies the condition. For example, for edge (3,6), the simple path connecting vertices 3 and 6 traverses vertices 3,5,6 in this order, and the minimum and maximum vertex numbers on the path are 3 and 6, respectively, thus satisfying the condition. By verifying the other edges similarly, it can be seen that this spanning tree satisfies the condition, so the answer is Yes.

On the other hand, consider the case where edge (1,5) is removed. The same spanning tree does not satisfy the condition. For edge (4,6), the simple path connecting vertices 4 and 6 traverses vertices 4,3,5,6 in this order, and the minimum and maximum vertex numbers on the path are 3 and 6, respectively, thus not satisfying the condition. It can also be shown that no other spanning tree satisfies the condition, so the answer is No.


Sample Input 2

5 4
1 2
2 3
3 4
4 5

Sample Output 2

No
No
No
No

Removing an edge may disconnect the graph.


Sample Input 3

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

Sample Output 3

No
No
No
Yes
Yes
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
F - Colorful Reversi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1100

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) があります。この整数列 A に対しては以下のような操作を行うことができます。

  • A_l=A_r, A_{l+1}=A_{l+2}=\dots=A_{r-1}, A_{l+1}\neq A_l を満たす l,r\ (1\leq l < r \leq N) を選ぶ。 A_{l+1},A_{l+2},\dots,A_{r-1} をすべて A_l で置き換える。この操作には r-l-1 のコストがかかる。

A_l=A_r, A_{l+1}=A_{l+2}=\dots=A_{r-1}, A_{l+1}\neq A_l を満たすl,r\ (1\leq l < r \leq N) が存在しなくなるまで操作を行うとき、一連の操作にかかるコストの総和の最小値を求めてください。

制約

  • 3 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq N
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

7
1 2 3 2 3 2 1

出力例 1

7

例えば (l,r)=(3,5), (2,6), (1,7) の順に操作を行うと A(1,2,3,2,3,2,1)\rightarrow (1,2,3,3,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1) と変化し、上記のような l,r が存在しなくなります。このような一連の操作にかかるコストの総和は 1+3+5=9 です。

一方、 (l,r)=(2,4), (4,6), (1,7) の順に操作を行うと A(1,2,3,2,3,2,1)\rightarrow (1,2,2,2,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1) と変化し、このような一連の操作にかかるコストの総和は 1+1+5=7 となります。


入力例 2

5
1 2 3 4 5

出力例 2

0

入力例 3

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

出力例 3

44

Score : 1100 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N. On this sequence, the following operation can be performed:

  • Choose l and r (1\leq l < r \leq N) such that A_l=A_r, A_{l+1}=A_{l+2}=\dots=A_{r-1}, and A_{l+1}\neq A_l. Replace each of A_{l+1},A_{l+2},\dots,A_{r-1} with A_l. The cost of this operation is r-l-1.

You will repeat this operation until there is no l and r (1\leq l < r \leq N) such that A_l=A_r, A_{l+1}=A_{l+2}=\dots=A_{r-1}, and A_{l+1}\neq A_l. Find the minimum total cost of such a series of operations.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

7
1 2 3 2 3 2 1

Sample Output 1

7

For example, if you perform the operation with (l,r)=(3,5), (2,6), (1,7) in this order, A changes as follows: (1,2,3,2,3,2,1)\rightarrow (1,2,3,3,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1), after which there is no l and r with the said property. The total cost of this series of operations is 1+3+5=9.

On the other hand, if you perform the operation with (l,r)=(2,4), (4,6), (1,7) in this order, A changes as follows: (1,2,3,2,3,2,1)\rightarrow (1,2,2,2,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1). The total cost of this series of operations is 1+1+5=7.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

0

Sample Input 3

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

Sample Output 3

44