A - Chinchirorin

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君が 3 つのサイコロを振ったところそれぞれ a,b,c の目が出ました。

a,b,c のうちある 2 つが同じときは残りの 1 つのサイコロの目を、同じものがないときは 0 を出力してください。

制約

  • 1 \leq a,b,c \leq 6
  • a,b,c は全て整数である。

入力

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

a b c

出力

a,b,c のうちある 2 つが同じときは残りの 1 つのサイコロの目を、同じものがないときは 0 を出力せよ。


入力例 1

2 5 2

出力例 1

5

1 つめと 3 つめのサイコロの目がともに 2 であるので、残り 1 つの目である 5 を出力します。


入力例 2

4 5 6

出力例 2

0

サイコロの目はどの 2 つも相異なるため 0 を出力します。


入力例 3

1 1 1

出力例 3

1

どの 2 つのサイコロの目も同じであり、そのいずれを選んだ場合でも残り 1 つの目は 1 となります。

Score : 100 points

Problem Statement

Takahashi threw three dice, and they showed three numbers a, b, and c.

If there are two same numbers among a, b, and c, print the remaining number. Otherwise, print 0.

Constraints

  • 1 \leq a,b,c \leq 6
  • All of a, b, and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

If there are two same numbers among a, b, and c, print the remaining number. Otherwise, print 0.


Sample Input 1

2 5 2

Sample Output 1

5

The first and third dice both showed 2, so we should print the number on the remaining dice, which is 5.


Sample Input 2

4 5 6

Sample Output 2

0

Any two numbers are different, so we should print 0.


Sample Input 3

1 1 1

Sample Output 3

1

Any two numbers are the same. Whichever two dice we choose, the number on the remaining dice will be 1.

B - AtCoder Condominium

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

AtCoder マンションは 1 階から N 階までの N 階建てのマンションです。 各階には K 室の部屋があり、1 号室から K 号室まで番号が振られています。

ここで N,K1 桁の整数であり、 i 階の j 号室の部屋番号は i0j で表されます。 例えば、1 階の 2 号室の部屋番号は 102 です。

マンションの管理人である高橋君は各部屋番号を 3 桁の整数とみなし、 AtCoder マンションに存在するすべての部屋について足しあわせたらいくつになるのか興味を持ちました。 その値を求めてください。

制約

  • 1 \leq N,K \leq 9
  • N,K は整数である。

入力

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

N K

出力

答えを出力せよ。


入力例 1

1 2

出力例 1

203

AtCoder マンションは 101, 1022 部屋からなります。 101+102=203 です。


入力例 2

3 3

出力例 2

1818

Score : 200 points

Problem Statement

A condominium AtCoder has N floors, called the 1-st floor through the N-th floor. Each floor has K rooms, called the 1-st room through the K-th room.

Here, both N and K are one-digit integers, and the j-th room on the i-th floor has the room number i0j. For example, the 2-nd room on the 1-st floor has the room number 102.

Takahashi, the manager, got interested in the sum of the room numbers of all rooms in the condominium, where each room number is seen as a three-digit integer. Find this sum.

Constraints

  • 1 \leq N,K \leq 9
  • N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.


Sample Input 1

1 2

Sample Output 1

203

The condominium has two rooms 101 and 102. We have 101+102=203.


Sample Input 2

3 3

Sample Output 2

1818
C - Friends and Travel costs

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

10^{100}+1 個の村があり、それぞれ村 0, 村 1, \ldots, 村 10^{100} と番号がついています。
0 以上 10^{100}-1 以下の全ての整数 i について、村 i1 円を払う事で村 (i+1) に移動することができます。 それ以外の移動方法はありません。

太郎君は初め K 円を持った状態で村 0 におり、その後、可能な限り大きな番号の村まで進もうとします。
太郎君には N 人の友達がいます。i 人目の友達は村 A_i にいて、太郎君が村 A_i に着いたときに B_i 円を太郎君に渡します。
太郎君が最終的にたどり着く村の番号を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^{18}
  • 1 \leq B_i \leq 10^9
  • 入力は全て整数である。

入力

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

N K
A_1 B_1
:
A_N B_N

出力

答えを出力せよ。


入力例 1

2 3
2 1
5 10

出力例 1

4

