A - トーナメント敗退ラウンド

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 266

問題文

N 人の選手が参加するシングルエリミネーション方式のトーナメントが開催されます。ここで N2 のべき乗です。

各選手には 1 から N までの番号が付けられており、選手 i の強さは S_i です。すべての選手の強さは互いに異なり、対戦では必ず強さの値が大きい選手が勝ちます。負けた選手はトーナメントから脱落し、最終的に 1 人の優勝者が決まるまで試合が続けられます。

N2 のべき乗であるため、トーナメントは全体でちょうど R = \log_2 N ラウンド行われます。各ラウンドの対戦の組み合わせは次のように決まります。

  • ラウンド 1 選手 2i-1 と選手 2i が対戦します。これをラウンド 1 の第 i 試合とします(i = 1, 2, \ldots, N/2)。
  • ラウンド rr \geq 2): 前のラウンド(ラウンド r-1)の第 2j-1 試合の勝者と第 2j 試合の勝者が対戦します。これをラウンド r の第 j 試合とします(j = 1, 2, \ldots, N/2^r)。

各ラウンドの試合番号は 1 から始まる番号でラウンドごとに付け直されます。ラウンド R では試合は 1 試合のみとなり、その勝者が優勝者です。

各選手について、その選手が敗北するラウンド番号を求めてください。ただし、優勝者は一度も敗北しないため、0 を出力してください。ラウンドは 1 から始まる番号で数えます。

制約

  • 2 \leq N \leq 2^{19}
  • N2 のべき乗である
  • 1 \leq S_i \leq N1 \leq i \leq N
  • S_i はすべて異なる
  • 入力はすべて整数である

入力

N
S_1 S_2 \ldots S_N

1 行目には選手の人数 N が与えられる。

2 行目には各選手の強さ S_1, S_2, \ldots, S_N がスペース区切りで与えられる。

出力

N 個の整数をスペース区切りで 1 行に出力せよ。i 番目の値は、選手 i が敗北するラウンド番号である。優勝者については 0 を出力せよ。


入力例 1

4
1 4 3 2

出力例 1

1 0 2 1

入力例 2

8
8 1 6 2 5 3 7 4

出力例 2

0 1 2 1 2 1 3 1

入力例 3

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

出力例 3

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

入力例 4

32
32 1 24 9 16 17 8 25 31 2 23 10 15 18 7 26 30 3 22 11 14 19 6 27 29 4 21 12 13 20 5 28

出力例 4

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

入力例 5

2
1 2

出力例 5

1 0

Score : 266 pts

Problem Statement

A single-elimination tournament is held with N players. Here, N is a power of 2.

Each player is assigned a number from 1 to N, and the strength of player i is S_i. All players have distinct strengths, and in any match, the player with the greater strength value always wins. A player who loses is eliminated from the tournament, and matches continue until a single champion is determined.

Since N is a power of 2, the tournament consists of exactly R = \log_2 N rounds. The matchups for each round are determined as follows:

  • Round 1: Player 2i-1 and player 2i play against each other. This is called match i of round 1 (i = 1, 2, \ldots, N/2).
  • Round r (r \geq 2): The winner of match 2j-1 and the winner of match 2j from the previous round (round r-1) play against each other. This is called match j of round r (j = 1, 2, \ldots, N/2^r).

Match numbers within each round are numbered starting from 1 and are reassigned for each round. In round R, there is only one match, and its winner is the champion.

For each player, determine the round number in which that player is defeated. However, since the champion is never defeated, output 0 for the champion. Rounds are numbered starting from 1.

Constraints

  • 2 \leq N \leq 2^{19}
  • N is a power of 2
  • 1 \leq S_i \leq N (1 \leq i \leq N)
  • All S_i are distinct
  • All input values are integers

Input

N
S_1 S_2 \ldots S_N

The first line gives the number of players N.

The second line gives the strengths S_1, S_2, \ldots, S_N of each player, separated by spaces.

Output

Output N integers separated by spaces on a single line. The i-th value is the round number in which player i is defeated. For the champion, output 0.


