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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#[allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

#[allow(dead_code)]
fn yn(result: bool) {
    if result {
        println!("Yes");
    } else {
        println!("No");
    }
}

fn main() {
    let v = read_vec::<i32>();
    let n = v[0] as usize;
    let m = v[1] as usize;
    let ab = read_vec2::<u64>(n as u32);

    let inf = std::u64::MAX >> 1;
    let mut dp = vec![vec![inf; m+1]; n+1];
    let mut bi = vec![vec![0u64; m+1]; n+1];

    for i in 0..n+1 { dp[i][0] = 0; }

    for j in 1..m+1 {
        for i in 1..n+1 {
            let a = ab[i-1][0];
            let b = ab[i-1][1];

            //println!("({}, {}): {}, {} + {}, {}", i, j, dp[i - 1][j], dp[i][j-1], a, bi[i][j-1]);
            let dp1 = dp[i - 1][j];
            let dp2 = dp[i][j - 1] + a;
            if dp1 <= dp2 {
                dp[i][j] = dp1;
            } else if dp2 < dp1 {
                if bi[i][j - 1] >= b  {
                    dp[i][j] = dp[i - 1][j - (bi[i][j - 1] as usize)] + a * b;
                    bi[i][j] = bi[i][j - 1];
                } else {
                    dp[i][j] = dp2;
                    bi[i][j] = bi[i][j-1] + 1;
                }
            }
        }
    }
    //println!("{:?}", dp);
    //println!("{:?}", bi);
    println!("{}", dp[n][m]);
}