太郎君は以下のように動きます:

  • 0 から村 11 円払って移動する。所持金は 2 円となる。
  • 1 から村 21 円払って移動する。所持金は 1 円となる。
  • 21 人目の友達から 1 円受け取り、所持金は 2 円となる。
  • 2 から村 31 円払って移動する。所持金は 1 円となる。
  • 3 から村 41 円払って移動する。所持金は 0 円となり、この村には友達もいないため村 4 で止まる。

よって、 4 を出力します。


入力例 2

5 1000000000
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000

出力例 2

6000000000

答えが 32 bit 整数に収まらないことがある事に注意してください。


入力例 3

3 2
5 5
2 1
2 2

出力例 3

10

同じ村に複数の友達がいる可能性もあります。

Score : 300 points

Problem Statement

There are 10^{100}+1 villages, labeled with numbers 0, 1, \ldots, 10^{100}.
For every integer i between 0 and 10^{100}-1 (inclusive), you can pay 1 yen (the currency) in Village i to get to Village (i + 1). There is no other way to travel between villages.

Taro has K yen and is in Village 0 now. He will try to get to a village labeled with as large a number as possible.
He has N friends. The i-th friend, who is in Village A_i, will give Taro B_i yen when he reaches Village A_i.
Find the number with which the last village he will reach is labeled.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^{18}
  • 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 K
A_1 B_1
:
A_N B_N

Output

Print the answer.


Sample Input 1

2 3
2 1
5 10

Sample Output 1

4

Takahashi will travel as follows:

  • Go from Village 0 to Village 1, for 1 yen. Now he has 2 yen.
  • Go from Village 1 to Village 2, for 1 yen. Now he has 1 yen.
  • Get 1 yen from the 1-st friend in Village 2. Now he has 2 yen.
  • Go from Village 2 to Village 3, for 1 yen. Now he has 1 yen.
  • Go from Village 3 to Village 4, for 1 yen. Now he has 0 yen, and he has no friends in this village, so his journey ends here.

Thus, we should print 4.


Sample Input 2

5 1000000000
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000

Sample Output 2

6000000000

Note that the answer may not fit into a 32-bit integer.


Sample Input 3

3 2
5 5
2 1
2 2

Sample Output 3

10

He may have multiple friends in the same village.

D - Pond

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

AtCoder 公園の敷地は東西南北に広がる N\times N のマス目からなっており、北から i 番目かつ西から j 番目のマスの高さは A_{i,j} で与えられます。

公園の管理者である高橋君はここに K \times K の区画の池を作る事にしました。

池を作るにあたって、高橋君は AtCoder 公園の敷地内に完全に含まれる K \times K の区画であってその区画に含まれるマスの高さの中央値が最も低いようなものを選ぼうと考えました。そのような区画のマスの高さの中央値を求めてください。

ここで、 K \times K の区画に含まれるマスの高さの中央値とはその区画に含まれる K^2 個のマスのうち \left\lfloor\frac{K^2}{2}\right\rfloor +1 番目に高いマスの高さを指します。また、\lfloor x\rfloorx 以下の最大の整数を表します。

制約

  • 1 \leq K \leq N \leq 800
  • 0 \leq A_{i,j} \leq 10^9
  • 入力は全て整数である。

入力

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

N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
:
A_{N,1} A_{N,2} \ldots A_{N,N}

出力

答えを出力せよ。


入力例 1

3 2
1 7 0
5 8 11
10 4 2

出力例 1

4

北から i 番目で西から j 番目のマスを (i,j) で表すとして、 池の候補となる 2\times 2 の区画は、\{(1,1),(1,2),(2,1),(2,2)\}, \{(1,2),(1,3),(2,2),(2,3)\}, \{(2,1),(2,2),(3,1),(3,2)\}, \{(2,2),(2,3),(3,2),(3,3)\}4 つです。
K=2 のとき、各区画に含まれるマスの高さの中央値は各区画に含まれるマスのうち \left\lfloor\frac{2^2}{2}\right\rfloor+1=3 番目に高いマスの高さとなるので、それぞれの区画の中央値は 5, 7, 5, 4 であり、このうち最小である 4 を出力します。


入力例 2

3 3
1 2 3
4 5 6
7 8 9

出力例 2

5

Score : 400 points

Problem Statement

The land of a park AtCoder is an N\times N grid with east-west rows and north-south columns. The height of the square at the i-th row from the north and j-th column from the west is given as A_{i,j}.

Takahashi, the manager, has decided to build a square pond occupying K \times K squares in this park.

To do this, he wants to choose a square section of K \times K squares completely within the park whose median of the heights of the squares is the lowest. Find the median of the heights of the squares in such a section.

