C - Ball and Box Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900900

問題文

球橋さんと箱木さんはボールと箱を使ったゲームをします。

最初、球橋さんは MM 種類のボールをそれぞれ 1010010^{100} 個ずつ持っていて、 箱木さんは 1010010^{100} 円持っています。 また、NN 個の箱があり、ii 番目の箱の容量は ViV_i で、値段は PiP_i 円です。ゲーム中、箱木さんはいつでも好きな箱を買うことができます。

このゲームでは、ゲームが終わるまで以下の操作を繰り返します。

  1. 球橋さんはボールを 11 つ選び、箱木さんに渡す。
  2. 箱木さんは渡されたボールを受け取るか、受け取らずにゲームを終えるかを選ぶ。
  3. ボールを受け取った場合、箱木さんは購入済みの箱を 11 つ選び、受け取ったボールを入れる。
  4. ボールを入れた箱が以下の条件を満たしている場合、箱木さんは 11 円をもらう。そうでない場合、ゲームを終える。
    • 箱の中のボールの個数は、その箱の容量以下である。
    • 箱の中のボールの種類は、すべて同じである。

球橋さんは、ゲーム終了時の箱木さんの所持金がなるべく少なくなるための最適な行動をし、反対に、箱木さんはなるべく多くなるための最適な行動をします。 ゲームを通して、箱木さんの所持金はいくら増えますか?

ただし、両者ともにすべての情報が公開されているとします。特に、球橋さんは、それぞれの箱について、容量と値段、どの種類のボールがいくつ入っているかを見ることができます。 また、箱木さんの初期の所持金は十分多く、お金が足りなくて箱が買えなくなることはないことに注意してください。

11 つの入力ファイルにつき、TT 個のテストケースを解いてください。

制約

  • 1T,N,M3×1051\le T,N,M\le 3\times 10^5
  • 1Vi,Pi1091\le V_i,P_i \le 10^9
  • TT 個のテストケースに対する NN の総和は 3×1053\times 10^5 以下
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで casei\mathrm{case}_iii 番目のテストケースを意味する。

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
\vdots
caseT\mathrm{case}_T

各テストケースは以下の形式で与えられる。

NN MM
V1V_1 P1P_1
V2V_2 P2P_2
\vdots
VNV_N PNP_N

出力

両者が最適に行動したときの、箱木さんの最終的な所持金と最初の所持金の差を出力せよ。


入力例 1Copy

Copy
3
3 2
1 1000000000
3 1
3 1
1 300000
1000000000 1
10 4
22 5
26 45
72 21
47 39
97 2
75 35
82 24
17 46
32 22
28 67

出力例 1Copy

Copy
2
0
28

最初のテストケースでは 22 種類のボールと 33 つの箱を使います。 22 種類のボールをそれぞれ白のボールと黒のボールと呼び、ii 種類目の箱を箱 ii と呼ぶことにします。 このテストケースについて、所持金が 22 円増えるゲームの進み方の例を示します。

  1. 球橋さんが白のボールを選び、渡す。
  2. 箱木さんはボールを受け取り、箱 2211 円で買って白のボールを入れる。
    • 22 には白のボールが 11 個入っている。これは条件を満たしているため、箱木さんは 11 円をもらう。
  3. 球橋さんが白のボールを選び、渡す。
  4. 箱木さんはボールを受け取り、箱 22 に白のボールを入れる。
    • 22 には白のボールが 22 個入っている。これは条件を満たしているため、箱木さんは 11 円をもらう。
  5. 球橋さんが黒のボールを選び、渡す。
  6. 箱木さんはボールを受け取り、箱 3311 円で買って黒のボールを入れる。
    • 33 には黒のボールが 11 個入っている。これは条件を満たしているため、箱木さんは 11 円をもらう。
  7. 球橋さんが白のボールを選び、渡す。
  8. 箱木さんはボールを受け取り、箱 22 に白のボールを入れる。
    • 22 には白のボールが 33 個入っている。これは条件を満たしているため、箱木さんは 11 円をもらう。
  9. 球橋さんが白のボールを選び、渡す。
  10. 箱木さんは受け取らずにゲームを終えることを選ぶ。

