A - Robot Balance

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

高橋くんは、頭パーツを 1 個と体パーツを 1 個組み合わせてロボットを 1 体作ることができます。 ロボットは頭パーツの重さが体パーツの重さより大きいと倒れてしまいます。

現在、高橋くんは頭パーツと体パーツを 1 個ずつ持っています。 高橋くんが持っている頭パーツの重さは H グラム、体パーツの重さは B グラムです。

高橋くんは、体パーツを重くすることで、ロボットを倒れないようにしたいです。 高橋くんが作るロボットが倒れないようにするためには、体パーツをあと何グラム重くする必要があるか求めてください。

制約

  • 1\le H\le100
  • 1\le B\le100
  • 入力はすべて整数

入力

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

H B

出力

答えを出力せよ。


入力例 1

43 1

出力例 1

42

高橋くんは、体パーツを 42 グラム重くすることで、頭パーツと体パーツの重さを等しくすることができます。

体パーツを重くする量が 42 グラム未満のとき、高橋くんが作るロボットは倒れてしまうため、42 を出力してください。


入力例 2

4 31

出力例 2

0

高橋くんがこのままロボットを作ってもロボットは倒れないため、0 を出力してください。


入力例 3

1 1

出力例 3

0

Score : 100 points

Problem Statement

Takahashi can combine a head part and a body part to create a robot. A robot falls over if the weight of the head part is greater than the weight of the body part.

Currently, he has one head part and one body part. The weight of the head part is H grams, and the weight of the body part is B grams.

He wants to make the body part heavier so that the robot does not fall over. Find how many more grams the body part needs to be made heavier so that his robot does not fall over.

Constraints

  • 1\le H\le100
  • 1\le B\le100
  • All input values are integers.

Input

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

H B

Output

Print the answer.


Sample Input 1

43 1

Sample Output 1

42

By making the body part 42 grams heavier, Takahashi can make the weights of the head part and body part equal.

When the amount by which the body part is made heavier is less than 42 grams, his robot will fall over, so print 42.


Sample Input 2

4 31

Sample Output 2

0

Even if he creates the robot as is, the robot will not fall over, so print 0.


Sample Input 3

1 1

Sample Output 3

0
B - Scary Fee

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150

問題文

あなたはコワスギ銀行の通帳を持っています。コワスギ銀行の預金通帳には、引き出し額に応じて手数料が変わるという怖すぎる性質があります。

コワスギ銀行の通帳からお金を引き出すには、1\,000 円単位で引き出し額を指定したうえで、引き出し額 1\,000 円あたり C 円の手数料を残高から別途支払う必要があります。銀行の残高が 0 円未満になる引き出しを行うことはできません。

あなたが持っているコワスギ銀行の通帳の残高が X 円のとき、そこから最大で何円を引き出せますか?

制約

  • 1 \leq X \leq 10^7
  • 1 \leq C \leq 999
  • 入力される値はすべて整数

入力

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

X C

出力

答えを出力せよ。


入力例 1

650000 8

出力例 1

644000

644\,000 円を引き出すために必要な手数料は 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 円になるので、644\,000 + 5\,152 \leq 650\,000 より、644\,000 円は引き出すことができます。

一方で、645\,000 円引き出すために必要な手数料は 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 円になるので、645\,000 + 5\,160 > 650\,000 より、645\,000 円は引き出すことができません。


入力例 2

1003 4

出力例 2

0

まったくお金が引き出せないこともありえます。


入力例 3

10000000 24

出力例 3

9765000

Score : 150 points

Problem Statement

You have a bankbook from The Terrifying Bank. The deposit passbook of the bank has a terrifyingly scary property that the commission fee changes according to the withdrawal amount.

To withdraw money from the passbook, you need to specify the withdrawal amount in units of 1\,000 yen, and pay a commission fee of C yen per 1\,000 yen of withdrawal amount separately from the balance. Withdrawals are not allowed if they would leave the balance below 0 yen.

When the balance of your passbook from the bank is X yen, what is the maximum amount of money you can withdraw from it?

Constraints

  • 1 \leq X \leq 10^7
  • 1 \leq C \leq 999
  • All input values are integers.