Sample Input 1

4
1 4 3 2

Sample Output 1

1 0 2 1

Sample Input 2

8
8 1 6 2 5 3 7 4

Sample Output 2

0 1 2 1 2 1 3 1

Sample Input 3

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

Sample Output 3

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

Sample Input 4

32
32 1 24 9 16 17 8 25 31 2 23 10 15 18 7 26 30 3 22 11 14 19 6 27 29 4 21 12 13 20 5 28

Sample Output 4

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

Sample Input 5

2
1 2

Sample Output 5

1 0
B - 果物の収穫

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は果樹園でアルバイトをしています。この果樹園には N 本の果物の木が一列に並んでおり、左から i 番目の木には A_i 個の果物がなっています。

高橋君はこれから収穫作業を行います。収穫には専用のカートを使いますが、このカートは一列に並んだ木に沿って連続した区間しか移動できないため、連続して並んだちょうど K 本の木を選び、その K 本の木になっている果物をすべて収穫します。より正確には、ある整数 l1 \leq l \leq N - K + 1)を選び、左から l 番目の木から l + K - 1 番目の木までの K 本の木になっている果物をすべて収穫します。

高橋君は収穫できる果物の個数を最大化したいと考えています。連続する K 本の木を最適に選んだとき、収穫できる果物の個数の最大値を求めてください。

制約

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

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、木の本数を表す整数 N と、選択する連続した木の本数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各木になっている果物の個数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • A_i は左から i 番目の木になっている果物の個数を表す。

出力

連続する K 本の木を最適に選んだとき、収穫できる果物の個数の最大値を 1 行で出力せよ。


入力例 1

5 3
2 5 3 8 1

出力例 1

16

入力例 2

7 2
10 4 8 6 15 3 9

出力例 2

21

入力例 3

10 4
100 250 180 320 90 410 275 150 380 200

出力例 3

1215

Score : 333 pts

Problem Statement

Takahashi works part-time at an orchard. In this orchard, N fruit trees are lined up in a row, and the i-th tree from the left has A_i fruits.

Takahashi is about to perform the harvest. He uses a special cart for harvesting, but since this cart can only move along a contiguous section of the row of trees, he must choose exactly K consecutive trees and harvest all the fruits from those K trees. More precisely, he chooses an integer l (1 \leq l \leq N - K + 1) and harvests all the fruits from the K trees starting from the l-th tree to the (l + K - 1)-th tree from the left.

Takahashi wants to maximize the number of fruits he can harvest. Find the maximum number of fruits that can be harvested when the K consecutive trees are chosen optimally.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of trees and an integer K representing the number of consecutive trees to select, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the number of fruits on each tree, separated by spaces.
  • A_i represents the number of fruits on the i-th tree from the left.

Output

Print in one line the maximum number of fruits that can be harvested when the K consecutive trees are chosen optimally.


Sample Input 1

5 3
2 5 3 8 1

Sample Output 1

16

Sample Input 2

7 2
10 4 8 6 15 3 9

Sample Output 2

21

Sample Input 3

10 4
100 250 180 320 90 410 275 150 380 200

Sample Output 3

1215
C - お買い物プラン

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君はお祭りの屋台街にやってきました。屋台街には N 個の屋台があり、それぞれの屋台で食べ物が1つずつ売られています。

高橋君の所持金は M 円です。屋台 i1 \leq i \leq N)の食べ物の値段は T_i 円で、これを購入すると満足度 P_i が得られます。高橋君は各屋台について、食べ物を購入するか購入しないかのいずれかを選びます。同じ屋台の食べ物を2つ以上購入することはできません。また、どの屋台の食べ物も購入しないことも許されます。その場合、満足度の合計は 0 です。

高橋君は、購入する食べ物の値段の合計が所持金 M 円以下となるように屋台の組み合わせを選び、得られる満足度の合計を最大化したいと考えています。

満足度の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 15
  • 1 \leq M \leq 10^4
  • 1 \leq T_i \leq 10^41 \leq i \leq N
  • 1 \leq P_i \leq 10^41 \leq i \leq N
  • 入力はすべて整数である

