Under construction

Still being built. Things may change

Chocolate Buying

Interview

Time limit1sMemory limit128 MB

Summary
Given N chocolate types with a cost per piece and a number of cows wanting each, spend a budget B to satisfy as many cows as possible.
Level

Medium4 of 10

Topics
Greedy, Sorting, Array, Implementation
Solved
No attempts yet

Problem

Bessie and the herd love chocolate, so Farmer John is buying them some.

The chocolate store carries NN (1≤N≤100,0001 \le N \le 100{,}000) kinds of chocolate, each available in essentially unlimited quantity. Each type ii costs PiP_i (1≤Pi≤10181 \le P_i \le 10^{18}) per piece, and there are CiC_i (1≤Ci≤10181 \le C_i \le 10^{18}) cows that want that type.

Farmer John has a budget of BB (1≤B≤10181 \le B \le 10^{18}) to spend on chocolate for the cows. What is the maximum number of cows he can satisfy? Every cow wants exactly one type of chocolate and is satisfied only by that type.

For example, suppose Farmer John has 50 to spend across 5 types of chocolate. Eleven cows in total have the following preferences:

Chocolate typeCost per pieceCows preferring this type
153
211
3104
472
5601

Farmer John cannot buy chocolate type 5, because he does not have enough money. Even if it cost only 50, it would be a wasteful purchase, since it would satisfy just one cow.

Starting from the cheapest chocolate, he can buy 1 piece of type 2 for 1×1=11 \times 1 = 1, leaving 50−1=4950 - 1 = 49; then 3 pieces of type 1 for 3×5=153 \times 5 = 15, leaving 49−15=3449 - 15 = 34; then 2 pieces of type 4 for 2×7=142 \times 7 = 14, leaving 34−14=2034 - 14 = 20; then 2 pieces of type 3 for 2×10=202 \times 10 = 20, leaving 20−20=020 - 20 = 0.

He therefore satisfies 1+3+2+2=81 + 3 + 2 + 2 = 8 cows.

Input

  • Line 1: Two space-separated integers, NN and BB.
  • Lines 2 to N+1N+1: Line ii contains two space-separated integers describing chocolate type ii: PiP_i and CiC_i.

Output

  • Line 1: A single integer, the maximum number of cows that Farmer John can satisfy.

Examples3

  1. Example 1

    Input
    5 50
    5 3
    1 1
    10 4
    7 2
    60 1
    
    Expected output
    8
    
  2. Example 2

    Input
    1 10
    5 2
    
    Expected output
    2
    
  3. Example 3

    Input
    1 1
    2 5
    
    Expected output
    0