A - Apple Pie

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

林檎が A 個、林檎の欠片が P 個あります。

林檎 1 個は、砕くことで林檎の欠片 3 個になります。また、林檎の欠片 2 個を鍋で煮込むことで、アップルパイが 1 個作れます。

今ある材料で作れるアップルパイの最大数を求めてください。

制約

  • 入力は全て整数である。
  • 0 \leq A, P \leq 100

入力

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

A P

出力

今ある材料で作れるアップルパイの最大数を出力せよ。


入力例 1

1 3

出力例 1

3

3 つある林檎の欠片のうち 2 つを鍋で煮込むことで、まず 1 つのアップルパイを作ることができます。林檎の欠片が 1 つ残りますが、これと、1 つある林檎を砕いて新しく得た 3 つの林檎の欠片から、さらに 2 つのアップルパイを作れます。


入力例 2

0 1

出力例 2

0

残念ながら 1 つもアップルパイを作ることができません。


入力例 3

32 21

出力例 3

58

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

  • All values in input are integers.
  • 0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.


Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.


Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.


Sample Input 3

32 21

Sample Output 3

58
B - Guidebook

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

あなたは美味しいレストランを紹介する本を書くことにしました。 あなたは N 個のレストラン、レストラン 1、レストラン 2、レストラン N を紹介しようとしています。レストラン iS_i 市にあり、あなたは 100 点満点中 P_i 点と評価しています。 異なる 2 個のレストランに同じ点数がついていることはありません。

この本では、次のような順でレストランを紹介しようとしています。

  • 市名が辞書順で早いものから紹介していく。
  • 同じ市に複数レストランがある場合は、点数が高いものから紹介していく。

この本で紹介される順にレストランの番号を出力してください。

制約

  • 1 ≤ N ≤ 100
  • S は英小文字のみからなる長さ 1 以上 10 以下の文字列
  • 0 ≤ P_i ≤ 100
  • P_i は整数
  • P_i ≠ P_j (1 ≤ i < j ≤ N)

入力

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

N
S_1 P_1
:
S_N P_N

出力

N 行出力せよ。i 行目 (1 ≤ i ≤ N) には、i 番目に紹介されるレストランの番号を出力せよ。


入力例 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

出力例 1

3
4
6
1
5
2

3 種類の市名は辞書順で kazan < khabarovsk < moscow です。 それぞれの市について、点数が高いレストランから順に紹介されていきます。よって、レストランは 3,4,6,1,5,2 の順に紹介されていきます。


入力例 2

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

出力例 2

10
9
8
7
6
5
4
3
2
1

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score.

You want to introduce the restaurants in the following order:

  • The restaurants are arranged in lexicographical order of the names of their cities.
  • If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

  • 1 ≤ N ≤ 100
  • S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • 0 ≤ P_i ≤ 100
  • P_i is an integer.
  • P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.


Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.


Sample Input 2

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

Sample Output 2

10
9
8
7
6
5
4
3
2
1
C - Switches

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

on と off の状態を持つ N 個の スイッチと、M 個の電球があります。スイッチには 1 から N の、電球には 1 から M の番号がついています。

電球 ik_i 個のスイッチに繋がっており、スイッチ s_{i1}, s_{i2}, ..., s_{ik_i} のうち on になっているスイッチの個数を 2 で割った余りが p_i に等しい時に点灯します。

全ての電球が点灯するようなスイッチの on/off の状態の組み合わせは何通りあるでしょうか。

制約

  • 1 \leq N, M \leq 10
  • 1 \leq k_i \leq N
  • 1 \leq s_{ij} \leq N
  • s_{ia} \neq s_{ib} (a \neq b)
  • p_i0 または 1
  • 入力は全て整数である

入力

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

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

出力

全ての電球が点灯するようなスイッチの on/off の状態の組み合わせの個数を出力せよ。


入力例 1

2 2
2 1 2
1 2
0 1

出力例 1

1
  • 電球 1 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1, 2
  • 電球 2 は、次のスイッチのうち奇数個が on の時に点灯します: スイッチ 2

(スイッチ 1、スイッチ 2) の状態としては (on,on),(on,off),(off,on),(off,off) が考えられますが、このうち (on,on) のみが条件を満たすので、1 を出力してください。


入力例 2

2 3
2 1 2
1 1
1 2
0 0 1

出力例 2

0
  • 電球 1 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1, 2
  • 電球 2 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1
  • 電球 3 は、次のスイッチのうち奇数個が on の時に点灯します: スイッチ 2

