Problem:
Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.

Solution in Lua (5.3.2) (judge verdict: Accepted)

local DBG = false
function dbgpr(...)
    if DBG then
        io.write("[dbg]")
        print(...)
    end
end

function dbgpr_t(tbl)
    if DBG then
        dbgpr(tbl)
        io.write("[dbg]")
        for i,v in ipairs(tbl) do
            io.write(i)
            io.write(":")
            io.write(tostring(v))
            io.write(" ")
        end
        print("")
    end
end

function dbgpr_t2d(tbl2d)
    if DBG then
        dbgpr(tbl2d)
        for i,t in ipairs(tbl2d) do
            io.write("[dbg]")
            for j,v in ipairs(t) do
                io.write("(" .. tostring(i) .. "," .. tostring(j) .. "): ")
                io.write(tostring(v))
                io.write("; ")
            end
            print("")
        end
    end
end
function create_tbl(a, initial)
    local tbl = {}
    for i=1,a do
        tbl[i] = initial
    end
    return tbl
end

function create_2d_tbl(a, b, initial)
    local tbl = {}
    for i=1,a do
        local t = {}
        for j=1,b do
            t[j] = initial
        end
        tbl[i] = t
    end
    return tbl
end

function parse_problem()
    local N, M = io.read("n", "n")
    local stores = {}
    for i=1,N do
        local a, b = io.read("n", "n")
        stores[i] = {a, b}
    end
    table.sort(stores, function (x, y) return x[1] < y[1] end)
    return N, M, stores
end

function main()
    local N, M, stores = parse_problem()
    dbgpr(N, M)
    dbgpr("====stores")
    dbgpr_t2d(stores)
    local remain = M
    local total_price = 0
    local idx = 1
    while remain > 0 do
        local unit_price, stock = stores[idx][1], stores[idx][2]
        local units = stock
        if remain - units < 0 then
            units = remain
        end
        total_price = total_price + unit_price * units
        remain = remain - units
        dbgpr(idx, remain)
        idx = idx + 1
    end
    print(total_price)
end

main()