A - Find Multiple

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

配点 : 100

問題文

A 以上 B 以下であるような C の倍数を、1 つ出力してください。

条件を満たす数が存在しない場合は -1 を出力してください。

制約

  • 1 \leq A \leq B \leq 1000
  • 1 \leq C \leq 1000
  • 入力は全て整数

入力

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

A B C

出力

答えを出力せよ。
条件を満たす数が存在しない場合は -1 を出力せよ。


入力例 1

123 456 100

出力例 1

200

300400 も正解です。


入力例 2

630 940 314

出力例 2

-1

Score : 100 points

Problem Statement

Print a number between A and B (inclusive) that is a multiple of C.

If there is no such number, print -1.

Constraints

  • 1 \leq A \leq B \leq 1000
  • 1 \leq C \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the answer.
If there is no number with the desired property, print -1.


Sample Input 1

123 456 100

Sample Output 1

200

300 or 400 would also be accepted.


Sample Input 2

630 940 314

Sample Output 2

-1
B - Potions

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

配点 : 100

問題文

ナオヒロ君はモンスターを飼っています。モンスターの現在の体力は H です。
また、ナオヒロ君は N 種類の傷薬を持っています。傷薬は効き目の弱い順に 1 から N までの番号がついています。
傷薬 n をモンスターに与えると、モンスターの体力が P_n 増加します。ここで、P_1 \lt P_2 \lt \dots \lt P_N が成り立ちます。

ナオヒロ君は傷薬を 1 つモンスターに与えることで、モンスターの体力を X 以上にしたいです。
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力してください。(制約下においてそのような傷薬が存在することが保証されています。)

制約

  • 2 \leq N \leq 100
  • 1 \leq H \lt X \leq 999
  • 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
  • 入力される値はすべて整数

入力

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

N H X
P_1 P_2 \dots P_N

出力

目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力せよ。


入力例 1

3 100 200
50 200 999

出力例 1

2

それぞれの傷薬をモンスターに 1 つ与えたときのモンスターの体力の変化は以下の通りです。

  • 傷薬 1 をモンスターに与えるとモンスターの体力は 100 + 50 = 150 になります。
  • 傷薬 2 をモンスターに与えるとモンスターの体力は 100 + 200 = 300 になります。
  • 傷薬 3 をモンスターに与えるとモンスターの体力は 100 + 999 = 1099 になります。

与えた後に体力が X = 200 以上になっている傷薬は、傷薬 2 と傷薬 3 です。このうち最も効き目の弱い傷薬である傷薬 2 が答えになります。


入力例 2

2 10 21
10 999

出力例 2

2

入力例 3

10 500 999
38 420 490 585 613 614 760 926 945 999

出力例 3

4

Score : 100 points

Problem Statement

Naohiro has a monster. The monster's current health is H.
He also has N kinds of potions, numbered from 1 to N in ascending order of effectiveness.
If you give the monster potion n, its health will increase by P_n. Here, P_1 \lt P_2 \lt \dots \lt P_N.

He wants to increase the monster's health to X or above by giving it one of the potions.
Print the number of the least effective potion that can achieve the purpose. (The constraints guarantee that such a potion exists.)

Constraints

  • 2 \leq N \leq 100
  • 1 \leq H \lt X \leq 999
  • 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
  • All input values are integers.

Input

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

N H X
P_1 P_2 \dots P_N

Output

Print the number of the least effective potion that can achieve the purpose.


Sample Input 1

3 100 200
50 200 999

Sample Output 1

2

Below is the change in the monster's health when one of the potions is given to the monster.

  • If potion 1 is given, the monster's health becomes 100 + 50 = 150.
  • If potion 2 is given, the monster's health becomes 100 + 200 = 300.
  • If potion 3 is given, the monster's health becomes 100 + 999 = 1099.

The potions that increase the monster's health to at least X = 200 are potions 2 and 3. The answer is the least effective of them, which is potion 2.


Sample Input 2

2 10 21
10 999

Sample Output 2

2

Sample Input 3

10 500 999
38 420 490 585 613 614 760 926 945 999

Sample Output 3

4
C - Minimize Abs 1

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

