A - Penalty

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君はあるコンテストに参加し、A 回の誤提出をしてから、開始 T 分後に正解したそうです。

以下の式で計算されるペナルティの値を求めてください。

5\times A + T

制約

  • 0 \leq A \leq 10
  • 1 \leq T \leq 100
  • 入力は全て整数

入力

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

A T

出力

答えを出力せよ。


入力例 1

3 10

出力例 1

25

高橋君は 3 回の誤提出をしてから、開始 10 分後に正解したので、ペナルティの値は 25 です。


入力例 2

1 8

出力例 2

13

入力例 3

0 10

出力例 3

10

Problem Statement

Takahashi participated in a contest. He made A wrong submissions before being accepted T minutes into the contest.

Compute the penalty value represented by the following expression:

5\times A + T.

Constraints

  • 0 \leq A \leq 10
  • 1 \leq T \leq 100
  • All values in the input are integers.

Input

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

A T

Output

Print the answer.


Sample Input 1

3 10

Sample Output 1

25

Takahashi made 3 wrong submissions before being accepted 10 minutes into the contest, so the penalty value is 25.


Sample Input 2

1 8

Sample Output 2

13

Sample Input 3

0 10

Sample Output 3

10
B - Hall of Fame

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

競技プログラミングのコンテストを主催するサイトの 1 つである AtCoder では、Rating最高値が 2800 以上になると「殿堂入り」します。
高橋くんの AtCoder の現在の Rating は C 、Rating最高値は H です。
高橋くんが「殿堂入り」しているなら o 、していないなら x と出力してください。

制約

  • C,H は整数
  • 0 \le C \le H \le 4400

入力

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

C H

出力

問題文の指示に従って出力せよ。


入力例 1

3085 3085

出力例 1

o

高橋くんの現在の Rating は 3085 、Rating最高値も 3085 です。
よって、高橋くんは殿堂入りしています。


入力例 2

2580 2631

出力例 2

x

入力例 3

0 2800

出力例 3

o

たとえ現在の Rating が 2800 未満でも、 Rating最高値が 2800 以上であれば高橋くんは殿堂入りしています。

Problem Statement

In AtCoder, the programming contest hosting website, those whose maximum rating is 2800 or greater is in the "hall of fame".
Takahashi's current rating is C and the maximum rating is H.
Print o if he is in the "hall of fame", and x otherwise.

Constraints

  • C and H are integers.
  • 0 \le C \le H \le 4400

Input

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

C H

Output

Print the answer as instructed in the problem statement.


Sample Input 1

3085 3085

Sample Output 1

o

Takahashi's current rating is 3085, and his maximum rating is 3085 too.
Thus, Takahashi is in the hall of fame.


Sample Input 2

2580 2631

Sample Output 2

x

Sample Input 3

0 2800

Sample Output 3

o

Even if his current rating is less than 2800, he is still in the hall of fame as long as his maximum rating is 2800 or greater.

C - Drawing Circle

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

2 次元平面上に点 (X, Y) を中心とする半径 R の円があります。
整数の組 (i, j) について、点 (i, j) が円の内部あるいは周上にあるとき S_{i,j} = #、そうでないとき S_{i,j} = . と定めます。
-N \leq i,j \leq N を満たす整数の組 (i, j) について、S_{i,j} を出力してください。

制約

  • -100 \leq X, Y \leq 100
  • 1 \leq R \leq 100
  • 1 \leq N \leq 200
  • S_{i,j} = # ならば -N \leq i,j \leq N
  • 入力される値はすべて整数

入力

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

X Y R N

出力

2N+1 行出力せよ。i 行目には、S_{i-N-1,-N}, S_{i-N-1,-N+1}, \ldots, S_{i-N-1,N-1}, S_{i-N-1,N} をこの順に空白区切りで出力せよ。


入力例 1

1 -2 1 3

出力例 1

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

2 次元平面上に点 (1,-2) を中心とする半径 1 の円があります。
(i,j)(0,-2),(1,-3),(1,-2),(1,-1),(2,-2) のいずれかのとき S_{i,j} = #、そうでないとき S_{i,j} = .です。


入力例 2

-3 1 4 8

出力例 2

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

入力例 3

0 0 10 10

出力例 3

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

Problem Statement

On a two-dimensional plane, there is a circle of radius R centered at point (X, Y).
For an integer pair (i, j), let us define S_{i,j} = # if point (i, j) is in the interior or on the circumference of the circle, and S_{i,j} = . otherwise.
Print S_{i,j} for all integer pairs (i, j) such that -N \leq i,j \leq N.

Constraints

  • -100 \leq X, Y \leq 100
  • 1 \leq R \leq 100
  • 1 \leq N \leq 200
  • If S_{i,j} = #, then -N \leq i,j \leq N.
  • All values in the input are integers.

Input

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

X Y R N

Output

Print (2N+1) lines. The i-th line should contain S_{i-N-1,-N}, S_{i-N-1,-N+1}, \ldots, S_{i-N-1,N-1}, and S_{i-N-1,N} in this order, separated by spaces.