Input

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

X C

Output

Output the answer.


Sample Input 1

650000 8

Sample Output 1

644000

The commission fee required to withdraw 644\,000 yen is 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 yen, so since 644\,000 + 5\,152 \leq 650\,000, you can withdraw 644\,000 yen.

On the other hand, the commission fee required to withdraw 645\,000 yen is 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 yen, so since 645\,000 + 5\,160 > 650\,000, you cannot withdraw 645\,000 yen.


Sample Input 2

1003 4

Sample Output 2

0

It is possible that no money can be withdrawn at all.


Sample Input 3

10000000 24

Sample Output 3

9765000
C - Base 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

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

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

制約

  • A_i0 または 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 - Misjudge the Time

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

高橋君は置き時計を買いました。
この時計は、現在の時刻が 24 時制で \mathrm{AB}\mathrm{CD} 分であるときに図 1 のように時刻を表します。
例えば図 2 では、時計は 758 分を示しています。

時刻の表示方法をより形式的に説明すると次のようになります。
現在の時刻が 24 時制で hm 分であるとします。ここで 24 時制とは、時間を 0 以上 23 以下の整数で、分を 0 以上 59 以下の整数で表す時刻の表現方法を言います。
h10 の位を A, 1 の位を B, m10 の位を C, 1 の位を D とします。(ただし h, m1 桁である場合は先行ゼロを追加して考えます。)
このとき時計は左上に A を、左下に B を、右上に C を、右下に D を表示します。

image

高橋君は、次の条件を満たす時刻を 見間違えやすい時刻 と呼ぶことにしました。

  • 時計の表示の右上と左下を入れ替えても、それに対応する 24 時制の時刻が存在する。

例えば 図 32013 分を示していますが、時計の表示の右上と左下を入れ替えると 213 分を意味する表示になります。よって 2013 分は見間違えやすい時刻です。

今、時計は HM 分を示しています。
(現時点も含めて)以降はじめて訪れる見間違えやすい時刻を 24 時制で答えてください。

制約

  • 0 \leq H \leq 23
  • 0 \leq M \leq 59
  • H, M は整数

入力

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

H M

出力

答えを hm 分とする。ここで h, m0 \leq h \leq 23, 0 \leq m \leq 59 である必要がある。
このとき h, m を以下の形式で出力せよ。

h m

なお、h, m2 桁に揃えるために先行ゼロをつけた形式で出力しても正答と見なされる。


入力例 1

1 23

出力例 1

1 23

123 分は見間違えやすい時刻です。なぜならば、時計の表示の右上と左下を入れ替えると 213 分を意味する表示になるからです。
よって答えは 123 分です。
なお、01 23 のように先行ゼロをつけた形式で出力しても正答として扱われます。


入力例 2

19 57

出力例 2

20 0

1957 分以降ではじめて訪れる見間違えやすい時刻は 200 分です。


入力例 3

20 40

出力例 3

21 0

24 時制では 240 分という表記は合法でないのに注意してください。

Score : 200 points

Problem Statement

Takahashi bought a table clock.
The clock shows the time as shown in Figure 1 at \mathrm{AB}:\mathrm{CD} in the 24-hour system.
For example, the clock in Figure 2 shows 7:58.

The format of the time is formally described as follows.
Suppose that the current time is m minutes past h in the 24-hour system. Here, the 24-hour system represents the hour by an integer between 0 and 23 (inclusive), and the minute by an integer between 0 and 59 (inclusive).
Let A be the tens digit of h, B be the ones digit of h, C be the tens digit of m, and D be the ones digit of m. (Here, if h has only one digit, we consider that it has a leading zero; the same applies to m.)
Then, the clock shows A in its top-left, B in its bottom-left, C in its top-right, and D in its bottom-right.

image

Takahashi has decided to call a time a confusing time if it satisfies the following condition:

  • after swapping the top-right and bottom-left digits on the clock, it still reads a valid time in the 24-hour system.

For example, the clock in Figure 3 shows 20:13. After swapping its top-right and bottom-left digits, it reads 21:03. Thus, 20:13 is a confusing time.