配点 : 200

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) 及び整数 L,R が与えられます。ここで L,RL\leq R を満たします。

i=1,2,\ldots,N について以下の 2 つの条件を共に満たす整数 X_i を求めてください。なお、求める整数は常に一意に定まります。

  • L\leq X_i \leq R
  • L 以上 R 以下であるようなどの整数 Y についても |X_i - A_i| \leq |Y-A_i| を満たす

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq L\leq R \leq 10^9
  • 1\leq A_i\leq 10^9
  • 入力は全て整数

入力

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

N L R
A_1 \ldots A_N

出力

i=1,2,\ldots,N について X_i を空白区切りで出力せよ。


入力例 1

5 4 7
3 1 4 9 7

出力例 1

4 4 4 7 7

i=1 では、

  • |4-3|=1
  • |5-3|=2
  • |6-3|=3
  • |7-3|=4

より X_i = 4 です。


入力例 2

3 10 10
11 10 9

出力例 2

10 10 10

Score : 200 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N and integers L and R such that L\leq R.

For each i=1,2,\ldots,N, find the integer X_i that satisfies both of the following conditions. Note that the integer to be found is always uniquely determined.

  • L\leq X_i \leq R.
  • For every integer Y such that L \leq Y \leq R, it holds that |X_i - A_i| \leq |Y - A_i|.

Constraints

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

Input

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

N L R
A_1 \ldots A_N

Output

Print X_i for i=1,2,\ldots,N, separated by spaces.


Sample Input 1

5 4 7
3 1 4 9 7

Sample Output 1

4 4 4 7 7

For i=1:

  • |4-3|=1
  • |5-3|=2
  • |6-3|=3
  • |7-3|=4

Thus, X_i = 4.


Sample Input 2

3 10 10
11 10 9

Sample Output 2

10 10 10
D - 326-like Numbers

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

配点 : 200

問題文

3 桁の正整数であって、百の位の数と十の位の数の積が一の位の数と等しいものを 326-like number と呼びます。

例えば 326,400,144 は 326-like number であり、623,777,429 は 326-like number ではありません。

整数 N が与えられるので、N 以上の最小の 326-like number を求めてください。なお、制約の条件下で答えは必ず存在します。

制約

  • 100 \leq N \leq 919
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

320

出力例 1

326

320,321,322,323,324,325 は 326-like number ではなく、326 は 326-like number です。


入力例 2

144

出力例 2

144

144 は 326-like number です。


入力例 3

516

出力例 3

600

Score : 200 points

Problem Statement

A 326-like number is a three-digit positive integer where the product of the hundreds and tens digits equals the ones digit.

For example, 326,400,144 are 326-like numbers, while 623,777,429 are not.

Given an integer N, find the smallest 326-like number greater than or equal to N. It always exists under the constraints.

Constraints

  • 100 \leq N \leq 919
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

320

Sample Output 1

326

320,321,322,323,324,325 are not 326-like numbers, while 326 is a 326-like number.


Sample Input 2

144

Sample Output 2

144

144 is a 326-like number.


Sample Input 3

516

Sample Output 3

600
E - abc285_brutmhyhiizp

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

配点 : 300

問題文

別世界の AtCoder で開催されている AtCoder Big Contest では、 10^{16} 問の問題が一度に出題されます。
問題の ID は 1 問目から順に A, B, ..., Z, AA, AB, ..., ZZ, AAA, ... と付けられています。

つまり、 ID は以下の順番で付けられています。

  • 長さ 1 の英大文字からなる文字列を辞書順に並べたもの
  • 長さ 2 の英大文字からなる文字列を辞書順に並べたもの
  • 長さ 3 の英大文字からなる文字列を辞書順に並べたもの
  • ...

このコンテストに含まれる問題の ID である文字列 S が与えられるので、それが何問目か答えてください。

制約

  • S は AtCoder Big Contest に含まれる問題の ID として正しい

入力

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

S

出力

答えを整数として出力せよ。


入力例 1

AB

出力例 1

28

ID が AB である問題は、 AtCoder Big Contest の 28 問目です。


入力例 2

C

出力例 2

3

ID が C である問題は、 AtCoder Big Contest の 3 問目です。