Sample Input 1

1 -2 1 3

Sample Output 1

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

On a two-dimensional plane, there is a circle of radius 1 centered at point (1,-2).
We have S_{i,j} = # if (i,j) is one of (0,-2),(1,-3),(1,-2),(1,-1), and (2,-2), and S_{i,j} = . otherwise.


Sample Input 2

-3 1 4 8

Sample Output 2

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

Sample Input 3

0 0 10 10

Sample Output 3

. . . . . . . . . . # . . . . . . . . . .
. . . . . . # # # # # # # # # . . . . . .
. . . . # # # # # # # # # # # # # . . . .
. . . # # # # # # # # # # # # # # # . . .
. . # # # # # # # # # # # # # # # # # . .
. . # # # # # # # # # # # # # # # # # . .
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
# # # # # # # # # # # # # # # # # # # # #
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
. # # # # # # # # # # # # # # # # # # # .
. . # # # # # # # # # # # # # # # # # . .
. . # # # # # # # # # # # # # # # # # . .
. . . # # # # # # # # # # # # # # # . . .
. . . . # # # # # # # # # # # # # . . . .
. . . . . . # # # # # # # # # . . . . . .
. . . . . . . . . . # . . . . . . . . . .
D - Recorder

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君はあるテレビ番組(以下, 番組と表記)をレコーダーで録画していようとしています。
時刻 0 の時点で番組は時刻 A から時刻 A + R まで放送される予定です。しかし、番組編成の都合によって番組の放送時間は変更になることがあります。そこで高橋君は、番組を漏れなく録画するために以下の行動を取ることにしました。

  • 時刻 0 以降(時刻 0 を含む)のすべての D の倍数の時刻に以下の操作を行う。(この操作に時間はかからない。)
    • 現在の時刻を t, 時刻 t 時点での番組の放送が始まる予定の時刻を p, 放送が終わる予定の時刻を q とする。t \lt q である場合、時刻 \max(p, t) から時刻 q までの期間を録画予約する。

ただし、高橋君の持っているレコーダーは複数の録画予約を扱うことができるのに注意してください。つまり、すでに 1 個以上の録画予約が存在するときに、新たに高橋君が録画予約を行った場合でも、それ以前に行った録画予約は取り消されず、すべての録画予約が遂行されます。

前述の通り、時刻 0 の時点で番組は時刻 A から時刻 A + R まで放送される予定でした。ところが時刻 B に、番組の放送時刻が時刻 C から時刻 C + R までに変更されました。(ここで B < A, B < C, A \neq C かつ A,B,C はいずれも D の倍数でないことが保証されます。)

高橋君が番組を漏れなく録画できるかを判定してください。より厳密に述べると、以下の命題が真であるかを判定してください。

  • C 以上 C + R 以下のすべての時刻 t に対して、次の条件を満たす録画予約が少なくとも 1 つ存在する。
    • 録画予約が時刻 p から時刻 q までを期間とするとき、p \leq t \leq q が成り立つ。

制約

  • 1 \leq A, B, C, D, R \leq 1000
  • A, B, C, D, R は整数
  • B \lt \min(A, C)
  • A \neq C
  • A, B, C はいずれも D の倍数でない

入力

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

A B C D R

出力

問題文にある命題が真である場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

5 3 15 20 20

出力例 1

Yes

高橋君の行動を説明すると次のようになります。

  • 時刻 0 の時点で番組は時刻 5 から時刻 25 まで放送される予定です。
  • 高橋君は時刻 0 に時刻 5 から時刻 25 までの期間を録画予約します。
  • 時刻 3 に番組の放送予定時刻が時刻 15 から時刻 35 までに変更されます。
  • 高橋君は時刻 20 に時刻 20 から時刻 35 までの期間を録画予約します。
  • 時刻 40 以降は番組の放送が終わる予定の時刻を過ぎているため録画予約を行いません。

15 \leq t \leq 20 の期間は 1 回目の録画予約で、20 \leq t \leq 35 の期間は 2 回目の録画予約でカバーされています。よって答えは Yes です。


入力例 2

5 3 27 30 20

出力例 2

No

入力例 3

930 542 605 509 582

出力例 3

No

Problem Statement

Takahashi is trying to record a certain television program (hereafter called "the program") with a recorder.
At time 0, the program is scheduled to be broadcasted from time A to time A + R. However, the broadcast time of the program may change due to programming circumstances. Therefore, Takahashi has decided to take the following actions to record the entire program.

  • Perform the following operation at all times that are multiples of D after time 0 (including time 0). (This operation takes no time.)
    • Let t be the current time, and p and q be the program's start and end times scheduled at time t, respectively. If t \lt q, reserve to record from time \max(p, t) to time q.

Here, note that Takahashi's recorder can handle multiple recording reservations. In other words, even if Takahashi makes a new recording reservation when there is already one or more recording reservations, the previous recording reservations are not canceled, and all recording reservations are carried out.

