A - Probably English

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英小文字からなる N 個の文字列 W_1,W_2,\dots,W_N が与えられます。
これらのうち一つ以上が and, not, that, the, you のいずれかと一致するなら Yes 、そうでないなら No と出力してください。

制約

  • N は 1 以上 100 以下の整数
  • 1 \le |W_i| \le 50 ( |W_i| は文字列 W_i の長さ )
  • W_i は英小文字からなる

入力

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

N
W_1 W_2 \dots W_N

出力

答えを出力せよ。


入力例 1

10
in that case you should print yes and not no

出力例 1

Yes

例えば W_4= you なので、 Yes と出力します。


入力例 2

10
in diesem fall sollten sie no und nicht yes ausgeben

出力例 2

No

文字列 W_i はいずれも、 and, not, that, the, you のいずれとも一致しません。

Score : 100 points

Problem Statement

You are given N strings W_1,W_2,\dots,W_N consisting of lowercase English letters.
If one or more of these strings equal and, not, that, the, or you, then print Yes; otherwise, print No.

Constraints

  • N is an integer between 1 and 100, inclusive.
  • 1 \le |W_i| \le 50 (|W_i| is the length of W_i.)
  • W_i consists of lowercase English letters.

Input

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

N
W_1 W_2 \dots W_N

Output

Print the answer.


Sample Input 1

10
in that case you should print yes and not no

Sample Output 1

Yes

We have, for instance, W_4= you, so you should print Yes.


Sample Input 2

10
in diesem fall sollten sie no und nicht yes ausgeben

Sample Output 2

No

None of the strings W_i equals any of and, not, that, the, and you.

B - chukodai

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英小文字からなる文字列 S が与えられます。

S の先頭から a 文字目と b 文字目を入れ替えて得られる文字列を出力してください。

制約

  • S は英小文字からなる文字列
  • S の長さ |S| は、 2 \leq |S| \leq 10 を満たす
  • 1 \leq a < b \leq |S|
  • a, b は整数

入力

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

S
a b

出力

答えを出力せよ。


入力例 1

chokudai
3 5

出力例 1

chukodai

chokudai の 3 文字目 o と 5 文字目 u を入れ替えると chukodai となります。


入力例 2

aa
1 2

出力例 2

aa

この入力例では、S の 1 文字目と 2 文字目を入れ替えて得られる文字列は、元の S と同じになります。


入力例 3

aaaabbbb
1 8

出力例 3

baaabbba

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters.

Swap the a-th and b-th characters from the beginning of S and print the resulting string.

Constraints

  • S is a string consisting of lowercase English letters.
  • The length of S, |S|, satisfies 2 \leq |S| \leq 10.
  • 1 \leq a < b \leq |S|
  • a and b are integers.

Input

Input is given from Standard Input in the following format:

S
a b

Output

Print the answer.


Sample Input 1

chokudai
3 5

Sample Output 1

chukodai

After swapping the 3-rd character o and 5-th character u of chokudai, we have chukodai.


Sample Input 2

aa
1 2

Sample Output 2

aa

In this sample, after swapping the 1-st and 2-nd characters of S, we have the same string as S.


Sample Input 3

aaaabbbb
1 8

Sample Output 3

baaabbba
C - Pasta

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君の家には N 本の麺からなるパスタがあり、i 本目の麺の長さは A_i です。
高橋君はこれから M 日間の食事計画を立てており、 i 日目にはパスタの麺のうち長さがちょうど B_i であるようなものを 1 本選び、食べようと考えています。 もし、1 日目から M 日目の間に 1 日でもそのような麺が無い日があれば、食事計画は失敗となります。 また、同じ麺を複数の日に食べることはできません。

高橋君が食事計画を最後まで実行することは可能ですか?

制約

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

高橋君が食事計画を最後まで実行できる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 2
1 1 3
3 1

出力例 1

Yes

1 日目に 3 本目の麺を、2 日目に 1 本目の麺を食べれば良いので、高橋君の食事計画は実行可能です。


入力例 2

1 1
1000000000
1

出力例 2

No

長さがちょうど 1 の麺が存在する必要があります。


入力例 3

5 2
1 2 3 4 5
5 5

出力例 3

No

長さが 5 の麺は 1 本しか存在しないため、2 日目に食事をとる事が出来ません。

Score : 200 points

Problem Statement