The clock now shows H:M.
Find the next confusing time (including now) in the 24-hour system.

Constraints

  • 0 \leq H \leq 23
  • 0 \leq M \leq 59
  • H and M are integers.

Input

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

H M

Output

Let h:m be the answer, where h and m must satisfy 0 \leq h \leq 23 and 0 \leq m \leq 59.
Print h and m in the following format:

h m

Your answer is considered correct even if h contains a leading zero to represent it as a 2-digit integer; the same applies to m.


Sample Input 1

1 23

Sample Output 1

1 23

1:23 is a confusing time because, after swapping its top-right and bottom-left digits on the clock, it reads 2:13.
Thus, the answer is 1:23.
Your answer is considered correct even if you print 01 23 with a leading zero.


Sample Input 2

19 57

Sample Output 2

20 0

The next confusing time after 19:57 is 20:00.


Sample Input 3

20 40

Sample Output 3

21 0

Note that 24:00 is an invalid notation in the 24-hour system.

E - 11/22 Substring

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

この問題における 11/22 文字列の定義は A 問題および E 問題と同じです。

文字列 T が以下の条件を全て満たすとき、T11/22 文字列 と呼びます。

  • |T| は奇数である。ここで、|T|T の長さを表す。
  • 1 文字目から \frac{|T|+1}{2} - 1 文字目までが 1 である。
  • \frac{|T|+1}{2} 文字目が / である。
  • \frac{|T|+1}{2} + 1 文字目から |T| 文字目までが 2 である。

例えば 11/22, 111/222, / は 11/22 文字列ですが、1122, 1/22, 11/2222, 22/11, //2/2/211 はそうではありません。

1, 2, / からなる長さ N の文字列 S が与えられます。S/1 個以上含みます。
11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S1, 2, / からなる長さ N の文字列
  • S/1 個以上含む

入力

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

N
S

出力

11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を出力せよ。


入力例 1

8
211/2212

出力例 1

5

S2 文字目から 6 文字目からなる部分文字列は 11/22 で、これは 11/22 文字列です。S の部分文字列のうち 11/22 文字列であるものはこれが最長です。よって 5 が答えです。


入力例 2

5
22/11

出力例 2

1

入力例 3

22
/1211/2///2111/2222/11

出力例 3

7

Score : 300 points

Problem Statement

The definition of an 11/22 string in this problem is the same as in Problems A and E.

A string T is called an 11/22 string when it satisfies all of the following conditions:

  • |T| is odd. Here, |T| denotes the length of T.
  • The 1-st through (\frac{|T|+1}{2} - 1)-th characters are all 1.
  • The (\frac{|T|+1}{2})-th character is /.
  • The (\frac{|T|+1}{2} + 1)-th through |T|-th characters are all 2.

For example, 11/22, 111/222, and / are 11/22 strings, but 1122, 1/22, 11/2222, 22/11, and //2/2/211 are not.

You are given a string S of length N consisting of 1, 2, and /, where S contains at least one /.
Find the maximum length of a (contiguous) substring of S that is an 11/22 string.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of 1, 2, and /.
  • S contains at least one /.

Input

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

N
S

Output

Print the maximum length of a (contiguous) substring of S that is an 11/22 string.


Sample Input 1

8
211/2212

Sample Output 1

5

The substring from the 2-nd to 6-th character of S is 11/22, which is an 11/22 string. Among all substrings of S that are 11/22 strings, this is the longest. Therefore, the answer is 5.


Sample Input 2

5
22/11

Sample Output 2

1

Sample Input 3

22
/1211/2///2111/2222/11

Sample Output 3

7
F - Jumping Takahashi

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 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
G - Many Segments 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

長さ N の正整数列 L=(L_1,L_2,\ldots,L_N), R=(R_1,R_2,\ldots,R_N) と整数 M が与えられます。

以下の条件を共に満たす整数の組 (l,r) の個数を求めてください。

  • 1\le l \le r \le M
  • 全ての 1\le i\le N に対し区間 [l,r] は区間 [L_i,R_i] を完全には含まない。