Here, the median of the heights of the squares in a K \times K section is the height of the (\left\lfloor\frac{K^2}{2}\right\rfloor +1)-th highest square among the K^2 squares in the section, where \lfloor x\rfloor is the greatest integer not exceeding x.

Constraints

  • 1 \leq K \leq N \leq 800
  • 0 \leq A_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
:
A_{N,1} A_{N,2} \ldots A_{N,N}

Output

Print the answer.


Sample Input 1

3 2
1 7 0
5 8 11
10 4 2

Sample Output 1

4

Let (i,j) denote the square at the i-th row from the north and j-th column from the west. We have four candidates for the 2 \times 2 section occupied by the pond: \{(1,1),(1,2),(2,1),(2,2)\}, \{(1,2),(1,3),(2,2),(2,3)\}, \{(2,1),(2,2),(3,1),(3,2)\}, \{(2,2),(2,3),(3,2),(3,3)\}.
When K=2, since \left\lfloor\frac{2^2}{2}\right\rfloor+1=3, the median of the heights of the squares in a section is the height of the 3-rd highest square, which is 5, 7, 5, 4 for the candidates above, respectively. We should print the lowest of these: 4.


Sample Input 2

3 3
1 2 3
4 5 6
7 8 9

Sample Output 2

5
E - White Pawn

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N を正の整数とします。 行と列にそれぞれ 0 から 2N までの番号が付いた (2N+1)\times (2N+1) のマス目があり、行 i かつ列 j に属するマスを (i,j) で表します。

白のポーンが 1 つあり、最初 (0,N) に置かれています。 黒のポーンは M 個あり、i 個目の黒のポーンは (X_i, Y_i) に置かれています。

白のポーンが (i,j) にあるとき、あなたは以下のいずれかの操作で白のポーンを動かすことができます。

  • 0\leq i\leq 2N-1, 0 \leq j\leq 2N を満たし、(i+1,j) に黒のポーンが無いならば、白のポーンを (i+1,j) に動かす。
  • 0\leq i\leq 2N-1, 0 \leq j\leq 2N-1 を満たし、(i+1,j+1) に黒のポーンが有るならば、白のポーンを (i+1,j+1) に動かす。
  • 0\leq i\leq 2N-1, 1 \leq j\leq 2N を満たし、(i+1,j-1) に黒のポーンが有るならば、白のポーンを (i+1,j-1) に動かす。

黒のポーンは動かすことができません。

この操作を繰り返した結果、(2N,Y) に白のポーンが置かれている状態にできるような Y の値としてあり得るものの個数を求めてください。

制約

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 2\times 10^5
  • 1 \leq X_i \leq 2N
  • 0 \leq Y_i \leq 2N
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N M
X_1 Y_1
:
X_M Y_M

出力

答えを出力せよ。


入力例 1

2 4
1 1
1 2
2 0
4 2

出力例 1

3

(4,0), (4,1), (4,2)3 つへはそれぞれ次のように動かせます:

  • (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,2)
  • (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,1)
  • (0,2)\to (1,1)\to (2,0)\to (3,0)\to (4,0)

一方、 (4,3)(4,4) へは動かすことができません。 よって、 3 を出力します。


入力例 2

1 1
1 1

出力例 2

0

白のポーンを (0,1) から動かすことはできません。

Score : 500 points

Problem Statement

Let N be a positive integer. We have a (2N+1)\times (2N+1) grid where rows are numbered 0 through 2N and columns are also numbered 0 through 2N. Let (i,j) denote the square at Row i and Column j.

We have one white pawn, which is initially at (0, N). Also, we have M black pawns, the i-th of which is at (X_i, Y_i).

When the white pawn is at (i, j), you can do one of the following operations to move it:

  • If 0\leq i\leq 2N-1, 0 \leq j\leq 2N hold and (i+1,j) does not contain a black pawn, move the white pawn to (i+1, j).
  • If 0\leq i\leq 2N-1, 0 \leq j\leq 2N-1 hold and (i+1,j+1) does contain a black pawn, move the white pawn to (i+1,j+1).
  • If 0\leq i\leq 2N-1, 1 \leq j\leq 2N hold and (i+1,j-1) does contain a black pawn, move the white pawn to (i+1,j-1).

You cannot move the black pawns.

Find the number of integers Y such that it is possible to have the white pawn at (2N, Y) by repeating these operations.

