A - UFO Invasion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

ストーリー

俺はマーフィ。プログラミングコンテストが趣味の MAD HACKER だ。
ある晩、コンテストサイトでいつものように MAD な超絶解答を提出しようとしていたら、街の上空に巨大な UFO が現れた。 突然の事態に世界中は大混乱。楽しみにしていたプロコンも即中止になってしまった。
それから 1 週間、UFO は解読不能のメッセージを発信しながら空中に鎮座している。 俺は、レーティングを上げるせっかくのチャンスが奪われた怒りに震え上がっていた。
あの UFO... 絶対ハックしてやるぜ。
まずは頭を働かせるため、俺はありったけの ZONe を買いに行くことにした。

問題文

長さ 12 の文字列 S が与えられます。S の中に ZONe という文字列は (連続する部分文字列として) いくつ含まれるでしょうか?

制約

  • S は英字からなる長さ 12 の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abcdZONefghi

出力例 1

1

S5 文字目から 8 文字目までが ZONe となっています。


入力例 2

ZONeZONeZONe

出力例 2

3

入力例 3

helloAtZoner

出力例 3

0

S8 文字目から 11 文字目までが Zone となっていますが、これは ZONe ではありません。

Score : 100 points

Problem Statement

You are given a string S of length 12. How many occurrences of ZONes does it contain as contiguous substrings?

Constraints

  • S is a string of length 12 consisting of English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

abcdZONefghi

Sample Output 1

1

The 5-th through 8-th characters of S form a ZONe.


Sample Input 2

ZONeZONeZONe

Sample Output 2

3

Sample Input 3

helloAtZoner

Sample Output 3

0

The 8-th through 11-th characters of S form a Zone, which is not a ZONe.

B - Sign of Friendship

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

ストーリー

まずは友好の印として、UFO の操縦プログラムをクラッシュさせてみよう。俺は机の上の ZONe 缶に偽装した USB から、地球最強のコンピュータウイルス「KARATE」を取り出した。
こいつを UFO に送りつけてやる。UFO がどんなシステムを使っているかは分からないが、あらゆるシステムをクラッシュさせてきた KARATE は、きっと効くはずだ。

問題文

あなたは今、高さ 1000 の非常に高いタワーの下にいます。タワーから距離 D 離れた位置の上空 H の高さに UFO がおり(入出力例 1 の図を参照してください)、あなたは UFO に電波を届けたいです。
タワーと UFO の間には遮蔽物が N 個あります。i 番目の遮蔽物はタワーから UFO の方向に向かって距離 d_i の場所に位置していて、高さは h_i です。
あなたはタワーを上って、あなたと UFO の間の直線上に遮蔽物が 1 つも無い状態にしたいです。上る必要のある最低の高さを求めてください。
なお、地面は凹凸のない水平面であり、タワー及び遮蔽物は地面と垂直に建っているものとします。
また、あなたと UFO の間の直線上にちょうど遮蔽物の上端があるとき、その遮蔽物には遮蔽されていないものとします。

制約

  • 入力は全て整数
  • 1 ≤ N ≤ 100
  • 1 ≤ d_i < D ≤ 1000
  • 1 ≤ h_i < H ≤ 1000

入力

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

N D H
d_1 h_1
d_2 h_2
\vdots
d_N h_N

出力

答えを出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-3} 以下であれば正解と判定される。


入力例 1

1 10 10
3 5

出力例 1

2.857142857142857

タワーを \frac{20}{7} 上ると、図のようにあなたと UFO の間の直線上に遮蔽物が 1 つも無い状態になります。


入力例 2

1 10 10
3 2

出力例 2

0.0

タワーを登らずとも UFO に電波を届けることができます。


入力例 3

5 896 483
228 59
529 310
339 60
78 266
659 391

出力例 3

245.3080684596577

Score : 200 points

Problem Statement

You are now standing under a very high tower, of height 1000. At a position whose distance from the tower is D, there is a UFO up in the sky at height H (see the figure at Sample Input / Output 1), and you want to send it a radio wave.
There are N obstacles between the tower and the UFO. The i-th obstacle stands at the position d_i meters from the tower towards the UFO, and has a height of h_i. You want to climb up the tower so that no obstacle crosses the line between you and the UFO. Find the minimum height you need to climb.
We assume that the ground is a flat plane, and the towers and obstacles stand vertically from the ground.
Additionally, if the top of an obstacle lies exactly on the line connecting you and the UFO, we assume that the obstacle does not cross that line.