電球 2 を点灯させるためには スイッチ 1 が off, 電球 3 を点灯させるためにはスイッチ 2 が on である必要がありますが、この時電球 1 は点灯しません。

よって、全ての電球が点灯するようなスイッチの on/off の状態の組み合わせは存在しないので、0 を出力してください。


入力例 3

5 2
3 1 2 5
2 2 3
1 0

出力例 3

8

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

  • 1 \leq N, M \leq 10
  • 1 \leq k_i \leq N
  • 1 \leq s_{ij} \leq N
  • s_{ia} \neq s_{ib} (a \neq b)
  • p_i is 0 or 1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.


Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1
  • Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.
  • Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.


Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0
  • Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.
  • Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.
  • Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.


Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8
D - equeue

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

あなたは誕生日プレゼントとして友人から dequeue D を貰いました。

D は左右に長い筒であり、N 個の宝石が一列に詰められています。

宝石の価値は左から順に V_1, V_2, ..., V_N です。負の価値の宝石が詰められている場合もあります。

はじめ、あなたは 1 つも宝石を持っていません。

あなたは、D に対して以下の 4 種類の操作から 1 つを選んで実行することを K 回まで行うことができます。

  • 操作 A: D に詰められた宝石のうち、左端の宝石を取り出して手に入れる。D が空の場合、この操作を行えない。

  • 操作 B: D に詰められた宝石のうち、右端の宝石を取り出して手に入れる。D が空の場合、この操作を行えない。

  • 操作 C: 持っている宝石を 1 つ選んで D の左端に詰める。宝石を持っていない場合、この操作を行えない。

  • 操作 D: 持っている宝石を 1 つ選んで D の右端に詰める。宝石を持っていない場合、この操作を行えない。

操作終了後に持っている宝石の価値の合計の最大値を求めてください。

制約

  • 入力は全て整数である。
  • 1 \leq N \leq 50
  • 1 \leq K \leq 100
  • -10^7 \leq V_i \leq 10^7

入力

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

N K
V_1 V_2 ... V_N

出力

操作終了後に持っている宝石の価値の合計の最大値を出力せよ。


入力例 1

6 4
-10 8 2 1 2 6

出力例 1

14

以下の順に操作を行うことで、価値 8, 6 の宝石をそれぞれ 1 個ずつ手に入れることができ、このときの合計価値 14 が最大です。

  • 操作 A を行い、D の左端から価値 -10 の宝石を取り出します。
  • 操作 B を行い、D の右端から価値 6 の宝石を取り出します。
  • 操作 A を行い、D の左端から価値 8 の宝石を取り出します。
  • 操作 D を行い、D の右端に価値 -10 の宝石を詰めます。

入力例 2

6 4
-6 -100 50 -2 -5 -3

出力例 2

44

入力例 3

6 3
-6 -100 50 -2 -5 -3

出力例 3

0

操作を行わないのが最適です。

Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

  • Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

  • Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

  • Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

  • Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 50
  • 1 \leq K \leq 100
  • -10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.


Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

  • Do operation A. You take out the jewel of value -10 from the left end of D.
  • Do operation B. You take out the jewel of value 6 from the right end of D.
  • Do operation A. You take out the jewel of value 8 from the left end of D.
  • Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.

E - Roadwork

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

東西に無限に続く 1 本の大通りがあり、数直線とみなすことができます。

この大通り上で N 回道路工事が行われます。 i 番目の道路工事は時刻 S_i - 0.5 から時刻 T_i - 0.5 まで座標 X_i を通行止めにします。

Q 人の人が座標 0 に立っています。 i 番目の人は時刻 D_i に座標 0 を出発し、速度 1 で正の方向へ歩き続けます。 歩いている途中で通行止めとなっている地点に到達した場合には、そこで歩くのをやめます。

Q 人それぞれが進む距離を求めてください。

制約

  • 入力は全て整数
  • 1 \leq N, Q \leq 2 \times 10^5
  • 0 \leq S_i < T_i \leq 10^9
  • 1 \leq X_i \leq 10^9
  • 0 \leq D_1 < D_2 < ... < D_Q \leq 10^9
  • i \neq j かつ X_i = X_j の時、区間 [S_i, T_i) と 区間 [S_j, T_j) は重ならない

入力

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

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

出力

Q 行出力せよ。

i 行目には i 番目の人が進む距離を出力せよ。 ただし i 番目の人が無限に歩き続ける場合は、代わりに -1 を出力せよ。


入力例 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

出力例 1

2
2
10
-1
13
-1