There is pasta consisting of N noodles at Takahashi's home. The length of the i-th noodle is A_i.
Takahashi has a meal plan for the next M days. On the i-th day, he is going to choose a pasta noodle of length exactly B_i and eat it. If no such noodle is available on any day, his plan fails. Additionally, he cannot eat the same noodle on multiple days.

Can Takahashi accomplish his meal plan?

Constraints

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If Takahashi can accomplish his meal plan, print Yes; otherwise, print No.


Sample Input 1

3 2
1 1 3
3 1

Sample Output 1

Yes

He can eat the 3-rd noodle on the 1-st day and the 1-st noodle on the 2-nd day, so his meal plan is feasible.


Sample Input 2

1 1
1000000000
1

Sample Output 2

No

A noodle of length exactly 1 is needed.


Sample Input 3

5 2
1 2 3 4 5
5 5

Sample Output 3

No

Since there are only 1 noodle of length 5, he cannot have a meal on the 2-nd day.

D - 1122 String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150 点

問題文

文字列 T が以下の 3 つの条件をすべてみたすとき、かつそのときに限り、T を 1122 文字列 と呼びます。

  • \lvert T \rvert は偶数である。ここで、\lvert T \rvert は T の長さを表す。
  • 1\leq i\leq \frac{\lvert T \rvert}{2} をみたす整数 i について、T の (2i-1) 文字目と 2i 文字目は等しい。
  • 各文字は T にちょうど 0 個または 2 個現れる。すなわち、T に含まれる文字は T にちょうど 2 回ずつ登場する。

英小文字のみからなる文字列 S が与えられるので、S が 1122 文字列であるならば Yes を、そうでないならば No を出力してください。

制約

  • S は英小文字のみからなる長さ 1 以上 100 以下の文字列

入力

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

S

出力

S が 1122 文字列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

aabbcc

出力例 1

Yes

S=aabbcc は 1122 文字列の条件をすべてみたしているため、Yes を出力します。


入力例 2

aab

出力例 2

No

S=aab は長さが奇数であり、 1 つめの条件をみたしていないため、No を出力します。


入力例 3

zzzzzz

出力例 3

No

S=zzzzzz は z が 6 個含まれており、 3 つめの条件をみたしていないため、No を出力します。

Score : 150 points

Problem Statement

A string T is called a 1122 string if and only if it satisfies all of the following three conditions:

  • \lvert T \rvert is even. Here, \lvert T \rvert denotes the length of T.
  • For each integer i satisfying 1\leq i\leq \frac{|T|}{2}, the (2i-1)-th and 2i-th characters of T are equal.
  • Each character appears in T exactly zero or two times. That is, every character contained in T appears exactly twice in T.

Given a string S consisting of lowercase English letters, print Yes if S is a 1122 string, and No otherwise.

Constraints

  • S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

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

S

Output

If S is a 1122 string, print Yes; otherwise, print No.


Sample Input 1

aabbcc

Sample Output 1

Yes

S=aabbcc satisfies all the conditions for a 1122 string, so print Yes.


Sample Input 2

aab

Sample Output 2

No

S=aab has an odd length and does not satisfy the first condition, so print No.


Sample Input 3

zzzzzz

Sample Output 3

No

S=zzzzzz contains six zs and does not satisfy the third condition, so print No.


E - Cards Query Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \colon 箱 i に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

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

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 1 を 2 度出力することに注意してください。
  • 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
  • 数 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

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

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
F - Black Intervals

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

左右一列に N 個のマスが並んでいます。 最初、すべてのマスは白く塗られています。

Q 個のクエリを順に処理してください。i 個目のクエリでは 1 以上 N 以下の整数 A_i が与えられ、次の操作を行います。

左から A_i 番目のマスの色を反転させる。 具体的には、左から A_i 番目のマスが、白く塗られていたならば黒く塗り、黒く塗られていたならば白く塗る。
その後、黒く塗られたマスが連続している区間の個数を求める。

ここで、黒く塗られたマスが連続している区間とは次をすべてみたす整数の組 (l,r) (1\leq l\leq r\leq N) を指す。

  • 左から l, l+1, \ldots, r 番目のマスはすべて黒く塗られている。
  • l=1 であるか、または左から (l-1) 番目のマスは白く塗られている。
  • r=N であるか、または左から (r+1) 番目のマスは白く塗られている。

制約

  • 1\leq N,Q\leq 5\times 10^5
  • 1\leq A_i\leq N
  • 入力はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_Q