入力例 3

BRUTMHYHIIZP

出力例 3

10000000000000000

ID が BRUTMHYHIIZP である問題は、 AtCoder Big Contest の 10^{16} 問目、すなわち最終問題です。

Score : 300 points

Problem Statement

In a parallel universe, AtCoder holds AtCoder Big Contest, where 10^{16} problems are given at once.
The IDs of the problems are as follows, from the 1-st problem in order: A, B, ..., Z, AA, AB, ..., ZZ, AAA, ...

In other words, the IDs are given in the following order:

  • the strings of length 1 consisting of uppercase English letters, in lexicographical order;
  • the strings of length 2 consisting of uppercase English letters, in lexicographical order;
  • the strings of length 3 consisting of uppercase English letters, in lexicographical order;
  • ...

Given a string S that is an ID of a problem given in this contest, find the index of the problem. (See also Samples.)

Constraints

  • S is a valid ID of a problem given in AtCoder Big Contest.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

AB

Sample Output 1

28

The problem whose ID is AB is the 28-th problem of AtCoder Big Contest, so 28 should be printed.


Sample Input 2

C

Sample Output 2

3

The problem whose ID is C is the 3-rd problem of AtCoder Big Contest, so 3 should be printed.


Sample Input 3

BRUTMHYHIIZP

Sample Output 3

10000000000000000

The problem whose ID is BRUTMHYHIIZP is the 10^{16}-th (last) problem of AtCoder Big Contest, so 10^{16} should be printed as an integer.

F - Large Queue

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

配点 : 300

問題文

空の整数列 A=() があります。クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 2 種類です。

  • タイプ 1 : 1 c x の形式で与えられる。A の末尾に xc 個追加する。
  • タイプ 2 : 2 k の形式で与えられる。A の先頭 k 要素を削除し、削除した k 個の整数の総和を出力する。このとき、k はその時点での A の長さ以下であることが保証される。

制約

  • 1 \leq Q \leq 2 \times 10^{5}
  • タイプ 1 のクエリにおいて、 1 \leq c \leq 10^{9}
  • タイプ 1 のクエリにおいて、 1 \leq x \leq 10^{9}
  • タイプ 2 のクエリにおいて、その時点での A の長さを n として、 1 \leq k \leq \min(10^{9},n)
  • 入力はすべて整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、 \text{query}_ii 個目のクエリを表し、以下のいずれかの形式である。

1 c x
2 k

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 2 のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

11
19
  • 1 個目のクエリ: A の末尾に 32 個追加する。このとき、A=(3,3) となる。
  • 2 個目のクエリ: A の末尾に 54 個追加する。このとき、A=(3,3,5,5,5,5) となる。
  • 3 個目のクエリ: A の先頭 3 要素を削除する。このとき、削除した 3 個の整数の総和は 3+3+5=11 となるため 11 と出力する。削除後、A=(5,5,5) となる。
  • 4 個目のクエリ: A の末尾に 26 個追加する。このとき、A=(5,5,5,2,2,2,2,2,2) となる。
  • 5 個目のクエリ: A の先頭 5 要素を削除する。このとき、削除した 5 個の整数の総和は 5+5+5+2+2=19 となるため 19 と出力する。削除後、A=(2,2,2,2) となる。

入力例 2

10
1 75 22
1 81 72
1 2 97
1 84 82
1 2 32
1 39 57
2 45
1 40 16
2 32
2 42

出力例 2

990
804
3024

入力例 3

10
1 160449218 954291757
2 17217760
1 353195922 501899080
1 350034067 910748511
1 824284691 470338674
2 180999835
1 131381221 677959980
1 346948152 208032501
1 893229302 506147731
2 298309896

出力例 3

16430766442004320
155640513381884866
149721462357295680

Score : 300 points

Problem Statement

There is an empty integer sequence A=(). You are given Q queries, and you need to process them in the given order. There are two types of queries:

  • Type 1: Given in the format 1 c x. Add c copies of x to the end of A.
  • Type 2: Given in the format 2 k. Remove the first k elements from A and output the sum of the removed k integers. It is guaranteed that k is at most the length of A at that time.

