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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static C.MyUtil;

namespace C
{
	internal class Program
	{
		public static void Main(string[] args)
		{
			int n, m;
			Input(out n, out m);
			var ary = new Tuple<long, long>[n];

			for (var i = 0; i < n; i++)
			{
				long a, b;
				Input(out a, out b);
				ary[i] = new Tuple<long, long>(a, b);
			}

			Array.Sort(ary, (x, y) => x.Item1 > y.Item1 ? 1 : -1);

			var result = 0L;
			var count = 0L;
			for (var i = 0;; i++)
			{
				if (count + ary[i].Item2 >= m)
				{
					result += (m - count) * ary[i].Item1;
					Console.WriteLine(result);
					return;
				}

				result += ary[i].Item1 * ary[i].Item2;
				count += ary[i].Item2;
			}
		}
	}

	public static class MyUtil
	{
		/**************************************************
		 * 入力
		 **************************************************/

		public static string InString => Console.ReadLine().Trim();
		public static int InInt => int.Parse(InString);
		public static long InLong => long.Parse(InString);
		public static double InDouble => double.Parse(InString);
		public static char InChar => char.Parse(InString);

		public static IEnumerable<string> InStrings =>
			InString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

		public static IEnumerable<int> InInts => InStrings.Select(int.Parse);
		public static IEnumerable<long> InLongs => InStrings.Select(long.Parse);
		public static IEnumerable<double> InDoubles => InStrings.Select(double.Parse);
		public static IEnumerable<char> InChars => InStrings.Select(char.Parse);

		public static string[] InStringArray => InStrings.ToArray();
		public static int[] InIntArray => InInts.ToArray();
		public static long[] InLongArray => InLongs.ToArray();
		public static double[] InDoubleArray => InDoubles.ToArray();
		public static char[] InCharArray => InChars.ToArray();

		public static List<string> InStringList => InStrings.ToList();
		public static List<int> InIntList => InInts.ToList();
		public static List<long> InLongList => InLongs.ToList();
		public static List<double> InDoubleList => InDoubles.ToList();
		public static List<char> InCharList => InChars.ToList();

		public static void Input<T>(out T variable) { variable = ChangeValue<T>(InString); }

		public static void Input<T1, T2>(out T1 variable1, out T2 variable2)
		{
			var ary = InStringArray;
			variable1 = ChangeValue<T1>(ary[0]);
			variable2 = ChangeValue<T2>(ary[1]);
		}

		public static void Input<T1, T2, T3>(out T1 variable1, out T2 variable2, out T3 variable3)
		{
			var ary = InStringArray;
			variable1 = ChangeValue<T1>(ary[0]);
			variable2 = ChangeValue<T2>(ary[1]);
			variable3 = ChangeValue<T3>(ary[2]);
		}

		public static void Input<T1, T2, T3, T4>(out T1 variable1, out T2 variable2, out T3 variable3, out T4 variable4)
		{
			var ary = InStringArray;
			variable1 = ChangeValue<T1>(ary[0]);
			variable2 = ChangeValue<T2>(ary[1]);
			variable3 = ChangeValue<T3>(ary[2]);
			variable4 = ChangeValue<T4>(ary[3]);
		}

		public static void Input<T1, T2, T3, T4, T5>(out T1 variable1,
		                                             out T2 variable2,
		                                             out T3 variable3,
		                                             out T4 variable4,
		                                             out T5 variable5)
		{
			var ary = InStringArray;
			variable1 = ChangeValue<T1>(ary[0]);
			variable2 = ChangeValue<T2>(ary[1]);
			variable3 = ChangeValue<T3>(ary[2]);
			variable4 = ChangeValue<T4>(ary[3]);
			variable5 = ChangeValue<T5>(ary[4]);
		}

		public static void Input<T1, T2, T3, T4, T5, T6>(out T1 variable1,
		                                                 out T2 variable2,
		                                                 out T3 variable3,
		                                                 out T4 variable4,
		                                                 out T5 variable5,
		                                                 out T6 variable6)
		{
			var ary = InStringArray;
			variable1 = ChangeValue<T1>(ary[0]);
			variable2 = ChangeValue<T2>(ary[1]);
			variable3 = ChangeValue<T3>(ary[2]);
			variable4 = ChangeValue<T4>(ary[3]);
			variable5 = ChangeValue<T5>(ary[4]);
			variable6 = ChangeValue<T6>(ary[5]);
		}

		/**************************************************
		 * 出力
		 **************************************************/

		public static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };

		public static void Print(string output) { sw.WriteLine(output); }

		public static void Print<T>(IEnumerable<T> outputs) { Print(string.Join(" ", outputs)); }

		public static void Print(params object[] outputs) { Print(string.Join(" ", outputs)); }


		/**************************************************
		 * 中間メソッド
		 **************************************************/

		private static bool IsEqualType<T1, T2>() { return typeof(T1) == typeof(T2); }

		private static T1 ChangeType<T1, T2>(T2 src) { return (T1) Convert.ChangeType(src, typeof(T1)); }

		private static T ChangeValue<T>(string s)
		{
			if (IsEqualType<T, int>()) { return ChangeType<T, int>(int.Parse(s)); }

			if (IsEqualType<T, long>()) { return ChangeType<T, long>(long.Parse(s)); }

			if (IsEqualType<T, double>()) { return ChangeType<T, double>(double.Parse(s)); }

			if (IsEqualType<T, char>()) { return ChangeType<T, char>(char.Parse(s)); }

			return ChangeType<T, string>(s);
		}
	}
}