Constraints

  • All values in input are integers.
  • 1 ≤ N ≤ 100
  • 1 ≤ d_i < D ≤ 1000
  • 1 ≤ h_i < H ≤ 1000

Input

Input is given from Standard Input in the following format:

N D H
d_1 h_1
d_2 h_2
\vdots
d_N h_N

Output

Print the answer.
Your output will be judged as correct when its absolute or relative error from our answer is at most 10^{-3}.


Sample Input 1

1 10 10
3 5

Sample Output 1

2.857142857142857

If you climb the tower to the height of \frac{20}{7}, no obstacle will cross the line between you and the UFO, as shown below.


Sample Input 2

1 10 10
3 2

Sample Output 2

0.0

You can send a radio wave to the UFO without climbing up the tower.


Sample Input 3

5 896 483
228 59
529 310
339 60
78 266
659 391

Sample Output 3

245.3080684596577
C - MAD TEAM

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

ストーリー

さて、本格的に UFO と対峙する仲間を集めることにしよう。それも、とびきり MAD で優秀な。
俺は数多の天才たちと競い合ってきた「AtCoder」上でメンバーを集めることにした。
名の知れたプログラマに片っ端から声をかけてもいいが、どうせなら得意分野のバランスが良い少数精鋭で最高なチームを作るとしよう。

問題文

N 人のメンバー候補がおり、それぞれの人は、パワー・スピード・テクニック・知識・発想力の 5 種類の能力値を持っています。
i 番目の人のパワーは A_i 、スピードは B_i 、テクニックは C_i 、知識は D_i 、発想力は E_i です。
あなたは、N 人のメンバー候補から 3 人を選び、1 つのチームを作ります。
チーム全体のパワーをチームメンバーのパワーの最大値で定義します。スピード・テクニック・知識・発想力についても同様に定義します。
チームの総合力を、チーム全体のパワー・スピード・テクニック・知識・発想力の最小値で定義します。
チームの総合力としてありえる最大値を求めてください。

制約

  • 入力は全て整数
  • 3 ≤ N ≤ 3000
  • 1 ≤ A_i, B_i, C_i, D_i, E_i ≤ 10^9

入力

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

N
A_1 B_1 C_1 D_1 E_1
A_2 B_2 C_2 D_2 E_2
\vdots
A_N B_N C_N D_N E_N

出力

答えを出力せよ。


入力例 1

3
3 9 6 4 6
6 9 3 1 1
8 8 9 3 7

出力例 1

4

3 人全員をチームに入れるほかありません。
この時、チーム全体の各能力値は以下のようになります。

  • チーム全体のパワー : \max(3, 6, 8) = 8
  • チーム全体のスピード : \max(9, 9, 8) = 9
  • チーム全体のテクニック : \max(6, 3, 9) = 9
  • チーム全体の知識 : \max(4, 1, 3) = 4
  • チーム全体の発想力 : \max(6, 1, 7) = 7

したがって、チームの総合力は \min(8, 9, 9, 4, 7) = 4 となります。


入力例 2

5
6 13 6 19 11
4 4 12 11 18
20 7 19 2 5
15 5 12 20 7
8 7 6 18 5

出力例 2

13

1, 2, 3 番目の人を採用すると、チームの総合力は \min(20, 13, 19, 19, 18) = 13 です。


入力例 3

10
6 7 5 18 2
3 8 1 6 3
7 2 8 7 7
6 3 3 4 7
12 8 9 15 9
9 8 6 1 10
12 9 7 8 2
10 3 17 4 10
3 1 3 19 3
3 14 7 13 1

出力例 3

10

Score : 400 points

Problem Statement

You want to choose three persons from N candidates to form a team.
Each candidate has five parameters: power, speed, technique, knowledge, and inventiveness.
The power, speed, technique, knowledge, and the inventiveness of the i-th candidate are A_i, B_i, C_i, D_i, and E_i, respectively.
Let us define your team's power as the maximum of the members' powers. The team's speed, technique, knowledge, and inventiveness are defined similarly.
Then, let us define your team's total strength as the minimum of the team's power, speed, technique, knowledge, and inventiveness.
Find the maximum possible value of your team's total strength.