Constraints

  • 1 \leq Q \leq 2 \times 10^{5}
  • In type 1 queries, 1 \leq c \leq 10^{9}.
  • In type 1 queries, 1 \leq x \leq 10^{9}.
  • In type 2 queries, letting n be the length of A at that time, 1 \leq k \leq \min(10^{9},n).
  • All input values are integers.

Input

The input is given from standard input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

where \text{query}_i represents the i-th query and is in one of the following formats:

1 c x
2 k

Output

Let q be the number of type 2 queries. Output q lines. The i-th line should contain the answer to the i-th type 2 query.


Sample Input 1

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

Sample Output 1

11
19
  • 1st query: Add 2 copies of 3 to the end of A. Then, A=(3,3).
  • 2nd query: Add 4 copies of 5 to the end of A. Then, A=(3,3,5,5,5,5).
  • 3rd query: Remove the first 3 elements from A. Then, the sum of the removed 3 integers is 3+3+5=11, so output 11. After removal, A=(5,5,5).
  • 4th query: Add 6 copies of 2 to the end of A. Then, A=(5,5,5,2,2,2,2,2,2).
  • 5th query: Remove the first 5 elements from A. Then, the sum of the removed 5 integers is 5+5+5+2+2=19, so output 19. After removal, A=(2,2,2,2).

Sample Input 2

10
1 75 22
1 81 72
1 2 97
1 84 82
1 2 32
1 39 57
2 45
1 40 16
2 32
2 42

Sample Output 2

990
804
3024

Sample Input 3

10
1 160449218 954291757
2 17217760
1 353195922 501899080
1 350034067 910748511
1 824284691 470338674
2 180999835
1 131381221 677959980
1 346948152 208032501
1 893229302 506147731
2 298309896

Sample Output 3

16430766442004320
155640513381884866
149721462357295680
G - Takahashi the Wall Breaker

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

配点 : 400

問題文

高橋君は魚屋にウナギを買いに行こうとしています。

高橋君の住む町は縦 H マス横 W マスからなるグリッド状の区画に分かれており、 各区画は道か壁かのいずれかです。
以下、上から i 番目 (1\leq i \leq H) かつ左から j 番目 (1\leq j\leq W) の区画を区画 (i,j) で表します。
各区画の情報は H 個の長さ W の文字列 S_1,S_2,\ldots,S_H で与えられ、 具体的には S_ij 文字目 (1\leq i \leq H,1\leq j\leq W). のとき区画 (i,j) が道であることを、 S_ij 文字目が # のとき区画 (i,j) が壁であることを表します。

高橋君は次の 2 種類の行動を好きな順番で繰り返し行うことができます。

  • 上下左右に隣接する、町の中の区画であって、道であるようなものに移動する。
  • 上下左右の方向を一つ決め、その方向に前蹴りを行う。
    高橋君が前蹴りを行うと、現在いる区画からその方向に 1 つ前の区画および 2 つ前の区画について、その区画が壁ならば道に変えることができる。
    ここで、1 つ前の区画または 2 つ前の区画が町の外である場合でも前蹴りを行うことができるが、町の外が変化することはない。

高橋君は最初、区画 (A,B) におり、区画 (C,D) にある魚屋まで移動したいです。
ここで、高橋君の最初にいる区画および魚屋のある区画は道であることが保証されます。
高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を求めてください。

制約

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • S_i., # のみからなる長さ W の文字列
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H,W,A,B,C,D は整数
  • 高橋君の最初にいる区画および魚屋のある区画は道である。

入力

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

H W
S_1
S_2
\vdots
S_H
A B C D

出力

高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を出力せよ。


入力例 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

出力例 1

1

高橋君は最初、区画 (1,1) にいます。
道である区画への移動を繰り返して区画 (7,4) まで移動することができます。
区画 (7,4) において左方向に対して前蹴りを行うと区画 (7,3) と区画 (7,2) が壁から道に変わります。
その後、道である区画(道に変わった区画を含む)への移動を繰り返して、区画 (7,1) にある魚屋に移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 2

2 2
.#
#.
1 1 2 2

出力例 2

1