入力

N M
T_1 P_1
T_2 P_2
\vdots
T_N P_N
  • 1 行目には、屋台の数を表す整数 N と、所持金(円)を表す整数 M が、スペース区切りで与えられる。
  • 続く N 行の i 行目(1 \leq i \leq N)には、屋台 i の食べ物の値段 T_i(円)と、購入したときに得られる満足度 P_i が、スペース区切りで与えられる。

出力

高橋君が所持金以内で得られる満足度の合計の最大値を 1 行で出力してください。


入力例 1

3 10
3 5
4 6
5 8

出力例 1

14

入力例 2

4 5
3 4
3 5
6 10
2 3

出力例 2

8

入力例 3

8 50
12 20
15 25
10 18
8 12
20 35
5 8
18 30
7 10

出力例 3

86

入力例 4

15 5000
300 450
1200 1800
450 700
800 1100
150 200
2000 3500
600 950
350 500
900 1400
1100 1600
250 380
700 1050
500 780
400 620
1500 2200

出力例 4

8160

入力例 5

1 1
2 100

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi has come to a street of festival stalls. There are N stalls on the street, and each stall sells one type of food item.

Takahashi has M yen. The food at stall i (1 \leq i \leq N) costs T_i yen, and purchasing it gives a satisfaction of P_i. For each stall, Takahashi chooses either to purchase the food or not. He cannot purchase more than one item from the same stall. It is also allowed to not purchase food from any stall at all. In that case, the total satisfaction is 0.

Takahashi wants to choose a combination of stalls such that the total cost of the purchased food items does not exceed his budget of M yen, and he wants to maximize the total satisfaction.

Find the maximum total satisfaction.

Constraints

  • 1 \leq N \leq 15
  • 1 \leq M \leq 10^4
  • 1 \leq T_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
  • All input values are integers.

Input

N M
T_1 P_1
T_2 P_2
\vdots
T_N P_N
  • The first line contains an integer N representing the number of stalls and an integer M representing the amount of money (in yen), separated by a space.
  • The following N lines each describe a stall. The i-th of these lines (1 \leq i \leq N) contains the price T_i (in yen) of the food at stall i and the satisfaction P_i obtained by purchasing it, separated by a space.

Output

Print the maximum total satisfaction that Takahashi can obtain within his budget, on a single line.


Sample Input 1

3 10
3 5
4 6
5 8

Sample Output 1

14

Sample Input 2

4 5
3 4
3 5
6 10
2 3

Sample Output 2

8

Sample Input 3

8 50
12 20
15 25
10 18
8 12
20 35
5 8
18 30
7 10

Sample Output 3

86

Sample Input 4

15 5000
300 450
1200 1800
450 700
800 1100
150 200
2000 3500
600 950
350 500
900 1400
1100 1600
250 380
700 1050
500 780
400 620
1500 2200

Sample Output 4

8160

Sample Input 5

1 1
2 100

Sample Output 5

0
D - 分岐する棚の整理

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は、位置 1 から位置 M までの整数座標が付いた一直線の棚に、N 個の置物を配置しました。

i 番目の置物 (1 \leq i \leq N) の初期位置は A_i です。複数の置物が同じ位置にあっても構いません。また、置物同士は互いに干渉しません。

この初期配置を状態 0 と呼びます。各状態は、棚の上に残っている置物の集合と、それぞれの位置の情報を保持しています。

これから Q 個の新しい状態を番号 1, 2, \ldots, Q の順に作ります。

状態 j (j = 1, 2, \ldots, Q) は、以下の手順で作られます。

  1. すでに存在する状態 P_j (0 \leq P_j \leq j-1) の内容(棚の上に残っている置物とその位置)をコピーして、新しい状態 j の初期内容とする。
  2. 状態 j に対して次の操作を 1 回だけ 行う。
  • C_jL のとき:状態 j の棚の上にあるすべての置物の位置を D_j だけ減らす(左に D_j スライドさせる)。
  • C_jR のとき:状態 j の棚の上にあるすべての置物の位置を D_j だけ増やす(右に D_j スライドさせる)。
  • この結果、位置が 1 未満または M より大きくなった置物は棚から落下し、状態 j から取り除かれる。