Constraints

  • All values in input are integers.
  • 3 ≤ N ≤ 3000
  • 1 ≤ A_i, B_i, C_i, D_i, E_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1 D_1 E_1
A_2 B_2 C_2 D_2 E_2
\vdots
A_N B_N C_N D_N E_N

Output

Print the answer.


Sample Input 1

3
3 9 6 4 6
6 9 3 1 1
8 8 9 3 7

Sample Output 1

4

We have no choice but to choose all three of them.
Then, the team's parameters will be as follows:

  • power: \max(3, 6, 8) = 8;
  • speed: \max(9, 9, 8) = 9;
  • technique: \max(6, 3, 9) = 9;
  • knowledge: \max(4, 1, 3) = 4;
  • inventiveness: \max(6, 1, 7) = 7.

Thus, the team's total strength will be \min(8, 9, 9, 4, 7) = 4.


Sample Input 2

5
6 13 6 19 11
4 4 12 11 18
20 7 19 2 5
15 5 12 20 7
8 7 6 18 5

Sample Output 2

13

If we choose the 1-st, 2-nd, and 3-rd candidates, the team's total strength will be \min(20, 13, 19, 19, 18) = 13.


Sample Input 3

10
6 7 5 18 2
3 8 1 6 3
7 2 8 7 7
6 3 3 4 7
12 8 9 15 9
9 8 6 1 10
12 9 7 8 2
10 3 17 4 10
3 1 3 19 3
3 14 7 13 1

Sample Output 3

10
D - Message from Aliens

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

ストーリー

1 週間後、選りすぐりのプログラマ達が集まった。みなそれぞれに MAD なスキルを持つ曲者揃いだ。 早速 UFO との直接対決を開始しよう。
ずっとメッセージを送り続けているにもかかわらず放置されている UFO は心なしか少しイラついているように見える。 急いで UFO からのメッセージを解読しなければ。

問題文

暗号文 S が与えられます。この暗号文は、以下の操作で解読することが出来ます。

  • T を空文字列とする。
  • i = 1, 2, \dots, |S| について、順番に以下を行う。 (|S|S の長さを表す)
    • Si 文字目が R のとき、T を反転させる。
    • Si 文字目が R でないとき、その文字を T の末尾に加える。
  • この操作の後、T の中に同じ文字が 2 つ連続で並んでいたら、その 2 文字を取り除く。この操作を出来る限り続ける。 (最終的に得られる文字列は取り除く順番によらないことが証明できる)

この操作で得られる文字列 T を出力してください。

制約

  • S は英小文字と R からなる
  • 1 ≤ |S| ≤ 5 \times 10^5

入力

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

S

出力

答えを出力せよ。


入力例 1

ozRnonnoe

出力例 1

zone

以下のように解読できます。

  • 初め、T は空文字列である。
  • oT の末尾に加える。To となる。
  • zT の末尾に加える。Toz となる。
  • T を反転する。 Tzo となる。
  • nT の末尾に加える。Tzon となる。
  • oT の末尾に加える。Tzono となる。
  • nT の末尾に加える。Tzonon となる。
  • nT の末尾に加える。Tzononn となる。
  • oT の末尾に加える。Tzononno となる。
  • eT の末尾に加える。Tzononnoe となる。
  • 2 連続で並んでいる n を削除する。Tzonooe となる。
  • 2 連続で並んでいる o を削除する。Tzone となる。

入力例 2

hellospaceRhellospace

出力例 2


空文字列が答えになる場合もあります。

Score : 300 points

Problem Statement

You are given a ciphertext S. It can be decrypted by the following procedure:

  • Let T be an empty string.
  • For each i = 1, 2, \dots, |S| in this order, do the following: (|S| denotes the length of S)
    • if the i-th character of S is R, reverse T;
    • if the i-th character of S is not R, add that character to the end of T.
  • After the operation above, if there are two consecutive same characters in T, remove those two characters. Repeat this operation as long as it can be done. (We can prove that the final string obtained does not depend on the order the characters are removed.)

Print the string T obtained by this procedure.

