#P9977. [USACO23DEC] Bovine Acrobatics S

[USACO23DEC] Bovine Acrobatics S

Problem Description

Farmer John has decided to have his cows perform acrobatics. First, FJ weighed his cows and found that they have NN (1N2×1051 \le N \le 2 \times 10^5) distinct weights. Specifically, for all i[1,N]i \in [1, N], there are aia_i cows whose weight is wiw_i units (1ai1091 \le a_i \le 10^9, 1wi1091 \le w_i \le 10^9).

His most famous act requires the cows to form a balanced cow tower. A cow tower consists of some cows, where each cow stands on top of the next cow. A cow tower is balanced if and only if every cow being stepped on is at least KK (1K1091 \le K \le 10^9) units heavier than the cow directly standing on it. Every cow may be used as part of a cow tower.

If FJ wants to create at most MM (1M1091 \le M \le 10^9) cow towers, what is the maximum number of cows that can be used as part of the cow towers?

Input Format

The first line contains three space-separated integers NN, MM, and KK.

The next NN lines each contain two space-separated integers wiw_i and aia_i. It is guaranteed that all wiw_i are distinct.

Output Format

Print the maximum number of cows in the cow towers when FJ uses the best strategy.

3 5 2
9 4
7 6
5 5
14
3 5 3
5 5
7 6
9 4
9

Hint

Sample Explanation 1

FJ can use cows of weights 5,7,95, 7, 9 to create four balanced cow towers, and then use cows of weights 5,75, 7 to create one more.

Sample Explanation 2

FJ can use cows of weights 5,95, 9 to create four balanced cow towers, and then use one cow of weight 77 to create one more. Alternatively, he can use cows of weights 5,95, 9 to create four balanced cow towers, and then use one cow of weight 55 to create one more.

Test Point Properties

  • Test points 353-5 satisfy M5000M \le 5000 and the total number of cows does not exceed 50005000.
  • Test points 6116-11 satisfy that the total number of cows does not exceed 21052 \cdot 10^5.
  • Test points 121712-17 have no additional constraints.

Translated by ChatGPT 5