Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
現在 AtCoder で開催されている AtCoder Regular Contest (ARC) には、Div. 1 と Div. 2 の 2 種類が存在します。 ARC Div. 1 では レーティング が 1600 以上 2999 以下の人が、ARC Div. 2 ではレーティングが 1200 以上 2399 以下の人がそれぞれ Rated 対象 となります。
正整数 R, X が与えられます。
レーティングが R の人は ARC Div. X において Rated 対象ですか?
制約
- 1\leq R \leq 4229
- 1\leq X \leq 2
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
R X
出力
レーティングが R の人が ARC Div. X において Rated 対象ならば Yes を、そうでないならば No を出力せよ。
入力例 1
2000 1
出力例 1
Yes
2000 は 1600 以上 2999 以下であるため、レーティングが 2000 の人は ARC Div. 1 において Rated 対象です。
入力例 2
1000 1
出力例 2
No
1000 は 1600 未満であるため、レーティングが 1000 の人は ARC Div. 1 において Rated 対象ではありません。
入力例 3
1500 2
出力例 3
Yes
1500 は 1200 以上 2399 以下であるため、レーティングが 1500 の人は ARC Div. 2 において Rated 対象です。
入力例 4
2800 2
出力例 4
No
2800 は 2399 より大きいため、レーティングが 2800 の人は ARC Div. 2 において Rated 対象ではありません。
Score : 100 points
Problem Statement
AtCoder Regular Contest (ARC) currently has two divisions: Div. 1 and Div. 2. In ARC Div. 1, participants whose rating is between 1600 and 2999, inclusive, are rated. In ARC Div. 2, participants whose rating is between 1200 and 2399, inclusive, are rated.
You are given positive integers R and X.
Determine whether a person with rating R is rated in ARC Div. X.
Constraints
- 1 \le R \le 4229
- 1 \le X \le 2
- All input values are integers.
Input
The input is given from Standard Input in the following format:
R X
Output
If a person with rating R is rated in ARC Div. X, output Yes; otherwise, output No.
Sample Input 1
2000 1
Sample Output 1
Yes
Because 2000 lies between 1600 and 2999, a person with rating 2000 is rated in ARC Div. 1.
Sample Input 2
1000 1
Sample Output 2
No
Because 1000 is less than 1600, a person with rating 1000 is not rated in ARC Div. 1.
Sample Input 3
1500 2
Sample Output 3
Yes
Because 1500 lies between 1200 and 2399, a person with rating 1500 is rated in ARC Div. 2.
Sample Input 4
2800 2
Sample Output 4
No
Because 2800 exceeds 2399, a person with rating 2800 is not rated in ARC Div. 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0123456789 に加えて 10,11,12,13,14,15 に対応する数字として ABCDEF を使う 16 進表記では、0 以上 255 以下の整数は 1 桁または 2 桁になります。
例えば、0 や 12 は 16 進表記では 0 や C と 1 桁になり、99 や 255 は 16 進表記では 63 や FF と 2 桁になります。
0 以上 255 以下の整数 N を、必要に応じて先頭に 0 を加えることでちょうど 2 桁の 16 進表記に変換してください。
注記
英大文字と英小文字は区別されます。特に、16 進表記の数字として ABCDEF の代わりに abcdef を使うことは出来ません。
制約
- 0 \leq N \leq 255
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
99
出力例 1
63
99 は 16 進表記で 63 です。
入力例 2
12
出力例 2
0C
12 は 16 進表記で C です。
要求されているのはちょうど 2 桁の 16 進表記に変換することなので、C の先頭に 0 を加えた 0C が答えです。
入力例 3
0
出力例 3
00
入力例 4
255
出力例 4
FF
Score : 100 points
Problem Statement
In the hexadecimal system, where the digits ABCDEF corresponding to 10,11,12,13,14, and 15 are used in addition to 0123456789, every integer between 0 and 255 is represented as a 1- or 2-digit numeral.
For example, 0 and 12 are represented as 1-digit hexadecimal numerals 0 and C; 99 and 255 are represented as 2-digit hexadecimals 63 and FF.
Given an integer N between 0 and 255, convert it to an exactly two-digit hexadecimal numeral, prepending leading 0s if necessary.
Notes
The judge is case-sensitive. Specifically, you cannot use abcdef as hexadecimal digits instead of ABCDEF.
Constraints
- 0 \leq N \leq 255
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
99
Sample Output 1
63
99 is represented as 63 in hexadecimal.
Sample Input 2
12
Sample Output 2
0C
12 is represented as C in hexadecimal.
Since we ask you to convert it to a two-digit hexadecimal numeral, the answer is 0C, where 0 is prepended to C.
Sample Input 3
0
Sample Output 3
00
Sample Input 4
255
Sample Output 4
FF
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
A 匹のスライムがいます。
すぬけくんが 1 回叫ぶたびに、スライムは K 倍に増殖します。
スライムが B 匹以上になるには、すぬけくんは最小で何回叫ぶ必要があるでしょうか?
制約
- 1 \leq A \leq B \leq 10^9
- 2 \leq K \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B K
出力
答えを出力せよ。
入力例 1
1 4 2
出力例 1
2
はじめ、スライムが 1 匹います。すぬけくんが 1 回叫ぶとスライムは 2 匹になり、 2 回叫ぶとスライムは 4 匹になります。4 匹以上になるためには、最小で 2 回叫ぶ必要があります。
入力例 2
7 7 10
出力例 2
0
はじめからスライムは 7 匹います。
入力例 3
31 415926 5
出力例 3
6
Score : 200 points
Problem Statement
There are A slimes.
Each time Snuke shouts, the slimes multiply by K times.
In order to have B or more slimes, at least how many times does Snuke need to shout?
Constraints
- 1 \leq A \leq B \leq 10^9
- 2 \leq K \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B K
Output
Print the answer.
Sample Input 1
1 4 2
Sample Output 1
2
We start with one slime. After Snuke's first shout, we have two slimes; after his second shout, we have four slimes. Thus, he needs to shout at least twice to have four or more slimes.
Sample Input 2
7 7 10
Sample Output 2
0
We have seven slimes already at the start.
Sample Input 3
31 415926 5
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
xy 平面上に、同一直線上にない 3 点 A(x_A,y_A), B(x_B,y_B),C(x_C,y_C) があります。三角形 ABC が直角三角形であるかどうか判定してください。
制約
- -1000\leq x_A,y_A,x_B,y_B,x_C,y_C\leq 1000
- 3 点 A,B,C は同一直線上にない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
x_A y_A x_B y_B x_C y_C
出力
三角形 ABC が直角三角形であるならば Yes を、そうでないならば No を出力せよ。
入力例 1
0 0 4 0 0 3
出力例 1
Yes
三角形 ABC は直角三角形です。