コピーにより新しい状態を作っても、コピー元の状態は一切変化しません。

なお、コピー元の状態番号 P_j の選び方により、状態 0 を根とする木構造が形成されます。

j = 1, 2, \ldots, Q について、状態 j において棚から落下した置物の個数(すなわち N から状態 j に残っている置物の個数を引いた値)を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq M (1 \leq i \leq N)
  • 0 \leq P_j \leq j - 1 (1 \leq j \leq Q)
  • C_jL または R (1 \leq j \leq Q)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq Q)
  • 入力はすべて整数(C_j を除く)

入力

N M Q
A_1 A_2 \cdots A_N
P_1 C_1 D_1
P_2 C_2 D_2
\vdots
P_Q C_Q D_Q
  • 1 行目には、置物の個数 N、棚の右端の位置 M、作る状態の個数 Q が、スペース区切りで与えられる。
  • 2 行目には、各置物の初期位置 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目から Q 行にわたって、新しい状態の作り方が与えられる。
  • 2 + j 行目には、状態 j のコピー元の状態番号 P_j、移動方向 C_j、移動量 D_j が、スペース区切りで与えられる。

出力

Q 行出力せよ。

j 行目には、状態 j において棚から落下した置物の個数(N から状態 j に残っている置物の個数を引いた値)を整数で出力せよ。


入力例 1

5 10 4
1 3 5 8 10
0 R 2
0 L 1
1 L 4
2 R 10

出力例 1

1
1
2
5

入力例 2

6 7 6
2 2 4 6 7 7
0 L 1
0 R 1
1 R 3
2 L 5
3 L 2
5 R 10

出力例 2

0
2
3
5
3
6

入力例 3

15 100 12
1 5 10 20 20 35 50 65 70 80 90 95 100 42 58
0 R 10
0 L 15
1 R 40
1 L 5
2 R 60
2 L 10
3 L 30
4 R 25
5 R 1
6 L 1
8 L 70
10 R 90

出力例 3

2
3
7
2
10
5
7
4
10
5
10
14

入力例 4

30 1000000000 25
1 2 3 100 1000 100000 123456789 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 999999999 1000000000 42 4242 424242 987654321 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 999999998
0 R 1
0 L 1
1 R 999999998
1 L 50
2 R 123456789
2 L 999999999
3 L 500000000
3 R 2
4 R 1000
4 L 1000
5 R 700000000
5 L 200000000
6 R 1
7 L 1
8 R 400000000
9 L 400000000
10 R 600000000
11 L 600000000
12 R 333333333
13 L 333333333
14 R 999999999
15 L 999999999
16 R 123
20 L 456
24 R 789

出力例 4

1
1
29
5
7
30
29
30
7
7
20
15
30
29
30
18
20
20
17
30
30
30
18
30
30

入力例 5

1 1 1
1
0 R 1000000000

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi has placed N figurines on a straight shelf with integer coordinates from position 1 to position M.

The initial position of the i-th figurine (1 \leq i \leq N) is A_i. Multiple figurines may be at the same position. Also, figurines do not interfere with each other.

This initial arrangement is called state 0. Each state holds the information about the set of figurines remaining on the shelf and their respective positions.

We will create Q new states, numbered 1, 2, \ldots, Q, in order.

State j (j = 1, 2, \ldots, Q) is created by the following procedure:

  1. Copy the contents of an already existing state P_j (0 \leq P_j \leq j-1) (the figurines remaining on the shelf and their positions) to use as the initial contents of the new state j.
  2. Perform the following operation exactly once on state j:
  • If C_j is L: Decrease the position of every figurine on the shelf in state j by D_j (slide them D_j to the left).
  • If C_j is R: Increase the position of every figurine on the shelf in state j by D_j (slide them D_j to the right).
  • As a result, any figurine whose position becomes less than 1 or greater than M falls off the shelf and is removed from state j.

