A - Cabbages

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君はキャベツ屋さんにやってきました。

キャベツ屋さんでは、 キャベツを 1X 円で買うことができます。
ただし、キャベツを A 個よりも多く買う場合、A+1 個目以降に買うキャベツについては 1Y 円で買うことができます。(ここで、Y \lt X が保証されます。)

高橋君がキャベツを N 個買うために必要な金額を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A \leq 10^5
  • 1 \leq Y \lt X \leq 100
  • 入力はすべて整数

入力

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

N A X Y

出力

高橋君が N 個のキャベツを買うために必要な金額を出力せよ。


入力例 1

5 3 20 15

出力例 1

90

1 個目から 3 個目までのキャベツは、120 円で買うことができます。
4 個目と 5 個目のキャベツは、115 円で買うことができます。
よって、5 個のキャベツを買うために必要な金額は、20+20+20+15+15 = 90 円です。


入力例 2

10 10 100 1

出力例 2

1000

Score : 100 points

Problem Statement

Takahashi is visiting a shop specializing in cabbage.

The shop sells cabbage for X yen (Japanese currency) per head.
However, if you buy more than A heads of cabbage at once, the (A+1)-th and subsequent heads will be sold for Y yen per head.
(It is guaranteed that Y \lt X. See Sample Input/Output 1 for clarity.)

Print the amount of money needed to buy N heads of cabbage.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A \leq 10^5
  • 1 \leq Y \lt X \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A X Y

Output

Print the amount of money needed to buy N heads of cabbage (as an integer).


Sample Input 1

5 3 20 15

Sample Output 1

90

You need to pay 20 yen for each of the 1-st through 3-rd heads of cabbage, and 15 yen for each of the 4-th and 5-th heads of cabbage.
Thus, you need to pay a total of 20+20+20+15+15 = 90 yen for the 5 heads of cabbage.


Sample Input 2

10 10 100 1

Sample Output 2

1000
B - Bouzu Mekuri

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 枚のカードからなる山札があります。
それぞれのカードは、「良いカード」か「悪いカード」かのどちらかです。

高橋君と青木君は、この山札を使って対戦ゲームをします。
このゲームでは、2 人は交互に山札の一番上のカードを引いて、そのカードを食べます。
先に悪いカードを食べたプレイヤーの負けです。(ここで、山札には少なくとも 1 枚の悪いカードが含まれていることが保証されます。)

01 からなる文字列 S が与えられます。i = 1, 2, \ldots, N について、

  • Si 文字目が 0 のとき、山札の上から i 番目のカードが良いカードであることを表します。
  • Si 文字目が 1 のとき、山札の上から i 番目のカードが悪いカードであることを表します。

高橋君が先手でゲームを始めるとき、高橋君と青木君のどちらが負けるかを答えてください。

制約

  • 1 \leq N \leq 10^5
  • N は整数
  • S01 からなる長さ N の文字列
  • S は少なくとも 1 個の 1 を含む。

入力

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

N
S

出力

高橋君が先手でゲームを始めるとき、高橋君と青木君のどちらが負けるかを答えよ。
高橋君が負けるならば Takahashi 、青木君が負けるならば Aoki と出力せよ。


入力例 1

5
00101

出力例 1

Takahashi

まず、高橋君が良いカードを食べ、次に青木君が良いカードを食べ、その後に高橋君が悪いカードを食べます。
高橋君が先に悪いカードを食べるので高橋君が負けます。よって、Takahashi と出力します。


入力例 2

3
010

出力例 2

Aoki

Score : 200 points

Problem Statement

We have a deck of N cards.
Each of these cards is good or bad.

Using this deck, Takahashi and Aoki will play a game against each other.
In the game, the players alternately draw the topmost card and eat it.
The player who first gets to eat a bad card loses the game. (Here, it is guaranteed that the deck contains at least one bad card.)

You are given a string S consisting of 0 and 1. For each i = 1, 2, \ldots, N,

  • if the i-th character of S is 0, it means that the i-th card from the top of the deck is good;
  • if the i-th character of S is 1, it means that the i-th card from the top of the deck is bad.

Which player will lose when Takahashi goes first in the game?

Constraints

  • 1 \leq N \leq 10^5
  • N is an integer.
  • S is a string of length N consisting of 0 and 1.
  • S contains at least one occurrence of 1.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the name of the player who will lose when Takahashi goes first in the game: Takahashi or Aoki.


