A - Pawn on a Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

上下左右に広がる HW 列のマス目があり、各マスにはコマが置かれているか、何も置かれていないかのどちらかです。

マス目の状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって表され、
S_ij 文字目が # のとき上から i 行目かつ左から j 列目のマスにはコマが置かれていることを、
S_ij 文字目が . のとき上から i 行目かつ左から j 列目のマスには何も置かれていないことを表しています。

マス目上のマスのうち、コマが置かれているようなものの個数を求めてください。

制約

  • 1\leq H,W \leq 10
  • H,W は整数
  • S_i#. のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H

出力

コマが置かれているマスの個数を整数で出力せよ。


入力例 1

3 5
#....
.....
.##..

出力例 1

3
  • 上から 1 行目かつ左から 1 列目のマス
  • 上から 3 行目かつ左から 2 列目のマス
  • 上から 3 行目かつ左から 3 列目のマス

の計 3 つのマスにコマが置かれているため、3 を出力します。


入力例 2

1 10
..........

出力例 2

0

どのマスにもコマは置かれていないため、0 を出力します。


入力例 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

出力例 3

16

Score : 100 points

Problem Statement

There is a grid with H rows from top to bottom and W columns from left to right. Each square has a piece placed on it or is empty.

The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W.
If the j-th character of S_i is #, the square at the i-th row from the top and j-th column from the left has a piece on it;
if the j-th character of S_i is ., the square at the i-th row from the top and j-th column from the left is empty.

How many squares in the grid have pieces on them?

Constraints

  • 1\leq H,W \leq 10
  • H and W are integers.
  • S_i is a string of length W consisting of # and ..

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Print the number of squares with pieces as an integer.


Sample Input 1

3 5
#....
.....
.##..

Sample Output 1

3

The following three squares have pieces on them:

  • the square at the 1-st row from the top and 1-st column from the left;
  • the square at the 3-rd row from the top and 2-nd column from the left;
  • the square at the 3-rd row from the top and 3-rd column from the left.

Thus, 3 should be printed.


Sample Input 2

1 10
..........

Sample Output 2

0

Since no square has a piece on it, 0 should be printed.


Sample Input 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

Sample Output 3

16
B - Tires

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

末尾が er または ist であるような文字列 S が与えられます。
S の末尾が er である場合は er を、 ist である場合は ist を出力してください。

制約

  • 2 \le |S| \le 20
  • S は英小文字のみからなる。
  • S の末尾は er または ist である。

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

er

S="atcoder" の末尾は er です。


入力例 2

tourist

出力例 2

ist

入力例 3

er

出力例 3

er

Score : 100 points

Problem Statement

You are given a string S ending with er or ist.
If S ends with er, print er; if it ends with ist, print ist.

Constraints

  • 2 \le |S| \le 20
  • S consists of lowercase English letters.
  • S ends with er or ist.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

er

S="atcoder" ends with er.


Sample Input 2

tourist

Sample Output 2

ist

Sample Input 3

er

Sample Output 3

er
C - Who is missing?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。

高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。

高橋君が抜き取ったカードに書かれていた整数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • k \, (1 \leq k \leq N) に対し、A_i = k となる i4 個以下である。
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \ldots A_{4N - 1}

出力

答えを出力せよ。


入力例 1

3
1 3 2 3 3 2 2 1 1 1 2

出力例 1

3

高橋君が抜き取ったカードには 3 が書かれています。


入力例 2

1
1 1 1

出力例 2

1

入力例 3

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

出力例 3

2

Score : 200 points

Problem Statement

We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.

Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.

Find the integer written on the card removed by Takahashi.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_{4N - 1}

Output

Print the answer.


Sample Input 1

3
1 3 2 3 3 2 2 1 1 1 2

Sample Output 1

3

Takahashi removed a card with 3 written on it.


Sample Input 2

1
1 1 1

Sample Output 2

1

Sample Input 3

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

Sample Output 3

2
D - Langton's Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

HW 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。

高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。

  • 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。

