A - Black Square

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

10^{100} 行 10^{100} 列のマス目があります。
以下では、上から i 行目かつ左から j 列目のマスをマス (i,j) と表します。

マス目のうち、マス (P,Q) を一番左上のマスとした 100\times 100 マスの領域のみが黒く塗られており、それ以外のマスは白く塗られています。
マス (X,Y) が黒く塗られているか判定してください。

制約

  • 1 \leq P,Q,X,Y \leq 10000
  • P,Q,X,Y は整数

入力

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

P Q
X Y

出力

マス (X,Y) が黒く塗られているならば Yes を、そうでないならば No を出力せよ。


入力例 1

3 3
5 10

出力例 1

Yes

マス (3,3) を最も左上のマスとする 100\times 100 マスの領域が黒く塗られています。
マス (5,10) はこの領域に含まれているため、Yes を出力します。


入力例 2

5 5
10 1000

出力例 2

No

マス (5,5) を最も左上のマスとする 100\times 100 マスの領域が黒く塗られています。
マス (10,1000) はこの領域に含まれていないため、No を出力します。


入力例 3

1 2
1 1

出力例 3

No

マス (1,2) を最も左上のマスとする 100\times 100 マスの領域が黒く塗られています。
マス (1,1) はこの領域に含まれていないため、No を出力します。

Score : 100 points

Problem Statement

There is a grid with 10^{100} rows and 10^{100} columns.
In what follows, the cell at the i-th row from the top and j-th column from the left is denoted as cell (i,j).

In this grid, only the 100\times 100 region with cell (P,Q) as the top-left cell is painted black, and all other cells are painted white.
Determine whether cell (X,Y) is painted black.

Constraints

  • 1 \leq P,Q,X,Y \leq 10000
  • P,Q,X,Y are integers.

Input

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

P Q
X Y

Output

If cell (X,Y) is painted black, print Yes; otherwise, print No.


Sample Input 1

3 3
5 10

Sample Output 1

Yes

The 100\times 100 region with cell (3,3) as the top-left cell is painted black.
Cell (5,10) is included in this region, so print Yes.


Sample Input 2

5 5
10 1000

Sample Output 2

No

The 100\times 100 region with cell (5,5) as the top-left cell is painted black.
Cell (10,1000) is not included in this region, so print No.


Sample Input 3

1 2
1 1

Sample Output 3

No

The 100\times 100 region with cell (1,2) as the top-left cell is painted black.
Cell (1,1) is not included in this region, so print No.

B - Find Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

AtCoder 村には N 本の橋があり、i 本目( i は 1 以上 N 以下の整数)の橋の高さは H_i です。
ここで、AtCoder 村にある N 本の橋のうち、どの相異なる 2 本の橋も高さが異なります。

AtCoder 村で最も高い橋は何本目の橋か出力してください。

制約

  • 1\leq N \leq 100
  • 1\leq H_i \leq 10^9
  • H_i はすべて異なる
  • 入力はすべて整数

入力

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

N
H_1 H_2 \ldots H_N

出力

AtCoder 村で最も高い橋は何本目の橋かを、整数で出力せよ。


入力例 1

3
50 80 70

出力例 1

2

AtCoder 村には 3 本の橋があります。
1,2,3 本目の橋の高さはそれぞれ, 50,80,70 であり、 最も高い橋は 2 本目の橋です。
よって、2 を出力します。


入力例 2

1
1000000000

出力例 2

1

AtCoder 村に橋が 1 本しか存在しないため、2 本目以降の橋は存在せず、最も高い橋は 1 本目の橋となります。


入力例 3

10
22 75 26 45 72 81 47 29 97 2

出力例 3

9

AtCoder 村には 10 本の橋があり、それらのうち最も高い橋は 9 番目の橋(高さは 97 )です。

Score : 100 points

Problem Statement