As mentioned earlier, the program was scheduled to be broadcasted from time A to time A + R at time 0. However, at time B, the broadcast time of the program was changed to be from time C to time C + R. (Here, it is guaranteed that B < A, B < C, A \neq C, and none of A, B, C is a multiple of D.)

Please determine whether Takahashi can record the entire program. More precisely, determine whether the following proposition is true.

  • For all times t between C and C + R, inclusive, there is at least one recording reservation that satisfies the following conditions.
    • If the recording reservation is from time p to time q, it holds that p \leq t \leq q.

Constraints

  • 1 \leq A, B, C, D, R \leq 1000
  • A, B, C, D, and R are integers.
  • B \lt \min(A, C)
  • A \neq C
  • None of A, B, and C is a multiple of D.

Input

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

A B C D R

Output

If the proposition in the problem statement is true, print Yes; otherwise, print No.

Sample Explanation


Sample Input 1

5 3 15 20 20

Sample Output 1

Yes

Takahashi's actions can be described as follows.

  • At time 0, the program is scheduled to be broadcasted from time 5 to time 25.
  • Takahashi reserves to record from time 5 to time 25 at time 0.
  • At time 3, the scheduled broadcast time of the program is changed to be from time 15 to time 35.
  • Takahashi reserves to record from time 20 to time 35 at time 20.
  • Takahashi does not make any recording reservations after time 40 as it is past the scheduled end time of the program.

The period 15 \leq t \leq 20 is covered by the first recording reservation, and the period 20 \leq t \leq 35 is covered by the second recording reservation. Therefore, the answer is Yes.


Sample Input 2

5 3 27 30 20

Sample Output 2

No

Sample Input 3

930 542 605 509 582

Sample Output 3

No
E - Total Score

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

あるプログラミングコンテストでは N 問の問題が出題され、そのうち i 問目に正解すると A_i 点が与えられます。

この N 問の中からちょうど K 問を選んで解くことを考えます。
「選んだ問題を全て解いた時に得られる合計得点」を全ての選び方に対して求めたとき、その和はいくつになりますか?

制約

  • 入力は全て整数
  • 1 \le K \le N \le 8
  • 1 \le A_i \le 2718

入力

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

N K
A_1 A_2 \dots A_N

出力

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


入力例 1

4 2
100 200 300 400

出力例 1

3000

このコンテストでは 4 問の問題が出題され、このうち 2 問を解くことを考えます。選び方は以下の 6 通りです。

  • 1 問目と 2 問目を解く。得られる合計得点は 300 点である。
  • 1 問目と 3 問目を解く。得られる合計得点は 400 点である。
  • 1 問目と 4 問目を解く。得られる合計得点は 500 点である。
  • 2 問目と 3 問目を解く。得られる合計得点は 500 点である。
  • 2 問目と 4 問目を解く。得られる合計得点は 600 点である。
  • 3 問目と 4 問目を解く。得られる合計得点は 700 点である。

これらの合計得点の和は 3000 点です。


入力例 2

1 1
2718

出力例 2

2718

入力例 3

8 4
597 1035 2048 994 1843 601 7 659

出力例 3

272440

Problem Statement

In a certain programming contest, N problems are set, and if you answer the i-th problem correctly, you are given A_i points.

Consider choosing and solving exactly K problems out of these N problems.
When you calculate the "total score obtained when all the selected problems are solved" for all ways of choosing problems, what is the sum of those total scores?

Constraints

  • All input values are integers.
  • 1 \le K \le N \le 8
  • 1 \le A_i \le 2718

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

4 2
100 200 300 400

Sample Output 1

3000

In this contest, 4 problems are set, and we consider solving 2 of them. There are 6 ways to choose the problems as follows.

  • Solve the 1-st and 2-nd problems. The total score obtained is 300 points.
  • Solve the 1-st and 3-rd problems. The total score obtained is 400 points.
  • Solve the 1-st and 4-th problems. The total score obtained is 500 points.
  • Solve the 2-nd and 3-rd problems. The total score obtained is 500 points.
  • Solve the 2-nd and 4-th problems. The total score obtained is 600 points.
  • Solve the 3-rd and 4-th problems. The total score obtained is 700 points.

The sum of these total scores is 3000 points.


Sample Input 2

1 1
2718

Sample Output 2

2718

Sample Input 3

8 4
597 1035 2048 994 1843 601 7 659

Sample Output 3

272440
F - Numbering

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の数列 A が与えられるので、以下の条件を全て満たす数列 B を求めてください。
なお、この数列 B は一意に定まることが示せます。

  • B(1,2,...,N) を並べ替えて作ることのできる数列である
  • 全ての整数組 (i,j)(i \neq j) について以下を満たす
    • もし A_i<A_j なら、 B_i<B_j
    • もし A_i=A_j かつ i<j なら、 B_i<B_j

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N
A_1 A_2 \dots A_N

出力

答えを以下の形式で出力せよ。

B_1 B_2 \dots B_N

