A - Water Station

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

配点 : 100

問題文

全長 100\;\mathrm{km} のマラソンコースがあります。 マラソンコースにはスタートから 5\;\mathrm{km} おきに給水所が設置されており、スタート地点・ゴール地点とあわせて 21 箇所の給水所があります。

高橋くんは、このマラソンコースの N\;\mathrm{km} 地点にいます。 高橋くんに最も近い給水所は何 \mathrm{km} 地点の給水所か求めてください。

この問題の制約のもとで、最も近い給水所が 1 つに定まることが証明できます。

制約

  • 0\leq N\leq100
  • N は整数

入力

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

N

出力

高橋くんがいる地点に最も近い給水所が何 \mathrm{km} 地点にあるかを 1 行で出力せよ。


入力例 1

53

出力例 1

55

高橋くんは、マラソンコースの 53\;\mathrm{km} 地点にいます。 55\;\mathrm{km} 地点の給水所までの距離は 2\;\mathrm{km} で、これより近い給水所はありません。 よって、55 を出力してください。


入力例 2

21

出力例 2

20

高橋くんはマラソンコースを戻ることもできます。


入力例 3

100

出力例 3

100

給水所はスタートやゴールにもあります。 また、高橋くんはすでに給水所にいることもあります。

Score : 100 points

Problem Statement

There is an ultramarathon course totaling 100\;\mathrm{km}. Water stations are set up every 5\;\mathrm{km} along the course, including the start and goal, for a total of 21.

Takahashi is at the N\;\mathrm{km} point of this course. Find the position of the nearest water station to him.

Under the constraints of this problem, it can be proven that the nearest water station is uniquely determined.

Constraints

  • 0\leq N\leq100
  • N is an integer.

Input

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

N

Output

Print the distance between the start and the water station nearest to Takahashi, in kilometers, in a single line.


Sample Input 1

53

Sample Output 1

55

Takahashi is at the 53\;\mathrm{km} point of the course. The water station at the 55\;\mathrm{km} point is 2\;\mathrm{km} away, and there is no closer water station. Therefore, you should print 55.


Sample Input 2

21

Sample Output 2

20

Takahashi could also go back the way.


Sample Input 3

100

Sample Output 3

100

There are also water stations at the start and goal. Additionally, Takahashi may already be at a water station.

B - Five Integers

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

配点 : 100

問題文

与えられる 5 つの整数 A, B, C, D, E の中に何種類の整数があるかを出力してください。

制約

  • 0 \leq A, B, C, D, E \leq 100
  • 入力はすべて整数

入力

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

A B C D E

出力

答えを出力せよ。


入力例 1

31 9 24 31 24

出力例 1

3

与えられる 5 つの整数 31, 9, 24, 31, 24 の中には、9, 24, 31 という 3 種類の整数があります。 よって、3 を出力します。


入力例 2

0 0 0 0 0

出力例 2

1

Score : 100 points

Problem Statement

Print how many distinct integers there are in given five integers A, B, C, D, and E.

Constraints

  • 0 \leq A, B, C, D, E \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E

Output

Print the answer.


Sample Input 1

31 9 24 31 24

Sample Output 1

3

In the given five integers 31, 9, 24, 31, and 24, there are three distinct integers 9, 24, and 31. Thus, 3 should be printed.


Sample Input 2

0 0 0 0 0

Sample Output 2

1
C - Triple Metre

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

配点 : 200

問題文

文字列 S が文字列 T の部分文字列であるとは、次の条件を満たすような整数 i, j (1 \leq i \leq j \leq |T|) が存在することを言います。

  • Ti 文字目から j 文字目までを順番を変えずに抜き出してできる文字列が S と一致する。

文字列 Toxx10^5 個結合した文字列として定めます。
文字列 S が与えられるので、 ST の部分文字列である場合は Yes を、そうでない場合は No を出力してください。

制約

  • Sox のみからなる文字列である。
  • S の長さは 1 以上 10 以下である。

入力

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

S

出力

S が条件を満たす場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

xoxxoxxo

出力例 1

Yes

