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++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <stdio.h>
#include <math.h> 
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <string>
#include <numeric>
#include <bitset>
#include <queue>
#include <map>
#include <typeinfo>

using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } //最大公約数


int main() {
	
	//入力
	long long int N, M;
	cin >> N >> M;

	vector<vector<long long int>> shop;
	for (int i = 0; i < N; ++i) {
		vector<long long int>tmp;
		long long int cost, num;
		cin >> cost >> num;
		tmp.push_back(cost);
		tmp.push_back(num);
		shop.push_back(tmp);
	}

	//貪欲に求めてみる
	long long int sum_cost = 0;

	//最も価格が安い商品を探す
	vector<long long int> kakaku;
	for (int i = 0; i < N; ++i) {
		kakaku.push_back(shop[i][0]);
	}
	sort(kakaku.begin(), kakaku.end());

	int kakaku_cnt = 0; //kakakuの何番目の要素かを管理する。
	int rp_cnt = 0; //本数の加算カウンタ
	while (1) { //∞ループ
		if (rp_cnt == M) { //必要本数に到達したときbreak
			cout << sum_cost << endl;
			exit(0);
		}
		for (int i = 0; i < N; ++i) { //店の数でループ
			if (kakaku_cnt >= kakaku.size()) {
				break;
			}
			if (kakaku[kakaku_cnt] == shop[i][0]) {
				int rp_cnt_own = 0; //その価格での本数カウンタ
				while (1) {
					sum_cost = sum_cost + shop[i][0];
					rp_cnt++;
					rp_cnt_own++;
					if (rp_cnt == M || rp_cnt_own == shop[i][1]) { //ある店での本数の上限に達するか必要な本数に達したときbreak
						if (rp_cnt == M) { //必要本数に到達したときbreak
							cout << sum_cost << endl;
							exit(0);
						}
						kakaku_cnt++; //足りていない場合は次に安いものをとるため
						break;
					}
				}
			}
		}
	}
}