There are N bridges in AtCoder Village. The height of the bridge numbered i is H_i (i is an integer between 1 and N).
Every two different bridges in the village have different heights.

Print the number representing the highest bridge in the village.

Constraints

  • 1\leq N \leq 100
  • 1\leq H_i \leq 10^9
  • All H_i are different.
  • All values in the input are integers.

Input

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

N
H_1 H_2 \ldots H_N

Output

Print the integer representing the highest bridge in AtCoder village.


Sample Input 1

3
50 80 70

Sample Output 1

2

The village has three bridges.
The first, second, and third bridges have heights of 50, 80, and 70, respectively, so the second bridge is the highest.
Thus, 2 should be printed.


Sample Input 2

1
1000000000

Sample Output 2

1

The village has only one bridge, so the first bridge is the highest.


Sample Input 3

10
22 75 26 45 72 81 47 29 97 2

Sample Output 3

9

The village has ten bridges, and the ninth bridge (with a height of 97) is the highest.

C - racecar

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。
1 以上 N 以下の 相異なる 整数 i,j であって、S_i と S_j をこの順に連結した文字列が回文となるようなものが存在するか判定してください。

ただし、長さ M の文字列 T が回文であるとは、任意の 1\leq i\leq M について、T の i 文字目と (M+1-i) 文字目が一致していることをいいます。

制約

  • 2\leq N\leq 100
  • 1\leq \lvert S_i\rvert \leq 50
  • N は整数
  • S_i は英小文字のみからなる文字列
  • S_i はすべて異なる。

入力

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

N
S_1
S_2
\vdots
S_N

出力

問題文の条件をみたす i,j が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
ab
ccef
da
a
fe

出力例 1

Yes

(i,j)=(1,4) とすると、S_1=ab と S_4=a をこの順に連結した文字列は aba となり、 これは回文であるため条件をみたしています。
よって、Yes を出力します。

また、(i,j)=(5,2) としても、S_5=fe と S_2=ccef をこの順に連結した文字列は feccef となり、やはり条件をみたしています。


入力例 2

3
a
b
aba

出力例 2

No

S_1, S_2, S_3 のうち、どの相異なる 2 つの文字列を繋げても回文となりません。 よって、No を出力します。
問題文における i,j は相異なる必要があることに注意してください。


入力例 3

2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

出力例 3

Yes

Score : 200 points

Problem Statement

You are given N strings S_1,S_2,\ldots,S_N consisting of lowercase English letters.
Determine if there are distinct integers i and j between 1 and N, inclusive, such that the concatenation of S_i and S_j in this order is a palindrome.

A string T of length M is a palindrome if and only if the i-th character and the (M+1-i)-th character of T are the same for every 1\leq i\leq M.

Constraints

  • 2\leq N\leq 100
  • 1\leq \lvert S_i\rvert \leq 50
  • N is an integer.
  • S_i is a string consisting of lowercase English letters.
  • All S_i are distinct.

Input

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

N
S_1
S_2
\vdots
S_N

Output

If there are i and j that satisfy the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1

5
ab
ccef
da
a
fe

Sample Output 1

Yes

If we take (i,j)=(1,4), the concatenation of S_1=ab and S_4=a in this order is aba, which is a palindrome, satisfying the condition.
Thus, print Yes.

Here, we can also take (i,j)=(5,2), for which the concatenation of S_5=fe and S_2=ccef in this order is feccef, satisfying the condition.


Sample Input 2

3
a
b
aba

Sample Output 2

No

No two distinct strings among S_1, S_2, and S_3 form a palindrome when concatenated. Thus, print No.
Note that the i and j in the statement must be distinct.


Sample Input 3

2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 3

Yes
D - 456

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

6 つの面を持つサイコロが 3 個あります。
i 個目のサイコロの j 個目の面には A_{i,j} が書かれています。
どのサイコロも、各面が出る確率は \frac{1}{6} です。

これらのサイコロを同時に振ったとき、4,5,6 の書かれた目が 1 つずつ出る確率を求めてください。

