Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
A, B, C からなる空でない文字列 T に対して、以下の 2 種類の操作を好きな順で好きな回数だけ行い、空文字列とすることができる時、これを「よい文字列」と呼びます。
- 操作 1 :文字列に存在する同一の文字を 2 つ選び、削除する(同一の文字が 2 つ以上ない場合は行えない)
- 操作 2 :文字列に存在する A,B,Cを 1 つずつ選び、削除する(A,B,Cがそれぞれ 1 つ以上ない場合は行えない)
例えば、ABACA は、次のように操作を行うことで空文字列にできるため、よい文字列です。
- 2,4,5 文字目を選んで削除する(操作 2)。文字列は AAになる。
- 1,2 文字目を選んで削除する(操作 1)。文字列は空文字列になる。
A, B, C, ? からなる長さ N の文字列 S が与えられます。? をそれぞれ A, B, C のいずれかに置き換えてできる文字列であって、よい文字列を連続する部分文字列として K 個以上含むものはいくつあるでしょうか。ただし、同じ文字列であるような部分文字列であっても、元の文字列内での位置が異なる場合は別々に数えます。
答えを 998244353 で割った余りを求めてください。
制約
- 1 \leq N \leq 50
- 0 \leq K \leq N(N+1)/2
- N,K は整数である
- |S|=N
- S は A,B,C,?からなる文字列である
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを 998244353 で割った余りを整数で出力せよ。
入力例 1
4 2 A?AB
出力例 1
1
? を A, B, C に置き換えてできる文字列は AAAB, ABAB, ACAB の 3 つあります。
このうち AAAB は 1,2 文字目の AA および 2,3 文字目の AA がよい文字列であるため、連続する部分文字列としてよい文字列を 2 個含みます。ここで、同じ文字列であるような部分文字列であっても、元の文字列内での位置が異なる場合は別々に数えることに注意してください。
一方、ABAB が含むよい文字列は ABAB の 1 個のみです。また、ACAB も CAB の 1 個しかよい文字列を含みません。
入力例 2
50 411 ??AB??C???????????????????????????????A???C????A??
出力例 2
457279314
答えを 998244353 で割った余りを出力してください。
入力例 3
1 0 A
出力例 3
1
Score : 500 points
Problem Statement
For a non-empty string T consisting of A, B, and C, we call it a good string if it can be turned into an empty string by performing the following two types of operations any number of times in any order.
- Operation 1: Choose two identical characters in the string and delete them (cannot be performed if there are not two or more identical characters).
- Operation 2: Choose one A, oneB, and oneCin the string and delete them (cannot be performed if there are not one or more of each ofA,B, andC).
For example, ABACA is a good string because it can be turned into an empty string by performing the operations as follows:
- Choose the 2nd, 4th, and 5th characters and delete them (Operation 2). The string becomes AA.
- Choose the 1st and 2nd characters and delete them (Operation 1). The string becomes an empty string.
You are given a string S of length N consisting of A, B, C, and ?. How many ways are there to replace each ? with A, B, or C to form a string that contains at least K good strings as contiguous substrings? Substrings are counted separately if they are at different positions in the original string, even if they are identical strings.
Find the count modulo 998244353.
Constraints
- 1 \leq N \leq 50
- 0 \leq K \leq \frac{N(N+1)}{2}
- N and K are integers.
- |S| = N
- S is a string consisting of A,B,C, and?.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer modulo 998244353.
Sample Input 1
4 2 A?AB
Sample Output 1
1
By replacing ? with A, B, or C, we can obtain the following three strings: AAAB, ABAB, ACAB.
Among these, AAAB contains two good substrings: the AA at positions 1,2 and the AA at positions 2,3. Note that even if the substrings are identical as strings, they are counted separately if they are at different positions in the original string.
On the other hand, ABAB contains only one good substring ABAB. Also, ACAB contains only one good substring CAB.
Sample Input 2
50 411 ??AB??C???????????????????????????????A???C????A??
Sample Output 2
457279314
Print the count modulo 998244353.
Sample Input 3
1 0 A
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
円周上を N 等分する位置に、点 0,1, \ldots , N-1 がこの順に並んでおり、Alice が点 0 に、Bob が点 K にいます。また、初め全ての点は白色に塗られています。両者は、Alice から始めて交互に次のような操作を行います。
- その時点で白色である点を 1 つ選び、黒色に塗る。ただし、操作後に、操作者と円の中心を結ぶ直線に対して、各点の色が線対称でなければいけない。
操作者が上記の条件を満たす操作ができなければ、そこで一連の操作を打ち切ります。
両者とも、最終的に最も多くの点を黒く塗ることができるように協力して最善の選択をします。一連の操作が全て終了したときに全ての点を黒く塗ることができているかどうかを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えてください。
制約
- 1\leq T\leq 10^5
- 2\leq N\leq 2\times 10^5
- 1\leq K\leq N-1
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots 
\mathrm{case}_T
各テストケース \mathrm{case}_i (1\leq i \leq T) は以下の形式である。
N K
出力
T 行出力せよ。i 行目には、i 番目のテストケースについて、全ての点を黒く塗ることができるなら Yes 、できないなら No と出力せよ。
入力例 1
4 6 2 6 3 6 1 200000 100000
出力例 1
Yes No Yes No
N=6, K=2 の場合、例えば以下のような順で操作を行うことで全ての点を黒く塗ることができます。
- Alice が点 3 を黒く塗る。
- Bob が点 1 を黒く塗る。
- Alice が点 5 を黒く塗る。
- Bob が点 2 を黒く塗る。
- Alice が点 4 を黒く塗る。
- Bob が点 0 を黒く塗る。