出力

Q 行出力せよ。i 行目 (1\leq i\leq Q) には i 個目のクエリの答えを出力せよ。


入力例 1

5 7
2 3 3 5 1 5 2

出力例 1

1
1
1
2
2
1
1

以下では、左から i 番目のマスを マス i と表します。
各クエリの後は次のような状態になっています。

  • 1 個目のクエリの後、マス 2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2) の 1 つです。
  • 2 個目のクエリの後、マス 2,3 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,3) の 1 つです。
  • 3 個目のクエリの後、マス 2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2) の 1 つです。
  • 4 個目のクエリの後、マス 2,5 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2), (5,5) の 2 つです。
  • 5 個目のクエリの後、マス 1,2,5 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,2), (5,5) の 2 つです。
  • 6 個目のクエリの後、マス 1,2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,2) の 1 つです。
  • 7 個目のクエリの後、マス 1 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,1) の 1 つです。

よって、1,1,1,2,2,1,1 を改行区切りで出力します。


入力例 2

1 2
1 1

出力例 2

1
0

2 個目のクエリの後、すべてのマスは白く塗られているため、2 行目には 0 を出力します。


入力例 3

3 3
1 3 2

出力例 3

1
2
1

Score : 350 points

Problem Statement

There are N squares arranged in a row from left to right. Initially, all squares are painted white.

Process Q queries in order. The i-th query gives an integer A_i between 1 and N, inclusive, and performs the following operation:

Flip the color of the A_i-th square from the left. Specifically, if the A_i-th square from the left is painted white, paint it black; if it is painted black, paint it white.
Then, find the number of intervals of consecutively painted black squares.

Here, an interval of consecutively painted black squares is a pair of integers (l,r) (1\leq l\leq r\leq N) that satisfy all of the following:

  • The l-th, (l+1)-th, \ldots, r-th squares from the left are all painted black.
  • Either l=1, or the (l-1)-th square from the left is painted white.
  • Either r=N, or the (r+1)-th square from the left is painted white.

Constraints

  • 1\leq N,Q\leq 5\times 10^5
  • 1\leq A_i\leq N
  • All input values are integers.

Input

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

N Q
A_1 A_2 \ldots A_Q

Output

Output Q lines. On the i-th line (1\leq i\leq Q), output the answer to the i-th query.


Sample Input 1

5 7
2 3 3 5 1 5 2

Sample Output 1

1
1
1
2
2
1
1

Below, the i-th square from the left is referred to as square i.
After each query, the state is as follows:

  • After the 1st query, only square 2 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,2).
  • After the 2nd query, squares 2,3 are painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,3).
  • After the 3rd query, only square 2 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,2).
  • After the 4th query, squares 2,5 are painted black. There are 2 intervals of consecutively painted black squares: (l,r)=(2,2), (5,5).
  • After the 5th query, squares 1,2,5 are painted black. There are 2 intervals of consecutively painted black squares: (l,r)=(1,2), (5,5).
  • After the 6th query, only squares 1,2 are painted black. There is 1 interval of consecutively painted black squares: (l,r)=(1,2).
  • After the 7th query, only square 1 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(1,1).

Thus, output 1,1,1,2,2,1,1 separated by newlines.


Sample Input 2

1 2
1 1

Sample Output 2

1
0

After the 2nd query, all squares are painted white, so output 0 on the 2nd line.


Sample Input 3

3 3
1 3 2

Sample Output 3

1
2
1
G - Relative Position

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

座標平面上に 1 から N の番号がついた N 人の人がいます。
人 1 は原点にいます。

次の形式の情報が M 個与えられます。

  • 人 A_i から見て、人 B_i は、x 軸正方向に X_i、y 軸正方向に Y_i 離れた位置にいる

それぞれの人がいる座標を求めてください。一意に定まらないときはそのことを報告してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1\leq A_i, B_i \leq N
  • A_i \neq B_i
  • -10^9 \leq X_i,Y_i \leq 10^9
  • 入力は全て整数である
  • 与えられる情報は矛盾しない

入力

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

N M
A_1 B_1 X_1 Y_1
\vdots
A_M B_M X_M Y_M

出力

N 行出力せよ。
人 i のいる座標が一意に定まらないとき、i 行目には undecidable と出力せよ。
人 i のいる座標が (s_i,t_i) と一意に定まるとき、i 行目には s_i,t_i をこの順に空白区切りで出力せよ。