入力例 1

10
2 3 4 4 4 4 3 3 2 1

出力例 1

2 4 7 8 9 10 5 6 3 1

数列 A=(2,3,4,4,4,4,3,3,2,1) であるとき、数列 B=(2,4,7,8,9,10,5,6,3,1) です。


入力例 2

1
1000000000

出力例 2

1

入力例 3

20
30 10 40 10 50 90 20 60 50 30 50 80 90 70 90 30 20 30 80 40

出力例 3

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

Problem Statement

Given a sequence A of length N, find a sequence B that satisfies all of the following conditions.
We can prove that this sequence B is unique.

  • B is a sequence obtained by rearranging (1,2,...,N).
  • For all integer pairs (i,j)(i \neq j), the following conditions are satisfied:
    • if A_i<A_j, then B_i<B_j;
    • if A_i=A_j and i<j, then B_i<B_j.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer in the following format.

B_1 B_2 \dots B_N

Sample Input 1

10
2 3 4 4 4 4 3 3 2 1

Sample Output 1

2 4 7 8 9 10 5 6 3 1

When the sequence A=(2,3,4,4,4,4,3,3,2,1), the sequence B=(2,4,7,8,9,10,5,6,3,1).


Sample Input 2

1
1000000000

Sample Output 2

1

Sample Input 3

20
30 10 40 10 50 90 20 60 50 30 50 80 90 70 90 30 20 30 80 40

Sample Output 3

5 1 9 2 11 18 3 14 12 6 13 16 19 15 20 7 4 8 17 10
G - N-SAT

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 個の変数 x_1,x_2,\ldots,x_N にそれぞれ 0 または 1 を割り当てる方法であって、次の条件を満たすものが存在するかどうかを判定してください。

  • i=1,2,\ldots,M すべてに対し、次の条件を満たす。
    • x_{a_{i,j}}=b_{i,j} を満たす整数 j (1 \leq j \leq k_i) が存在する。

制約

  • 1 \leq N \leq 15
  • 1 \leq M \leq 100
  • 1 \leq k_i \leq N
  • 1 \leq a_{i,1} \lt a_{i,2} \lt \ldots \lt a_{i,k_i} \leq N
  • b_{i,j} \in \{0,1 \}
  • 入力はすべて整数

入力

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

N M
k_1
a_{1,1} b_{1,1}
\vdots
a_{1,k_1} b_{1,k_1}
\vdots
k_M
a_{M,1} b_{M,1}
\vdots
a_{M,k_M} b_{M,k_M}

出力

条件を満たす割り当て方が存在するならば Yes と、存在しなければ No と出力せよ。


入力例 1

5 3
1
4 0
1
5 0
2
2 0
3 1

出力例 1

Yes

一例として、x_1=1, x_2=0,x_3=0,x_4=0,x_5=0 とすると条件を満たします。


入力例 2

1 2
1
1 0
1
1 1

出力例 2

No

Problem Statement

Determine if there is a way to assign 0 or 1 to each of N variables x_1,x_2,\ldots,x_N so that:

  • for all i=1,2,\ldots,M,
    • there exists an integer j (1 \leq j \leq k_i) such that x_{a_{i,j}}=b_{i,j}.

Constraints

  • 1 \leq N \leq 15
  • 1 \leq M \leq 100
  • 1 \leq k_i \leq N
  • 1 \leq a_{i,1} \lt a_{i,2} \lt \ldots \lt a_{i,k_i} \leq N
  • b_{i,j} \in \{0,1 \}
  • All values in the input are integers.

Input

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

N M
k_1
a_{1,1} b_{1,1}
\vdots
a_{1,k_1} b_{1,k_1}
\vdots
k_M
a_{M,1} b_{M,1}
\vdots
a_{M,k_M} b_{M,k_M}

Output

Print Yes if there is a way to assign values, and No otherwise.


Sample Input 1

5 3
1
4 0
1
5 0
2
2 0
3 1

Sample Output 1

Yes

For example, assigning x_1=1, x_2=0,x_3=0,x_4=0, and x_5=0 satisfies the condition.


Sample Input 2

1 2
1
1 0
1
1 1

Sample Output 2

No
H - Magical Sticks Again

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

正整数 N1 つ以上の相異なる正整数の和で表すとき、最大でいくつの正整数を用いて表すことができますか?

制約

  • N1 以上 10^{18} 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

7

出力例 1

3

7=1+2+4 と表すことができます。

74 個以上の異なる正整数の和によって表すことはできないため、3 が答えです。


入力例 2

1

出力例 2

1

入力例 3

164312478120341038

出力例 3

573258192

Problem Statement

When you represent an integer N as the sum of one or more distinct positive integers, at most how many positive integers can be used?

Constraints

  • N is an integer between 1 and 10^{18}, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

7

Sample Output 1

3

We can represent 7 as 1+2+4.

Since we cannot represent 7 as the sum of four or more distinct positive integers, 3 is the answer.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

164312478120341038

Sample Output 3