Sample Input 1

5
00101

Sample Output 1

Takahashi

First, Takahashi will eat a good card. Next, Aoki will eat a good card. Then, Takahashi will eat a bad card.
Thus, Takahashi will be the first to eat a bad card, so we should print Takahashi.


Sample Input 2

3
010

Sample Output 2

Aoki
C - Colorful Candies

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 個のキャンディが左右一列に並んでいます。
それぞれのキャンディは、色 1、色 2\ldots、色 10^9の、10^9 種類の色のうちいずれかの色をしています。
i = 1, 2, \ldots, N について、左から i 番目のキャンディの色は色 c_i です。

高橋君は並んでいるキャンディのうち、連続して並んだ K 個のキャンディをもらうことができます。
すなわち、1 \leq i \leq N-K+1 を満たす整数 i を選んで、 左から i 番目、i+1 番目、i+2 番目、\ldotsi+K-1 番目のキャンディをもらうことができます。

高橋君はいろいろな色のキャンディを食べたいので、 もらうキャンディに含まれる色の種類数が多いほどうれしい気持ちになります。
高橋君がもらうキャンディに含まれる色の種類数の最大値を出力してください。

制約

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq c_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
c_1 c_2 \ldots c_N

出力

高橋君がもらうキャンディに含まれる色の種類数の最大値を出力せよ。


入力例 1

7 3
1 2 1 2 3 3 1

出力例 1

3

高橋君が左から 3 番目から 5 番目のキャンディをもらうと、もらうキャンディに含まれる色は 3 種類になり、これが最大です。


入力例 2

5 5
4 4 4 4 4

出力例 2

1

高橋君は並んでいるすべてのキャンディをもらうことが出来ますが、もらうキャンディに含まれる色は 1 種類です。


入力例 3

10 6
304621362 506696497 304621362 506696497 834022578 304621362 414720753 304621362 304621362 414720753

出力例 3

4

Score : 300 points

Problem Statement

There are N candies arranged in a row from left to right.
Each of these candies has one color that is one of the 10^9 colors called Color 1, Color 2, \ldots, and Color 10^9.
For each i = 1, 2, \ldots, N, the color of the i-th candy from the left is Color c_i.

From this row, Takahashi can choose K consecutive candies and get them.
That is, he can choose an integer i such that 1 \leq i \leq N-K+1 and get the i-th, (i+1)-th, (i+2)-th, \ldots, (i+K-1)-th candy from the left.

Takahashi likes to eat colorful candies, so the more variety of colors his candies have, the happier he will be.
Print the maximum possible number of distinct colors in candies he gets.

Constraints

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq c_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
c_1 c_2 \ldots c_N

Output

Print the maximum possible number of distinct colors in candies Takahashi gets.


Sample Input 1

7 3
1 2 1 2 3 3 1

Sample Output 1

3

If Takahashi gets the 3-rd through 5-th candies, they will have 3 distinct colors, which is the maximum possible number.


Sample Input 2

5 5
4 4 4 4 4

Sample Output 2

1

Takahashi can get all of these candies, but they are in a single color.


Sample Input 3

10 6
304621362 506696497 304621362 506696497 834022578 304621362 414720753 304621362 304621362 414720753

Sample Output 3

4
D - National Railway

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋王国は HW 列のグリッドで表されます。北から i 行目、西から j 列目のマスを (i, j) で表します。

このたび、高橋王国の国民から交通の利便性のために鉄道の建設を求める声が多数寄せられ、国王の高橋君は鉄道を建設しなければならなくなりました。
鉄道建設は以下の 2 つのステップからなります。

  • まず、2 つの異なるマスを選び、それぞれに駅を建設する。マス (i, j) に駅を建設すると A_{i,j} 円の費用がかかる。
  • その後、建設した 2 つの駅を結ぶ線路を建設する。2 つの駅がマス (i, j) とマス (i', j') にあるとき、これらを結ぶ線路の建設をすると C \times (|i-i'| + |j-j'|) 円の費用がかかる。(ここで、|x|x の絶対値を表す。)

高橋君は国民の利便性を上げることよりも、鉄道建設にかかる費用を少なく抑えることを優先したいと考えています。
鉄道建設にかかる費用として考えられる最小値を出力してください。