Constraints

  • S consists of lowercase English letters and R.
  • 1 ≤ |S| ≤ 5 \times 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

ozRnonnoe

Sample Output 1

zone

We can decrypt it as follows:

  • Initially, T is an empty string.
  • Add o at the end of T, making it o.
  • Add z at the end of T, making it oz.
  • Reverse T, making it zo.
  • Add n at the end of T, making it zon.
  • Add o at the end of T, making it zono.
  • Add n at the end of T, making it zonon.
  • Add n at the end of T, making it zononn.
  • Add o at the end of T, making it zononno.
  • Add e at the end of T, making it zononnoe.
  • Delete two consecutive ns from T, making it zonooe.
  • Delete two consecutive os from T, making it zone.

Sample Input 2

hellospaceRhellospace

Sample Output 2


The answer may be an empty string.

E - Sneaking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

ストーリー

暗号解読を進めていると、仲間のムーアが突如 UFO に吸い込まれ、連れ去られてしまった。 ムーアは UFO との通信システムをほぼ 1 人で開発していたため、このままでは UFO と交信することができない!
デスマーチが横行していたブラックスタートアップ時代を思い出す。 バス係数{} = 1 のチームはいつだって脆いものだ。
仕方がない、UFO 内に乗り込んで直接話すしかなさそうだ。 上空を見上げると、UFO から梯子のようなものが下されている。
だがよく見るとボロボロで所々腐り落ちているようだ。 どうにかしてうまい登り方を考えなければ。

問題文

2 次元平面があり、あなたは今いる座標 (1, 1) から UFO のある座標 (R, C) に移動したいです。
あなたが (r, c) にいるとき、あなたは以下の 4 種類の移動を行うことができます。

  • (r, c) から (r, c + 1) に移動する。A_{r, c} のコストがかかる。この移動は c < C のとき使える。
  • (r, c) から (r, c - 1) に移動する。A_{r, c - 1} のコストがかかる。この移動は c > 1 のとき使える。
  • (r, c) から (r + 1, c) に移動する。B_{r, c} のコストがかかる。この移動は r < R のとき使える。
  • 1 ≤ i < r を満たす整数 i1 つ選び、(r, c) から (r - i, c) に移動する。1 + i のコストがかかる。

(1, 1) から (R, C) に移動するために必要な最小のコストを求めてください。

制約

  • 入力は全て整数
  • 2 ≤ R, C ≤ 500
  • 0 ≤ A_{i,j} < 10^3
  • 0 ≤ B_{i,j} < 10^3

入力

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

R C
A_{1,1} \cdots A_{1,C-1}
\vdots
A_{R,1} \cdots A_{R,C-1}
B_{1,1} \cdots B_{1,C}
\vdots
B_{R-1,1} \cdots B_{R-1,C}

出力

答えを出力せよ。


入力例 1

3 3
10 1
10 10
1 10
1 10 1
1 10 1

出力例 1

9

以下のように移動するとコスト 9 が達成できます。

  • (1, 1) から (2, 1) に移動する。コストが 1 かかる。
  • (2, 1) から (3, 1) に移動する。コストが 1 かかる。
  • (3, 1) から (3, 2) に移動する。コストが 1 かかる。
  • (3, 2) から (1, 2) に移動する。コストが 3 かかる。
  • (1, 2) から (1, 3) に移動する。コストが 1 かかる。
  • (1, 3) から (2, 3) に移動する。コストが 1 かかる。
  • (2, 3) から (3, 3) に移動する。コストが 1 かかる。

入力例 2

7 11
42 77 94 76 40 66 43 28 66 23
27 34 41 31 83 13 64 69 81 82
23 81 0 22 39 51 4 37 84 43
62 37 82 86 26 67 45 78 85 2
79 18 72 62 68 84 69 88 19 48
0 27 21 51 71 13 87 45 39 11
74 57 32 0 97 41 87 96 17 98
69 58 76 32 51 16 38 68 86 82 64
53 47 33 7 51 75 43 14 96 86 70
80 58 12 76 94 50 59 2 1 54 25
14 14 62 28 12 43 15 70 65 44 41
56 50 50 54 53 34 16 3 2 59 88
27 85 50 79 48 86 27 81 78 78 64

出力例 2

498

入力例 3

4 4
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

出力例 3