573258192
I - Maximum GCD

Time Limit: 1 sec / Memory Limit: 1024 MiB

問題文

N 個の整数 A_1,A_2,\ldots,A_N が与えられます。

この中から K 個を選ぶとき、それらの最大公約数の最大値を求めてください。

制約

  • 1 \leq K \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^6
  • 入力は全て整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

最大公約数の最大値を出力せよ。


入力例 1

3 2
2 3 4

出力例 1

2

24 を選んだとき最大公約数は 2 となり、最大です。


入力例 2

3 3
5 5 5

出力例 2

5

入力例 3

10 4
225707 265514 765350 617700 837712 187034 799142 35549 784661 961512

出力例 3

2

Problem Statement

You are given N integers A_1, A_2, \ldots, A_N.

Consider choosing K of them. Find the maximum possible greatest common divisor of the chosen integers.

Constraints

  • 1 \leq K \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^6
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Print the maximum possible greatest common divisor.


Sample Input 1

3 2
2 3 4

Sample Output 1

2

If you choose 2 and 4, the greatest common divisor will be 2, which is the maximum.


Sample Input 2

3 3
5 5 5

Sample Output 2

5

Sample Input 3

10 4
225707 265514 765350 617700 837712 187034 799142 35549 784661 961512

Sample Output 3

2
J - Ninja

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

ある忍者が N 体の魔物と一体ずつ順番に戦います。i 番目の魔物の攻撃力は a_i、体力は b_i であり、魔物は体力が 0 以下になった瞬間に倒れます。
なお、魔物は地上にいるか飛んでいるかのどちらかであり、x_i = 0 なら i 番目の魔物は地上におり、x_i = 1 なら飛んでいます。

忍者は初め、M 枚の手裏剣を持っています。それぞれの手裏剣は一度投げると二度と使えません。
忍者と i 番目の魔物は以下のように戦います。

  • まず、忍者が持っている手裏剣から 0 枚以上を選んで魔物に投げる。投げた枚数だけ魔物の体力が減り、飛んでいる魔物に 1 枚以上の手裏剣を投げた場合、その魔物は地上に降りる。
  • その後、忍者と魔物が交互に相手を攻撃する。忍者の攻撃により魔物の体力は 1 減り、魔物の攻撃により忍者の体力は a_i 減る。なお、魔物が地上にいるなら忍者が先に攻撃し、飛んでいるなら魔物が先に攻撃する。

忍者の最初の体力と、全ての魔物を倒し終わった後の体力の差としてあり得る最小値を求めてください。
なお、忍者の体力は十分に大きいとします。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq M \leq 10^{11}
  • 1 \leq a_i, b_i \leq 3 \times 10^5
  • x_i = 0 または x_i = 1
  • 入力される値は全て整数

入力

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

N M
a_1 b_1 x_1
a_2 b_2 x_2
\vdots
a_N b_N x_N

出力

答えを出力せよ。


入力例 1

2 1
2 2 1
5 2 0

出力例 1

4

忍者が以下のように戦うと、忍者の体力は合計で 4 減少し、これが最適です。

  • 1 番目の魔物には手裏剣を投げない。
    • 魔物は飛んでいるので、魔物が先に忍者を攻撃する。忍者の体力が 2 減る。
    • 忍者が攻撃する。魔物の体力が 1 になる。
    • 魔物が攻撃する。忍者の体力が 2 減る。
    • 忍者が攻撃する。魔物は体力が 0 になり、倒れる。
  • 2 番目の魔物に手裏剣を 1 枚投げる。魔物の体力は 1 になる。
    • 魔物は地上にいるので、忍者が先に魔物を攻撃する。魔物は体力が 0 になり、倒れる。

入力例 2

1 2
1 4 1

出力例 2

1

忍者が以下のように戦うと、忍者の体力は合計で 1 減少し、これが最適です。

  • 1 番目の魔物に手裏剣を 2 枚投げる。魔物の体力は 2 になり、地上に降りる。
    • 魔物は地上にいるので、忍者が先に魔物を攻撃する。魔物の体力が 1 になる。
    • 魔物が攻撃する。忍者の体力が 1 減る。
    • 忍者が攻撃する。魔物は体力が 0 になり、倒れる。

入力例 3

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

出力例 3

25

Problem Statement

A ninja will fight against N monsters one by one in order. The attack power of the i-th monster is a_i, and its health is b_i. A monster is immediately defeated when its health becomes 0 or less.
Each monster is either on the ground or flying. If x_i = 0, the i-th monster is on the ground, and if x_i = 1, it is flying.

The ninja initially has M shurikens. Each shuriken can be used only once by throwing it.
The ninja and the i-th monster fight as follows:

  • First, the ninja chooses zero or more shurikens from the ones he has and throws them at the monster. The monster's health decreases by the number of shurikens thrown, and if one or more shurikens are thrown at a flying monster, it lands on the ground.
  • Then, the ninja and the monster take turns attacking each other. Each attack by the ninja decreases the monster's health by 1, and each attack by the monster decreases the ninja's health by a_i. Here, if the monster is on the ground, the ninja attacks first, and if it is flying, the monster attacks first.