制約

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • 入力される数値はすべて整数

入力

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

H W N

出力

H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。


入力例 1

3 4 5

出力例 1

.#..
##..
....

グリッドの各マスは操作によって以下のように変化します。

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

入力例 2

2 2 1000

出力例 2

..
..

入力例 3

10 10 10

出力例 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#

Score: 250 points

Problem Statement

There is a grid with H rows and W columns; initially, all cells are painted white. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.

Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.

  • If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.

Constraints

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

H W N

Output

Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.


Sample Input 1

3 4 5

Sample Output 1

.#..
##..
....

The cells of the grid change as follows due to the operations:

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

Sample Input 2

2 2 1000

Sample Output 2

..
..

Sample Input 3

10 10 10

Sample Output 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#
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 \coloni に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \coloni が書かれたカードが入っている箱の番号を昇順ですべて答える

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

  • 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}_qq 個目のクエリを表しており、次のいずれかの形式で与えられる。

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 です。
    • 12 度出力することに注意してください。
  • 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 4 には数 1 が書かれたカードが 2 枚入っていますが、41 回しか出力しないことに注意してください。
  • 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 - Circular Playlist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 曲からなるプレイリストがあり、曲には 1, \dots, N の番号が付けられています。
i の長さは A_i 秒です。

プレイリストを再生すると、曲 1、曲 2\ldots、曲 N の順に流れます。曲 N が流れ終わると、再び曲 1 から順に流れていきます。ある曲の途中で次の曲が流れることはなく、曲が流れ終わると、その瞬間に次の曲が流れ始めます。

プレイリストを再生してから T 秒後に流れているのはどの曲ですか?また、その曲が流れ始めてから何秒の時点ですか?
ただし、T 秒後ちょうどに曲が切り替わるような入力は与えられません。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • プレイリストを再生して T 秒後ちょうどに曲が切り替わることはない
  • 入力される値は全て整数

入力

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

N T
A_1 \ldots A_N

出力

プレイリストを再生してから T 秒後に流れている曲の番号と、その曲が流れ始めてから何秒たったかを表す整数を空白区切りで出力せよ。


入力例 1

3 600
180 240 120

出力例 1

1 60

プレイリストを再生してからの様子は次のようになります。

  • 0 秒後から 180 秒後まで曲 1 が流れる。
  • 180 秒後から 420 秒後まで曲 2 が流れる。
  • 420 秒後から 540 秒後まで曲 3 が流れる。
  • 540 秒後から 720 秒後まで曲 1 が流れる。
  • 720 秒後から 960 秒後まで曲 2 が流れる。
  • \qquad\vdots

600 秒後の時点で流れているのは曲 1 であり、流れ始めて 60 秒の時点です。


入力例 2

3 281
94 94 94

出力例 2

3 93

入力例 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

6 678912340

Score : 300 points

Problem Statement

We have a playlist with N songs numbered 1, \dots, N.
Song i lasts A_i seconds.

When the playlist is played, song 1, song 2, \ldots, and song N play in this order. When song N ends, the playlist repeats itself, starting from song 1 again. While a song is playing, the next song does not play; when a song ends, the next song starts immediately.

At exactly T seconds after the playlist starts playing, which song is playing? Also, how many seconds have passed since the start of that song?
There is no input where the playlist changes songs at exactly T seconds after it starts playing.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • The playlist does not change songs at exactly T seconds after it starts playing.
  • All values in the input are integers.

Input

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

N T
A_1 \ldots A_N

Output

Print an integer representing the song that is playing at exactly T seconds after the playlist starts playing, and an integer representing the number of seconds that have passed since the start of that song, separated by a space.


Sample Input 1

3 600
180 240 120

Sample Output 1

1 60

When the playlist is played, the following happens. (Assume that it starts playing at time 0.)

  • From time 0 to time 180, song 1 plays.
  • From time 180 to time 420, song 2 plays.
  • From time 420 to time 540, song 3 plays.
  • From time 540 to time 720, song 1 plays.
  • From time 720 to time 960, song 2 plays.
  • \qquad\vdots