N=6, K=3 の場合、例えば以下のような進行が考えられます。実は、どのようにしても全ての点を黒く塗ることはできません。
- Alice が点 3 を黒く塗る。
- Bob が点 0 を黒く塗る。
- Alice はどの点を黒く塗っても自身から見て線対称にできないため、操作を行えない。
Score : 600 points
Problem Statement
On a circle, there are N equally spaced points numbered 0,1,\ldots,N-1 in this order, with Alice at point 0 and Bob at point K. Initially, all points are colored white. Starting with Alice, they alternately perform the following operation:
- Choose one of the currently white points and color it black. Here, after the operation, the coloring of the points must be symmetric with respect to the straight line connecting the operator and the center of the circle.
If the operator cannot perform an operation satisfying the above condition, the sequence of operations ends there.
Both players cooperate and make the best choices to maximize the total number of points colored black in the end. Determine whether all points are colored black at the end of the sequence of operations.
You are given T test cases to solve.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N-1
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots 
\mathrm{case}_T
Each test case \mathrm{case}_i (1 \leq i \leq T) is in the following format:
N K
Output
Print T lines. The i-th line should contain Yes if all points can be colored black for the i-th test case, and No otherwise.
Sample Input 1
4 6 2 6 3 6 1 200000 100000
Sample Output 1
Yes No Yes No
For N=6 and K=2, all points can be colored black by, for example, performing operations in the following order:
- Alice colors point 3 black.
- Bob colors point 1 black.
- Alice colors point 5 black.
- Bob colors point 2 black.
- Alice colors point 4 black.
- Bob colors point 0 black.