Find the minimum possible difference between the ninja's initial health and his health after defeating all the monsters.
Assume that the ninja's health is sufficiently large.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq M \leq 10^{11}
  • 1 \leq a_i, b_i \leq 3 \times 10^5
  • x_i = 0 or x_i = 1.
  • All input values are integers.

Input

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

N M
a_1 b_1 x_1
a_2 b_2 x_2
\vdots
a_N b_N x_N

Output

Print the answer.


Sample Input 1

2 1
2 2 1
5 2 0

Sample Output 1

4

If the ninja fights as follows, his health will decrease by a total of 4, which is optimal:

  • Do not throw any shurikens at the first monster.
    • Since the monster is flying, it attacks the ninja first. The ninja's health decreases by 2.
    • The ninja attacks. The monster's health becomes 1.
    • The monster attacks. The ninja's health decreases by 2.
    • The ninja attacks. The monster's health becomes 0, and it is defeated.
  • Throw one shuriken at the second monster. The monster's health becomes 1.
    • Since the monster is on the ground, the ninja attacks first. The monster's health becomes 0, and it is defeated.

Sample Input 2

1 2
1 4 1

Sample Output 2

1

If the ninja fights as follows, his health will decrease by a total of 1, which is optimal:

  • Throw two shurikens at the first monster. The monster's health becomes 2, and it lands on the ground.
    • Since the monster is on the ground, the ninja attacks first. The monster's health becomes 1.
    • The monster attacks. The ninja's health decreases by 1.
    • The ninja attacks. The monster's health becomes 0, and it is defeated.

Sample Input 3

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

Sample Output 3

25
K - Swap to Sort

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

(1,2,\ldots,N) の並び替えである長さ N の数列 P=(P_1,P_2,\ldots,P_N) があります。

あなたはこの数列に対して、以下の操作を 0 回以上の好きな回数だけ行うことができます。

  • 1 以上 N-1 以下の整数 i を選び、Pi 番目の要素と i+1 番目の要素を入れ替える。この操作には、入れ替える 2 数の和に等しいコストがかかる。

P を昇順にソートするために必要な最小コストを求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • N は整数である
  • P(1,2,\ldots,N) を並び替えた数列である

入力

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

N
P_1 P_2 \ldots P_N

出力

答えを出力せよ。


入力例 1

4
3 2 1 4

出力例 1

12

例えば次のように 3 回の操作を行うことで P を昇順にソートすることができます。

  • i=1 と選び、32 を入れ替える。5 のコストがかかり、数列は (2,3,1,4) になる。
  • i=2 と選び、31 を入れ替える。4 のコストがかかり、数列は (2,1,3,4) になる。
  • i=1 と選び、21 を入れ替える。3 のコストがかかり、数列は (1,2,3,4) になる。

コスト 12 未満で数列を昇順にソートすることはできないため、答えは 12 になります。


入力例 2

9
3 1 4 5 9 2 6 8 7

出力例 2

96

入力例 3

6
1 2 3 4 5 6

出力例 3

0

操作をする必要がないこともあります。

Problem Statement

There is a sequence of length N, P=(P_1,P_2,\ldots,P_N), which is a permutation of (1,2,\ldots,N).

You can perform the following operation on this sequence any number of times, possibly zero:

  • Choose an integer i between 1 and N-1, inclusive, and swap the i-th element and the (i+1)-th element of P. The cost of this operation is the sum of the two numbers being swapped.

Find the minimum cost required to sort P in ascending order.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • N is an integer.
  • P is a permutation of (1,2,\ldots,N).

Input

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

N
P_1 P_2 \ldots P_N

Output

Print the answer.


Sample Input 1

4
3 2 1 4

Sample Output 1

12

For example, you can sort P in ascending order by performing the following three operations:

  • Choose i=1 to swap 3 and 2. This costs 5, and the sequence becomes (2,3,1,4).
  • Choose i=2 to swap 3 and 1. This costs 4, and the sequence becomes (2,1,3,4).
  • Choose i=1 to swap 2 and 1. This costs 3, and the sequence becomes (1,2,3,4).

Since it is impossible to sort the sequence in ascending order with a cost less than 12, the answer is 12.


Sample Input 2

9
3 1 4 5 9 2 6 8 7

Sample Output 2

96

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

0

Sometimes, no operations are needed.

L - XOR Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

HW 列の非負整数からなる行列 A が与えられます。

あなたは以下の操作を任意の回数(0 回でもよい)行うことができます。

  • 1 \le i \le H を満たす整数 i と非負整数 X を選んで、行列 Ai 行目の各要素 A_{i,j}A_{i,j}\ \mathrm{XOR}\ X で置き換える。
  • 1 \le j \le W を満たす整数 j と非負整数 X を選んで、行列 Aj 列目の各要素 A_{i,j}A_{i,j}\ \mathrm{XOR}\ X で置き換える。