最終的に、箱 22 には白のボールが 33 個、箱 33 には黒のボールが 11 個入っています。 合計で 22 円使って 44 円もらったので、所持金は 22 円増えました。

22 つめのテストケースでは、球橋さんは、箱木さんにお金を稼がせないような行動ができます。

Score : 900900 points

Problem Statement

Mr. Ball and Mr. Box will play a game with balls and boxes.

Initially, Mr. Ball has 1010010^{100} balls of each of MM different types, and Mr. Box has 1010010^{100} yen. There are NN boxes, where the ii-th box has capacity ViV_i and costs PiP_i yen. During the game, Mr. Box can buy any box at any time.

In this game, the following operations are repeated until the game ends:

  1. Mr. Ball chooses one ball and gives it to Mr. Box.
  2. Mr. Box either accepts the ball or ends the game without accepting it.
  3. If Mr. Box accepts the ball, he chooses one of his purchased boxes and puts the ball in it.
  4. If the box with the ball satisfies the following conditions, Mr. Box receives 11 yen. Otherwise, the game ends.
    • The number of balls in the box does not exceed its capacity.
    • All balls in the box are of the same type.

Mr. Ball will play optimally to minimize Mr. Box's final money, while Mr. Box will play optimally to maximize it. How much will Mr. Box's money increase throughout the game?

Here, both players have access to all information. In particular, Mr. Ball can see the capacity, price, and contents (type and number of balls) of each box. Also, note that Mr. Box's initial money is large enough that he will never run out of money to buy boxes.

Solve TT test cases for each input file.

Constraints

  • 1T,N,M3×1051\le T,N,M\le 3\times 10^5
  • 1Vi,Pi1091\le V_i,P_i \le 10^9
  • The sum of NN over the TT test cases is at most 3×1053\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where casei\mathrm{case}_i represents the ii-th test case:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
\vdots
caseT\mathrm{case}_T

Each test case is given in the following format:

NN MM
V1V_1 P1P_1
V2V_2 P2P_2
\vdots
VNV_N PNP_N

Output

Print the difference between Mr. Box's final and initial money when both players play optimally.


Sample Input 1Copy

Copy
3
3 2
1 1000000000
3 1
3 1
1 300000
1000000000 1
10 4
22 5
26 45
72 21
47 39
97 2
75 35
82 24
17 46
32 22
28 67

Sample Output 1Copy

Copy
2
0
28

In the first test case, there are two types of balls and three boxes. Let us call the two types of balls white and black balls, and call the ii-th box box ii. Here is an example of how the game could proceed where the money increases by 22 yen.

  1. Mr. Ball chooses and gives a white ball.
  2. Mr. Box accepts the ball, buys box 22 for 11 yen, and puts the white ball in it.
    • Box 22 contains 11 white ball. This satisfies the conditions, so Mr. Box receives 11 yen.
  3. Mr. Ball chooses and gives a white ball.
  4. Mr. Box accepts the ball and puts it in box 22.
    • Box 22 contains 22 white balls. This satisfies the conditions, so Mr. Box receives 11 yen.
  5. Mr. Ball chooses and gives a black ball.
  6. Mr. Box accepts the ball, buys box 33 for 11 yen, and puts the black ball in it.
    • Box 33 contains 11 black ball. This satisfies the conditions, so Mr. Box receives 11 yen.
  7. Mr. Ball chooses and gives a white ball.
  8. Mr. Box accepts the ball and puts it in box 22.
    • Box 22 contains 33 white balls. This satisfies the conditions, so Mr. Box receives 11 yen.
  9. Mr. Ball chooses and gives a white ball.
  10. Mr. Box chooses to end the game without accepting it.

Finally, box 22 contains 33 white balls and box 33 contains 11 black ball. Mr. Box spent 22 yen and received 44 yen, so his money increased by 22 yen.

In the second test case, Mr. Ball can play in a way that prevents Mr. Box from earning any money.



2025-03-29 (Sat)
10:22:19 +00:00