高橋君は最初、区画 (1,1) にいます。
右方向に対して前蹴りを行うと、区画 (1,2) を壁から道に変えることができます。
区画 (1,1) から右方向に 2 つ前のマスは町の外であるため、変化しません。
その後、高橋君は区画 (1,2) へ移動し、区画 (2,2) にある魚屋へ移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 3

1 3
.#.
1 1 1 3

出力例 3

1

前蹴りを行う際、それにより道に変えられ得る区画の中に魚屋のある区画が含まれていても問題ありません。 具体的には魚屋のある区画はもともと道であるため変化せず、特に前蹴りによって魚屋が壊れることはありません。


入力例 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

出力例 4

3

Score : 400 points

Problem Statement

Takahashi is about to go buy eel at a fish shop.

The town where he lives is divided into a grid of H rows and W columns. Each cell is either a road or a wall.
Let us denote the cell at the i-th row from the top (1\leq i \leq H) and the j-th column from the left (1\leq j \leq W) as cell (i,j).
Information about each cell is given by H strings S_1,S_2,\ldots,S_H, each of length W. Specifically, if the j-th character of S_i (1\leq i \leq H,1\leq j\leq W) is ., cell (i,j) is a road; if it is #, cell (i,j) is a wall.

He can repeatedly perform the following two types of actions in any order:

  • Move to an adjacent cell (up, down, left, or right) that is within the town and is a road.
  • Choose one of the four directions (up, down, left, or right) and perform a front kick in that direction.
    When he performs a front kick, for each of the cells at most 2 steps away in that direction from the cell he is currently in, if that cell is a wall, it becomes a road.
    If some of the cells at most 2 steps away are outside the town, a front kick can still be performed, but anything outside the town does not change.

He starts in cell (A,B), and he wants to move to the fish shop in cell (C,D).
It is guaranteed that both the cell where he starts and the cell with the fish shop are roads.
Find the minimum number of front kicks he needs in order to reach the fish shop.

Constraints

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • Each S_i is a string of length W consisting of . and #.
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H, W, A, B, C, and D are integers.
  • The cell where Takahashi starts and the cell with the fish shop are roads.

Input

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

H W
S_1
S_2
\vdots
S_H
A B C D

Output

Print the minimum number of front kicks needed for Takahashi to reach the fish shop.


Sample Input 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

Sample Output 1

1

Takahashi starts in cell (1,1).
By repeatedly moving to adjacent road cells, he can reach cell (7,4).
If he performs a front kick to the left from cell (7,4), cells (7,3) and (7,2) turn from walls to roads.
Then, by continuing to move through road cells (including those that have become roads), he can reach the fish shop in cell (7,1).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 2

2 2
.#
#.
1 1 2 2

Sample Output 2

1

Takahashi starts in cell (1,1).
When he performs a front kick to the right, cell (1,2) turns from a wall to a road.
The cell two steps to the right of (1,1) is outside the town, so it does not change.
Then, he can move to cell (1,2) and then to the fish shop in cell (2,2).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 3

1 3
.#.
1 1 1 3

Sample Output 3

1

When performing a front kick, it is fine if the fish shop’s cell is within the cells that could be turned into a road. Specifically, the fish shop’s cell is a road from the beginning, so it remains unchanged; particularly, the shop is not destroyed by the front kick.


Sample Input 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

Sample Output 4

3
H - Last Rook

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

配点 : 500

問題文

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。

縦横 N マスのチェス盤と N 個のルークの駒があります。以下では上から i 行目、左から j 列目のマスを (i, j) と表します。
チェス盤のマスにルークを置くことを考えます。ただし、あなたは次の条件をすべて満たすようにルークをチェス盤に置く必要があります。

  • 1 つの行に 2 個以上のルークが存在しない。
  • 1 つの列に 2 個以上のルークが存在しない。

今、チェス盤に N-1 個のルークが上の条件をすべて満たした状態で置かれています。あなたはルークが置かれていないマスを 1 つ選び、そのマスにルークを置くことにしました。(上の条件をすべて満たすようにルークを置くことができるマスは少なくとも 1 つ以上存在することが証明できます。)

