alexjercan/bugnet huggingface dataset:
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    vector<pair<int, int>> pairs(N);
    long long int sum = 0;

    for (int i = 0; i < N; i++) {
      cin >> pairs[i].second >> pairs[i].first;
      sum += pairs[i].first * pairs[i].second;
    }

    sort(pairs.rbegin(), pairs.rend());

    int cur = 0;

    while (true) {
      if (M >= pairs[cur].second) {
        sum -= pairs[cur].first * pairs[cur].second;
        M -= pairs[cur].second;
        cur++;
      } else {
        sum -= M * pairs[cur].first;
        M = 0;
      }

      if (cur == N || M == 0) {
        break;
      }
    }

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    int sum = 0;
    int count = 0;
    vector<pair<int, int>> pairs(N);

    for (int i = 0; i < N; i++) {
      cin >> pairs[i].first >> pairs[i].second;
    }

    sort(pairs.rbegin(), pairs.rend());

    int i = 0;
    while (count < M) {
      pairs[i].first--;
      count++;

      if (pairs[i].first == 0) {
        i++;
      }
    }

    for (int i = 0; i < N; i++) {
      sum += pairs[i].first * pairs[i].second;
    }

    cout << sum << endl;
  }

  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#define MAX 10000
#define INF 100000000
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, m, d, p;
  while (cin >> n >> m, n || m) {
    priority_queue<P> que;
    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      que.push(P(p, d));
    }
    int ans = 0;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (m - p.second < 0) {
        ans += (p.second - m) * p.first;
        break;
      }
      m -= p.second;
    }
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ans += p.second * p.first;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 10000
#define INF 100000000
#include <vector>
using namespace std;
int n, m, d[MAX], p[MAX];
int func(int now, int ene, int mon) {
  /*if(dp[now][ene]!=-1)
  return dp[now][ene];
  */
  if (now == n)
    return ene;
  int ans = INF;
  for (int i = 0; i <= mon && i <= d[now]; i++)
    ans = min(ans, func(now + 1, ene + p[now] * (d[now] - i), mon - i));
  return /*dp[now][ene]=*/ans;
}
int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      cin >> d[i] >> p[i];
    cout << func(0, 0, m) << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    int d[n], p[n];
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
    }
    int pm = 0, nowp;
    while (m > 0) {
      for (int i = 0; i < n; i++) {
        pm = max(pm, p[i]);
        if (pm == p[i])
          nowp = i;
      }
      if (pm == 0)
        break;
      if (m >= d[nowp]) {
        m = m - d[nowp];
        d[nowp] = 0;
        p[nowp] = 0;
      } else {
        d[nowp] = d[nowp] - m;
        m = 0;
      }
      pm = 0;
    }
    int ptotal = 0;
    for (int i = 0; i < n; i++) {
      ptotal = ptotal + p[i] * d[i];
    }
    cout << ptotal << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    int d[n], p[n];
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
    }
    int pm = 0, nowp;
    while (m > 0) {
      for (int i = 0; i < n; i++) {
        pm = max(pm, p[i]);
        if (pm == p[i])
          nowp = i;
      }
      if (m >= d[nowp]) {
        m = m - d[nowp];
        d[nowp] = 0;
        p[nowp] = 0;
      } else {
        d[nowp] = d[nowp] - m;
        m = 0;
      }
      pm = 0;
    }
    int ptotal = 0;
    for (int i = 0; i < n; i++) {
      ptotal = ptotal + p[i] * d[i];
    }
    cout << ptotal << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

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

class Road {
public:
  int d, p;
  Road() {}
  Road(int d = 0, int p = 0) : d(d), p(p) {}
  bool operator<(const Road r) const { return p < r.p; }
};

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<Road> r;
    int d, p;
    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      Road tmp(d, p);
      r.push_back(tmp);
    }

    sort(r.begin(), r.end());
    int i = n - 1;
    for (int j = 0; j < m; j++) {
      r[i].d--;
      if (r[i].d == 0) {
        if (i > 0)
          i--;
        else
          break;
      }
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += r[j].d * r[j].p;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

class Road {
public:
  int d, p;
  Road() {}
  Road(int d = 0, int p = 0) : d(d), p(p) {}
  bool operator<(const Road r) const { return p < r.p; }
};

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<Road> r;
    int d, p;
    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      Road tmp(d, p);
      r.push_back(tmp);
    }

    sort(r.begin(), r.end());
    int i = n - 1;
    for (int j = 0; j < m; j++) {
      r[i].d--;
      if (r[i].d == 0)
        i--;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += r[j].d * r[j].p;
    }
    cout << sum << endl;
  }

  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

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