At time 600, song 1 is playing, and 60 seconds have passed since the start of that song.


Sample Input 2

3 281
94 94 94

Sample Output 2

3 93

Sample Input 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

6 678912340
G - K-th Nearest

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

数直線上に N+Q 個の点 A_1,\dots,A_N,B_1,\dots,B_Q があり、点 A_i の座標は a_i、点 B_j の座標は b_j です。

j=1,2,\dots,Q それぞれについて、以下の問題に答えてください。

  • A_1,A_2,\dots,A_N のうち点 B_j との距離が k_j 番目に近い点を X としたとき、点 X と点 B_j との距離を求めよ。 より厳密には、点 A_i と点 B_j との距離を d_i として、(d_1,d_2,\dots,d_N) を昇順に並び替えてできる列を (d_1',d_2',\dots,d_N') としたとき、d_{k_j}' を求めよ。

制約

  • 1\leq N,Q \leq 10^5
  • -10^8\leq a_i,b_j \leq 10^8
  • 1\leq k_j\leq N
  • 入力は全て整数

入力

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

N Q
a_1 a_2 \dots a_N
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

出力

Q 行出力せよ。 l\ (1\leq l \leq Q) 行目には、j=l としたときの問題に対する答えを整数として出力せよ。


入力例 1

4 3
-3 -1 5 6
-2 3
2 1
10 4

出力例 1

7
3
13

1 番目のクエリについて説明します。

A_1,A_2,A_3,A_4 と点 B_1 との距離は順に 1,1,7,8 なので、点 B_1 との距離が 3 番目に近いのは点 A_3 です。 よって、点 A_3 と点 B_1 との距離である 7 を出力します。


入力例 2

2 2
0 0
0 1
0 2

出力例 2

0
0

同じ座標に複数の点がある可能性もあります。


入力例 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

出力例 3

11
66
59
54
88

Score : 425 points

Problem Statement

There are N+Q points A_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point A_i has a coordinate a_i and point B_j has a coordinate b_j.

For each j=1,2,\dots,Q, answer the following question:

  • Let X be the point among A_1,A_2,\dots,A_N that is the k_j-th closest to point B_j. Find the distance between points X and B_j. More formally, let d_i be the distance between points A_i and B_j. Sort (d_1,d_2,\dots,d_N) in ascending order to get the sequence (d_1',d_2',\dots,d_N'). Find d_{k_j}'.

Constraints

  • 1 \leq N, Q \leq 10^5
  • -10^8 \leq a_i, b_j \leq 10^8
  • 1 \leq k_j \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
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

Output

Print Q lines. The l-th line (1 \leq l \leq Q) should contain the answer to the question for j=l as an integer.


Sample Input 1

4 3
-3 -1 5 6
-2 3
2 1
10 4

Sample Output 1

7
3
13

Let us explain the first query.

The distances from points A_1, A_2, A_3, A_4 to point B_1 are 1, 1, 7, 8, respectively, so the 3rd closest to point B_1 is point A_3. Therefore, print the distance between point A_3 and point B_1, which is 7.


Sample Input 2

2 2
0 0
0 1
0 2

Sample Output 2

0
0

There may be multiple points with the same coordinates.


Sample Input 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

Sample Output 3

11
66
59
54
88
H - Path Decomposition of a Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

NK 頂点の木が与えられます。頂点には 1,2,\dots,NK の番号がついており、i 番目 (i=1,2,\dots,NK-1) の辺は頂点 u_i,v_i を双方向に結んでいます。

この木を N 本の長さ K のパスに分解できるか判定してください。より詳細には、以下を満たす N \times K 行列 P が存在するかどうか判定してください。

  • P_{1,1},\dots,P_{1,K},P_{2,1},\dots,P_{N,K}1,2,\dots,NK の並べ替えである。
  • i=1,2,\dots,N,\;j=1,2,\dots,K-1 について、頂点 P_{i,j} と頂点 P_{i,j+1} を結ぶ辺が存在する。

制約

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

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

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

出力

N 本の長さ K のパスに分解できるなら Yes を、そうでないなら No を出力せよ。


入力例 1

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

出力例 1

Yes

頂点 1,2 からなるパス、頂点 3,4 からなるパス、頂点 5,6 からなるパスに分解することができます。


入力例 2

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

出力例 2

No

Score : 475 points

Problem Statement

You are given a tree with NK vertices. The vertices are numbered 1,2,\dots,NK, and the i-th edge (i=1,2,\dots,NK-1) connects vertices u_i and v_i bidirectionally.

Determine whether this tree can be decomposed into N paths, each of length K. More precisely, determine whether there exists an N \times K matrix P satisfying the following:

  • P_{1,1}, \dots, P_{1,K}, P_{2,1}, \dots, P_{N,K} is a permutation of 1,2,\dots,NK.
  • For each i=1,2,\dots,N and j=1,2,\dots,K-1, there is an edge connecting vertices P_{i,j} and P_{i,j+1}.

Constraints

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • The given graph is a tree.
  • All input values are integers.

Input

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

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

Output

If it is possible to decompose the tree into N paths each of length K, print Yes. Otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

It can be decomposed into a path with vertices 1,2, a path with vertices 3,4, and a path with vertices 5,6.


Sample Input 2

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

Sample Output 2

No
I - Divide the Cake

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder Land で高橋君と青木君はイチゴの乗ったケーキを食べることになりました。

AtCoder Landのケーキは円形で、N 個の半径方向の切れ込みによって N 個の扇形ピースに区切られています。

切れ込みを時計回りの順に切れ込み 1, 切れ込み 2, \ldots, 切れ込み N と番号をふったとき、時計回りに切れ込み i (1 \leq i \leq N-1) から切れ込み i+1 までの間の部分をピース i と呼びます。また、時計回りに切れ込み N から切れ込み 1 までの間の部分をピース N と呼びます。

ピース i (1 \leq i \leq N) の上には A_i 個のイチゴが乗っています。

高橋君と青木君はこれから以下のゲームをします。

  • まず、高橋君が N 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み i とする。
  • 次に、青木君が高橋君の選んだ切れ込み以外の N-1 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み j とする。
  • 高橋君は切れ込み i から時計回りに見ていって、切れ込み j までの範囲のピースをすべてもらう。
  • 青木君は、残りのピースをすべてもらう。
  • 高橋君がもらったピースの 1 ピースあたりのイチゴの個数の平均値が青木君がもらったピースの 1 ピースあたりのイチゴの個数の平均値以上であるとき、高橋君の勝ちです。そうでないとき、青木君の勝ちです。

青木君が勝つために最適な切れ込みを選ぶとき、高橋君は必ず勝つためにはどの切れ込みを選べばよいか求めてください。そのような切れ込みが、存在しないときは -1 を、複数存在する場合はそのうち番号が最小のものを答えてください。

制約

  • 2 \leq N \leq 5 \times 10^{5}
  • 0 \leq A_i \leq 5 \times 10^{5}
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えとなる切れ込みの番号を出力せよ。答えとなる切れ込みが存在しない場合は -1 を出力せよ。


入力例 1

3
3 8 5

出力例 1

2

高橋君が切れ込み 1 を選んだ時、青木君は切れ込み 2 を選ぶと高橋君はピース 1 のみを、青木君はピース 2,3 をもらいます。高橋君のもらったピースの上に乗ってるイチゴの個数の平均値は 3、青木君のもらったピースの上に乗ってるイチゴの個数の平均値は 6.5 です。よって青木君が勝ちます。

高橋君が切れ込み 2 を選んだ時、青木君はどの切れ込みを選んでも高橋君が勝ちます。よって、答えは 2 です。


入力例 2

15
4096 64 512 1 256 16384 8 1024 2048 2 128 32 4 16 8192

出力例 2

15