ただし、あなたはチェス盤のどのマスにルークが置かれているかを直接知ることはできません。
そのかわりに、ジャッジシステムに対して以下の質問を 20 回まで行うことができます。

  • 整数 A, B, C, D1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N を満たすように選ぶ。そして、A \leq i \leq B, C \leq j \leq D を満たすマス (i, j) からなる長方形領域に置かれているルークの個数を聞く。

ルークを置くことができるマスを見つけてください。

制約

  • 2 \leq N \leq 10^3
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。

最初に、チェス盤のサイズ N を標準入力から受け取ってください。

N

次に、ルークを置くことができるマスが見つかるまで質問を繰り返してください。
質問は、以下の形式で標準出力に出力してください。

? A B C D

これに対する応答は、次の形式で標準入力から与えられます。

T

ここで、T は質問に対する答えです。ただし、不正な質問を行った、あるいは質問の回数が 20 回を超えた場合は T-1 となります。

ジャッジが -1 を返した場合、提出はすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。

ルークを置くことができるマスを見つけたら、そのマスを (X, Y) として、解答を以下の形式で出力してください。その後、ただちにプログラムを終了してください。

! X Y

答えが複数ある場合、どれを出力しても正解とみなされます。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。

入出力例

以下は、N=3(1, 2), (2, 1) にルークが置かれている場合の入出力例です。

入力 出力 説明
3 まず整数 N が与えられます。
? 1 2 1 3 (A,B,C,D)=(1,2,1,3) として質問を行います。
2 質問の答えは 2 なので、ジャッジはその値を返します。
? 2 3 1 1 (A,B,C,D)=(2,3,1,1) として質問を行います。
1 質問の答えは 1 なので、ジャッジはその値を返します。
? 1 3 3 3 (A,B,C,D)=(1,3,3,3) として質問を行います。
0 質問の答えは 0 なので、ジャッジはその値を返します。
! 3 3 答えは (3, 3) だとわかったので、それを出力します。

Score : 500 points

Problem Statement