HW 列の非負整数および -1 からなる行列 B が与えられます。

操作によって B_{i,j} \neq -1 となる整数の組 (i,j) 全てに対して A_{i,j}=B_{i,j} となるようにすることができるか判定してください。

\mathrm{XOR} とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 1 \le H,W \le 500
  • 0 \le A_{i,j} < 2^{30}
  • -1 \le B_{i,j} < 2^{30}
  • 入力は全て整数である。

入力

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
B_{1,1} B_{1,2} \dots B_{1,W}
B_{2,1} B_{2,2} \dots B_{2,W}
\vdots
B_{H,1} B_{H,2} \dots B_{H,W}

出力

条件を満たすことができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

2 2
1 2
3 4
-1 3
1 7

出力例 1

Yes

始め、A は以下のようになっています。

1 2
3 4

2 列目に対して X=1 として操作を行うと、以下のようになります。

1 3
3 5

2 行目に対して X=2 として操作を行うと、以下のようになります。

1 3
1 7

よって、条件を満たすことができるため Yes を出力します。


入力例 2

2 2
1 2
3 4
5 3
1 7

出力例 2

No

この場合、どのようにしても条件を満たすことができないため No を出力します。

Problem Statement

You are given an H-by-W matrix A consisting of non-negative integers.

You can perform the following operations any number of times (possibly zero):

  • Choose an integer i satisfying 1 \le i \le H and a non-negative integer X, and replace each element A_{i,j} in the i-th row of the matrix A with A_{i,j}\ \mathrm{XOR}\ X.
  • Choose an integer j satisfying 1 \le j \le W and a non-negative integer X, and replace each element A_{i,j} in the j-th column of the matrix A with A_{i,j}\ \mathrm{XOR}\ X.

You are also given an H-by-W matrix B consisting of non-negative integers and -1.

Determine if it is possible to make A_{i,j}=B_{i,j} for all pairs of integers (i,j) such that B_{i,j} \neq -1 by performing the operations.

What is \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A, B, denoted as A \oplus B, is defined as follows:

  • The 2^k (k \geq 0) place of A \oplus B in binary notation is 1 if exactly one of the 2^k places of A and B in binary notation is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary notation: 011 \oplus 101 = 110).
In general, the bitwise \mathrm{XOR} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proven that this does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 1 \le H,W \le 500
  • 0 \le A_{i,j} < 2^{30}
  • -1 \le B_{i,j} < 2^{30}
  • All input values are integers.

Input

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
B_{1,1} B_{1,2} \dots B_{1,W}
B_{2,1} B_{2,2} \dots B_{2,W}
\vdots
B_{H,1} B_{H,2} \dots B_{H,W}

Output

If it is possible to satisfy the condition, print Yes; otherwise, print No.


Sample Input 1

2 2
1 2
3 4
-1 3
1 7

Sample Output 1

Yes

Initially, A is as follows:

1 2
3 4

Performing the operation with X=1 on the second column, we get:

1 3
3 5

Performing the operation with X=2 on the second row, we get:

1 3
1 7

Thus, it is possible to satisfy the condition, so print Yes.


Sample Input 2

2 2
1 2
3 4
5 3
1 7

Sample Output 2

No

In this case, it is impossible to satisfy the condition no matter what, so print No.

M - Distance on the Line

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

数直線上に 1 から N までの番号がついた N 個の点があります。N 個の点は次の条件を満たします。

  • i と点 i+1 の距離は d_i である。(1 \leq i \leq N - 1)

このとき、点 1 と点 N の距離としてあり得る最小値を求めてください。

制約

  • 2 \leq N \leq 1000
  • 1 \leq d_i \leq 2000
  • 入力される値はすべて整数

入力

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

N
d_1 d_2 \dots d_{N-1}

出力

答えを出力せよ。


入力例 1

4
5 2 4

出力例 1

1

i が存在している地点の座標を x_i とします。例えば (x_1,x_2,x_3,x_4)=(0,-5,-3,1) は条件を満たして、このとき点 1 と点 4 の距離は 1 です。


入力例 2

10
314 159 265 358 979 323 846 264 338

出力例 2

2

Problem Statement

There are N points on a number line, numbered from 1 to N. The N points satisfy the following condition:

  • The distance between point i and point i+1 is d_i. (1 \leq i \leq N - 1)

Find the minimum possible distance between point 1 and point N.

Constraints

  • 2 \leq N \leq 1000
  • 1 \leq d_i \leq 2000
  • All input values are integers.

Input

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

N
d_1 d_2 \dots d_{N-1}

Output

Print the answer.


Sample Input 1

4
5 2 4

Sample Output 1

1

Let x_i be the coordinate of the point i. For example, (x_1,x_2,x_3,x_4)=(0,-5,-3,1) satisfies the condition, and in this case, the distance between point 1 and point 4 is 1.


Sample Input 2

10
314 159 265 358 979 323 846 264 338

