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;
using System.Linq;

namespace abc121_c
{
    class Program
    {
        static void Main(string[] args)
        {
            var NM = Console.ReadLine().Split(' ').Select(x => decimal.Parse(x)).ToList();
            var N = NM[0];
            var M = NM[1];
            
            var pq = new PriorityQueue<decimal>();

            for (int i = 1; i <= N; i++)
            {
                var AB = Console.ReadLine().Split(' ').Select(x => decimal.Parse(x)).ToList();
                var v = (AB[0] * -10000000000) - AB[1];
                pq.Push(v);
            }

            decimal r = 0;

            while (true)
            {
                decimal v = pq.Top;
                decimal A = Math.Truncate(v / -10000000000);
                decimal B = -1 * v - A * 10000000000;

                if(M >= B)
                {
                    r += A * B;
                    M -= B;
                }
                else 
                {
                    r += A * M;
                    M = 0;
                }

                pq.Pop();

                if (M == 0) break;
            }

            Console.WriteLine(r);

        }


        /// <summary>
        /// 優先度付き待ち行列
        /// </summary>
        /// <typeparam name="T">要素の型</typeparam>
        public class PriorityQueue<T>
          where T : IComparable<T>
        {
            #region フィールド

            public IList<T> buffer;

            #endregion
            #region 初期化

            public PriorityQueue() { this.buffer = new List<T>(); }
            public PriorityQueue(int capacity) { this.buffer = new List<T>(capacity); }

            #endregion
            #region ヒープ操作

            /// <summary>
            /// ヒープ化されている配列リストに新しい要素を追加する。
            /// </summary>
            /// <param name="array">対象の配列リスト</param>
            static void PushHeap(IList<T> array, T elem)
            {
                int n = array.Count;
                array.Add(elem);

                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (array[n].CompareTo(array[i]) > 0)
                    {
                        T tmp = array[n]; array[n] = array[i]; array[i] = tmp;
                    }
                    n = i;
                }
            }

            /// <summary>
            /// ヒープから最大値を削除する。
            /// </summary>
            /// <param name="array">対象の配列リスト</param>
            static void PopHeap(IList<T> array)
            {
                int n = array.Count - 1;
                array[0] = array[n];
                array.RemoveAt(array.Count - 1);

                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (array[j].CompareTo(array[j + 1]) < 0))
                        j++;
                    if (array[i].CompareTo(array[j]) < 0)
                    {
                        T tmp = array[j]; array[j] = array[i]; array[i] = tmp;
                    }
                    i = j;
                }
            }

            #endregion
            #region 要素の挿入・削除

            /// <summary>
            /// 要素のプッシュ。
            /// </summary>
            /// <param name="elem">挿入したい要素</param>
            public void Push(T elem)
            {
                PushHeap(this.buffer, elem);
            }

            /// <summary>
            /// 要素を1つポップ。
            /// </summary>
            /// <remarks>
            /// 今回の実装では、先頭要素の読み出しと削除は別に行う。
            /// この Pop では削除のみ。
            /// 読み出しには Top プロパティを使う。
            /// </remarks>
            public void Pop()
            {
                PopHeap(this.buffer);
            }

            /// <summary>
            /// 先頭要素の読み出し。
            /// </summary>
            public T Top
            {
                get { return this.buffer[0]; }
            }

            public int Count
            {
                get { return this.buffer.Count; }
            }

            #endregion
        }

    }
}