入力例 2
-4 3 2 1 3 4
出力例 2
Yes
三角形 ABC は直角三角形です。

入力例 3
2 4 -3 2 1 -2
出力例 3
No
三角形 ABC は直角三角形ではありません。

Score : 200 points
Problem Statement
In the xy-plane, there are three points A(x_A, y_A), B(x_B, y_B), and C(x_C, y_C) that are not collinear. Determine whether the triangle ABC is a right triangle.
Constraints
- -1000 \leq x_A, y_A, x_B, y_B, x_C, y_C \leq 1000
- The three points A, B, and C are not collinear.
- 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 Yes if the triangle ABC is a right triangle, and No otherwise.
Sample Input 1
0 0 4 0 0 3
Sample Output 1
Yes
The triangle ABC is a right triangle.

Sample Input 2
-4 3 2 1 3 4
Sample Output 2
Yes
The triangle ABC is a right triangle.

Sample Input 3
2 4 -3 2 1 -2
Sample Output 3
No
The triangle ABC is not a right triangle.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N カップのアイスクリームがあります。
i カップ目の味は F_i 、美味しさは S_i ( S_i は偶数 ) です。
あなたは、 N 個のカップの中から 2 つを選んで食べることにしました。
このときの満足度は次のように定義されます。
- 食べたアイスクリームの美味しさを s,t ( 但し、 s \ge t ) とする。
- 2 つのカップの味が異なるなら、満足度は \displaystyle s+t である。
- そうでないなら、満足度は \displaystyle s + \frac{t}{2} である。
満足度として達成可能な最大値を求めてください。
制約
- 入力は全て整数
- 2 \le N \le 3 \times 10^5
- 1 \le F_i \le N
- 2 \le S_i \le 10^9
- S_i は偶数
入力
入力は以下の形式で標準入力から与えられる。
N F_1 S_1 F_2 S_2 \vdots F_N S_N
出力
答えを整数として出力せよ。
入力例 1
4 1 4 2 10 2 8 3 6
出力例 1
16
2 カップ目と 4 カップ目のアイスを食べることを考えます。
- 2 カップ目の味は 2 、美味しさは 10 です。
- 4 カップ目の味は 3 、美味しさは 6 です。
- 両者の味は異なるので、満足度は 10+6=16 です。
以上より、満足度 16 を達成できます。
満足度を 16 より大きくすることはできません。
入力例 2
4 4 10 3 2 2 4 4 12
出力例 2
17
1 カップ目と 4 カップ目のアイスを食べることを考えます。
- 1 カップ目の味は 4 、美味しさは 10 です。
- 4 カップ目の味は 4 、美味しさは 12 です。
- 両者の味は同じなので、満足度は 12+\frac{10}{2}=17 です。
以上より、満足度 17 を達成できます。
満足度を 17 より大きくすることはできません。
Score : 300 points
Problem Statement
We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are F_i and S_i, respectively (S_i is an even number).
You will choose and eat two of the N cups.
Your satisfaction here is defined as follows.
- Let s and t (s \ge t) be the deliciousness of the eaten cups.
- If the two cups have different flavors, your satisfaction is \displaystyle s+t.
- Otherwise, your satisfaction is \displaystyle s + \frac{t}{2}.
Find the maximum achievable satisfaction.
Constraints
- All input values are integers.
- 2 \le N \le 3 \times 10^5
- 1 \le F_i \le N
- 2 \le S_i \le 10^9
- S_i is even.
Input
Input is given from Standard Input in the following format:
N F_1 S_1 F_2 S_2 \vdots F_N S_N
Output
Print the answer as an integer.
Sample Input 1
4 1 4 2 10 2 8 3 6
Sample Output 1
16
Consider eating the second and fourth cups.
- The second cup has a flavor of 2 and deliciousness of 10.
- The fourth cup has a flavor of 3 and deliciousness of 6.
- Since they have different flavors, your satisfaction is 10+6=16.
Thus, you can achieve the satisfaction of 16.
You cannot achieve a satisfaction greater than 16.
Sample Input 2
4 4 10 3 2 2 4 4 12
Sample Output 2
17
Consider eating the first and fourth cups.
- The first cup has a flavor of 4 and deliciousness of 10.
- The fourth cup has a flavor of 4 and deliciousness of 12.
- Since they have the same flavor, your satisfaction is 12+\frac{10}{2}=17.
Thus, you can achieve the satisfaction of 17.
You cannot achieve a satisfaction greater than 17.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス, 横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、グリッド上には、ある 縦横 2 マス以上 の部分長方形の内部にあるマスにクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていません。
形式的に説明すると、以下の条件を全て満たす 4 つの整数の組 (a,b,c,d) がただ 1 つ存在します。
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- グリッド上のマスのうち、a \leq i \leq b, c \leq j \leq d を満たす全てのマス (i, j) にはクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていない。
ところが、すぬけ君がグリッド上のクッキーのどれか 1 枚を取って食べてしまいました。
すぬけ君がクッキーを取ったマスは、クッキーが置かれていない状態に変わります。
すぬけ君がクッキーを食べた後のグリッドの状態が入力として与えられます。
マス (i, j) の状態は文字 S_{i,j} として与えられて、# はクッキーが置かれているマスを, . はクッキーが置かれていないマスを意味します。
すぬけ君が食べたクッキーが元々置かれていたマスを答えてください。(答えは一意に定まります。)
制約
- 2 \leq H, W \leq 500
- S_{i,j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
出力
すぬけ君が食べたクッキーが元々置かれていたマスを (i, j) とする。i, j をこの順に空白区切りで出力せよ。
入力例 1
5 6 ...... ..#.#. ..###. ..###. ......
出力例 1
2 4
はじめ、クッキーは (2, 3) を左上、(4, 5) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (2, 4) にあるクッキーを食べたことがわかります。よって (2, 4) を出力します。
入力例 2
3 2 #. ## ##
出力例 2
1 2
はじめ、クッキーは (1, 1) を左上、(3, 2) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (1, 2) にあるクッキーを食べたことがわかります。
入力例 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
出力例 3
2 5
Score : 300 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.
Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long, and no cookie on the other squares.
Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions.
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d, and no cookie on the other squares.
However, Snuke took and ate one of the cookies on the grid.
The square that contained that cookie is now empty.
As the input, you are given the state of the grid after Snuke ate the cookie.
The state of the square (i, j) is given as the character S_{i,j}, where # means a square with a cookie, and . means a square without one.
Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
Constraints
- 2 \leq H, W \leq 500
- S_{i,j} is
#or..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
Output
Let (i, j) the square contained the cookie eaten by Snuke. Print i and j in this order, separated by a space.
Sample Input 1
5 6 ...... ..#.#. ..###. ..###. ......
Sample Output 1
2 4
Initially, cookies were on the squares inside the rectangle with (2, 3) as the top-left corner and (4, 5) as the bottom-right corner, and Snuke ate the cookie on (2, 4). Thus, you should print (2, 4).
Sample Input 2
3 2 #. ## ##
Sample Output 2
1 2
Initially, cookies were placed on the squares inside the rectangle with (1, 1) as the top-left corner and (3, 2) as the bottom-right corner, and Snuke ate the cookie at (1, 2).
Sample Input 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
Sample Output 3
2 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは以下のいずれかの形式です。
1 x: A_x と A_{x+1} の値を入れ替える。2 l r: \displaystyle \sum_{l\leq i\leq r} A_i の値を求める。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq Q \leq 5\times 10^5
- 1\leq A_i \leq 10^4
- 1 種類目のクエリについて、1\leq x \leq N-1
- 2 種類目のクエリについて、1\leq l\leq r \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、\text{query}_i は i 番目のクエリを表し、以下のいずれかの形式で与えられる。
1 x
2 l r
出力
2 種類目のクエリの個数を q として、q 行出力せよ。 i 行目 (1\leq i \leq q) には、2 種類目のクエリのうち i 個目のものに対する答えを出力せよ。
入力例 1
4 4 2 7 1 8 1 2 2 1 2 1 1 2 2 4
出力例 1
3 17
- 1 番目のクエリでは、A_2 と A_3 の値を入れ替えます。これにより、A=(2,1,7,8) になります。
- 2 番目のクエリでは、A_1+A_2 の値を求めます。答えは 2+1=3 です。
- 3 番目のクエリでは、A_1 と A_2 の値を入れ替えます。これにより、A=(1,2,7,8) になります。
- 4 番目のクエリでは、A_2+A_3+A_4 の値を求めます。答えは 2+7+8=17 です。
入力例 2
8 10 22 75 26 45 72 81 47 29 2 2 7 2 6 8 2 4 4 1 2 2 1 3 1 1 2 2 4 1 2 1 4 2 1 1
出力例 2
346 157 45 123 142 26
Score : 400 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.
Process Q queries in order. Each query is in one of the following formats:
1 x: Swap the values of A_x and A_{x+1}.2 l r: Find the value of \displaystyle \sum_{l\leq i\leq r} A_i.
Constraints
- 2\leq N \leq 2\times 10^5
- 1\leq Q \leq 5\times 10^5
- 1\leq A_i \leq 10^4
- For queries of the first type, 1\leq x \leq N-1.
- For queries of the second type, 1\leq l\leq r \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 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i represents the i-th query and is given in one of the following formats:
1 x
2 l r
Output
Let q be the number of queries of the second type, and output q lines. The i-th line (1\leq i \leq q) should contain the answer to the i-th query of the second type.
Sample Input 1
4 4 2 7 1 8 1 2 2 1 2 1 1 2 2 4
Sample Output 1
3 17
- In the 1-st query, swap the values of A_2 and A_3. This makes A=(2,1,7,8).
- In the 2-nd query, find the value of A_1+A_2. The answer is 2+1=3.
- In the 3-rd query, swap the values of A_1 and A_2. This makes A=(1,2,7,8).
- In the 4-th query, find the value of A_2+A_3+A_4. The answer is 2+7+8=17.
Sample Input 2
8 10 22 75 26 45 72 81 47 29 2 2 7 2 6 8 2 4 4 1 2 2 1 3 1 1 2 2 4 1 2 1 4 2 1 1
Sample Output 2
346 157 45 123 142 26
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
「ユ木」を、以下の手順で生成することができる木と定義します。
- 正整数 x,y を選ぶ
- 頂点を 1 つ用意する
- 別に x 個の頂点を用意し、それぞれを手順 2 で用意した頂点と辺で結ぶ
- 手順 3 で用意した x 個の頂点それぞれに、 y 個の葉をつける
x=4,y=2 のユ木を下図に示します。手順 2,3,4 で用意される頂点をそれぞれ赤、青、緑で示しています。

N 頂点の木 T が与えられます。頂点には 1 から N の番号が付けられており、 i\;(=1,2,\dots,N-1) 番目の辺は頂点 u_i と頂点 v_i を結びます。
T の 0 個以上の頂点とそれに隣接する辺を削除して 1 つのユ木にするとき、削除する頂点数の最小値を求めてください。なお、本問題の制約下で、T をかならずユ木にすることができます。
制約
- 3 \leq N \leq 3 \times 10^5
- 1 \leq u_i < v_i \leq N
- 与えられるグラフは木である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
答えを出力せよ。
入力例 1
8 1 3 2 3 3 4 4 5 5 6 5 7 4 8
出力例 1
1
頂点 8 を削除することで、与えられた木を x=2,y=2 のユ木にすることができます。
入力例 2
3 1 2 2 3
出力例 2
0
与えられた木はすでに x=1,y=1 のユ木です。
入力例 3
10 1 3 1 2 5 7 6 10 2 8 1 6 8 9 2 7 1 4
出力例 3
3
Score : 450 points
Problem Statement
A "Snowflake Tree" is defined as a tree that can be generated by the following procedure:
- Choose positive integers x,y.
- Prepare one vertex.
- Prepare x more vertices, and connect each of them to the vertex prepared in step 2.
- For each of the x vertices prepared in step 3, attach y leaves to it.
The figure below shows a Snowflake Tree with x=4,y=2. The vertices prepared in steps 2, 3, 4 are shown in red, blue, and green, respectively.

You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (i=1,2,\dots,N-1) connects vertices u_i and v_i.
Consider deleting zero or more vertices of T and the edges adjacent to them so that the remaining graph becomes a single Snowflake Tree. Find the minimum number of vertices that must be deleted. Under the constraints of this problem, it is always possible to transform T into a Snowflake Tree.
Constraints
- 3 \leq N \leq 3 \times 10^5
- 1 \leq u_i < v_i \leq N
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print the answer.
Sample Input 1
8 1 3 2 3 3 4 4 5 5 6 5 7 4 8
Sample Output 1
1
By deleting vertex 8, the given tree can be transformed into a Snowflake Tree with x=2,y=2.
Sample Input 2
3 1 2 2 3
Sample Output 2
0
The given tree is already a Snowflake Tree with x=1,y=1.
Sample Input 3
10 1 3 1 2 5 7 6 10 2 8 1 6 8 9 2 7 1 4
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
Atcoder 国には 1 から N の番号がついた N 個の街と 1 から M の番号がついた M 個の高速道路があります。
高速道路 i は街 A_i と街 B_i を双方向に結んでいます。
国王の高橋君は、新たに N-M-1 本の高速道路を建設し、次の 2 つの条件をともに満たそうとしています。
- すべての街同士は、高速道路をいくつか通って互いに行き来できる
- 各 i=1,\ldots,N について、街 i はちょうど D_i 本の高速道路と直接つながっている
条件を満たすような建設方法が存在するか判定し、存在するなら 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- i\neq j ならば、(A_i, B_i) \neq (A_j,B_j)
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
出力
条件を満たすような建設方法が存在しないとき -1 を出力せよ。
存在するとき、N-M-1 行出力せよ。i 行目には、i 番目に建設する高速道路が結ぶ 2 つの街の番号を空白区切りで出力せよ。
入力例 1
6 2 1 2 1 2 2 2 2 3 1 4
出力例 1
6 2 5 6 4 5
出力例のように、街 6 と2、街 5 と 6、街 4 と 5 をそれぞれ結ぶ高速道路を建設すると条件を満たすことができます。
この他にも、例えば 街 6 と4、街 5 と 6、街 2 と 5 を結ぶような高速道路を建設しても条件を満たすことができます。
入力例 2
5 1 1 1 1 1 4 2 3
出力例 2
-1
入力例 3
4 0 3 3 3 3
出力例 3
-1
Score : 500 points
Problem Statement
The Republic of Atcoder has N towns numbered 1 through N, and M highways numbered 1 through M.
Highway i connects Town A_i and Town B_i bidirectionally.
King Takahashi is going to construct (N-M-1) new highways so that the following two conditions are satisfied:
- One can travel between every pair of towns using some number of highways
- For each i=1,\ldots,N, exactly D_i highways are directly connected to Town i
Determine if there is such a way of construction. If it exists, print one.
Constraints
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- If i\neq j, then (A_i, B_i) \neq (A_j,B_j).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
Output
If there isn't a way of construction that satisfies the conditions, print -1.
If there is, print (N-M-1) lines. The i-th line should contain the indices of the two towns connected by the i-th highway to be constructed.
Sample Input 1
6 2 1 2 1 2 2 2 2 3 1 4
Sample Output 1
6 2 5 6 4 5
As in the Sample Output, the conditions can be satisfied by constructing highways connecting Towns 6 and 2, Towns 5 and 6, and Towns 4 and 5, respectively.
Another example to satisfy the conditions is to construct highways connecting Towns 6 and 4, Towns 5 and 6, and Towns 2 and 5, respectively.
Sample Input 2
5 1 1 1 1 1 4 2 3
Sample Output 2
-1
Sample Input 3
4 0 3 3 3 3
Sample Output 3
-1