制約

  • 2 \leq H, W \leq 1000
  • 1 \leq C \leq 10^9
  • 1 \leq A_{ij} \leq 10^9
  • 入力はすべて整数

入力

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

H W C
A_{1,1} A_{1,2} \cdots A_{1,W}
\vdots
A_{H,1} A_{H,2} \cdots A_{H,W}

出力

鉄道建設にかかる費用として考えられる最小値を出力せよ。


入力例 1

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

出力例 1

10

マス (1, 1) とマス (2, 3) に駅を建設すると、駅の建設費用が 1 + 3 = 4 円、 線路の建設費用が 2 \times (|1-2| + |1-3|) = 6 円となり、鉄道建設にかかる費用は 4+6 = 10 円となります。 これが費用として考えられる最小値です。


入力例 2

3 3 1000000000
1000000 1000000 1
1000000 1000000 1000000
1 1000000 1000000

出力例 2

1001000001

Score : 400 points

Problem Statement

The Kingdom of Takahashi can be represented as a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the north and j-th column from the west.

Recently, there have been more and more requests from the kingdom's citizens to build a railway, and now the king, Takahashi, has no choice but to build one.
The construction of the railway will have the following two phases.

  • First, choose two different squares and build a station on each of them. It costs A_{i,j} yen to build a station on the square (i, j).
  • Then, build a railway track connecting these two stations. This costs C \times (|i-i'| + |j-j'|) yen when the two stations are on the squares (i, j) and (i', j'). (|x| denotes the absolute value of x.)

Takahashi's priority is to spend as little as possible on this construction, rather than to improve convenience for the citizens.
Print the minimum possible total cost of the construction of the railway.

Constraints

  • 2 \leq H, W \leq 1000
  • 1 \leq C \leq 10^9
  • 1 \leq A_{ij} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W C
A_{1,1} A_{1,2} \cdots A_{1,W}
\vdots
A_{H,1} A_{H,2} \cdots A_{H,W}

Output

Print the minimum possible total cost of the construction of the railway.


Sample Input 1

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

Sample Output 1

10

If we build stations on the squares (1, 1) and (2, 3), it will cost 1 + 3 = 4 yen to build the stations and 2 \times (|1-2| + |1-3|) = 6 yen to build the track, for a total of 4+6 = 10 yen. This is the minimum possible total cost of the construction.


Sample Input 2

3 3 1000000000
1000000 1000000 1
1000000 1000000 1000000
1 1000000 1000000

Sample Output 2

1001000001
E - Ring MST

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の頂点と 0 本の辺からなる無向グラフがあります。 N 個の頂点をそれぞれ頂点 0、頂点 1、頂点 2\ldots、頂点 N-1と呼びます。

このグラフに対する M 種類の操作を考えます。
i = 1, 2, \ldots, M について、i 番目の操作は「 0 \leq x \lt N を満たす整数 x を選び、頂点 x と頂点 (x + A_i) \bmod N を結ぶ無向辺を追加する」という操作です。ただし、a \bmod bab で割った余りを表します。 i 番目の操作を 1 回行うと C_i 円の費用がかかります。

あなたは、M 種類の操作をそれぞれ好きな回数( 0 回でもよい)行うことができます。 例えば、3 種類の操作がある場合、1 番目の操作を 2 回、2 番目の操作を 0 回、3 番目の操作を 1 回行うという選択が可能です。

グラフを連結にすることが可能かどうかを判定し、可能な場合はそのためにかかる費用の合計として考えられる最小値を求めてください。

制約

  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq N-1
  • 1 \leq C_i \leq 10^9
  • 入力はすべて整数

入力

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

N M
A_1 C_1
A_2 C_2
\vdots
A_M C_M

出力

グラフを連結にすることが可能な場合は、そのためにかかる費用の合計として考えられる最小値を出力せよ。
グラフを連結にすることが不可能な場合は、-1 を出力せよ。


入力例 1

4 2
2 3
3 5

出力例 1

11

まず 1 番目の操作で頂点 0 と頂点 2 を結び、次に 1 番目の操作で頂点 1 と頂点 3 を結び、最後に 2 番目の操作で頂点 1 と頂点 0 を結ぶと、グラフは連結になります。 かかった費用の合計は 3+3+5 = 11 円で、これが考えられる最小値です。