Sample Output 2

2
N - Frequency Table

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

0 以上 M 未満の N 個の実数 x_1,\ldots,x_N があります。

これら N 個のうち、値が k 以上 k+1 未満であるようなものが C_k 個あることがわかっています (k=0,1,\ldots,M-1)

あなたは、x_1,\ldots,x_N の中央値と平均値の差の絶対値がどのくらい小さくなるか気になりました。

x_1,\ldots,x_N の中央値と平均値の差の絶対値は b 未満にならない」と言えるような最大の b を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq C_k
  • C_0+\ldots+C_{M-1}=N
  • 入力は全て整数である

入力

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

N M
C_0 C_1 \ldots C_{M-1}

出力

答えを出力せよ。
なお、真の解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

3 3
0 1 2

出力例 1

0.0

例えば x_1=1.5, x_2=2, x_3=2.5 のとき、これらの中央値と平均値はともに 2 になり、差の絶対値は 0 となります。
絶対値が 0 未満になることはないので、「中央値と平均値の差の絶対値は b 未満にならない」と言えるような最大の b0 となります。


入力例 2

4 5
2 1 0 0 1

出力例 2

0.25

例えば x_1=4, x_2=1-2\times 10^{-100}, x_3=0, x_4=2-2\times 10^{-100} のとき、これらの中央値は 1.5-2\times 10^{-100}、平均値は 1.75-10^{-100} となり、差の絶対値は 0.25+10^{-100} となります。
このように中央値と平均値の差の絶対値として 0.25 よりわずかに大きな値はありえますが、0.25 未満にはならないことが証明できます。よって答えは 0.25 となります。


入力例 3

10 10
1 0 0 0 0 0 0 0 0 9

出力例 3

0.4

真の解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解となります。

Problem Statement

There are N real numbers x_1,\ldots,x_N between 0 (inclusive) and M (exclusive).

Among these N numbers, it is known that there are C_k numbers between k (inclusive) and k+1 (exclusive) for k=0,1,\ldots,M-1.

You are curious about how small the absolute difference between the median and the mean of x_1,\ldots,x_N can be.

Find the largest b such that the absolute difference between the median and the mean of x_1,\ldots,x_N cannot be smaller than b.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq C_k
  • C_0+\ldots+C_{M-1}=N
  • All input values are integers.

Input

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

N M
C_0 C_1 \ldots C_{M-1}

Output

Print the answer.
Your output will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

3 3
0 1 2

Sample Output 1

0.0

For example, when x_1=1.5, x_2=2, x_3=2.5, the median and the mean of these numbers are both 2, and the absolute difference is 0.
Since the absolute difference cannot be smaller than 0, the largest b such that the absolute difference between the median and the mean cannot be smaller than b is 0.


Sample Input 2

4 5
2 1 0 0 1

Sample Output 2

0.25

For example, when x_1=4, x_2=1-2\times 10^{-100}, x_3=0, x_4=2-2\times 10^{-100}, the median is 1.5-2\times 10^{-100}, the mean is 1.75-10^{-100}, and the absolute difference is 0.25+10^{-100}.
As seen here, the absolute difference between the median and the mean can be slightly larger than 0.25, but we can prove that it cannot be smaller than 0.25. Therefore, the answer is 0.25.


Sample Input 3

10 10
1 0 0 0 0 0 0 0 0 9

Sample Output 3

0.4

Your output will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.

O - Number Sequence and Prime

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

整数 N,A,B が与えられるので、以下の問題を解いてください。

長さ N の等差数列 S=(S_1,S_2,\dots,S_N)S_i=A \times (i-1)+B として定義されます。
S にいくつ素数が含まれるか求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 5 \times 10^5
  • 1 \le A \le 10^6
  • 2 \le B \le 5 \times 10^{11}

入力

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

N A B

出力

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


入力例 1

10 3 4

出力例 1

4

S=(4,7,10,13,16,19,22,25,28,31) です。
このうち、素数は 7,13,19,314 つです。


入力例 2

99 1 2

出力例 2

25

100 以下の素数は全部で 25 個です。


入力例 3

314159 265358 499999999999

出力例 3

23166

Problem Statement

You are given integers N, A, and B. Solve the following problem.

An arithmetic sequence S = (S_1, S_2, \dots, S_N) of length N is defined as S_i = A \times (i - 1) + B.
Find the number of prime numbers in S.

Constraints

  • All input values are integers.
  • 1 \le N \le 5 \times 10^5
  • 1 \le A \le 10^6
  • 2 \le B \le 5 \times 10^{11}

Input

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

N A B

Output

Print the answer as an integer.


Sample Input 1

10 3 4

Sample Output 1

4

S = (4, 7, 10, 13, 16, 19, 22, 25, 28, 31).
Among these are four prime numbers: 7, 13, 19, 31.


Sample Input 2

99 1 2

Sample Output 2

25

There are 25 prime numbers not greater than 100.


Sample Input 3

314159 265358 499999999999

Sample Output 3

23166