制約

  • 1\le N,M\le 2\times 10^5
  • 1\le L_i\le R_i\le M
  • 入力は全て整数

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えを出力せよ。


入力例 1

2 4
1 2
3 4

出力例 1

5

(l,r)=(1,1),(2,2),(2,3),(3,3),(4,4)5 つが条件を満たします。

例えば (l,r)=(1,3) は条件を満たしません。これは、区間 [1,3] が区間 [1,2] を完全に含んでいるためです。


入力例 2

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

出力例 2

0

条件を満たす整数の組が存在しない場合もあります。


入力例 3

6 20
8 12
14 20
11 13
5 19
4 11
1 6

出力例 3

102

Score : 400 points

Problem Statement

You are given two sequences of positive integers of length N, L=(L_1,L_2,\ldots,L_N) and R=(R_1,R_2,\ldots,R_N), and an integer M.

Find the number of pairs of integers (l,r) that satisfy both of the following conditions:

  • 1\le l \le r \le M
  • For every 1\le i\le N, the interval [l,r] does not completely contain the interval [L_i,R_i].

Constraints

  • 1\le N,M\le 2\times 10^5
  • 1\le L_i\le R_i\le M
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the answer.


Sample Input 1

2 4
1 2
3 4

Sample Output 1

5

The five pairs (l,r)=(1,1),(2,2),(2,3),(3,3),(4,4) satisfy the conditions.

For example, (l,r)=(1,3) does not satisfy the conditions because the interval [1,3] completely contains the interval [1,2].


Sample Input 2

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

Sample Output 2

0

There may be cases where no pairs of integers satisfy the conditions.


Sample Input 3

6 20
8 12
14 20
11 13
5 19
4 11
1 6

Sample Output 3

102
H - 3 Team Division

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

N 人の人がおり、3 つのチームに分かれています。

人には 1, 2, \ldots, N の番号、チームには 1, 2, 3 の番号がついており、現在人 i はチーム A_i に所属しています。

各人には強さという値が定まっており、人 i の強さは B_i です。また、チームの強さをチームに所属する人の強さの和として定めます。

0 人以上の人が所属するチームを変更することですべてのチームの強さが等しくなるようにできるか判定してください。すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を求めてください。

ただし、チーム 1, 2, 3 の他に新たにチームを作ることはできません。

制約

  • 3 \leq N \leq 100
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • x \in \lbrace 1, 2, 3 \rbrace に対し、ある i が存在して A_i = x
  • 1 \leq B_i
  • \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
  • 入力される値はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を出力せよ。そうでない場合は -1 を出力せよ。


入力例 1

6
1 2
2 5
1 5
3 3
1 3
3 6

出力例 1

2

1 がチーム 3、人 4 がチーム 2 へと所属するチームを変更することですべてのチームの強さが 8 となります。


入力例 2

4
1 1
1 2
2 3
3 4

出力例 2

-1

入力例 3

3
1 1
2 1
3 1

出力例 3

0

入力例 4

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

出力例 4

3

Score : 450 points

Problem Statement

There are N people divided into three teams.

The people are numbered 1, 2, \ldots, N, and the teams are numbered 1, 2, 3. Currently, person i belongs to team A_i.

Each person has a value called strength; person i has a strength of B_i. The strength of a team is defined as the sum of the strengths of its members.

Determine whether it is possible for zero or more people to switch teams so that all teams have equal strength. If it is possible, find the minimum number of people who need to switch teams to achieve this.

You cannot create new teams other than teams 1, 2, 3.

Constraints

  • 3 \leq N \leq 100
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • For each x \in \lbrace 1, 2, 3 \rbrace, there exists some i with A_i = x.
  • 1 \leq B_i
  • \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If it is possible to make all teams have equal strength, print the minimum number of people who need to switch teams. Otherwise, print -1.


Sample Input 1

6
1 2
2 5
1 5
3 3
1 3
3 6

Sample Output 1

2

If person 1 switches to team 3 and person 4 switches to team 2, all teams will have a strength of 8.


Sample Input 2

4
1 1
1 2
2 3
3 4

Sample Output 2

-1

Sample Input 3

3
1 1
2 1
3 1

Sample Output 3