制約

  • A_{i,j} は 1 以上 6 以下の整数

入力

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}

出力

答えを出力せよ。 真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解とみなされる。


入力例 1

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

出力例 1

0.0277777778

求める確率は \frac{1}{36} です。 0.027778 などの出力でも正解となります。


入力例 2

4 5 6 4 5 6
4 4 5 5 6 6
6 5 4 4 5 6

出力例 2

0.2222222222

求める確率は \frac{2}{9} です。

Score : 200 points

Problem Statement

There are three dice, each with six faces.
The j-th face of the i-th die has A_{i,j} written on it.
For each die, each face comes up with probability \frac{1}{6}.

When these dice are rolled simultaneously, find the probability that the values 4,5,6 each appear exactly once.

Constraints

  • A_{i,j} is an integer between 1 and 6, inclusive.

Input

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}

Output

Output the answer. The answer is considered correct if the absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

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

Sample Output 1

0.0277777778

The desired probability is \frac{1}{36}. An output such as 0.027778 is also accepted.


Sample Input 2

4 5 6 4 5 6
4 4 5 5 6 6
6 5 4 4 5 6

Sample Output 2

0.2222222222

The desired probability is \frac{2}{9}.

E - Dice Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

長さ N の整数からなる数列 A=(A_1,\ldots,A_N) であって、以下の条件を全て満たすものは何通りありますか?

  • 1\le A_i \le M (1 \le i \le N)

  • \displaystyle\sum _{i=1}^N A_i \leq K

ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N, M \leq 50
  • N \leq K \leq NM
  • 入力は全て整数

入力

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

N M K

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

2 3 4

出力例 1

6

条件を満たす数列は以下の 6 つです。

  • (1,1)
  • (1,2)
  • (1,3)
  • (2,1)
  • (2,2)
  • (3,1)

入力例 2

31 41 592

出力例 2

798416518

答えを 998244353 で割った余りを出力してください。

Score : 300 points

Problem Statement

How many integer sequences of length N, A=(A_1, \ldots, A_N), satisfy all of the conditions below?

  • 1\le A_i \le M (1 \le i \le N)

  • \displaystyle\sum _{i=1}^N A_i \leq K

Since the count can get enormous, find it modulo 998244353.

Constraints

  • 1 \leq N, M \leq 50
  • N \leq K \leq NM
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.


Sample Input 1

2 3 4

Sample Output 1

6

The following six sequences satisfy the conditions.

  • (1,1)
  • (1,2)
  • (1,3)
  • (2,1)
  • (2,2)
  • (3,1)

Sample Input 2

31 41 592

Sample Output 2

798416518

Be sure to print the count modulo 998244353.

F - Count Connected Components

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。
グラフに含まれる連結成分の個数を求めてください。

注釈

単純無向グラフ とは、単純で辺に向きの無いグラフのことをいいます。
グラフが 単純 であるとは、グラフが自己ループや多重辺を含まないことをいいます。

あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。

制約

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq u_i, v_i \leq N
  • 入力で与えられるグラフは単純
  • 入力される値はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

5 3
1 2
1 3
4 5

出力例 1

2

与えられるグラフに含まれる連結成分は次の 2 個です。

  • 頂点 1, 2, 3 および辺 1, 2 からなる部分グラフ
  • 頂点 4, 5 および辺 3 からなる部分グラフ

image


入力例 2

5 0

出力例 2

5

入力例 3

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

出力例 3

1

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.
Find the number of connected components in this graph.

Notes

A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.

A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq u_i, v_i \leq N
  • The given graph is simple.
  • All values in the input are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the answer.


Sample Input 1

5 3
1 2
1 3
4 5

Sample Output 1

2

The given graph contains the following two connected components:

  • a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
  • a subgraph formed from vertices 4, 5, and edge 3.

image


Sample Input 2

5 0

Sample Output 2