Constraints

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 2\times 10^5
  • 1 \leq X_i \leq 2N
  • 0 \leq Y_i \leq 2N
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1
:
X_M Y_M

Output

Print the answer.


Sample Input 1

2 4
1 1
1 2
2 0
4 2

Sample Output 1

3

We can move the white pawn to (4,0), (4,1), and (4,2), as follows:

  • (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,2)
  • (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,1)
  • (0,2)\to (1,1)\to (2,0)\to (3,0)\to (4,0)

On the other hand, we cannot move it to (4,3) or (4,4). Thus, we should print 3.


Sample Input 2

1 1
1 1

Sample Output 2

0

We cannot move the white pawn from (0,1).

F - Weed

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君と青木君の家の庭には草 1, 草 2, \ldots, 草 NN 本の草が生えており、草 i の高さは A_i です。 高橋君と青木君は次の方法で庭の草抜きを行う事にしました。

  • まず、青木君が高々 K 本の草を選んで抜く。
  • その後、高橋君が次の操作を庭の草がすべて抜けるまで繰り返す。

    • 残っている草のうち高さが最大のものの高さを H とする。残っている草のうち、高さが \frac{H}{2} より高いものを一斉に抜く。

青木君は、高橋君の操作回数が最小となるようにした上で、自分の抜く本数を最小にしたいと考えています。 このときの高橋君の操作回数と青木君の抜く草の本数を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq K \leq N
  • 1 \leq A_i \leq 10^9
  • 入力は全て整数である。

入力

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

N K
A_1 A_2 \ldots A_N

出力

高橋君の操作回数と青木君の抜く草の本数をこの順に空白区切りで出力せよ。


入力例 1

4 1
2 3 4 9

出力例 1

2 1

例えば青木君が草 4 (高さ 9) を選んで抜いたとき、残りの草の中で最も高いものは草 3 であり、その高さは 4 です。 \frac{4}{2}=2 であり、2<3, 2<4 より 1 回目の操作で高橋君は草 2 と草 3 のみを抜くことができます。その後、 2 回目の操作で草 1 を抜き、高橋君は 2 回で操作を終えることができます。 一方で、青木君がどの草を 1 本選んだとしても高橋君は 1 回で操作を終えることはできません。

また、もし青木君が 1 本も抜かなかったとすると高橋君は 3 回操作する必要があるため、青木君は高橋君の操作回数を最小にするために最低 1 本は抜かなくてはなりません。


入力例 2

3 3
2 3 5

出力例 2

0 3

青木君が全ての草を抜いたとき高橋君は操作を行う必要がなく、明らかにこのときが最小です。


入力例 3

9 8
137 55 56 60 27 28 133 56 55

出力例 3

1 4

Score : 600 points

Problem Statement

Takahashi's and Aoki's garden is covered with N weeds, called Weed 1, Weed 2, \ldots, Weed N. The height of Weed i is A_i. Takahashi and Aoki have decided to pull these weeds, as follows:

  • First, Aoki will choose at most K weeds and pull them.
  • Then, Takahashi will repeat the following operation until all weeds are pulled.
    • Let H be the height of the tallest remaining weeds. Pull all weeds with heights above \frac{H}{2} at once.

Aoki wants to minimize the number of operations done by Takahashi. Also, he wants to minimize it by pulling the minimum number of weeds needed. Find the number of operations done by Takahashi and the number of weeds Aoki pulls in this case.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq K \leq N
  • 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 K
A_1 A_2 \ldots A_N

Output

Print the number of operations Takahashi will do and the number of weeds Aoki will pull, in this order, with a space in between.


Sample Input 1

4 1
2 3 4 9

Sample Output 1

2 1

For example, assume that Aoki chooses Weed 4, with height 9, and pulls it. Then, the tallest remaining weed is Weed 3, with height 4. We have \frac{4}{2}=2, and Takahashi will pull Weed 2 and 3 in the first operation, since 2<3 and 2<4. Then, he will pull Weed 1 in the second operation, completing his work in two operations. On the other hand, he will not complete his work in one operation, no matter which one weed Aoki chooses.

Also, if Aoki pulls no weed, Takahashi will have to do three operations, so Aoki must pull at least one weed to minimize the number of operations done by Takahashi.


Sample Input 2

3 3
2 3 5

Sample Output 2

0 3

If Aoki pulls all weeds, Takahashi has to do zero operations, which is obviously the smallest number possible.


Sample Input 3

9 8
137 55 56 60 27 28 133 56 55

Sample Output 3

1 4