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: Wrong Answer)

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#define debug cerr << "--" << __LINE__ << "--" << "\n"
using namespace std;

const double EPS = 1e-8;

static int roundToInt(double x){
    return (x < 0) ? -(int)(-x + 0.5) : (int)(x + 0.5);
}


class Var{ // NOTE: immutable
public:
    string sval;
    long long dval;
    enum VarType {STRING = 0, NUMBER = 1};
    VarType type;
    enum NumericState {UNKNOWN = -1, STRINGY = 0, NUMERIC = 1};
    mutable NumericState numericState;

    Var(){sval = ""; type = STRING; numericState = STRINGY;} // represent null?
    Var(string s){
        sval = s; type = STRING; numericState = UNKNOWN;
    }
    Var(long long d){dval = d; type = NUMBER; numericState = NUMERIC;}
    Var(const Var &v){
        sval = string(v.sval); dval = v.dval;
        type = v.type; numericState = v.numericState;
    }
    static bool isNumericString(const string &s) {
        char* ep;
        //cause side-effect: errno can be ERANGE after calling strtod
        strtoll(s.c_str(), &ep, 10);
        //Scratch 3.0 recognize the string cause underflows or overflows as Numeric
        return NULL != ep && '\0' == ep[0] && s[0] != '\0';
        // TODO: In Scratch '000' is regarded as non-numeric (but here regarded as numeric)
    }
    bool isNumeric() const{
        if (type == NUMBER) return true;
        if (numericState != UNKNOWN) return numericState == NUMERIC;
        bool numeric = isNumericString(sval);
        numericState = (numeric) ? NUMERIC : STRINGY;
        return numeric;
    }
    long long asNumber() const{
        if (type == NUMBER) return dval;
        return (isNumeric()) ? atoll(sval.c_str()) : 0.0;
    }
    static bool isNearInteger(const double &x){
        return true;
        // return fabs(round(x) - x) < EPS;
        // TODO: allow integer type in Var class
    }
    static bool isNearNumber(const double &x, const double &y){
        return true;
        // return fabs(x - y) < EPS;
    }
    string asString() const{
        if (type == STRING) return sval;
        if (isNearInteger(dval)) return to_string(roundToInt(dval));
        return to_string(dval);
    }
    Var operator+(const Var &y) const{
        return Var(this->asNumber() + y.asNumber());
    }
    Var operator+=(const Var &y){
        *this = *this + y;
        return *this;
    }
    Var operator-(const Var &y) const{
        return Var(this->asNumber() - y.asNumber());
    }
    Var operator*(const Var &y) const{
        return Var(this->asNumber() * y.asNumber());
    }
    Var operator/(const Var &y) const{
        return Var(this->asNumber() / y.asNumber());
    }
    Var operator%(const Var &y) const{
        return Var(fmod(this->asNumber(), y.asNumber()));
    }
    bool operator<(const Var &y) const{
        if (this->isNumeric() && y.isNumeric()){
            return this->asNumber() < y.asNumber();
        }// compare as number if both can be interpreted as numeric
        return this->asString() < y.asString();
    }
    bool operator>(const Var &y) const{
        return y < *this;
    }
    bool operator==(const Var &y) const{
        if (this->isNumeric() && y.isNumeric()){
            return this->asNumber() == y.asNumber();
        }// compare as numeric if both are numeric
        return this->asString() == y.asString();
    }
    friend ostream& operator << (ostream& os, const Var& p);
    friend istream& operator >> (istream& is, const Var& p);
};
ostream& operator << (ostream& os, const Var& p){
    os << p.asString();
    return os;
}
istream& operator >> (istream& is, Var& p){
    string s; is >> s; p = Var(s);
    return is;
}

Var letterOf(Var index, Var sourceString){
    /* index: 1-origined */
    string str = sourceString.asString();
    int idx = (int)(index.asNumber() - 1);
    // seem to be dirty but Scratch seems to do like this.
    // ex. letterOf(0.01, "world") == "w", letterOf(1.99, "world") == "w", letterOf(5.99, "world") == "d"
    if (0 <= idx && idx < str.size()) return Var(str.substr(idx, 1));
    return Var();
}