5

Sample Input 3

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

Sample Output 3

1
G - XNOR Operation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

この問題は G 問題の部分問題です。

0 と 1 からなる空でない文字列 S が次の条件を満たす時、S を美しい文字列と呼びます。

  • (条件) 次の一連の操作を S の長さが 1 になるまで行い、S に残った唯一の文字を 1 にすることができる。
    1. 1 \leq i \leq |S| - 1 を満たす整数 i を自由に選ぶ。
    2. 整数 x を次のように定義する。
      • S_i = 0 かつ S_{i+1}= 0 である場合、x = 1 とする。
      • S_i = 0 かつ S_{i+1}= 1 である場合、x = 0 とする。
      • S_i = 1 かつ S_{i+1}= 0 である場合、x = 0 とする。
      • S_i = 1 かつ S_{i+1}= 1 である場合、x = 1 とする。
    3. S_i と S_{i+1} を取り除き、それらがあった場所に x を数字とみなしたものを 1 個挿入する。
      例えば S= 10101 に対して i=2 を選んで操作を行った場合、操作後の文字列は 1001 になる。

0 と 1 からなる長さ N の文字列 T があります。
T の部分文字列である美しい文字列の個数を求めてください。ただし、2 つの部分文字列が文字列として同じでも、取り出す位置が異なるならば別々に数えます。

部分文字列とは S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、10 は 101 の部分文字列ですが、11 は 101 の部分文字列ではありません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • N は整数
  • T は 0 と 1 からなる長さ N の文字列

入力

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

N
T

出力

T の部分文字列である美しい文字列の個数を出力せよ。


入力例 1

3
110

出力例 1

3

T の 1 文字目から 2 文字目までを取り出してできる文字列 11 は美しい文字列です。なぜならば、i=1 として操作を行うと、操作後の文字列は 1 になるからです。
T の部分文字列である美しい文字列は次の 3 個です。

  • T の 1 文字目を取り出してできる文字列 1
  • T の 2 文字目を取り出してできる文字列 1
  • T の 1 文字目から 2 文字目までを取り出してできる文字列 11

入力例 2

4
0000

出力例 2

4

入力例 3

30
011011100101110111100010011010

出力例 3

225

Score : 425 points

Problem Statement

This problem is a subproblem of Problem G.

A non-empty string S consisting of 0 and 1 is called a beautiful string when it satisfies the following condition:

  • (Condition) You can perform the following sequence of operations until the length of S becomes 1 and make the only character remaining in S be 1.
    1. Choose any integer i satisfying 1 \leq i \leq |S| - 1.
    2. Define an integer x as follows:
      • If S_i = 0 and S_{i+1} = 0, let x = 1.
      • If S_i = 0 and S_{i+1} = 1, let x = 0.
      • If S_i = 1 and S_{i+1} = 0, let x = 0.
      • If S_i = 1 and S_{i+1} = 1, let x = 1.
    3. Remove S_i and S_{i+1}, and insert the digit corresponding to x in their place.
      For example, if S= 10101 and you choose i=2, the string after the operation is 1001.

You are given a string T of length N consisting of 0 and 1.
Find the number of beautiful strings that are substrings of T. Even if two substrings are identical as strings, count them separately if they are taken from different positions.

What are substrings? A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, 10 is a substring of 101, but 11 is not a substring of 101.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an integer.
  • T is a string of length N consisting of 0 and 1.

Input

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

N
T

Output

Print the number of beautiful strings that are substrings of T.


Sample Input 1

3
110

Sample Output 1

3

The string 11 obtained by taking the 1st through 2nd characters of T is a beautiful string, because if you choose i=1 and perform the operation, the string becomes 1. The beautiful strings that are substrings of T are the following three strings:

  • The string 1 obtained by taking the 1st character of T.
  • The string 1 obtained by taking the 2nd character of T.
  • The string 11 obtained by taking the 1st through 2nd characters of T.