入力例 2

6 1
3 4

出力例 2

-1

どのように操作を行ってもグラフを連結にすることができないので、-1 を出力します。

Score : 500 points

Problem Statement

We have an undirected graph with N vertices and 0 edges. Let us call the N vertices Vertex 0, Vertex 1, Vertex 2, \ldots, Vertex N-1.

Consider M kinds of operations on this graph.
For each i = 1, 2, \ldots, M, the operation of the i-th kind is to choose an integer x such that 0 \leq x \lt N and add an undirected edge connecting Vertex x and Vertex (x + A_i) \bmod N. Here, a \bmod b denotes the remainder when dividing a by b. It costs C_i yen to do the operation of the i-th kind once.

You can do these M kinds of operations any number of times (possibly zero) in any order. For example, if three kinds of operations are available, you can choose to do the first operation twice, the second zero times, and the third once.

Determine whether it is possible to make the graph connected. If it is possible, find the minimum total cost needed to do so.

Constraints

  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq N-1
  • 1 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 C_1
A_2 C_2
\vdots
A_M C_M

Output

If it is possible to make the graph connected, print the minimum total cost needed to do so.
If it is impossible to make the graph connected, print -1.


Sample Input 1

4 2
2 3
3 5

Sample Output 1

11

If we first do the operation of the first kind to connect Vertices 0 and 2, then do it again to connect Vertices 1 and 3, and finally the operation of the second kind to connect Vertices 1 and 0, the graph will be connected. The total cost incurred here is 3+3+5 = 11 yen, which is the minimum possible.


Sample Input 2

6 1
3 4

Sample Output 2

-1

There is no way to make the graph connected, so we should print -1.

F - Coprime Solitaire

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

テーブルの上に N 枚のカードが並んでいます。 i = 1, 2, \ldots, N について、i 枚目のカードの表には整数 A_i が、裏には整数 B_i が書かれています。
はじめ、すべてのカードは表が見えるように置かれています。

高橋君は好きな枚数( 0 枚でも良い)のカードを選んで裏返します。 その結果、以下の条件が満たされれば高橋君はうれしい気持ちになります。

  • 1 \leq i \lt j \leq N を満たす任意の整数のペア (i, j) について、i 枚目のカードの見えている面に書かれた整数と、j 枚目のカードの見えている面に書かれた整数が、互いに素である。

高橋君がうれしい気持ちになれる可能性があるかどうかを判定してください。

制約

  • 1 \leq N \leq 3 \times 10^4
  • 1 \leq A_i, B_i \leq 2 \times 10^6
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

高橋君がうれしい気持ちになれる可能性があるなら Yes を、可能性がないなら No を出力せよ。


入力例 1

3
2 5
10 9
4 8

出力例 1

Yes

はじめ、見えている整数は 2, 10, 4 です。 1 枚目のカードと 2 枚目のカードを裏返すと、見えている整数は 5, 9, 4 となり、高橋君はうれしい気持ちになります。よって Yes を出力します。


入力例 2

2
10 100
1000 10000

出力例 2

No

どのようにカードを裏返しても、高橋君はうれしい気持ちになりません。よって No を出力します。

Score : 600 points

Problem Statement

We have N cards arranged in a row on a table. For each i = 1, 2, \ldots, N, the i-th card has an integer A_i written on the front and an integer B_i written on the back. Initially, every card is placed face up.

Takahashi will choose any number of cards he likes (possibly zero) and flip them. Then, he will be happy if the following condition is satisfied:

  • for every pair of integers (i, j) such that 1 \leq i \lt j \leq N, the integers visible on the i-th and j-th cards are coprime.

Determine whether it is possible for Takahashi to be happy.

Constraints

  • 1 \leq N \leq 3 \times 10^4
  • 1 \leq A_i, B_i \leq 2 \times 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If it is possible for Takahashi to be happy, print Yes; otherwise, print No.


Sample Input 1

3
2 5
10 9
4 8

Sample Output 1

Yes

Initially, we see integers 2, 10, and 4. If we flip the first and second cards, we will see 5, 9, and 4, which will make Takahashi happy. Thus, we should print Yes.


Sample Input 2

2
10 100
1000 10000

Sample Output 2

No

There is no way to flip cards to make Takahashi happy, so we should print No.