入力例 1

3 2
1 2 2 1
1 3 -1 -2

出力例 1

0 0
2 1
-1 -2

3 人の位置関係は下図のようになっています。

図


入力例 2

3 2
2 1 -2 -1
2 3 -3 -3

出力例 2

0 0
2 1
-1 -2

3 人の位置関係は下図のようになっています。

図


入力例 3

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

出力例 3

0 0
0 0
0 0
undecidable
undecidable

同じ情報が複数回与えられたり、同じ座標に複数の人がいることもあります。

Score : 400 points

Problem Statement

There are N people numbered 1 to N on a coordinate plane.
Person 1 is at the origin.

You are given M pieces of information in the following form:

  • From person A_i's perspective, person B_i is X_i units away in the positive x-direction and Y_i units away in the positive y-direction.

Determine the coordinates of each person. If the coordinates of a person cannot be uniquely determined, report that fact.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1\leq A_i, B_i \leq N
  • A_i \neq B_i
  • -10^9 \leq X_i,Y_i \leq 10^9
  • All input values are integers.
  • The given information is consistent.

Input

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

N M
A_1 B_1 X_1 Y_1
\vdots
A_M B_M X_M Y_M

Output

Print N lines.
If the coordinates of person i cannot be uniquely determined, the i-th line should contain undecidable.
If they can be uniquely determined as (s_i,t_i), the i-th line should contain s_i and t_i in this order, separated by a space.


Sample Input 1

3 2
1 2 2 1
1 3 -1 -2

Sample Output 1

0 0
2 1
-1 -2

The figure below shows the positional relationship of the three people.

Figure


Sample Input 2

3 2
2 1 -2 -1
2 3 -3 -3

Sample Output 2

0 0
2 1
-1 -2

The figure below shows the positional relationship of the three people.

Figure


Sample Input 3

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

Sample Output 3

0 0
0 0
0 0
undecidable
undecidable

The same piece of information may be given multiple times, and multiple people may be at the same coordinates.

H - Takahashi and Animals

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

高橋君と N 匹の動物がいます。 N 匹の動物はそれぞれ動物 1 、動物 2 、\ldots 、動物 N と呼ばれます。

高橋君は下記の N 種類の行動をそれぞれ好きな回数だけ( 0 回でも良い)行います。

  • A_1 円払い、動物 1 と動物 2 に餌をあげる。
  • A_2 円払い、動物 2 と動物 3 に餌をあげる。
  • A_3 円払い、動物 3 と動物 4 に餌をあげる。
  • \cdots
  • A_i 円払い、動物 i と動物 (i+1) に餌をあげる。
  • \cdots
  • A_{N-2} 円払い、動物 (N-2) と動物 (N-1) に餌をあげる。
  • A_{N-1} 円払い、動物 (N-1) と動物 N に餌をあげる。
  • A_N 円払い、動物 N と動物 1 に餌をあげる。

上記の N 種類目の行動では、「動物 N と動物 1 に」餌をあげることに注意してください。

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力してください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力せよ。


入力例 1

5
2 5 3 2 5

出力例 1

7

高橋君が 1 種類目、3 種類目、4 種類目の行動をそれぞれ 1 回ずつ行うと、 動物 1 に 1 回、動物 2 に 1 回、動物 3 に 1 回、動物 4 に 2 回、動物 5 に 1 回餌をあげることになり、すべての動物にそれぞれ 1 回以上餌をあげることができます。 このときにかかる費用の合計は A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 円であり、これが考えられる最小値です。


入力例 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

出力例 2

426

Score : 500 points

Problem Statement

Takahashi is with N animals. The N animals are called Animal 1, Animal 2, \ldots, Animal N.

Takahashi will perform the following N kinds of action. Each action can be performed any number of (possibly zero) times.

  • Pay A_1 yen (the currency in Japan) to feed Animals 1 and 2.
  • Pay A_2 yen to feed Animals 2 and 3.
  • Pay A_3 yen to feed Animals 3 and 4.
  • \cdots
  • Pay A_i yen to feed Animals i and (i+1).
  • \cdots
  • Pay A_{N-2} yen to feed Animals (N-2) and (N-1).
  • Pay A_{N-1} yen to feed Animals (N-1) and N.
  • Pay A_N yen to feed Animals N and 1.