T のはじめの方を抜き出すと oxxoxxoxxoxx... となっています。
T3 文字目から 10 文字目までを抜き出した文字列は S と一致するので、 ST の部分文字列です。よって Yes を出力します。


入力例 2

xxoxxoxo

出力例 2

No

T から文字列をどのように抜き出しても S と一致しないので、ST の部分文字列でありません。よって No を出力します。


入力例 3

ox

出力例 3

Yes

Score : 200 points

Problem Statement

A string S is said to be a substring of a string T when there is a pair of integers i and j (1 \leq i \leq j \leq |T|) that satisfy the following condition.

  • The extraction of the i-th through j-th characters of T without changing the order equals S.

Let T be the concatenation of 10^5 copies of oxx.
Given a string S, print Yes if S is a substring of T, and No otherwise.

Constraints

  • S is a string consisting of o and x.
  • The length of S is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S satisfies the condition, print Yes; otherwise, print No.


Sample Input 1

xoxxoxxo

Sample Output 1

Yes

T begins like this: oxxoxxoxxoxx... Since the extraction of 3-rd through 10-th characters of T equals S, S is a substring of T, so Yes should be printed.


Sample Input 2

xxoxxoxo

Sample Output 2

No

Since there is no way to extract from T a string that equals S, S is not a substring of T, so No should be printed.


Sample Input 3

ox

Sample Output 3

Yes
D - Hammer

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

配点 : 200

問題文

数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。

座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。

高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。

制約

  • -1000 \leq X,Y,Z \leq 1000
  • X,Y,Z は相異なり、いずれも 0 でない
  • 入力に含まれる値は全て整数である

入力

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

X Y Z

出力

高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。


入力例 1

10 -10 1

出力例 1

10

高橋君はまっすぐゴールに向かうことができます。


入力例 2

20 10 -10

出力例 2

40

ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。


入力例 3

100 1 1000

出力例 3

-1

Score : 200 points

Problem Statement

Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.

There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.

Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.

Constraints

  • -1000 \leq X,Y,Z \leq 1000
  • X, Y, and Z are distinct, and none of them is 0.
  • All values in the input are integers.

Input

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

X Y Z

Output

If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.


Sample Input 1

10 -10 1

Sample Output 1

10

Takahashi can go straight to the goal.


Sample Input 2

20 10 -10

Sample Output 2

40

The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.


Sample Input 3

100 1 1000

Sample Output 3

-1
E - Path Graph?

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

配点 : 300

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, 2, \dots, N の番号が、辺には 1, 2, \dots, M の番号が付けられています。
i \, (i = 1, 2, \dots, M) は頂点 u_i, v_i を結んでいます。

このグラフがパスグラフであるか判定してください。

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

パスグラフとは 頂点に 1, 2, \dots, N の番号が付けられたN 頂点のグラフがパスグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
  • 全ての i = 1, 2, \dots, N-1 に対して、頂点 v_i, v_{i+1} を結ぶ辺が存在する
  • 整数 i, j1 \leq i, j \leq N, |i - j| \geq 2 を満たすならば、頂点 v_i, v_j を結ぶ辺は存在しない

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • 入力される値は全て整数
  • 入力で与えられるグラフは単純

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

与えられたグラフがパスグラフなら Yes、そうでないなら No と出力せよ。


入力例 1

4 3
1 3
4 2
3 2

出力例 1

Yes

与えらえたグラフは下図のようであり、これはパスグラフです。

example_00


入力例 2

2 0

出力例 2

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_01


入力例 3

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

出力例 3

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_02

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, 2, \dots, N, and the edges are numbered 1, 2, \dots, M.
Edge i \, (i = 1, 2, \dots, M) connects vertices u_i and v_i.

Determine if this graph is a path graph.

What is a simple undirected graph? A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.

What is a path graph? A graph with N vertices numbered 1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v_1, v_2, \dots, v_N) that is a permutation of (1, 2, \dots, N) and satisfies the following conditions:
  • For all i = 1, 2, \dots, N-1, there is an edge connecting vertices v_i and v_{i+1}.
  • If integers i and j satisfies 1 \leq i, j \leq N and |i - j| \geq 2, then there is no edge that connects vertices v_i and v_j.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • All values in the input are integers.
  • The graph given in the input is simple.

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 Yes if the given graph is a path graph; print No otherwise.