For N=6 and K=3, below is one possible progression. Actually, no matter what they do, they cannot color all points black.
- Alice colors point 3 black.
- Bob colors point 0 black.
- Alice cannot color any point black so that the coloring will be symmetric with respect to her line, so she cannot perform the operation.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
1 から N までの番号がついた N 人の村人が住む村があります。 各村人は、正直者であるか嘘つきであるかのどちらかです。また、村人のうち何人かは混乱しています。
あなたは、村人による証言を M 個手に入れました。この証言は、i=1,2, \ldots ,M に対して A_i, B_i, C_i で与えられ、
- C_i=0 であれば、村人 A_i が村人 B_i を正直者であると証言したこと
- C_i=1 であれば、村人 A_i が村人 B_i を嘘つきであると証言したこと
を表します。
全ての村人は、他の全ての村人について正直者と嘘つきのどちらであるかを知っており、あなたに対して次のような規則で証言を行ったことが分かっています。
- 混乱していない正直者は必ず正しい証言をする。
- 混乱していない嘘つきは必ず嘘の証言をする。
- 混乱している正直者は必ず嘘の証言をする。
- 混乱している嘘つきは必ず正しい証言をする。
すなわち、混乱していなければ正直者は正しい証言を、嘘つきは嘘の証言をしますが、混乱していると逆になります。
あなたは混乱している村人の組を予想することにしました。 混乱している村人の組を決めると、与えられた M 個の証言からなる証言の組が「矛盾する」かどうかが定まります。 ここで、証言の組が「矛盾する」というのは、各村人が正直者であるか嘘つきであるかをどのように決めても、証言の組の中に、村人が行う証言の規則に反するものが存在することを意味します。
与えられた証言の組が矛盾しないような混乱している村人の組を 1 つ見つけてください。 ただし、どのような村人の組が混乱しているとしても与えられた証言の組が矛盾する場合は、そのことを指摘してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \mathrm{min} \lbrace 2 \times 10^5,N(N-1) \rbrace
- 1 \leq A_i, B_i \leq N, A_i \neq B_i
- i\neq j のとき A_i\neq A_j または B_i\neq B_j
- C_i=0 または C_i=1
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
与えられた証言の組が矛盾しないような混乱している村人の組が存在するとき、混乱している村人の組を表す長さ N の文字列を出力せよ。このとき出力される文字列では、村人 i が混乱している場合 i 文字目が 1、そうでない場合 i 文字目が 0 であるようにせよ。
そのような混乱している村人の組が存在しないとき、-1 と出力せよ。
入力例 1
3 3 1 2 1 1 3 0 2 3 0
出力例 1
010
村人 1 が混乱していない正直者、村人 2 が混乱している嘘つき、村人 3 が混乱していない正直者であると仮定します。
このとき、村人 1 は、村人 2 が嘘つきである、村人 3 が正直者であると正しい証言をします。 また、村人 2 は嘘つきですが混乱しているため、村人 3 が正直者であると正しい証言をします。
したがって、与えられた証言が全て村人の証言の規則通りに得られるため、村人 2 のみ混乱していることを表す 010 は正当な出力の 1 つです。
入力例 2
3 6 1 2 1 1 3 0 2 1 1 2 3 0 3 1 1 3 2 0
出力例 2
-1
村人 2,3 が混乱していると仮定してみます。
このとき、各村人が正直者であるかどうかについて 2^3=8 通りの組み合わせがあります。 このうち、例えば、「村人 1 が(混乱していない)正直者、村人 2 が(混乱している)嘘つき、村人 3 が(混乱している)正直者」であるとすると、村人 2 は規則によれば正しい証言をするはずですが、村人 1 を嘘つきであると嘘の証言をしています。 他の組み合わせに対しても、同様に規則に反する証言が生じることを確認できます。
したがって、村人 2,3 が混乱しているとすると、与えられた証言の組は矛盾します。
実は、このケースでは、どのような村人の組が混乱しているとしても与えられた証言の組は矛盾します。
入力例 3
3 0
出力例 3
000
混乱している人数は任意であり、題意の条件が満たされるならば 0 人や全員でもよいです。
Score : 700 points
Problem Statement
There is a village with N villagers numbered from 1 to N. Each villager is honest or a liar. Additionally, some villagers are confused.
You have obtained M testimonies from the villagers. Each testimony is given by A_i, B_i, C_i for i=1,2,\ldots,M, representing:
- If C_i=0, villager A_i testified that villager B_i is honest.
- If C_i=1, villager A_i testified that villager B_i is a liar.
All villagers know whether every other villager is honest or a liar, and you know that they made their testimonies to you according to the following rules:
- An honest villager who is not confused always tells the truth.
- A liar who is not confused always tells lies.
- A confused honest villager always tells lies.
- A confused liar always tells the truth.
In other words, if they are not confused, honest villagers always tell the truth, and liars always tell lies, but if they are confused, it is reversed.
You have decided to guess the set of villagers who are confused. Given a choice of villagers who are confused, whether the set of testimonies "contradicts" or not is determined. Here, a set of testimonies is said to contradict if, no matter how you assign honest or liar statuses to the villagers, there is at least one testimony that violates the villagers' testimony rules.
Find a set of confused villagers such that the given set of testimonies does not contradict. If no such set of confused villagers exists, indicate that fact.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \mathrm{min} \lbrace 2 \times 10^5,N(N-1) \rbrace
- 1 \leq A_i, B_i \leq N, A_i \neq B_i
- A_i \neq A_j or B_i \neq B_j for i \neq j.
- C_i = 0 or 1.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
If there exists a set of confused villagers such that the given set of testimonies does not contradict, print a string of length N representing the set of confused villagers. In this string, the i-th character should be 1 if villager i is confused, and 0 otherwise.
If no such set of confused villagers exists, print -1.
Sample Input 1
3 3 1 2 1 1 3 0 2 3 0
Sample Output 1
010
Suppose villager 1 is an honest villager who is not confused, villager 2 is a confused liar, and villager 3 is an honest villager who is not confused.
In this case, villager 1 correctly testifies that villager 2 is a liar and villager 3 is honest. Also, villager 2, who is a liar but confused, tells the truth and testifies that villager 3 is honest.
Therefore, all given testimonies are consistent with the villagers' testimony rules, so 010, indicating that only villager 2 is confused, is one valid output.
Sample Input 2
3 6 1 2 1 1 3 0 2 1 1 2 3 0 3 1 1 3 2 0
Sample Output 2
-1
Suppose villagers 2 and 3 are confused.
In this case, there are 2^3=8 possible combinations for whether each villager is honest or a liar. Among them, for example, if villager 1 is an honest villager who is not confused, villager 2 is a confused liar, and villager 3 is a confused honest villager, then according to the rules, villager 2 should tell the truth, but they falsely testify that villager 1 is a liar.
You can confirm that also in other combinations, there will be some testimonies that violate the rules.
Therefore, if villagers 2 and 3 are confused, the given set of testimonies contradicts.
In fact, in this test case, no matter which villagers are confused, the given set of testimonies contradicts.
Sample Input 3
3 0
Sample Output 3
000
There may be any number of confused villagers, possibly zero or all.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1000 点
問題文
あなたは、以下の条件を満たすように、長さ 3 の数列を N 個作ります。
- k=1,2,3 の全てに対して、次の条件が成り立つ- 各数列の k 項目を集めたとき、1 から N までの整数がちょうど 1 回ずつ現れる
 