Note that the N-th action above feeds "Animals N and 1."

Print the minimum possible total cost to feed every animal at least once.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the minimum possible total cost to feed every animal at least once.


Sample Input 1

5
2 5 3 2 5

Sample Output 1

7

If Takahashi performs the 1-st, 3-rd, and 4-th actions once each, Animals 1, 2, 3, 4, and 5 are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 yen, which is the minimum possible.


Sample Input 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

Sample Output 2

426
I - Push and Carry

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

座標平面上に高橋君と荷物があります。

高橋君は現在 (X_A,Y_A) におり、荷物は (X_B,Y_B) にあります。 高橋君は荷物を (X_C,Y_C) まで運びたいです。

高橋君は (x,y) にいるとき、1 回の行動で次のいずれかの動きをすることができます。

  • (x+1,y) に移動する。移動前の時点で荷物が (x+1,y) にあった時、荷物を (x+2,y) に移動させる。
  • (x-1,y) に移動する。移動前の時点で荷物が (x-1,y) にあった時、荷物を (x-2,y) に移動させる。
  • (x,y+1) に移動する。移動前の時点で荷物が (x,y+1) にあった時、荷物を (x,y+2) に移動させる。
  • (x,y-1) に移動する。移動前の時点で荷物が (x,y-1) にあった時、荷物を (x,y-2) に移動させる。

荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を求めてください。

制約

  • -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
  • (X_A,Y_A)\neq (X_B,Y_B)
  • (X_B,Y_B)\neq (X_C,Y_C)
  • 入力はすべて整数

入力

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

X_A Y_A X_B Y_B X_C Y_C

出力

荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を出力せよ。


入力例 1

1 2 3 3 0 5

出力例 1

9

高橋君は次のように行動することで 9 回で荷物を (0,5) に運ぶことができます。

  • (2,2) へ移動する。
  • (3,2) へ移動する。
  • (3,3) へ移動する。荷物は (3,4) に移動する。
  • (3,4) へ移動する。荷物は (3,5) に移動する。
  • (4,4) へ移動する。
  • (4,5) へ移動する。
  • (3,5) へ移動する。荷物は (2,5) に移動する。
  • (2,5) へ移動する。荷物は (1,5) に移動する。
  • (1,5) へ移動する。荷物は (0,5) に移動する。

8 回以下で荷物を (0,5) に運ぶことができないので、9 を出力します。


入力例 2

0 0 1 0 -1 0

出力例 2

6

入力例 3

-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000

出力例 3

800000000000000003

Score : 525 points

Problem Statement

Takahashi and a cargo are on a coordinate plane.

Takahashi is currently at (X_A,Y_A), and the cargo is at (X_B,Y_B). He wants to move the cargo to (X_C,Y_C).

When he is at (x,y), he can make one of the following moves in a single action.

  • Move to (x+1,y). If the cargo is at (x+1,y) before the move, move it to (x+2,y).
  • Move to (x-1,y). If the cargo is at (x-1,y) before the move, move it to (x-2,y).
  • Move to (x,y+1). If the cargo is at (x,y+1) before the move, move it to (x,y+2).
  • Move to (x,y-1). If the cargo is at (x,y-1) before the move, move it to (x,y-2).

Find the minimum number of actions required to move the cargo to (X_C,Y_C).

Constraints

  • -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
  • (X_A,Y_A)\neq (X_B,Y_B)
  • (X_B,Y_B)\neq (X_C,Y_C)
  • All input values are integers.

Input

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

X_A Y_A X_B Y_B X_C Y_C

Output

Print the minimum number of actions required to move the cargo to (X_C,Y_C).


Sample Input 1

1 2 3 3 0 5

Sample Output 1

9

Takahashi can move the cargo to (0,5) in nine actions as follows.

  • Move to (2,2).
  • Move to (3,2).
  • Move to (3,3). The cargo moves to (3,4).
  • Move to (3,4). The cargo moves to (3,5).
  • Move to (4,4).
  • Move to (4,5).
  • Move to (3,5). The cargo moves to (2,5).
  • Move to (2,5). The cargo moves to (1,5).
  • Move to (1,5). The cargo moves to (0,5).

It is impossible to move the cargo to (0,5) in eight or fewer actions, so you should print 9.


Sample Input 2

0 0 1 0 -1 0

Sample Output 2

6

Sample Input 3

-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000

Sample Output 3

800000000000000003