0

Sample Input 4

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

Sample Output 4

3
I - Cat exercise

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 550

問題文

N 個のキャットタワーが左右一列に並んでおり、左から i 番目 (1\leq i\leq N) のタワーの高さは P_i です。
ここで、(P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の並び替えとなっています。
また、左から i 番目のタワーと j 番目のタワーの間の距離を \lvert i-j\rvert で定義します。

猫が一匹おり、最初、高さ N のキャットタワーの上にいます。
高橋君はタワーを一つ選んで撤去することを繰り返すことで、この猫を運動させたいです。

高橋君がタワーを撤去するとき、猫は以下のように移動します。

  • 撤去するタワーの上に猫がいない場合、猫は移動しません。
  • 撤去するタワーの上に猫がおり、かつそのタワーと左右に隣接するタワーのうち少なくとも一方が存在するとき、撤去されるタワーからスタートして隣接するタワーへの移動を繰り返して到達できるタワーのうち、撤去されるタワーを除いて最も高いタワーに猫が移動します。 このとき、移動前と移動後のタワーの間の距離だけ移動が発生します(移動前後および途中のタワーの高さは関係しません)。
  • 撤去するタワーの上に猫がおり、かつそのタワーと左右に隣接するタワーが存在しないとき、猫は高橋君の腕の中に飛び込み、運動はここで終了となります。このときには移動は発生しません。

ここで、撤去したタワーの間は詰めることはありません。 すなわち、最初の時点で左から i 番目のタワーと隣接しているタワーは(存在すれば)左から (i-1) 番目と (i+1) 番目のタワーのみであり、 その後 他のタワーの撤去によって新しく他のタワーと隣接するようになることはありません

運動が終了するまでの猫の移動距離の合計としてあり得る最大の値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • (P_1,P_2,\ldots, P_N)(1,2,\ldots,N) の並び替えである。
  • 入力はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

運動が終了するまでの猫の移動距離の合計としてあり得る最大の値を出力せよ。


入力例 1

5
5 3 4 1 2

出力例 1

5

最初、キャットタワーの高さは左から順に 5,3,4,1,2 です。
以下、左から i 番目 (1\leq i\leq 5) のキャットタワーをタワー i と呼びます。
最初、猫はタワー 1(高さ 5)にいます。

高橋君がタワー 1,2,3,5,4 の順に取り除くと猫は次のように移動します。

  • 高橋君がタワー 1 を取り除く。タワー 1 から隣接するタワーへの移動のみで移動できるタワーは(タワー 1 を除いて)タワー 2,3,4,5 である。猫はこのうち最も高いタワー 3(高さ 4 )に移動する。
  • 高橋君がタワー 2 を取り除く。猫は移動しない。
  • 高橋君がタワー 3 を取り除く。タワー 3 から隣接するタワーへの移動のみで移動できるタワーは(タワー 3 を除いて)タワー 4,5 である。猫はこのうち最も高いタワー 5(高さ 2 )に移動する。
  • 高橋君がタワー 5 を取り除く。タワー 5 から隣接するタワーへの移動のみで移動できるタワーは(タワー 5 を除いて)タワー 4 のみである。猫はタワー 4(高さ 1 )に移動する。
  • 高橋君がタワー 4 を取り除く。猫は高橋君の腕に飛び込み、運動は終了する。

このとき、猫の移動距離の合計は \lvert 1-3\rvert+\lvert 3-5\rvert+\lvert 5-4\rvert=5 となります。 移動距離の合計を 6 以上にする取り除き方は存在しないため、5 を出力します。


入力例 2

3
1 3 2

出力例 2

1

最初、キャットタワーの高さは左から順に 1,3,2 です。
以下、左から i 番目 (1\leq i\leq 3) のキャットタワーをタワー i と呼びます。
最初、猫はタワー 2(高さ 3)にいます。

高橋君がタワー 2,3 の順に取り除くと猫は次のように移動します。

  • 高橋君がタワー 2 を取り除く。タワー 2 から隣接するタワーへの移動のみで移動できるタワーは(タワー 2 を除いて)タワー 1,3 である。猫はこのうち最も高いタワー 3(高さ 2 )に移動する。
  • 高橋君がタワー 3 を取り除く。猫は高橋君の腕に飛び込み、運動は終了する。

このとき、猫の移動距離の合計は \lvert 2-3\rvert=1 となり、このときが最大です。
タワー 2 を取り除いた後も、タワー 1 とタワー 3 は隣接しているとみなされないことに注意してください。

Score : 550 points

Problem Statement

There are N cat towers arranged in a row from left to right, and the height of the i-th tower from the left (1\leq i\leq N) is P_i.
Here, (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
The distance between the i-th tower and the j-th tower from the left is defined as \lvert i-j\rvert.

There is one cat, initially on top of the cat tower of height N.
Takahashi wants to exercise this cat by repeatedly choosing and removing towers.

When Takahashi removes a tower, the cat moves as follows:

  • If the cat is not on top of the tower being removed, the cat does not move.
  • If the cat is on top of the tower being removed and at least one of the towers adjacent to the left or right of that tower exists, the cat moves to the tallest tower (excluding the tower being removed) among the towers that can be reached by repeatedly moving to adjacent towers starting from the tower being removed. At this time, the cat moves the distance between the tower before movement and the tower after movement (the heights of the towers before, after, and during movement do not matter).
  • If the cat is on top of the tower being removed and no towers adjacent to the left or right of that tower exist, the cat jumps into Takahashi's arms, and the exercise ends here. In this case, no movement occurs.

Here, the spaces between removed towers are not filled in. That is, the towers adjacent to the i-th tower from the left initially are only the (i-1)-th and (i+1)-th towers from the left (if they exist), and they will never become adjacent to other towers due to the removal of other towers afterward.

Find the maximum possible total distance the cat moves before the exercise ends.

Constraints

  • 1\leq N\leq 2\times 10^5
  • (P_1,P_2,\ldots, P_N) is a permutation of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Output the maximum possible total distance the cat moves before the exercise ends.


Sample Input 1

5
5 3 4 1 2

Sample Output 1

5

Initially, the heights of the cat towers are 5,3,4,1,2 from left to right.
Hereafter, the i-th cat tower from the left (1\leq i\leq 5) is called tower i.
Initially, the cat is on tower 1 (height 5).

If Takahashi removes towers in the order 1,2,3,5,4, the cat moves as follows:

  • Takahashi removes tower 1. The towers that can be reached by only moving to adjacent towers from tower 1 are towers 2,3,4,5 (excluding tower 1). The cat moves to tower 3 (height 4), which is the tallest among them.
  • Takahashi removes tower 2. The cat does not move.
  • Takahashi removes tower 3. The towers that can be reached by only moving to adjacent towers from tower 3 are towers 4,5 (excluding tower 3). The cat moves to tower 5 (height 2), which is the tallest among them.
  • Takahashi removes tower 5. The towers that can be reached by only moving to adjacent towers from tower 5 are only tower 4 (excluding tower 5). The cat moves to tower 4 (height 1).
  • Takahashi removes tower 4. The cat jumps into Takahashi's arms, and the exercise ends.

Here, the cat moves the total distance of \lvert 1-3\rvert+\lvert 3-5\rvert+\lvert 5-4\rvert=5. There is no way to remove towers that makes the total distance moved 6 or more, so output 5.


Sample Input 2

3
1 3 2

Sample Output 2

1

Initially, the heights of the cat towers are 1,3,2 from left to right.
Hereafter, the i-th cat tower from the left (1\leq i\leq 3) is called tower i.
Initially, the cat is on tower 2 (height 3).

If Takahashi removes towers in the order 2,3, the cat moves as follows:

  • Takahashi removes tower 2. The towers that can be reached by only moving to adjacent towers from tower 2 are towers 1,3 (excluding tower 2). The cat moves to tower 3 (height 2), which is the tallest among them.
  • Takahashi removes tower 3. The cat jumps into Takahashi's arms, and the exercise ends.

Here, the cat moves the total distance of \lvert 2-3\rvert=1, which is the maximum.
Note that after removing tower 2, tower 1 and tower 3 are not considered to be adjacent.