Sample Input 1

4 3
1 3
4 2
3 2

Sample Output 1

Yes

Illustrated below is the given graph, which is a path graph.

example_00


Sample Input 2

2 0

Sample Output 2

No

Illustrated below is the given graph, which is not a path graph.

example_01


Sample Input 3

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

Sample Output 3

No

Illustrated below is the given graph, which is not a path graph.

example_02

F - Product

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

配点 : 300

問題文

N 個の袋があります。
i には L_i 個のボールが入っていて、袋 ij(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。

それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?

ただし、書かれた数が同じであっても全てのボールは区別します。

制約

  • N \geq 2
  • L_i \geq 2
  • 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力に含まれる値は全て整数である。

入力

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

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

出力

答えを出力せよ。


入力例 1

2 40
3 1 8 4
2 10 5

出力例 1

2

13 番目のボールと袋 21 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
12 番目のボールと袋 22 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。


入力例 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

出力例 2

45

書かれた数が同じであっても全てのボールは区別することに注意してください。


入力例 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

出力例 3

0

総積が X になる取り出し方が 1 つも存在しないこともあります。

Score : 300 points

Problem Statement

We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.

We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?

Here, we distinguish all balls, even with the same numbers written on them.

Constraints

  • N \geq 2
  • L_i \geq 2
  • The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

Output

Print the answer.


Sample Input 1

2 40
3 1 8 4
2 10 5

Sample Output 1

2

When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.


Sample Input 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

Sample Output 2

45

Note that we distinguish all balls, even with the same numbers written on them.


Sample Input 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

Sample Output 3

0

There may be no way to make the product X.

G - Bitmask

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

配点 : 400

問題文

0, 1, ? からなる文字列 S および整数 N が与えられます。 S に含まれる ? をそれぞれ 0 または 1 に置き換えて 2 進整数とみなしたときに得られる値の集合を T とします。 たとえば、S= ?0? のとき、 T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace です。

T に含まれる N 以下の値のうち最大のものを (10 進整数として) 出力してください。 N 以下の値が T に含まれない場合は、代わりに -1 を出力してください。

制約

  • S0, 1, ? からなる文字列
  • S の長さは 1 以上 60 以下
  • 1\leq N \leq 10^{18}
  • N は整数

入力

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

S
N

出力

答えを出力せよ。


入力例 1

?0?
2

出力例 1

1

問題文中で例示したとおり、T=\lbrace 0,1,4,5\rbrace です。 T に含まれる N 以下の値は 01 なので、そのうちの最大である 1 を出力します。


入力例 2

101
4

出力例 2

-1

T=\lbrace 5\rbrace であるため、N 以下の値は T に含まれません。


入力例 3

?0?
1000000000000000000

出力例 3

5

Score : 400 points

Problem Statement

You are given an integer N and a string S consisting of 0, 1, and ?. Let T be the set of values that can be obtained by replacing each ? in S with 0 or 1 and interpreting the result as a binary integer. For instance, if S= ?0?, we have T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace.

Print (as a decimal integer) the greatest value in T less than or equal to N. If T does not contain a value less than or equal to N, print -1 instead.

Constraints

  • S is a string consisting of 0, 1, and ?.
  • The length of S is between 1 and 60, inclusive.
  • 1\leq N \leq 10^{18}
  • N is an integer.

Input

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

S
N

Output

Print the answer.


Sample Input 1

?0?
2

Sample Output 1

1

As shown in the problem statement, T=\lbrace 0,1,4,5\rbrace. Among them, 0 and 1 are less than or equal to N, so you should print the greatest of them, 1.


Sample Input 2

101
4

Sample Output 2

-1

We have T=\lbrace 5\rbrace, which does not contain a value less than or equal to N.


Sample Input 3

?0?
1000000000000000000

Sample Output 3

5
H - Red and Blue Tree

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

配点 : 500

問題文

N 頂点の木と、長さ M の数列 A=(A_1,\ldots,A_M)、整数 K が与えられます。
木の頂点には 1 から N の番号がつけられており、i 番目の辺は頂点 U_iV_i を結んでいます。

この木の N-1 個の辺をそれぞれ赤か青のどちらかに塗ります。そのような方法は 2^{N-1} 通りありますが、そのうち次の条件を満たすような塗り方の個数を 998244353 で割った余りを求めてください。

条件:
最初、駒を頂点 A_1 におく。i=1,\ldots,M-1 の順に、駒を頂点 A_i から頂点 A_{i+1} まで、辺をたどって最短経路で動かす。 これらの操作を最後まで行ったとき、赤く塗られた辺を通過した回数を R、青く塗られた辺を通過した回数を B とすると、R-B=K である。

制約

  • 2 \leq N \leq 1000
  • 2 \leq M \leq 100
  • |K| \leq 10^5
  • 1 \leq A_i \leq N
  • 1\leq U_i,V_i\leq N
  • 与えられるグラフは木である
  • 入力に含まれる値は全て整数である

入力

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

N M K
A_1 A_2 \ldots A_M
U_1 V_1
\vdots
U_{N-1} V_{N-1}

出力

答えを出力せよ。


入力例 1

4 5 0
2 3 2 1 4
1 2
2 3
3 4

出力例 1

2

1,3 番目の辺を赤く、2 番目の辺を青く塗ったとき、

  • 頂点 2 から頂点 3 への移動で赤い辺を 0 回、青い辺を 1
  • 頂点 3 から頂点 2 への移動で赤い辺を 0 回、青い辺を 1
  • 頂点 2 から頂点 1 への移動で赤い辺を 1 回、青い辺を 0
  • 頂点 1 から頂点 4 への移動で赤い辺を 2 回、青い辺を 1

それぞれ通過し、全体では赤い辺を 3 回、青い辺を 3 回通るため、条件を満たします。

図

この他、1,3 番目の辺を青く、2 番目の辺を赤く塗るときも条件を満たし、これら以外の塗り方は条件を満たさないため、答えは 2 通りです。


入力例 2

3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3

出力例 2

0

条件を満たす塗り方が存在しないこともあります。


入力例 3

10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

出力例 3

126

入力例 4

5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5

出力例 4

2

Score : 500 points

Problem Statement

Given are a tree with N vertices, a sequence of M numbers A=(A_1,\ldots,A_M), and an integer K.
The vertices are numbered 1 through N, and the i-th edge connects Vertices U_i and V_i.

We will paint each of the N-1 edges of this tree red or blue. Among the 2^{N-1} such ways, find the number of ones that satisfies the following condition, modulo 998244353.

Condition:
Let us put a piece on Vertex A_1, and for each i=1,\ldots,M-1 in this order, move it from Vertex A_i to Vertex A_{i+1} along the edges in the shortest path. After all of these movements, R-B=K holds, where R and B are the numbers of times the piece traverses a red edge and a blue edge, respectively.

Constraints

  • 2 \leq N \leq 1000
  • 2 \leq M \leq 100
  • |K| \leq 10^5
  • 1 \leq A_i \leq N
  • 1\leq U_i,V_i\leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_M
U_1 V_1
\vdots
U_{N-1} V_{N-1}

Output

Print the answer.


Sample Input 1

4 5 0
2 3 2 1 4
1 2
2 3
3 4

Sample Output 1

2

If we paint the 1-st and 3-rd edges red and the 2-nd edge blue, the piece will traverse the following numbers of red and blue edges:

  • 0 red edges and 1 blue edge when moving from Vertex 2 to 3,
  • 0 red edges and 1 blue edge when moving from Vertex 3 to 2,
  • 1 red edge and 0 blue edges when moving from Vertex 2 to 1,
  • 2 red edges and 1 blue edge when moving from Vertex 1 to 4,

for a total of 3 red edges and 3 blue edges, satisfying the condition.

Figure

Another way to satisfy the condition is to paint the 1-st and 3-rd edges blue and the 2-nd edge red. There is no other way to satisfy it, so the answer is 2.


Sample Input 2

3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3

Sample Output 2

0

There may be no way to paint the tree to satisfy the condition.


Sample Input 3

10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Sample Output 3

126

Sample Input 4

5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5

Sample Output 4

2
I - Permutation Distance

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

配点 : 500

問題文

(1,2,\ldots,N) の順列 P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。

すべての i\ (1\leq i\leq N) に対して、以下の値を求めてください。

  • D _ i=\displaystyle\min_{j\neq i}\left\lparen\left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert\right\rparen
順列とは (1,2,\ldots,N) の順列とは、(1,2,\ldots,N) を並べ替えて得られる数列のことをいいます。 つまり、長さ N の数列 Ai\ (1\leq i\leq N) がその中にちょうど 1 回だけ現れるとき、かつそのときに限り(1,2,\ldots,N) の順列です。

制約

  • 2 \leq N \leq 2\times10^5
  • 1 \leq P _ i \leq N\ (1\leq i\leq N)
  • i\neq j\implies P _ i\neq P _ j
  • 入力はすべて整数

入力

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

N
P _ 1 P _ 2 \ldots P _ N

出力

D _ i\ (1\leq i\leq N)i の昇順に空白区切りで出力せよ。


入力例 1

4
3 2 4 1

出力例 1

2 2 3 3 

たとえば、i=1 について

  • j=2 のとき、\left\lvert P _ i-P _ j\right\rvert=1,\left\lvert i-j\right\rvert=1 です。
  • j=3 のとき、\left\lvert P _ i-P _ j\right\rvert=1,\left\lvert i-j\right\rvert=2 です。
  • j=4 のとき、\left\lvert P _ i-P _ j\right\rvert=2,\left\lvert i-j\right\rvert=3 です。

よって、j=2 のとき \left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert=2 で最小となるので、D _ 1=2 です。


入力例 2

7
1 2 3 4 5 6 7

出力例 2

2 2 2 2 2 2 2 

入力例 3

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

出力例 3

3 3 3 5 3 4 3 3 4 2 2 4 4 4 4 7 

Score : 500 points

Problem Statement

You are given a permutation P=(P _ 1,P _ 2,\ldots,P _ N) of (1,2,\ldots,N).

Find the following value for all i\ (1\leq i\leq N):

  • D _ i=\displaystyle\min_{j\neq i}\left\lparen\left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert\right\rparen.
What is a permutation? A permutation of (1,2,\ldots,N) is a sequence that is obtained by rearranging (1,2,\ldots,N). In other words, a sequence A of length N is a permutation of (1,2,\ldots,N) if and only if each i\ (1\leq i\leq N) occurs in A exactly once.

Constraints

  • 2 \leq N \leq 2\times10^5
  • 1 \leq P _ i \leq N\ (1\leq i\leq N)
  • i\neq j\implies P _ i\neq P _ j
  • All values in the input are integers.

Input

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

N
P _ 1 P _ 2 \ldots P _ N

Output

Print D _ i\ (1\leq i\leq N) in ascending order of i, separated by spaces.


Sample Input 1

4
3 2 4 1

Sample Output 1

2 2 3 3 

For example, for i=1,

  • if j=2, we have \left\lvert P _ i-P _ j\right\rvert=1 and \left\lvert i-j\right\rvert=1;
  • if j=3, we have \left\lvert P _ i-P _ j\right\rvert=1 and \left\lvert i-j\right\rvert=2;
  • if j=4, we have \left\lvert P _ i-P _ j\right\rvert=2 and \left\lvert i-j\right\rvert=3.

Thus, the value is minimum when j=2, where \left\lvert P _ i-P _ j\right\rvert+\left\lvert i-j\right\rvert=2, so D _ 1=2.


Sample Input 2

7
1 2 3 4 5 6 7

Sample Output 2

2 2 2 2 2 2 2 

Sample Input 3

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

Sample Output 3

3 3 3 5 3 4 3 3 4 2 2 4 4 4 4 7