int N, M, d, p;
pair<int, int> prin[20000];
int main() {
  cin >> N >> M;
  while ((N != 0) || (M != 0)) {
    for (int i = 0; i < N; ++i) {
      cin >> d >> p;
      prin[i] = make_pair(-p, d);
    }
    sort(prin, prin + N);
    int money = prin[0].second;
    int k = 0;
    while (money <= M) {
      ++k;
      if (k > N - 1)
        break;
      money += prin[k].second;
    }
    if (money <= M)
      cout << 0 << endl;
    else {
      int ex = -prin[k].first * (money - M);
      for (int l = k + 1; l < N; ++l) {
        ex += -prin[l].first * prin[l].second;
      }
      cout << ex << endl;
    }
    cin >> N >> M;
  }
}

Error: 0

Fixed submission:

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

int N, M, d, p;
pair<int, int> prin[20000];
int main() {
  cin >> N >> M;
  while ((N != 0) || (M != 0)) {
    for (int i = 0; i < N; ++i) {
      cin >> d >> p;
      prin[i] = make_pair(-p, d);
    }
    sort(prin, prin + N);
    int money = prin[0].second;
    int k = 0;
    while (money <= M) {
      ++k;
      money += prin[k].second;
    }
    int ex = -prin[k].first * (money - M);
    for (int l = k + 1; l < N; ++l) {
      ex += -prin[l].first * prin[l].second;
    }
    cout << ex << endl;
    cin >> N >> M;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

void swap(int *x, int *y) {
  int *temp = x;
  x = y;
  y = temp;
}

void sort(int a[10005][2], int n) {
  rep(i, n) {
    rep(j, n) {
      if (a[j][1] < a[j + 1][1]) {
        swap(a[j][0], a[j + 1][0]);
        swap(a[j][1], a[j + 1][1]);
      }
    }
  }
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int info[10005][2] = {0};
    rep(i, n) { cin >> info[i][0] >> info[i][1]; }
    sort(info, n);
    for (int i = 0; m && i - 1 != n; i++) {
      if (info[i][1] == 0)
        continue;
      if (m >= info[i][0]) {
        m -= info[i][0];
        info[i][0] = 0;
      } else {
        info[i][0] -= m;
        m = 0;
      }
    }
    long long sum = 0;
    rep(i, n) { sum += (info[i][0] * info[i][1]); }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> vi;
    rep(i, n) {
      int d, p;
      cin >> d >> p;
      rep(i, d) { vi.pb(p); }
      sort(all(vi));
    }
    while (m--) {
      vi.pop_back();
    }
    int sum = 0;
    for (vector<int>::iterator it = vi.begin(); it != vi.end(); it++) {
      sum += *it;
    }
    cout << sum << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
int n, m;
pair<int, int> pii[10004];

void solve() {
  int d, p;
  rep(i, n) {
    cin >> d >> p;
    pii[i] = make_pair(-p, d);
  }
  sort(pii, pii + n);
  int ans = 0;

  rep(i, n) {
    int p2 = -pii[i].first;
    int dist = pii[i].second;
    // cout<<(-pii[i].first)<<","<<dist<<","<<pii[i].second<<endl;
    if (m - dist < 0) {
      ans += (dist - m) * p2;
      m = 0;
    } else {
      m -= dist;
    }
  }
  cout << ans << endl;
  rep(i, n) { pii[i] = make_pair(0, 0); }
}

int main() {
  while (cin >> n >> m) {
    if (n + m == 0)
      return 0;
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int n, m;
pair<int, int> pii[10004];

void solve() {
  int d, p;
  rep(i, n) {
    cin >> d >> p;
    pii[i] = make_pair(-p * d, p);
  }
  sort(pii, pii + n);
  int ans = 0;

  rep(i, n) {
    int dist = -pii[i].first / pii[i].second;
    // cout<<(-pii[i].first)<<","<<dist<<","<<pii[i].second<<endl;
    if (m - dist < 0) {
      ans += (dist - m) * pii[i].second;
      m = 0;
    } else {
      m -= dist;
    }
  }
  cout << ans << endl;
  rep(i, n) { pii[i] = make_pair(0, 0); }
}

int main() {
  while (cin >> n >> m) {
    if (n + m == 0)
      return 0;
    solve();
  }

  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

#define INF 999999999

int main() {
  int ans, i, j, k, n, m, d, p, dis;
  int at[11];
  while (1) {
    cin >> n >> m;
    if (!n)
      break;
    for (k = 0; k <= 10; k++)
      at[k] = 0;
    ans = 0;
    dis = 0;
    for (i = 0; i < n; i++) {
      cin >> d >> p;
      at[p] += d;
      dis += d;
    }
    dis -= m;
    for (j = 0; j <= 10 && dis > 0; j++) {
      if (at[j] <= dis) {
        dis -= at[j];
        ans += at[j] * j;
      } else {
        ans += dis * j;
        dis = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

#define INF 999999999

int main() {
  long long int ans, i, j, k, n, m, d, p, dis;
  vector<pair<int, int>> mat(10000);
  while (1) {
    cin >> n >> m;
    if (!n)
      break;
    mat.clear();
    dis = 0;
    ans = 0;
    for (i = 0; i < n; i++) {
      cin >> d >> p;
      mat[i] = make_pair(p, d);
      dis += d;
    }
    sort(mat.begin(), mat.begin() + n);
    dis -= m;
    if (dis <= 0) {
      cout << 0 << endl;
      continue;
    }
    int now = 0;
    while (1) {
      // cout << dis << " " << ans << " " << mat[now].second << " " <<
      // mat[now].first << endl;
      if (mat[now].second <= dis) {
        dis -= mat[now].second;
        ans += mat[now].second * mat[now].first;
        now++;
      } else {
        ans += dis * mat[now].first;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>

bool greaterfirst(std::pair<int, int> i, std::pair<int, int> j) {
  return (i.second > j.second);
}

int main() {
  while (1) {
    // 入力
    int N, M;
    std::cin >> N >> M;
    if (N == 0 && M == 0)
      return 0;
    int D, P;
    std::pair<int, int> DP[10000];
    for (int i = 0; i < N; i++) {
      std::cin >> D >> P;
      DP[i] = std::make_pair(D, P);
    }
    // 演算
    std::sort(DP, DP + N, greaterfirst);
    int answer = 0;
    for (int i = 0; i < N; i++) {
      answer += DP[i].second * DP[i].first;
    }
    int remains = M;
    for (int i = 0; remains > 0 && i < N; i++) {
      if (remains >= DP[i].first) {
        answer -= DP[i].second * DP[i].first;
      } else {
        answer -= DP[i].second * remains;
      }
      remains -= DP[i].first;
    }
    // 出力
    std::cout << answer << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>

int main() {
  int flag = true;
  while (flag) {
    // 入力
    int N, M;
    std::cin >> N >> M;
    int D, P;
    std::pair<int, int> PD[10000];
    for (int i = 0; i < N; i++) {
      std::cin >> D >> P;
      PD[i] = std::make_pair(P, D);
    }
    // 演算
    std::sort(PD, PD + N);
    int answer = 0;
    for (int i = 0; i < N; i++) {
      answer += PD[i].first * PD[i].second;
    }
    int remains = M;
    for (int i = 0; remains > 0; i++) {
      if (remains >= PD[i].second) {
        answer -= PD[i].first * PD[i].second;
      } else {
        answer -= PD[i].first * remains;
      }
      remains -= PD[i].second;
    }
    // 出力
    std::cout << answer << std::endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
ll N, M, sum, ans;
vector<P> V;

void init() {
  V.clear();
  sum = ans = 0;
}

int main() {

  while (cin >> N >> M && N + M) {
    init();

    for (int i = 0; i < N; i++) {
      P p;
      cin >> p.second >> p.first;
      V.push_back(p);
      sum += p.second;
    }

    sort(V.begin(), V.end());

    sum = max(0LL, sum - M);

    for (int i = 0; i < V.size(); i++) {
      if (V[i].second > sum) {
        ans += sum * V[i].first;
        break;
      } else {
        ans += V[i].first * V[i].second;
        sum -= V[i].second;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef unsigned long long ll;
typedef pair<ll, ll> P;
ll N, M, sum, ans;
vector<P> V;

void init() {
  V.clear();
  sum = ans = 0;
}

int main() {

  while (cin >> N >> M && N + M) {
    init();

    for (int i = 0; i < N; i++) {
      P p;
      cin >> p.second >> p.first;
      V.push_back(p);
      sum += p.second;
    }

    sort(V.begin(), V.end());

    sum -= M;

    for (int i = 0; V.size(); i++) {
      if (V[i].second > sum) {
        ans += sum * V[i].first;
        break;
      } else {
        ans += V[i].first * V[i].second;
        sum -= V[i].second;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

int main(void) {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vec<int> x(11, 0);
    rep(i, n) {
      int p, q;
      cin >> p >> q;
      x[q] += p;
    }
    int ok = 0;
    int ans = 0;
    for (int i = 10; i >= 0; i--) {
      if (ok) {
        ans += x[i] * i;
        continue;
      }
      m -= x[i];
      if (m < 0) {
        ans += -m * i;
        ok = 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

int main(void) {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vec<int> d(n), p(n);
    int sum = 0;
    rep(i, n) {
      cin >> d[i] >> p[i];
      sum += d[i];
    }
    vec<int> x(sum);
    int dis = 0;
    rep(i, n) {
      rep(j, d[i]) x[dis + j] = p[i];
      dis += d[i];
    }
    sort(x.begin(), x.end());

    int ans = 0;
    rep(i, sum - m) { ans += x[i]; }

    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<pair<int, int>> v;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      ans += d * p;
      v.push_back(make_pair(p, d));
    }
    sort(v.begin(), v.end());
    int i = v.size() - 1;
    while (m > 0) {
      ans -= min(m, v[i].second) * v[i].first;
      m -= min(m, v[i].second);
      i--;
      if (i < 0)
        break;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<pair<int, int>> v;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      ans += d * p;
      v.push_back(make_pair(p, d));
    }
    sort(v.begin(), v.end());
    int i = v.size() - 1;
    while (m > 0) {
      ans -= min(m, v[i].second) * v[i].first;
      m -= min(m, v[i].second);
      i--;
    }
    cout << ans << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vector<pii> res;
    int ans = 0;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      ans += a * b;
      res.push_back(mp(b, a));
    }
    sort(all(res), greater<pii>());
    rep(i, res.size()) {
      if (m - res[i].second > 0) {
        ans -= res[i].first * res[i].second;
        m -= res[i].second;
      } else {
        ans -= m * res[i].first;
        break;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define mp make_pair
signed main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vector<pii> rec;
    int al = 0;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      al += a * b;
      rec.push_back(mp(a, b));
    }
    sort(all(rec), greater<pii>());
    int memo = m;
    int cnt = 0;
    for (;;) {
      if (memo == 0)
        break;
      if (memo - rec[cnt].first >= 0) {
        al -= rec[cnt].first * rec[cnt].second;
        memo -= rec[cnt].first;
        cnt++;
      } else {
        al -= memo * rec[cnt].second;
        break;
      }
    }
    cout << al << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

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

typedef long long ll;
using namespace std;

bool paircmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, ans = 0;
    cin >> n >> m;
    if (!n)
      break;
    vector<pair<int, int>> v(n);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[i] = {a, b};
    }
    sort(ALL(v), paircmp);
    REP(i, n) {
      int a = min(m, v[i].first);
      m -= a;
      v[i].first -= a;
      ans += v[i].first * v[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

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

typedef long long ll;
using namespace std;

bool paircmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, ans = 0;
    cin >> n >> m;

    vector<pair<int, int>> v(n);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[i] = {a, b};
    }
    sort(ALL(v), paircmp);
    REP(i, n) {
      int a = min(m, v[i].first);
      m -= a;
      v[i].first -= a;
      ans += v[i].first * v[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}