class VarList{
public:
    vector<Var> data;
    VarList(const vector<Var> &x) { data = x; }
    void push_back(const Var &x){ data.push_back(x); }
    void pop_back(){ data.pop_back(); }
    void clear(){ data.clear(); }
    int size(){ return (int) data.size(); }
    Var getLineOfList(const Var &index) const{
        /* index: 1-origined */
        int idx = (int)index.asNumber() - 1;
        // (unlike 'letterOf', index==0.9 does not work.)
        if (0 <= idx && idx < data.size()) return data[idx];
        return Var();
    }
    void setLineOfListTo(const Var &index, const Var &v){
        /* index: 1-origined */
        int idx = (int)index.asNumber() - 1;
        if (0 <= idx && idx < data.size()) data[idx] = v;
    }
    void deleteLineOfList(const Var &index){
        /* index: 1-origined */
        string kwd = index.asString();
        if (kwd == "all"){
            data.clear();
        }else if (kwd == "last"){
            data.pop_back();
        }else{
            int idx = (int)index.asNumber() - 1;
            if (0 <= idx && idx < data.size()) data.erase(data.begin() + idx);
        }
    }
    void insertAtIndexOfList(const Var &item, const Var &index){
        /* index: 1-origined */
        int idx = (int)index.asNumber() - 1;
        if (0 <= idx && idx <= data.size()) data.insert(data.begin() + idx, item);   
    }
    void insertAtRandomOfList(const Var &item){
        int idx = rand() % (data.size() + 1);
        data.insert(data.begin() + idx, item);
    }
    string asString() const{
        /* concatenate elements of list with space */
        // TODO: concatenated without spaces only if all elements are single characters.
        // (Is it an official bug? (or feature?))
        string ret;
        for(int i=0;i<data.size();i++){
            if (i > 0) ret += ' ';
            ret += data[i].asString();
        }
        return ret;        
    }
    int itemNumOfList(const Var &item) const{
        auto itr = find(data.begin(), data.end(), item);
        if (itr == data.end()) return 0;
        return 1 + (int)(itr - data.begin());
        /* index: 1-origined */
    }
    friend ostream& operator << (ostream& os, const VarList& p);
};
ostream& operator << (ostream& os, const VarList& p){
    os << p.asString();
    return os;
}

double randUniform(double x, double y){
    if (x > y) return randUniform(y, x);
    if (Var::isNearInteger(x) && Var::isNearInteger(y)){
        int xi = roundToInt(x), yi = roundToInt(y);
        return xi + rand() % (yi - xi + 1);
    }else{
        return x + (y - x) * (0.0 + rand()) / RAND_MAX;
    }
}

Var buf_answer; // for "answer"

// ============================= Scripts =============================

// variable declaration
Var var_n(4);
Var var_div(8);
Var var_seg(9);
Var var_fb(1);
Var var_k(5);
Var var_sb(5);
Var var_fe(5);
Var var_se(5);
Var var_i(3);
Var var_j(5);
Var var_p(5);
Var var_N(4);
Var var_M(15);
Var var_buf(100000);
Var var___i(18);
Var var_ans(130);
Var var_buy(18);

// list declaration
VarList list_tmp({Var(2), Var(3), Var(6), Var(7)});
VarList list_ary({Var(2), Var(3), Var(6), Var(7)});
VarList list_A({});
VarList list_B({Var(18), Var(5), Var(10), Var(9)});
VarList list_retlist({Var(1000000000), Var(100000)});
VarList list_ary__idx({Var(2), Var(3), Var(1), Var(4)});
VarList list_tmp__idx({Var(2), Var(3), Var(1), Var(4)});

// prototype declaration of functions
int func_merge__sort__ary();
int main();
int func_string__to__retlist(const Var &arg_str);