Creating a new state by copying does not change the source state in any way.

Note that due to the way the source state number P_j is chosen, a tree structure rooted at state 0 is formed.

For each j = 1, 2, \ldots, Q, determine the number of figurines that have fallen off the shelf in state j (that is, the value of N minus the number of figurines remaining in state j).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq M (1 \leq i \leq N)
  • 0 \leq P_j \leq j - 1 (1 \leq j \leq Q)
  • C_j is L or R (1 \leq j \leq Q)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq Q)
  • All inputs are integers (except for C_j)

Input

N M Q
A_1 A_2 \cdots A_N
P_1 C_1 D_1
P_2 C_2 D_2
\vdots
P_Q C_Q D_Q
  • The first line contains the number of figurines N, the position of the right end of the shelf M, and the number of states to create Q, separated by spaces.
  • The second line contains the initial positions of the figurines A_1, A_2, \ldots, A_N, separated by spaces.
  • The following Q lines describe how to create the new states.
  • The (2 + j)-th line contains the source state number P_j for state j, the direction of movement C_j, and the amount of movement D_j, separated by spaces.

Output

Output Q lines.

On the j-th line, output the number of figurines that have fallen off the shelf in state j (the value of N minus the number of figurines remaining in state j) as an integer.


Sample Input 1

5 10 4
1 3 5 8 10
0 R 2
0 L 1
1 L 4
2 R 10

Sample Output 1

1
1
2
5

Sample Input 2

6 7 6
2 2 4 6 7 7
0 L 1
0 R 1
1 R 3
2 L 5
3 L 2
5 R 10

Sample Output 2

0
2
3
5
3
6

Sample Input 3

15 100 12
1 5 10 20 20 35 50 65 70 80 90 95 100 42 58
0 R 10
0 L 15
1 R 40
1 L 5
2 R 60
2 L 10
3 L 30
4 R 25
5 R 1
6 L 1
8 L 70
10 R 90

Sample Output 3

2
3
7
2
10
5
7
4
10
5
10
14

Sample Input 4

30 1000000000 25
1 2 3 100 1000 100000 123456789 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 999999999 1000000000 42 4242 424242 987654321 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 999999998
0 R 1
0 L 1
1 R 999999998
1 L 50
2 R 123456789
2 L 999999999
3 L 500000000
3 R 2
4 R 1000
4 L 1000
5 R 700000000
5 L 200000000
6 R 1
7 L 1
8 R 400000000
9 L 400000000
10 R 600000000
11 L 600000000
12 R 333333333
13 L 333333333
14 R 999999999
15 L 999999999
16 R 123
20 L 456
24 R 789

Sample Output 4

1
1
29
5
7
30
29
30
7
7
20
15
30
29
30
18
20
20
17
30
30
30
18
30
30

Sample Input 5

1 1 1
1
0 R 1000000000

Sample Output 5

1
E - プレイリストのジャンル偏り最小化

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

高橋君は 2N 曲の候補曲からプレイリストを作ろうとしています。

i 番目の曲 (1 \leq i \leq 2N) はジャンル B_i に属しており、再生時間は A_i 秒です。

高橋君は候補曲の中から 1 曲以上を選び(各曲は最大 1 回まで選べます)、好きな順番に並べてプレイリストを作ります。

ただし、選んだ曲の再生時間の合計は K 秒以上でなければなりません。

作ったプレイリストにおいて、同じジャンルの曲が連続して並んでいる区間に含まれる曲数の最大値を「偏りスコア」と呼びます。

より正確には、プレイリスト中の連続する曲の並びであって、それらがすべて同一ジャンルに属するようなものを考え、その曲数の最大値が偏りスコアです。

条件を満たす曲の選び方と並べ方すべての中で、偏りスコアの最小値を求めてください。

すべての曲を選んでも再生時間の合計が K 秒未満となり、条件を満たせない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 150
  • 1 \leq K \leq 10^9
  • 1 \leq B_i \leq 2N
  • 1 \leq A_i \leq 10^6
  • 入力はすべて整数である