Sample Input 2

4
0000

Sample Output 2

4

Sample Input 3

30
011011100101110111100010011010

Sample Output 3

225
H - Playlist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

高橋君は N 曲からなるプレイリストを持っています。 曲 i (1 \leq i \leq N) の長さは T_i 秒です。
高橋君は時刻 0 にプレイリストのランダム再生を開始しました。

ランダム再生では、N 曲の中から等確率で 1 つを選びその曲を最後まで再生することが繰り返されます。 ここで、曲の再生は休みなく行われ、1 つの曲が終わったらすぐに次に選ばれた曲が始まります。 また、同じ曲が連続して選ばれる事もあります。

時刻 0 から (X+0.5) 秒後に曲 1 が再生されている確率を \text{mod}998244353 で求めてください。

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 2 \leq N\leq 10^3
  • 0 \leq X\leq 10^4
  • 1 \leq T_i\leq 10^4
  • 入力はすべて整数

入力

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

N X
T_1 T_2 \ldots T_N

出力

時刻 0 から (X+0.5) 秒後にプレイリストの 1 番目の曲が再生されている確率を \text{mod}998244353 で出力せよ。


入力例 1

3 6
3 5 6

出力例 1

369720131

時刻 0 から 6.5 秒後に曲 1 が流れているパターンとしてあり得るのは、

  • 曲 1 \to 曲 1 \to 曲 1
  • 曲 2 \to 曲 1
  • 曲 3 \to 曲 1

の順で音楽が再生された場合であり、これらのいずれかが起こる確率は \frac{7}{27} となります。
369720131\times 27\equiv 7 \pmod{998244353} であるため、369720131 を出力します。


入力例 2

5 0
1 2 1 2 1

出力例 2

598946612

時刻 0 から 0.5 秒後には最初に再生された曲が再生されているため、求める確率は \frac{1}{5} となります。
同じ長さの異なる曲が存在することがあることに注意してください。


入力例 3

5 10000
1 2 3 4 5

出力例 3

586965467

Score : 450 points

Problem Statement

Takahashi has a playlist with N songs. Song i (1 \leq i \leq N) lasts T_i seconds.
Takahashi has started random play of the playlist at time 0.

Random play repeats the following: choose one song from the N songs with equal probability and play that song to the end. Here, songs are played continuously: once a song ends, the next chosen song starts immediately. The same song can be chosen consecutively.

Find the probability that song 1 is being played (X + 0.5) seconds after time 0, modulo 998244353.

How to print a probability modulo 998244353

It can be proved that the probability to be found in this problem is always a rational number. Also, the constraints of this problem guarantee that when the probability to be found is expressed as an irreducible fraction \frac{y}{x}, x is not divisible by 998244353.

Then, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 2 \leq N\leq 10^3
  • 0 \leq X\leq 10^4
  • 1 \leq T_i\leq 10^4
  • All input values are integers.

Input

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

N X
T_1 T_2 \ldots T_N

Output

Print the probability, modulo 998244353, that the first song in the playlist is being played (X+0.5) seconds after time 0.


Sample Input 1

3 6
3 5 6

Sample Output 1

369720131

Song 1 will be playing 6.5 seconds after time 0 if songs are played in one of the following orders.

  • Song 1 \to Song 1 \to Song 1
  • Song 2 \to Song 1
  • Song 3 \to Song 1

The probability that one of these occurs is \frac{7}{27}.
We have 369720131\times 27\equiv 7 \pmod{998244353}, so you should print 369720131.


Sample Input 2

5 0
1 2 1 2 1

Sample Output 2

598946612

0.5 seconds after time 0, the first song to be played is still playing, so the sought probability is \frac{1}{5}.
Note that different songs may have the same length.


Sample Input 3

5 10000
1 2 3 4 5

Sample Output 3

586965467
I - Cat exercise

Time Limit: 2 sec / Memory Limit: 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.