Problem:
Princess' Marriage
お姫様の嫁入り
English text is not available in this practice contest.
ある貧乏な国のおてんばで勇敢なお姫様は，ギャンブルの配当がパリミュチュエル方式で決定される事を知ることにより，ギャンブルについて詳しくなった気がしてギャンブルでの勝利を確信した．その結果，今までよりも更に多額のお金をつぎ込み，国民が納めた税金をすべて失うほどの負けを喫してしまった．この事態を重く受け止めた王様は，お姫様を隣の国へ嫁がせることにした．こうすることにより，お姫様を日頃の行いを反省させ，同時に隣国との交友を深めて財政援助をしてもらおうと考えたからである．
お姫様と隣国の王子様はお互いの事が気に入り，また両国の王様間でも政略結婚に関する同意がなされた．お姫様はなけなしのお金を手に意気揚々と隣の国へ出かけた．一方で，お姫様が嫁ぐ動機は王様の一方的な利益追及のためであり，快くないと考える隣国の王子様の側近は，お姫様を亡き者にするために道の途中に無数の刺客達を放った．
お姫様が通る道はすでに決められている．お姫様の通る道には合計
L
個の宿場がある．便宜上，出発地点および到着地点も宿場とし，各宿場を
S
1
,
S
2
, ...
S
L
と呼ぶことにする．お姫様は最初に
S
1
におり，昇順に (
S
2
,
S
3
... という順番で) 宿場を訪れて，最終的に
S
L
へ行くものとする．宿場では金銭を払って護衛を雇うことができ，お金がある限り好きな距離だけ契約してお姫様を守らせることができる．護衛を雇う費用は，距離1につき金1である．お姫様は通る区間内を部分的に守ってもらうことも出来ることに注意せよ．
S
i
と
S
i+1
間の距離は
D
i
，
S
i
と
S
i+1
間で距離1につき刺客に襲われる回数の期待値は
P
i
で与えられている．
お姫様が予算
M
を持っていて，刺客に襲われる回数の期待値が最小になるように護衛を雇ったときの，目的地までに刺客に襲われる回数の期待値を求めよ．
Input
入力は複数のデータセットからなる．各データセットは以下のような形式をとる．
N
M
D
1
P
1
D
2
P
2
...
D
N
P
N
各データセットの最初の行には二つの整数が与えられ，それぞれ区間の数
N
(1≦
N
≦10,000) と，お姫差が持つ予算
M
(0≦
M
≦1,000,000,000) を表す．次の
N
行はお姫様が通る道の情報を表す．各行は二つの整数が含まれており，
i
行目は区間の距離
D
i
(1≦
D
i
≦10,000) とその間を１単位距離移動したときに襲われる回数の期待値
P
i
(0≦
P
i
<=10) からなる．

入力の終端は
N
=0,
M
=0 となるデータセットであらわされる．このデータセットに対しては計算結果を出力してはならない．
Output
各データセット毎に，お姫様が目的地までに刺客に襲われる回数の期待値を出力せよ．
Sample Input
2 8
5 6
4 5
3 1
5 10
5 10
5 10
0 0
Output for the Sample Input
5
140

Submitted solutions:

code_snippet: //問題:https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015&lang=jp
//判定:AC

//学習コメント:
//辺の長さが同じ正方形を同時に数える処理に少し手間取った。

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <utility>
using namespace std;

int Prin(int n,int m){
    vector< pair<int,int> >A(n);
    int d,p;
    int ex=0;
    for(int i=0; i<n; i++){
        cin>>d>>p;
        ex+=d*p;
        A.at(i)=make_pair(p,d);
    }
    sort(A.begin(), A.end());
    reverse(A.begin(), A.end());
    int t=0;
    while(m>0){
        if(m>A.at(t).second){
            m-=A.at(t).second;
            ex-=(A.at(t).first*A.at(t).second);
            t++;
        }
        else{
            ex-=(A.at(t).first*m);
            m=0;
        }
        if(t==n){
            break;
        }
    }
    return ex;
}