0

Score : 500 points

Problem Statement

On a two-dimensional plane, you are now at coordinate (1, 1) and want to get to (R, C), the coordinates of the UFO.
When you are at (r, c), you can make the following four kinds of moves:

  • Move from (r, c) to (r, c + 1) at the cost of A_{r, c}. You can make this move when c < C.
  • Move from (r, c) to (r, c - 1) at the cost of A_{r, c - 1}. You can make this move when c > 1.
  • Move from (r, c) to (r + 1, c) at the cost of B_{r, c}. You can make this move when r < R.
  • Choose an integer i such that 1 ≤ i < r and move from (r, c) to (r - i, c) at the cost of 1 + i.

Find the minimum cost needed to move from (1, 1) to (R, C).

Constraints

  • All values in input are integers.
  • 2 ≤ R, C ≤ 500
  • 0 ≤ A_{i,j} < 10^3
  • 0 ≤ B_{i,j} < 10^3

Input

Input is given from Standard Input in the following format:

R C
A_{1,1} \cdots A_{1,C-1}
\vdots
A_{R,1} \cdots A_{R,C-1}
B_{1,1} \cdots B_{1,C}
\vdots
B_{R-1,1} \cdots B_{R-1,C}

Output

Print the answer.


Sample Input 1

3 3
10 1
10 10
1 10
1 10 1
1 10 1

Sample Output 1

9

You can achieve the cost of 9 as follows:

  • Move from (1, 1) to (2, 1) at the cost of 1.
  • Move from (2, 1) to (3, 1) at the cost of 1.
  • Move from (3, 1) to (3, 2) at the cost of 1.
  • Move from (3, 2) to (1, 2) at the cost of 3.
  • Move from (1, 2) to (1, 3) at the cost of 1.
  • Move from (1, 3) to (2, 3) at the cost of 1.
  • Move from (2, 3) to (3, 3) at the cost of 1.

Sample Input 2

7 11
42 77 94 76 40 66 43 28 66 23
27 34 41 31 83 13 64 69 81 82
23 81 0 22 39 51 4 37 84 43
62 37 82 86 26 67 45 78 85 2
79 18 72 62 68 84 69 88 19 48
0 27 21 51 71 13 87 45 39 11
74 57 32 0 97 41 87 96 17 98
69 58 76 32 51 16 38 68 86 82 64
53 47 33 7 51 75 43 14 96 86 70
80 58 12 76 94 50 59 2 1 54 25
14 14 62 28 12 43 15 70 65 44 41
56 50 50 54 53 34 16 3 2 59 88
27 85 50 79 48 86 27 81 78 78 64

Sample Output 2

498

Sample Input 3

4 4
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Sample Output 3

0
F - Encounter and Farewell

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

ストーリー

梯子を登りきり UFO に入ると、そこには捕らえられたムーアと、恐ろしい形相の宇宙人が待ち構えていた。 しかしそれ以上に、見覚えのある缶がうずたかく積まれているのが目に留まった。 間違いない。あれは間違いなく ZONe mad_hacker ver.1.0.0 だ。 そして mad_hacker を飲んでいるということは、この宇宙人も別の星で MADHACKING に勤しむエンジニアに違いない、と俺は確信した。 勉強会で初めての相手に話しかける時の如く、俺はやや緊張しながら口を開いた。
「...foo」
聞くや否や、宇宙人の目が輝いた。
「...bar!」

「fizz!」「buzz!」
ハッカーたちの魂が、星を超えて交わった。 すっかり意気投合した俺たちは、それぞれの星の言語やプログラミングパラダイムについて一晩中語り明かした。 気に入られ、その場でスカウトされた俺は地球人として初のミルキーウェイ(天の川)のスタートアップで働くことになったのだった。 さようなら、地球。ありがとう、ZONe mad_hacker。そして、hello, space!

問題文

