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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

namespace ABC121_C___Energy_Drink_Collector
{
    class Program
    {
        static void Main(string[] args)
        {
            var read = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);
            var N = read[0];
            var M = read[1];
            var shops = new PriorityQueue<Shop>(true);

            for(int i = 0 ; i < N ; i++)
            {
                read = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);
                shops.Push(new Shop(read[0],read[1]));
            }

            Shop currentShop  = currentShop = shops.Pop();
            long amount = 0;
            while(shops.Count() > 0)
            {
                if(currentShop.stock > M) break;
                M -= currentShop.stock;
                amount += currentShop.price * currentShop.stock;
                currentShop = shops.Pop();
            }
            amount += currentShop.price * M;

            Console.WriteLine(amount);
        }
    }
    class Shop : IComparable
    {
        public long price{get;set;}
        public long stock{get;set;}

        public int CompareTo(object _shop)
        {
            Shop shop = (Shop)_shop;
            if(this.price < shop.price) return -1;
            if(this.price > shop.price) return 1;
            if(this.price == shop.price) return 0;
            return 0;
        }
        public Shop(long _price,long _stock)
        {
            this.price = _price;
            this.stock = _stock;
        }
    }

    class PriorityQueue<T> where T : IComparable
{
    private List<T> nodes;
    private Boolean isAsc;

    public PriorityQueue(Boolean _isAsc)
    {
        this.nodes = new List<T>();
        this.isAsc = _isAsc;
    }

    public int Count()
    {
        return this.nodes.Count;
    }
    public T Pop()
    {
        T returnNode = this.nodes[0];
        this.nodes[0] = this.nodes[nodes.Count-1];
        this.nodes.RemoveAt(nodes.Count-1);
        int root = 0;
        while(root*2 + 1 < this.nodes.Count)
        {
            int child = root * 2 + 1;
            if(child+1<this.nodes.Count && compare(this.nodes[child+1],this.nodes[child]) > 0) child += 1;
            if(compare(this.nodes[child],this.nodes[root]) > 0)
            {
                swap(child,root);
                root = child;
            }else{
                break;
            }
        }
        return returnNode;
    }
    public void Push(T t)
    {
        this.nodes.Add(t);
        var addPosition = nodes.Count - 1;
        while(addPosition > 0)
        {
            var parent = (int)(addPosition-1)/2;
            if(compare(nodes[addPosition],nodes[parent]) > 0 )
            {
                swap(addPosition,parent);
                addPosition = parent;
            }else{
                break;
            }
     }
    }
    private int compare(T x,T y)
    {
        return this.isAsc ? y.CompareTo(x) : x.CompareTo(y);
    }
    private void swap(int x,int y)
    {
        T temp = nodes[x];
        nodes[x] = nodes[y];
        nodes[y] = temp;
    }
}
}