入力

N K
B_1 A_1
B_2 A_2
\vdots
B_{2N} A_{2N}
  • 1 行目には、候補曲数の半分を表す整数 N と、必要な再生時間の合計の下限を表す整数 K がスペース区切りで与えられる。
  • 続く 2N 行のうち i 行目 (1 \leq i \leq 2N) には、i 番目の曲のジャンル B_i と再生時間 A_i がスペース区切りで与えられる。

出力

偏りスコアの最小値を 1 行で出力してください。条件を満たすように曲を選べない場合は -1 を出力してください。


入力例 1

3 15
1 6
1 5
2 4
2 7
3 3
3 8

出力例 1

1

入力例 2

2 31
1 8
2 7
2 6
3 9

出力例 2

-1

入力例 3

8 120
1 15
1 12
2 20
2 7
3 9
3 18
4 25
5 5
5 14
6 11
7 30
7 6
8 10
9 16
10 8
10 13

出力例 3

1

入力例 4

20 650
1 45
1 32
1 28
2 60
2 15
3 22
3 48
4 35
4 40
5 10
5 55
6 70
6 18
7 25
7 27
8 80
9 12
9 44
10 33
10 36
11 90
12 14
12 29
13 50
13 52
14 20
15 65
15 17
16 31
16 39
17 75
18 11
18 24
19 46
20 58
21 13
22 41
23 30
24 62
25 19

出力例 4

1

入力例 5

1 2
1 1
1 1

出力例 5

2

Score : 433 pts

Problem Statement

Takahashi is trying to create a playlist from 2N candidate songs.

The i-th song (1 \leq i \leq 2N) belongs to genre B_i and has a duration of A_i seconds.

Takahashi selects one or more songs from the candidates (each song can be selected at most once) and arranges them in any order he likes to create a playlist.

However, the total duration of the selected songs must be at least K seconds.

In the created playlist, the maximum number of songs contained in a consecutive segment where all songs belong to the same genre is called the "bias score."

More precisely, consider all consecutive subsequences of songs in the playlist such that all songs in each subsequence belong to the same genre; the bias score is the maximum number of songs among all such subsequences.

Among all possible ways to select and arrange songs that satisfy the conditions, find the minimum possible bias score.

If the total duration is less than K seconds even when all songs are selected, and thus the condition cannot be satisfied, output -1.

Constraints

  • 1 \leq N \leq 150
  • 1 \leq K \leq 10^9
  • 1 \leq B_i \leq 2N
  • 1 \leq A_i \leq 10^6
  • All input values are integers

Input

N K
B_1 A_1
B_2 A_2
\vdots
B_{2N} A_{2N}
  • The first line contains an integer N representing half the number of candidate songs, and an integer K representing the lower bound on the total duration, separated by a space.
  • In the following 2N lines, the i-th line (1 \leq i \leq 2N) contains the genre B_i and the duration A_i of the i-th song, separated by a space.

Output

Output the minimum possible bias score in one line. If it is impossible to select songs satisfying the condition, output -1.


Sample Input 1

3 15
1 6
1 5
2 4
2 7
3 3
3 8

Sample Output 1

1

Sample Input 2

2 31
1 8
2 7
2 6
3 9

Sample Output 2

-1

Sample Input 3

8 120
1 15
1 12
2 20
2 7
3 9
3 18
4 25
5 5
5 14
6 11
7 30
7 6
8 10
9 16
10 8
10 13

Sample Output 3

1

Sample Input 4

20 650
1 45
1 32
1 28
2 60
2 15
3 22
3 48
4 35
4 40
5 10
5 55
6 70
6 18
7 25
7 27
8 80
9 12
9 44
10 33
10 36
11 90
12 14
12 29
13 50
13 52
14 20
15 65
15 17
16 31
16 39
17 75
18 11
18 24
19 46
20 58
21 13
22 41
23 30
24 62
25 19

Sample Output 4

1

Sample Input 5

1 2
1 1
1 1

Sample Output 5

2