この数列の列に対して、以下のように数列 a=(a_1,a_2, \ldots ,a_N), b=(b_1,b_2,\ldots ,b_N) を定義します。
- i 番目の数列を s_i 、i 番目の数列を逆順にしたものを t_i とし、これらを全て辞書順に並べた時、s_i が a_i 番目、t_i が b_i 番目である
- ただし、2N 個の数列の中に同一の数列が 2 個以上存在する場合には、a, b は定義されない
したがって、a, b が定義される場合には、a と b を合わせた数列には 1 から 2N までの整数がちょうど 1 回ずつ現れます。
長さ N の数列 A,B が与えられます。ただし、A の各項は 1 以上 2N 以下の整数であり、B の各項は 1 以上 2N 以下の整数または -1 です。 また、A と B を合わせた数列には -1 以外の整数は 1 回以下しか現れません。
a, b が定義され、1 以上 N 以下のすべての整数 i に対して
- a_i = A_i
- B_i \neq -1 ならば b_i = B_i
がともに成り立つとき、あり得る数列 a,b の組は何通りあるでしょうか。 答えを 998244353 で割った余りを求めてください。
制約
- 2\leq N\leq 3000
- 1\leq A_i\leq 2N
- 1\leq B_i\leq 2N または B_i=-1
- A と B を合わせた数列には -1 以外の整数は 1 回以下しか現れない。つまり、以下が成り立つ- i\neq j のとき A_i\neq A_j
- i\neq j かつ B_i,B_j\neq -1 のとき B_i\neq B_j
- A_i\neq B_j
 
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
答えを 998244353 で割った余りを整数で出力せよ。
入力例 1
3 2 3 6 -1 1 -1
出力例 1
1
例えば、3 つの数列を以下のように作った場合を考えます。
- (1,2,3)
- (2,1,1)
- (3,3,2)
このとき、s_i および t_i を辞書順に並べると、
t_2=(1,1,2)<s_1=(1,2,3)<s_2=(2,1,1)<t_3=(2,3,3)<t_1=(3,2,1)<s_3=(3,3,2)
となっているため、(a_1,a_2,a_3,b_1,b_2,b_3)=(2,3,6,5,1,4) です。このとき、a は与えられた A に一致し、b の第 2 項も B と一致しており、これは題意を満たす数列 a,b の 1 つです。
また、3 つの数列を以下のように作った場合は、s_1=t_1 となってしまうため a,b が定義されません。
- (1,2,1)
- (2,1,3)
- (3,3,2)
実は、題意を満たす数列は a=(2,3,6), b=(5,1,4) のみです。
入力例 2
15 5 16 1 12 30 20 4 13 9 8 24 21 26 28 17 -1 -1 6 -1 -1 -1 -1 -1 -1 -1 -1 29 -1 -1 -1
出力例 2
758094847
答えを 998244353 で割った余りを出力してください。
Score : 1000 points
Problem Statement
You are going to create N sequences of length 3, satisfying the following conditions.
- For each of k = 1,2,3, the following holds:- Among the k-th elements of the sequences, each integer from 1 through N appears exactly once.
 