1 番目の人は時刻 0 に座標 0 を出発し、時刻 2 に座標 2 に到着した時点で、1 番目の道路工事による通行止めによって歩くのをやめます。

2 番目の人は時刻 1 に座標 0 を出発し、時刻 3 に座標 2 に到着します。この時、1 番目の道路工事は既に終了していますが、4 番目の道路工事が始まっているため、同様に座標 2 で歩くのをやめます。

4 番目および 6 番目の人は、歩いている最中に通行止めに出くわさないため、無限に歩き続けます。

Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street. The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

  • All values in input are integers.
  • 1 \leq N, Q \leq 2 \times 10^5
  • 0 \leq S_i < T_i \leq 10^9
  • 1 \leq X_i \leq 10^9
  • 0 \leq D_1 < D_2 < ... < D_Q \leq 10^9
  • If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.


Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.

F - Frog Jump

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

無限に広がる池があり、数直線とみなせます。この池には N 個の蓮が浮かんでおり、それらは座標 0,1,2,....N-2,N-1 にあります。

あなたは、最初座標0 の蓮の上にいます。あなたは、以下の手順に従ってゲームを行うことにしました。

  • 1.正の整数 A,B を決める。得点ははじめ 0 である。

  • 2.現在の位置を x として、y=x+Aとする。x にある蓮を消して、y に移動する。

    • y=N-1 ならば、ゲームが終了する。
    • そうでなくて、y に蓮があるならば、得点が s_y 増加する。
    • そこに蓮がないならば、あなたは溺れる。得点が 10^{100} 減少して、ゲームが終了する。
  • 3.現在の位置を x として、y=x-Bとする。x にある蓮を消して、y に移動する。

    • y=N-1 ならば、ゲームが終了する。
    • そうでなくて、y に蓮があるならば、得点が s_y 増加する。
    • そこに蓮がないならば、あなたは溺れる。得点が 10^{100} 減少して、ゲームが終了する。
  • 4.手順2に戻る。

あなたは、最終得点をできるだけ大きくしたいです。 最適に A,B の値を決めたときの最終得点はいくらになるでしょうか。

制約

  • 3 \leqq N \leqq 10^5
  • -10^9 \leqq s_i \leqq 10^9
  • s_0=s_{N-1}=0
  • 入力はすべて整数である。

入力

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

N
s_0 s_1 ...... s_{N-1}

出力

最適に A,B の値を決めたときの最終得点を出力してください。


入力例 1

5
0 2 5 1 0

出力例 1

3

A = 3, B = 2 としたとき、ゲームは次のように進行します。

  • 座標 0 + 3 = 3 に移動し、得点が s_3 = 1 増加する。
  • 座標 3 - 2 = 1 に移動し、得点が s_1 = 2 増加する。
  • 座標 1 + 3 = 4 に移動し、得点 3 でゲームが終了する。

得点 4 以上でゲームを終了することはできないため、答えは 3 です。座標 2 にある蓮に乗ってその後溺れずに済ますことはできないことに注意してください。


入力例 2

6
0 10 -7 -4 -13 0

出力例 2

0

ここでの最適な戦略は、A = 5 を選んで (B の値は不問) ただちに最後の蓮に乗ることです。


入力例 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

出力例 3

59

Score : 600 points

Problem Statement

There is an infinitely large pond, which we consider as a number line. In this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1. On the lotus at coordinate i, an integer s_i is written.

You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:

  • 1. Choose positive integers A and B. Your score is initially 0.
  • 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
    • If y = N-1, the game ends.
    • If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
    • If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
  • 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
    • If y = N-1, the game ends.
    • If y \neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
    • If y \neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
  • 4. Go back to step 2.

You want to end the game with as high a score as possible. What is the score obtained by the optimal choice of A and B?

Constraints

  • 3 \leq N \leq 10^5
  • -10^9 \leq s_i \leq 10^9
  • s_0=s_{N-1}=0
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_0 s_1 ...... s_{N-1}

Output

Print the score obtained by the optimal choice of A and B.


Sample Input 1

5
0 2 5 1 0

Sample Output 1

3

If you choose A = 3 and B = 2, the game proceeds as follows:

  • Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.
  • Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.
  • Move to coordinate 1 + 3 = 4. The game ends with a score of 3.

There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.


Sample Input 2

6
0 10 -7 -4 -13 0

Sample Output 2

0

The optimal strategy here is to land the final lotus immediately by choosing A = 5 (the value of B does not matter).


Sample Input 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

Sample Output 3

59