int main(){
    int n,m;
    while(cin>>n>>m && n!=0){
        cout<<Prin(n,m)<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3268
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int N,M;
pair<int,int> PD[10010];

int main(){
    while(cin >> N >> M && N) {
        int d,p;
        for (int i = 0; i < N; ++i) {
            cin >> d >> p;
            PD[i] = make_pair(p,d);
        }
        sort(PD, PD + N, greater<pair<int,int>>());

        int S = 0;
        for (int i = 0; i < N; ++i) {
            S += PD[i].first * PD[i].second;
        }
        for (int i = 0; i < N; ++i) {
            if (M <= 0) break;
            int guarded = min(M, PD[i].second);
            S -= guarded * PD[i].first;
            M -= guarded;
        }
        cout << S << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3420
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool solve() {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) return false;
    vector<int> D(N), P(N);
    long long ans = 0;
    for (int i = 0; i < N; i++) {
        cin >> D[i] >> P[i];
        ans += D[i] * P[i];
    }
    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&] (int i, int j) {
        return P[i] > P[j];
    });
    for (int i : ord) {
        int d = min(D[i], M);
        D[i] -= d, M -= d;
        ans -= d * P[i];
    }
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

void init() {}

ll n, m;
vector<a2> v;
void input() {
    cin >> n >> m;
    v.resize(n);
    for(auto &x : v)
        cin >> x[1] >> x[0];
}

void solve() {
    ll ans = 0;
    for(auto &x : v)
        ans += x[0] * x[1];
    sort(v.rbegin(), v.rend());

    for(int i = 0; i < n && m; i++) {
        ll sub = min(m, v[i][1]);
        ans -= v[i][0] * sub;
        m -= sub;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    init();
    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3196
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)

int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

int solve() {
    ll n, m;
    cin >> n >> m;
	if(n == 0 && m == 0){return 1;}
    vector<ll> d(n),p(n);
    vector<pair<ll,ll>> d_p(n);
    rep(i, n){
        cin >> d[i] >> p[i];
        d_p[i].first = p[i];
        d_p[i].second = d[i];
    }
    sort(d_p.rbegin(),d_p.rend());
    ll ans = 0;
    rep(i, n){
        if(d_p[i].second <= m){
            m -= d_p[i].second;
            d_p[i].second = 0;
        }else{
            d_p[i].second -= m;
            m = 0;
            break;
        }
    }
    rep(i, n){
        ans += d_p[i].first * d_p[i].second;
    }
    cout << ans << endl;
    return 0;
}
int main() {
    while(solve() == 0);
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3332
final_rank: 7

code_snippet: // AOJ 2019 - Princess's Marriage
// JAG Practice Contest for Japan Domestic 2008 - Problem B

#include <bits/stdc++.h>
using namespace std;

bool solving() {
  int N, M; cin >> N >> M;
  if(N == 0) return false;

  vector<pair<int, int>> segment(N);
  for(int i=0; i<N; ++i) {
    int d, p; cin >> d >> p;
    segment[i] = {-p, d};
  }
  sort(segment.begin(), segment.end());

  long long ex = 0;
  for(int i=0; i<N; ++i) {
    if(segment[i].second <= M) {
      M -= segment[i].second;
    } else {
      ex -= segment[i].first * (segment[i].second - M);
      M = 0;
    }
  }
  cout << ex << endl;
  return true;
}

int main() {
  while(solving()) {}
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

ll intpow(ll a,ll b){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}

template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
    os << "[";
    for (size_t i = 0; i < vec.size(); ++i) {
        os << vec[i];
        if (i + 1 < vec.size()) os << ", ";
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
    os << "[";
    for (size_t j = 0; j < a.size(); ++j) {
        os << "(" << a[j].first << ", " << a[j].second << ")";
		if (j + 1 < a.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
	os << "[";
    for (size_t i = 0; i < mat.size(); ++i) {
		os << "[";
        for (size_t j = 0; j < mat[i].size(); ++j) {
            os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
			if (j + 1 < mat[i].size()) os << ", ";
        }
		os << "]";
		if (i + 1 < mat.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& [key, val] : m) {
        if (!first) os << ", ";
        os << key << ": " << val;
        first = false;
    }
    os << "}";
    return os;
}

template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}

void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}

template <typename T>
T sum(const std::vector<T>& v) {
    return std::accumulate(v.begin(), v.end(), T(0));
}

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

long long mod_mul(long long a, long long b, long long mod) {
    return (__int128)a * b % mod;
}

long long mod_pow(long long a, long long b, long long mod) {
    long long result = 1;
    a %= mod;
    while (b > 0) {
        if (b & 1) result = mod_mul(result, a, mod);
        a = mod_mul(a, a, mod);
        b >>= 1;
    }
    return result;
}

bool MillerRabin(ll x){
    
    if(x <= 1){return false;}
    if(x == 2){return true;}
    if((x & 1) == 0){return false;}
    vll test;
    if(x < (1 << 30)){
        test = {2, 7, 61};
    } 
    else{
        test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    }
    ll s = 0;
    ll d = x - 1;
    while((d & 1) == 0){
        s++;
        d >>= 1;
    }
    for(ll i:test){
        if(x <= i){return true;}
        ll y = mod_pow(i,d,x);
        if(y == 1 || y == x - 1){continue;}
        ll c = 0;
        for(int j=0;j<s-1;j++){
            y = mod_mul(y,y,x);
            if(y == x - 1){
                c = 1;
                break;
            }
        }
        if(c == 0){
            return false;
        }
    }
    return true;
}

int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    while(1){
        LL(n,m);
        if(n == 0 && m == 0){break;}
        vpll a;
        rep(i,n){
            LL(x,y);
            a.push_back({y,x});
        }
        sort(rall(a));
        ll ans = 0;
        for(auto [x,y]:a){
            ll z = min(y,m);
            y -= z;
            m -= z;
            ans += x * y;
        }
        print(ans);
    }
    
}
accuracy: 1
time_ms: 10
memory_kb: 3820
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
    int N,M,D,P,u,ans;
    cin >> N >> M;
    while(N!=0 or M!=0){
        ans = 0;
        vector<int>sumP(11,0);
        for(int i=0;i<N;i++){
            cin >> D >> P;
            sumP[P]+=D;
            ans += D * P;
        }
        
        u= 10;
        while(M > 0){
            ans -= min(M,sumP[u]) * u;
            M = M - sumP[u];
            u--;
            if(u == -1){
                ans = 0;
                break;
            }
        }
        cout << ans << endl;
        cin >> N >> M;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3436
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <functional>
using namespace std;
using ll = long long;
using lb = long double;

int main() {
    int n, m;
    while (true) {
        cin >> n >> m;
        if (n == 0 && m == 0) break;
        // sect: vector of pair<dist, exp>
        vector<pair<int, int>> sect;
        for (int i = 0; i < n; i++) {
            int d, p;
            cin >> d >> p;
            sect.push_back(make_pair(d, p));
        }

        while (m > 0) {
            int maxind = 0;
            // Search for sect where its dist is still above zero.
            while (maxind < n && sect[maxind].first == 0) maxind++;
            // If there's none, budget cannot be used anymore.
            if (maxind == n) break;
            int max = sect[maxind].second;
            // Search for max value of exp per dist, excluding its dist has already reached zero.
            for (int i = 0; i < n; i++) {
                if (sect[i].first != 0 && sect[i].second > max) {
                    max = sect[i].second;
                    maxind = i;
                }
            }
            // Decrease budget and maxind's dist. Dist reaches zero if whole of the section is paid from the budget.
            if (m - sect[maxind].first < 0) {   //If budget won't pay whole of the section, minus dist by budget and let budget be zero.
                sect[maxind].first -= m;
                m = 0;
            } else {    //If buget will pay whole of the section, minus budget by whole dist and let dist of the section be zero.
                m -= sect[maxind].first;
                sect[maxind].first = 0;
            }
        }
        //Calc dist * exp per dist. If dist is zero, whole of the section is paid from the budget. so exp is zero.
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans += sect[i].first * sect[i].second;
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 3500
final_rank: 20

code_snippet: #ifdef YONIHA
#include <yoniha/all.h>
using namespace atcoder;
#elifdef ATCODER
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
#else
#include <bits/stdc++.h>
#endif
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = (int)(n - 1); i >= 0; i--)
template <typename T> bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template <typename T> bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vs = vector<string>;
using pii = pair<int, int>;
/*
using mint = modint;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
*/

void solve(int n, int m){
  vector<pii> dp(n);
  for(auto&& [d, p] : dp) cin >> d >> p;
  sort(all(dp), [](pii x, pii y) {return x.second > y.second;});
  int ans = 0;
  for(auto [d, p] : dp){
    int use_money = min(m, d);
    m -= use_money;
    ans += (d - use_money) * p;
  }
  cout << ans << '\n';
}

signed main(){
  while(true){
    int n, m; cin >> n >> m;
    if(n == 0 && m == 0) return 0;
    solve(n, m);
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3620
final_rank: 18

code_snippet: // 問題：https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2019
//　判定：

//学習コメント：

#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
    int N,M,D,P,u,ans;
    cin >> N >> M;
    while(N!=0 or M!=0){
        ans = 0;
        vector<int>sumP(11,0);
        for(int i=0;i<N;i++){
            cin >> D >> P;
            sumP[P]+=D;
            ans += D * P;
        }
        u= 10;
        while(M > 0){
            ans -= min(M,sumP[u]) * u;
            M = M - sumP[u];
            u--;
            if(u == -1){
                ans = 0;
                break;
            }
        }
        cout << ans << endl;
        cin >> N >> M;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3052
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    vector<int> D(N), P(N);
    for (int i = 0; i < N; i++) {
      cin >> D[i] >> P[i];
    }
    vector<int> id(N, 0);
    iota(id.begin(), id.end(), 0);
    sort(id.begin(), id.end(), [&] (int a, int b) {
      if (P[a] == P[b]) {
        return D[a] < D[b];
      }
      return P[a] > P[b];
    });
    long long ans = 0;
    for (int i = 0; i < N; i++) {
      ans += (long long)D[i] * P[i];
    }
    for (int i = 0; i < N; i++) {
      if (M >= D[id[i]]) {
        ans -= P[id[i]] * D[id[i]];
        M -= D[id[i]];
      } else { 
        ans -= P[id[i]] * M;
        M = 0;
        break;
      }
    }
    cout << ans << '\n';
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3364
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>; using vs = vector<string>;
using pl = pair<long long, long long>;
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;
template <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}
template <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}
const ll INF = 1LL << 60;
const ld PI = acos(-1);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);
 
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
void solve();
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
  int num_tc = 1;
  //cin >> num_tc;
  rep(tc,num_tc){
    //cout << "Case #" << tc+1 << ": " ;// << endl;
    solve();
  }
}
//見切り発車で実装しては行けない．頭の中でコードが完全に出来上がるまで書き始めるな．
void solve(){
  while(true){
    int N,M;cin>>N>>M;
    if(N==0&&M==0)return;
    vector<pl> P(N);
    rep(i,N){
      ll a,b;cin>>a>>b;
      P[i] = {b,a};
    }
    sort(all(P),greater<pl>());
    ll ans =0;
    for(auto [p,d]:P){
      if(d <= M){
        M-=d;
      }else{
        d-=M;
        M = 0;
        ans+=d*p;
      }
    }
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3528
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 1001010;
const ll llINF = (long long)4e18 + 22000020;
const string endn = "\n";
template <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}
const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b){if (b < 0) a = -a, b = -b; if(a >= 0) return a / b; else return (a + 1) / b - 1;}
ll ceil(ll a, ll b){if (b < 0) a = -a, b = -b; if(a > 0) return (a - 1) / b + 1; else return a / b;}
ll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}
#ifdef DEBUG
#include <debug_slephy.cpp>
#else
#define debug(...)
#endif
// ================================== ここまでテンプレ ==================================

void solve(){
    ll n, m; cin >> n >> m;
    if(n == 0 && m == 0) exit(0);
    vector<pair<ll, ll>> dp(n);
    for(int i = 0; i < n; i++) cin >> dp[i].first >> dp[i].second;

    ll ans = 0;
    for(auto [d, p] : dp){
        ans += d * p;
    }

    sort(dp.begin(), dp.end(),
        [](const auto &p, const auto &q){
            const auto &[d1, p1] = p;
            const auto &[d2, p2] = q;
            if(p1 != p2) return p1 > p2;
            else return d1 < d2;
        }
    );

    ll remain = m;
    for(auto [d, p] : dp){
        ll g = min(remain, d);
        remain -= g;
        ans -= g * p;
    }
    cout << ans << endl;
}

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(true) solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3536
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
    while(true){
        ll N,M;
        cin >> N >> M;
        if(N==0&&M==0)break;
        vector<pair<int,int>>pair(N);
        for(ll i=0;i<N;i++){
            ll d,p;
            cin >> d >> p;
            pair[i].first=p;
            pair[i].second=d;
            // firstが金,secondが距離
        }
        sort(pair.rbegin(),pair.rend());
        int ans=0;
        for(int k=0;k<N;k++){
            if(M-pair[k].second>=0){
                M=M-pair[k].second;
            }else{
                ans+=(pair[k].second-M)*pair[k].first;
                M=0;
            }
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3440
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,k,n) for (int i = k; i < (n); ++i)
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<ll, ll> P;
const long long INF = 1e17;

int main(){
    while(true){
        int n,m;
        cin >> n >> m;
        if(n==0){
            break;
        }
        
        vector<int> d(n),p(n);
        vector<int> denger(11);
        for(int i=0;i<n;i++){
            cin >> d[i] >> p[i];
            denger[p[i]] += d[i];
        }

        for(int i=10;i>=1;i--){
            if(m-denger[i]>0){
                m -= denger[i];
                denger[i] = 0;
            }
            else{
                denger[i] -= m;
                m = 0;
            }
        }

        int sum = 0;
        int len = 0;
        for(int i=10;i>=1;i--){
            sum += denger[i] * i;
            len += denger[i];
        }

        cout << sum << endl;

    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3184
final_rank: 3

code_snippet: #include<bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

bool f(P A,P B){
    return A.second>B.second;
}

bool solve(){
    ll N,M;cin>>N>>M;
    if(N==0&&M==0) return 0;

    vp AB(N);rep(i,N) cin>>AB[i].first>>AB[i].second;
    ll ans=0;
    rep(i,N) ans+=AB[i].first*AB[i].second;
    sort(all(AB),f);
    rep(i,N){
        if(AB[i].first<=M) M-=AB[i].first,ans-=AB[i].first*AB[i].second;
        else ans-=M*AB[i].second,M=0;
    }

    cout << ans << endl;


    return 1;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(solve());
}
accuracy: 1
time_ms: 10
memory_kb: 3612
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;

ll n,m;

void solve() {
    vector<ll> v (11,0);
    int x,y;
    rep(i,n) {
        cin >> x >> y;
        v[y] += x;
    }
    ll ans = 0;
    rep(i,11) {
        if (v[10-i] <= m) {
            m -= v[10-i];
        }
        else {
            ans += (v[10-i]-m)*(10-i);
            m = 0;
        }
    }
    cout << ans << endl;
    return;
}

int main() {
    while (true) {
        cin >> n >> m;
        if (n == 0) {
            return 0;
        }
        solve();
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3348
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	pair<int,int> a[10000];
	int d,p;
	int ans=0,cn;
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++){
			cin>>d>>p;
			a[i]=make_pair(p,d);
			ans+=d*p;
		}
		sort(a,a+n);
		reverse(a,a+n);
		cn=0;
		while(m>0){
			if(m<a[cn].second){
				ans-=m*a[cn].first;
				m=0;
			}
			else{
				ans-=a[cn].first*a[cn].second;
				m-=a[cn].second;
			}
			cn++;
			if(cn==n)break;
		}
		cout<<ans<<endl;
		ans=0;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3212
final_rank: 4

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

ll solve(int N, ll M){
    vc<pair<ll, ll>> pd(N);
    rep(i, N) cin >> pd[i].second >> pd[i].first;
    sort(all(pd), [&](auto i, auto j){return i > j;});
    ll ans = 0;
    for (auto [p, d] : pd){
        ll tmp = min(d, M);
        d -= tmp;
        M -= tmp;
        ans += p * d;
    }
    return ans;
}

int main(){
    vc<ll> ans;
    while (true){
        int N; ll M; cin >> N >> M;
        if (N == 0) break;
        ans.push_back(solve(N, M));
    }
    for (auto x : ans) cout << x << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3220
final_rank: 5