For this sequence of sequences, define sequences a=(a_1,a_2,\ldots,a_N) and b=(b_1,b_2,\ldots,b_N) as follows.
- Let s_i be the i-th sequence, and let t_i be the reverse of the i-th sequence. When all of these are sorted in lexicographical order, s_i comes a_i-th, and t_i comes b_i-th.
- Here, if there are identical sequences among the 2N sequences, a and b are not defined.
Therefore, if a and b are defined, each integer from 1 through 2N appears exactly once in the concatenation of a and b.
You are given sequences A and B of length N, where each element of A is an integer between 1 and 2N, and each element of B is either an integer between 1 and 2N or -1. Also, in the concatenation of A and B, each integer other than -1 appears at most once.
How many pairs of sequences a,b are there such that a and b are defined and the following holds for each integer i from 1 through N?
- a_i = A_i.
- b_i = B_i if B_i \neq -1.
Find the count modulo 998244353.
Constraints
- 2 \leq N \leq 3000
- 1 \leq A_i \leq 2N
- 1 \leq B_i \leq 2N or B_i = -1.
- In the concatenation of A and B, each integer other than -1 appears at most once. That is,- A_i \neq A_j if i \neq j.
- B_i \neq B_j if i \neq j and B_i,B_j \neq -1.
- A_i \neq B_j.
 
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the count modulo 998244353.
Sample Input 1
3 2 3 6 -1 1 -1
Sample Output 1
1
For example, consider creating the following three sequences:
- (1,2,3)
- (2,1,1)
- (3,3,2)
In this case, when sorting s_i and t_i lexicographically, we have:
t_2 = (1,1,2) < s_1 = (1,2,3) < s_2 = (2,1,1) < t_3 = (2,3,3) < t_1 = (3,2,1) < s_3 = (3,3,2)
Thus, (a_1,a_2,a_3,b_1,b_2,b_3) = (2,3,6,5,1,4). Here, a matches the given A, and the second element of b also matches that of B, so this is one pair of sequences a,b satisfying the conditions.
On the other hand, if we create the following three sequences, s_1 and t_1 become identical, so a and b are not defined.
- (1,2,1)
- (2,1,3)
- (3,3,2)
In fact, a=(2,3,6), b=(5,1,4) is the only pair of sequences satisfying the conditions.
Sample Input 2
15 5 16 1 12 30 20 4 13 9 8 24 21 26 28 17 -1 -1 6 -1 -1 -1 -1 -1 -1 -1 -1 29 -1 -1 -1
Sample Output 2
758094847
Print the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1000 点
問題文
1 から 2N までの番号がついた 2N 個のマスが、マス 1 を上として上下一列に並んでいます。各マスには 1 つずつボールが入っており、時刻 t=0 において、マス i にあるボールの重さは、 i=1,2, \ldots ,N では m_i、 i=N+1,N+2, \ldots ,2N では 0 です。ただし、(m_1, m_2, \ldots , m_N) は 1 から N までの整数を並び替えた数列です。
以下では、重さ i のボールを「ボール i」、各ボールの入っているマスの番号を「ボールの位置」と呼ぶことにします。
時刻 t=0 以降、時刻が 1 進むごとに、重いボールが下に沈み、代わりに軽いボールが浮かび上がっていきます。
厳密には、以下の手順によって、時刻 t=t_0 における各ボールの位置から時刻 t=t_0+1 における各ボールの位置を定めます。
- まず、i=N,N-1,\ldots ,2,1 の順に、以下の操作を行う。
- ボール i の t=t_0+1 における位置がすでに定められている場合
- 何もしない。
- ボール i の t=t_0+1 における位置がまだ定められていない場合
- t=t_0 にボール i の 1 つ下のマスが存在し、このマスに入っているボール(ボール j とする)がボール i より軽いとき、ボール i と ボール j の t=t_0+1 における位置を、t=t_0 における両者の位置を交換したものとして定める。
- 上記にあてはまらないとき、ボール i の t=t_0+1 における位置を t=t_0 における位置と同じに定める。
- 続いて、ボール 0 のうちこの時点で t=t_0+1 における位置が定められていないものについて、それぞれ t=t_0+1 における位置を t=t_0 における位置と同じに定める。
このとき、ある時刻にボールが上から軽い順に並び、それ以降全てボールの位置が変化しなくなることが示せます。この状態に達する時刻を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq m_i \leq N
- i\neq j のとき m_i\neq m_j
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N m_1 m_2 \ldots m_N
出力
答えを整数で出力せよ。
入力例 1
3 2 3 1
出力例 1
6
時刻 t=0 から t=1 にかけての動きは次のように定まります。(必要に応じて、下の図も参考にしてください。)
- ボール 3 について、t=1 における位置はまだ定められていない。1 つ下のマスにはボール 1 があり、ボール 3 の方が重いため、t=1 には両者の位置を入れ替える。すなわち、ボール 3 の位置をマス 3、ボール 1 の位置をマス 2 に定める。
- ボール 2 について、t=1 における位置はまだ定められていない。1 つ下のマスにはボール 3 があり、これはボール 2 より重いため、ボール 2 の t=1 における位置を t=0 と同じに定める。
- ボール 1 について、t=1 における位置は先のステップで既に定められている。
- ボール 0 について、全て t=1 における位置はまだ定められていない。これらは全て t=1 での位置を t=0 と同じに定める。
続いて、時刻 t=1 から t=2 にかけての動きは次のように定まります。
- ボール 3 について、t=2 における位置はまだ定められていない。1 つ下のマスにはボール 0 があり、ボール 3 の方が重いため、t=2 には両者の位置を入れ替える。すなわち、ボール 3 の位置をマス 4、(ボール 3 の 1 つ下にあった)ボール 0 の位置をマス 3 に定める。
- ボール 2 について、t=2 における位置はまだ定められていない。1 つ下のマスにはボール 1 があり、ボール 2 の方が重いため、t=2 には両者の位置を入れ替える。すなわち、ボール 2 の位置をマス 2、ボール 1 の位置をマス 1 に定める。
- ボール 1 について、t=2 における位置は先のステップで既に定められている。
- ボール 0 について、t=1 にマス 4 にあったものの t=2 における位置は先のステップで既に定められている。それ以外について、t=2 での位置を t=1 と同じに定める。
この後も同様にボールの位置を定めていくと、時刻 t=6 に上から順にボール 0,0,0,1,2,3 が並び、以降ボールの位置が変化しないことが分かります。