あなたは、スタートアップの新製品として全ての惑星間を行き来出来るようなワープゲートを構築しようとしています。
惑星が N 個あり、0 から N - 1 までの番号がついています。ここで、ある整数 n が存在して、N = 2^n です。これらの惑星の間を高速に移動するために、2 惑星間を瞬時に移動出来るワープゲートを N - 1 個作成し、全ての惑星間を行き来出来るようなゲート網を作りたいです。しかし、星同士には相性があり、相性が悪い惑星間にはワープゲートを作ることが出来ません。
具体的には、相性の悪い惑星は数列 A = (A_1, A_2, \dots, A_M) で表され、ある整数 i が存在して a\ \mathrm{xor}\ b = A_i である場合、かつそのときに限り惑星 a と惑星 b の間にワープゲートを作ることが出来ません。
全ての惑星間を行き来できるようなゲート網を作ることができるか判定し、できる場合は N - 1 個のワープゲートの作り方を求めてください。

\mathrm{xor} とは

整数 a, b のビットごとの排他的論理和 a\ \mathrm{xor}\ b は、以下のように定義されます。

  • a\ \mathrm{xor}\ b を二進表記した際の 2^k (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{xor}\ 5 = 6 となります (二進表記すると: 011\ \mathrm{xor}\ 101 = 110)。

制約

  • 入力は全て整数
  • 1 ≤ n ≤ 18 を満たす整数 n が存在して、N = 2^n
  • 0 ≤ M ≤ N - 1
  • 0 < A_1 < A_2 < \dots < A_M < N

入力

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

N M
A_1 A_2 \cdots A_M

出力

全ての惑星間を行き来できるようなゲート網を作ることができない場合、-1 と出力せよ。

作ることができる場合、N - 1 個のワープゲートの作り方を以下の形式で出力せよ。

U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

これは、惑星 U_i と惑星 V_i の間にワープゲートを作ることを表す。


入力例 1

4 1
3

出力例 1

1 0
1 3
0 2

1\ \mathrm{xor}\ 0 = 1,\ 1\ \mathrm{xor}\ 3 = 2,\ 0\ \mathrm{xor}\ 2 = 2 であるためワープゲートを作ることができ、N - 1 個のワープゲートで全ての惑星間を行き来できるようになっているので、正解となります。
正解となる出力は他にも多数あります。


入力例 2

8 0

出力例 2

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

入力例 3

8 7
1 2 3 4 5 6 7

出力例 3

-1

Score : 600 points

Problem Statement

As your startup's new product, you are planning to build warp gates that allow travel between all planets.
There are N planets, numbered 0 through N-1, where there is an integer n such that N = 2^n. To allow fast travel between all these planets, you want to make N - 1 warp gates, each of which allows instant travel between two planets. However, for some pairs of planets that are incompatible with each other, you cannot make a warp gate between them.
More specifically, such pairs of planets incompatible with each other are represented by a sequence A = (A_1, A_2, \dots, A_M). If and only if there is an integer i such that a\ \mathrm{xor}\ b = A_i, you cannot make a warp gate between Planet a and Planet b.
Determine whether it is possible to make a network of warp gates allowing travel between every two planets. If it is possible, find one such way to make N - 1 warp gates.

What is \mathrm{xor}?

The bitwise \mathrm{xor} of integers a and b, a\ \mathrm{xor}\ b, is defined as follows:

  • When a\ \mathrm{xor}\ b is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of a and b is 1, and 0 otherwise.
For example, we have 3\ \mathrm{xor}\ 5 = 6 (in base two: 011\ \mathrm{xor}\ 101 = 110).

Constraints

  • All values in input are integers.
  • There exists an integer n such that 1 ≤ n ≤ 18 and N = 2^n.
  • 0 ≤ M ≤ N - 1
  • 0 < A_1 < A_2 < \dots < A_M < N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \cdots A_M

Output

If it is impossible to make a network of warp gates allowing travel between every two planets, print -1.

If it is possible to make such a network, print one way to make such N - 1 warp gates in the following format:

U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

It means you make a warp gate between Planet U_i and Planet V_i.


Sample Input 1

4 1
3

Sample Output 1

1 0
1 3
0 2

Since 1\ \mathrm{xor}\ 0 = 1,\ 1\ \mathrm{xor}\ 3 = 2,\ 0\ \mathrm{xor}\ 2 = 2, we can make those N - 1 warp gates, and they allow travel between every two planets, so this output will be considered correct.
There are many other possible outputs that will also be considered correct.


Sample Input 2

8 0

Sample Output 2

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

Sample Input 3

8 7
1 2 3 4 5 6 7

Sample Output 3

-1