

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