This is an interactive task (where your program interacts with the judge's program via input and output).

We have an N-by-N chessboard and N rooks. Below, the square at the i-th row from the top and j-th column from the left is denoted by (i, j).
Consider placing the rooks on squares of the chessboard. Here, you have to place the rooks so that all of the following conditions are satisfied.

  • No row contains two or more rooks.
  • No column contains two or more rooks.

Now, N-1 rooks are placed on the chessboard so that all of the above conditions are satisfied. You will choose a square that is not occupied by a rook and place a rook on that square. (It can be proved that there is at least one square on which a rook can be placed under the conditions.)

However, you cannot directly see which squares of the chessboard are occupied by a rook.
Instead, you may ask at most 20 questions to the judge in the following manner.

  • You choose integers A, B, C, and D such that 1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N, and ask the number of rooks in the rectangular region formed by the squares (i, j) such that A \leq i \leq B, C \leq j \leq D.

Find a square to place a rook.

Constraints

  • 2 \leq N \leq 10^3
  • N is an integer.

Input and Output

This is an interactive task (where your program interacts with the judge's program via input and output).

First, receive the size of the chessboard, N, from Standard Input.

N

Next, repeat asking a question until you find a square to place a rook.
A question should be printed to Standard Output in the following format:

? A B C D

The response will be given from Standard Input in the following format:

T

Here, T is the response to the question, or -1 if the question is invalid or more than 20 questions have been asked.

When the judge returns -1, the submission is already regarded as incorrect. In this case, terminate the program immediately.

When you find a square to place a rook, let (X, Y) be that square and print an answer in the following format. Then, terminate the program immediately.

! X Y

If there are multiple appropriate answers, any of them will be accepted.

Notes

  • Each time you print something, end it with a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, the verdict will be indeterminate.
  • Terminate the program immediately after printing an answer. Otherwise, the verdict will be indeterminate.

Sample Interaction

Below is an interaction where N=3 and the rooks are placed on (1, 2) and (2, 1).

Input Output Description
3 Judge first gives the integer N.
? 1 2 1 3 Participant asks a question with (A,B,C,D)=(1,2,1,3).
2 Judge returns the answer to the question, which is 2.
? 2 3 1 1 Participant asks a question with (A,B,C,D)=(2,3,1,1).
1 Judge returns the answer to the question, which is 1.
? 1 3 3 3 Participant asks a question with (A,B,C,D)=(1,3,3,3).
0 Judge returns the answer to the question, which is 0.
! 3 3 Participant finds the answer to be (3, 3) and prints it.
I - Hammer 2

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

配点 : 500

問題文

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

また、数直線上に N 枚の壁と N 本のハンマーがあります。

  • 座標 Y_1,Y_2,\dots,Y_N にはそれぞれタイプ 1,2,\dots,N の壁があります。
    • 最初、高橋君は壁を超えて移動することができません。
  • 座標 Z_1,Z_2,\dots,Z_N にはそれぞれタイプ 1,2,\dots,N のハンマーがあります。
    • 高橋君はハンマーのある座標に着くとそこにあるハンマーを手に入れます。
    • タイプ i のハンマーはタイプ i の壁を破壊するための専用のもので、タイプ i のハンマーを手に入れた後でなら、タイプ i の壁を破壊して通過できるようになります。

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

制約

  • 入力は全て整数
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • 合計 2 \times N + 1 個の座標 X,Y_i,Z_i は相異なる

入力

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

出力

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


入力例 1

3 10
-2 8 -5
5 -10 3

出力例 1

40

以下の手順により、移動距離 40 で高橋くんがゴールに到達でき、これが移動距離の最小です。

  • 座標 0 から高橋君が行動を開始する。
  • 座標 3 に行く。タイプ 3 のハンマーを手に入れる。
  • 座標 5 に行く。タイプ 1 のハンマーを手に入れる。
  • 座標 -2 に行く。タイプ 1 の壁を破壊する。
  • 座標 -5 に行く。タイプ 3 の壁を破壊する。
  • 座標 -10 に行く。タイプ 2 のハンマーを手に入れる。
  • 座標 8 に行く。タイプ 2 の壁を破壊する。
  • 座標 10 に行く。ここがゴールである。

入力例 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

出力例 2

1

ゴールに移動するために、ハンマーを手に入れる必要も壁を破壊する必要もない場合もあります。


入力例 3

1 100
30
60

出力例 3

-1

高橋君がタイプ 1 のハンマーを手に入れることは不可能であり、ゴールに辿り着くこともできません。


入力例 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

出力例 4

4078987507

Score : 500 points

Problem Statement

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

Also, there are N walls and N hammers on the number line.

  • At coordinates Y_1,Y_2,\dots,Y_N are walls of types 1,2,\dots,N, respectively.
    • Initially, Takahashi cannot get over the walls.
  • At coordinates Z_1,Z_2,\dots,Z_N are hammers of types 1,2,\dots,N, respectively.
    • When he arrives at a coordinate with a hammer, he obtains the hammer.
    • The hammer of type i is dedicated to destroying the wall of type i. After he obtains the hammer of type i, he can destroy the wall of type i and get over it.

Determine if he can reach the goal. If he can, find the minimum distance he travels.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • The (2 \times N + 1) coordinates X,Y_i and Z_i are distinct.

Input

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

Output

If Takahashi can reach the goal, print the minimum possible distance he travels as an integer.
Otherwise, print -1.


Sample Input 1

3 10
-2 8 -5
5 -10 3

Sample Output 1

40

Takahashi can reach the goal by traveling a distance of 40 as follows, which is the minimum possible:

  • He starts at coordinate 0.
  • He moves to coordinate 3 to obtain the hammer of type 3.
  • He moves to coordinate 5 to obtain the hammer of type 1.
  • He moves to coordinate -2 to destroy the wall of type 1.
  • He moves to coordinate -5 to destroy the wall of type 3.
  • He moves to coordinate -10 to obtain the hammer of type 2.
  • He moves to coordinate 8 to destroy the wall of type 2.
  • He moves to coordinate 10, which is the goal.

Sample Input 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

Sample Output 2

1

It may not be required that he obtains a hammer or destroys a wall to reach the goal.


Sample Input 3

1 100
30
60

Sample Output 3

-1

Takahashi cannot obtain the hammer of type 1, and neither can he reach the goal.


Sample Input 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

Sample Output 4

4078987507