// contents of functions
int func_merge__sort__ary(){
    var_n = Var(list_ary.size());
    list_tmp.deleteLineOfList(Var("all"));
    list_tmp__idx.deleteLineOfList(Var("all"));
    list_ary__idx.deleteLineOfList(Var("all"));
    var_i = Var(1);
    for (int pvimdswq=0;pvimdswq<var_n.asNumber();pvimdswq++){
        list_ary__idx.push_back(var_i);
        list_tmp.push_back(Var(0));
        list_tmp__idx.push_back(Var(0));
        var_i += Var(1);
    }
    var_n += Var(1);
    var_div = Var(1);
    while (!(!(var_div < var_n))){
        var_seg = Var(1);
        while (!(!(var_seg < var_n))){
            var_fb = var_seg;
            var_k = var_seg;
            if ((((var_fb + var_div) < var_n) || ((var_fb + var_div) == var_n))){
                var_sb = (var_fb + var_div);
                var_fe = (var_fb + var_div);
            } else {
                var_sb = var_n;
                var_fe = var_n;
            }
            if ((((var_sb + var_div) < var_n) || ((var_sb + var_div) == var_n))){
                var_se = (var_sb + var_div);
            } else {
                var_se = var_n;
            }
            var_i = var_fb;
            var_j = var_sb;
            while (!(!((var_i < var_fe) && (var_j < var_se)))){
                if (((list_ary.getLineOfList(var_i) < list_ary.getLineOfList(var_j)) || (list_ary.getLineOfList(var_i) == list_ary.getLineOfList(var_j)))){
                    list_tmp.setLineOfListTo(var_k, list_ary.getLineOfList(var_i));
                    list_tmp__idx.setLineOfListTo(var_k, list_ary__idx.getLineOfList(var_i));
                    var_i += Var(1);
                } else {
                    list_tmp.setLineOfListTo(var_k, list_ary.getLineOfList(var_j));
                    list_tmp__idx.setLineOfListTo(var_k, list_ary__idx.getLineOfList(var_j));
                    var_j += Var(1);
                }
                var_k += Var(1);
            }
            while (!(!(var_i < var_fe))){
                list_tmp.setLineOfListTo(var_k, list_ary.getLineOfList(var_i));
                list_tmp__idx.setLineOfListTo(var_k, list_ary__idx.getLineOfList(var_i));
                var_k += Var(1);
                var_i += Var(1);
            }
            while (!(!(var_j < var_se))){
                list_tmp.setLineOfListTo(var_k, list_ary.getLineOfList(var_j));
                list_tmp__idx.setLineOfListTo(var_k, list_ary__idx.getLineOfList(var_j));
                var_k += Var(1);
                var_j += Var(1);
            }
            var_p = var_seg;
            while (!(!(var_p < var_k))){
                list_ary.setLineOfListTo(var_p, list_tmp.getLineOfList(var_p));
                list_ary__idx.setLineOfListTo(var_p, list_tmp__idx.getLineOfList(var_p));
                var_p += Var(1);
            }
            var_seg = (var_seg + (var_div * Var(2)));
        }
        var_div = (var_div * Var(2));
    }
    var_n += Var(-1);
    return 0;
}

int main(){
    list_ary.clear();
    list_A.clear();
    list_B.clear();
    cin >> buf_answer;
    var_N = buf_answer;
    cin >> buf_answer;
    var_M = buf_answer;
    for (int uzrgikhs=0;uzrgikhs<var_N.asNumber();uzrgikhs++){
        cin >> buf_answer;
        list_ary.push_back(buf_answer);
        cin >> buf_answer;
        list_B.push_back(buf_answer);
    }
    func_merge__sort__ary();
    var_ans = Var(0);
    var_i = Var(1);
    for (int bxxvntcy=0;bxxvntcy<var_N.asNumber();bxxvntcy++){
        var_buy = list_B.getLineOfList(list_ary__idx.getLineOfList(var_i));
        if ((var_buy < var_M)){
            var_ans += (list_ary.getLineOfList(var_i) * var_buy);
            var_M += (Var(0) - var_buy);
        } else {
            var_ans += (list_ary.getLineOfList(var_i) * var_M);
            cout << var_ans << endl;
            return 0;
        }
        var_i += Var(1);
    }
    return 0;
}

int func_string__to__retlist(const Var &arg_str){
    list_retlist.clear();
    var_buf = Var("");
    var___i = Var(1);
    for (int okuazbmr=0;okuazbmr<Var(arg_str.asString().size()).asNumber();okuazbmr++){
        if ((letterOf(var___i, arg_str) == Var(" "))){
            list_retlist.push_back(var_buf);
            var_buf = Var("");
        } else {
            var_buf = Var(var_buf.asString() + letterOf(var___i, arg_str).asString());
        }
        var___i += Var(1);
    }
    if ((!(var_buf == Var("")))){
        list_retlist.push_back(var_buf);
    }
    return 0;
}