入力例 2
5 4 1 2 3 5
出力例 2
9
入力例 3
1 1
出力例 3
1
Score : 1000 points
Problem Statement
There are 2N cells numbered from 1 to 2N arranged vertically in a column with cell 1 at the top. Each cell contains one ball. The weight of the ball in cell i at time t=0 is m_i for i=1,2,\ldots,N, and 0 for i=N+1,N+2,\ldots,2N. Here, (m_1, m_2, \ldots, m_N) is a permutation of the integers from 1 to N.
In the following, we will refer to the ball of weight i as ball i, and the cell number where each ball is located as the position of the ball.
From time t=0 onwards, every time the time advances by 1, the heavier balls sink downward, and the lighter balls rise upward.
Formally, the positions of each ball at time t=t_0+1 are determined from their positions at time t=t_0 by the following procedure.
- First, for i=N,N-1,\ldots,2,1 in this order, perform the following operation.
- If the position of ball i at t=t_0+1 has already been determined:
- Do nothing.
- If the position of ball i at t=t_0+1 has not been determined:
- If there exists a cell immediately below ball i at t=t_0, and the ball in that cell (call it ball j) is lighter than ball i, set the positions of balls i and j at t=t_0+1 to be swapped from their positions at t=t_0.
- Otherwise, set the position of ball i at t=t_0+1 to be the same as at t=t_0.
- Next, for all balls of weight 0 whose positions at t=t_0+1 have not been determined at this point, set their positions at t=t_0+1 to be the same as at t=t_0.
It can be shown that at some time, the balls will be arranged from top to bottom in ascending order of weight, and their positions will no longer change. Find the time when this state is reached.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq m_i \leq N
- m_i \neq m_j for i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N m_1 m_2 \ldots m_N
Output
Print the answer as an integer.
Sample Input 1
3 2 3 1
Sample Output 1
6
The movements from time t=0 to t=1 are determined as follows. (Refer to the diagram below if necessary.)
- For ball 3, its position at t=1 has not yet been determined. The cell immediately below it contains ball 1, and ball 3 is heavier, so swap their positions for t=1. That is, set the position of ball 3 to cell 3, and ball 1 to cell 2.
- For ball 2, its position at t=1 has not yet been determined. The cell immediately below it contains ball 3, which is heavier than ball 2, so set the position of ball 2 at t=1 to be the same as at t=0.
- For ball 1, its position at t=1 has already been determined in the earlier step.
- For balls of weight 0, none of their positions at t=1 have been determined. Set their positions at t=1 to be the same as at t=0.
Next, the movements from time t=1 to t=2 are determined as follows.
- For ball 3, its position at t=2 has not yet been determined. The cell immediately below it contains ball 0, and ball 3 is heavier, so swap their positions for t=2. That is, set the position of ball 3 to cell 4, and ball 0 (the one that was below ball 3) to cell 3.
- For ball 2, its position at t=2 has not yet been determined. The cell immediately below it contains ball 1, and ball 2 is heavier, so swap their positions for t=2. That is, set the position of ball 2 to cell 2, and ball 1 to cell 1.
- For ball 1, its position at t=2 has already been determined in the earlier step.
- For balls of weight 0, the one that was at cell 4 at t=1 has already had its position at t=2 determined in the earlier step. For the others, set their positions at t=2 to be the same as at t=1.
Continuing to determine the positions of the balls in this way, at time t=6, the balls will be arranged from top to bottom as balls 0,0,0,1,2,3, and their positions will no longer change.

Sample Input 2
5 4 1 2 3 5
Sample Output 2
9
Sample Input 3
1 1
Sample Output 3
1