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

#include <iostream>
#include <string>
using namespace std;
int d[10000], p[10000];
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int load = 0;
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
      load += d[i];
    }
    if (load <= m)
      cout << "0" << endl;
    else {
      int count = 0;
      for (int i = 0; i < n - 1; i++) {
        for (int j = n - 1; j > i; j--) {
          if (p[j] < p[j - 1]) {
            int tmp = p[j];
            p[j] = p[j - 1];
            p[j - 1] = tmp;
            tmp = d[j];
            d[j] = d[j - 1];
            d[j - 1] = tmp;
          }
        }
      }
      int i = 0;
      m = load - m;
      while (true) {
        if (m <= d[i]) {
          count += m * p[i];
          break;
        }
        count += d[i] * p[i];
        m -= d[i];
        i++;
      }
      cout << count << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int d[10001], p[10001];
int main() {
  long long n, m, count, answer;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    count = 0;
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
      count += d[i];
    }
    count -= m;
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (p[j] < p[j - 1]) {
          int tmp = p[j];
          p[j] = p[j - 1];
          p[j - 1] = tmp;
          tmp = d[j];
          d[j] = d[j - 1];
          d[j - 1] = tmp;
        }
      }
    }
    answer = 0;
    int i = 0;
    while (true) {
      if (d[i] <= count) {
        answer += p[i] * d[i];
        count -= d[i];
      } else {
        answer += p[i] * count;
        break;
      }
      i++;
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int table[11] = {0};
    int d, p;
    int max = 0;
    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      table[p] += d;
      if (max < p)
        max = p;
    }
    for (int i = max; i >= 0; i--) {
      if (table[i] <= m) {
        m -= table[i];
        table[i] = 0;
      } else {
        table[i] -= m;
        m = 0;
        break;
      }
    }
    long long int sum = 0;
    for (int i = 0; i <= max; i++) {
      sum += i * table[i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

class Data {
public:
  int dist;
  int pos;
  int cnt;
  Data() {}
  Data(int d, int p) : dist(d), pos(p), cnt(d * p) {}
};

bool operator>(const Data &d1, const Data &d2) {
  return (d1.cnt == d2.cnt) ? (d1.pos > d2.pos) : (d1.cnt > d2.cnt);
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<Data> vec(n);
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      vec[i] = Data(d, p);
    }
    sort(vec.begin(), vec.end(), greater<Data>());
    for (int i = 0; i < m; i++) {
      vec[0].cnt -= vec[0].pos;
      if (vec[0].cnt < 0)
        vec[0].cnt = 0;
      for (int j = 1; j < n; j++) {
        if (vec[j] > vec[j - 1])
          swap(vec[j], vec[j - 1]);
      }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += vec[i].cnt;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n, m;
P ps[11111];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    rep(i, n) cin >> ps[i].se >> ps[i].fi;
    sort(ps, ps + n, greater<P>());
    int res = 0;
    rep(i, n) {
      if (m == 0)
        res += ps[i].fi * ps[i].se;
      else if (m > ps[i].se) {
        m -= ps[i].se;
      } else {
        res += (ps[i].se - m) * ps[i].fi;
        m = 0;
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n, m;
P ps[11111];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    rep(i, n) cin >> ps[i].se >> ps[i].fi;
    sort(ps, ps + n, greater<P>());
    int res = 0, i = 0;
    while (1) {
      m -= ps[i].se;
      if (m <= 0) {
        res += -m * ps[i].fi;
        break;
      }
      i++;
    }
    repl(j, i + 1, n) { res += ps[i].fi * ps[i].se; }
    cout << res << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> P;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

const int MAX = 200000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const int MOD = 1000000007;

int N, d, p;
ll M, ans = 0LL;

void solve(void) {
  while (1) {
    scanf("%d %lld\\n", &N, &M);
    if (N == 0 && M == 0)
      return;
    ans = 0LL;
    vector<P> v(N);
    rep(N, i) {
      scanf("%d %d\\n", &d, &p);
      v.pb(P(p, d));
      ans += p * d;
    }
    sort(all(v));
    reverse(all(v));
    for (P pd : v) {
      if (pd.second > M) {
        ans -= pd.first * M;
        break;
      }
      ans -= pd.first * pd.second;
      M -= pd.second;
    }
    printf("%lld\\n", ans);
  }
}

int main(void) {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> P;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

const int MAX = 200000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const int MOD = 1000000007;

int N, d, p;
ll M, ans = 0LL;

void solve(void) {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      return;
    ans = 0LL;
    vector<P> v(N);
    rep(N, i) {
      scanf("%d %d\\n", &d, &p);
      v.pb(P(p, d));
      ans += p * d;
    }
    sort(all(v));
    reverse(all(v));
    for (P pd : v) {
      if (pd.second > M) {
        ans -= pd.first * M;
        break;
      }
      ans -= pd.first * pd.second;
      M -= pd.second;
    }
    cout << ans << endl;
  }
}

int main(void) {
  solve();
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

int ways[11];

int main() {
loop:
  int n, m;
  for (int i = 0; i < 11; i++)
    ways[i] = 0;
  cin >> n >> m;
  if (!n)
    return 0;
  for (int i = 0; i < n; i++) {
    int d, p;
    cin >> d >> p;
    ways[p] += d;
  }
  for (int i = 10; i > 0; i--) {
    if (ways[i] < m) {
      m -= ways[i];
      ways[i] = 0;
    } else {
      ways[i] -= m;
      m = 0;
    }
  }
  int ans = 0;
  for (int i = 1; i <= 10; i++)
    ans += ways[i] * i;
  cout << ans << endl;
  goto loop;
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

int ways[11];

int main() {
loop:
  int n, m;
  for (int i = 0; i < 11; i++)
    ways[i] = 0;
  cin >> n >> m;
  if (!n)
    return 0;
  for (int i = 0; i < n; i++) {
    int d, p;
    cin >> d >> p;
    ways[p] += d;
  }
  while (m > 0) {
    for (int i = 10; i > 0; i--) {
      if (ways[i] < m) {
        m -= ways[i];
        ways[i] = 0;
      } else {
        ways[i] -= m;
        m = 0;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= 10; i++)
    ans += ways[i] * i;
  cout << ans << endl;
  goto loop;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  long long n, m;
  while (cin >> n >> m && n > 0) {

    vector<pair<int, int>> pd(n);
    int d, p;

    REP(i, n) {
      cin >> d;
      cin >> p;
      pd[i] = make_pair(p, d);
    }

    /* 各区間における襲われる期待値を昇順 */
    sort(pd.begin(), pd.end());

    /* 総合距離から守ることができ'ない'距離 guard を算出 */
    long long d_sum = 0;
    REP(i, n) d_sum += pd[i].second;
    long long guard = d_sum - m;

    /* guard分の期待値 p_sum を算出　*/
    long long p_sum = 0;

    for (int i = 0; i < n; i++) {
      if (guard <= 0)
        break;

      if (guard >= pd[i].second) {
        guard -= pd[i].second;
        p_sum += (pd[i].first * pd[i].second);
      }

      else if (m > 0) {
        p_sum += (pd[i].first * guard);
        guard = 0;
      }
    }

    cout << p_sum << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  long long n, m;
  while (cin >> n >> m && n > 0) {

    vector<pair<int, int>> pd(n);
    int d, p;

    REP(i, n) {
      cin >> d;
      cin >> p;
      pd[i] = make_pair(p, d);
    }

    /* 各区間における襲われる期待値を昇順 */
    sort(pd.begin(), pd.end());

    /* 総合距離から守ることができ'ない'距離 guard を算出 */
    long long d_sum = 0;
    REP(i, n) d_sum += pd[i].second;
    long long guard = d_sum - m;

    /* guard分の期待値 p_sum を算出　*/
    long long p_sum = 0;

    for (int i = 0; m > 0 || i < n; i++) {
      if (m >= pd[i].second) {
        m -= pd[i].second;
        p_sum += (pd[i].first * pd[i].second);
      }

      else if (m > 0) {
        p_sum += (pd[i].first * m);
        m = 0;
      }
    }

    cout << p_sum << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (1) {

    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    pair<int, int> a[10000];
    unsigned long long int count = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
      swap(a[i].first, a[i].second);
      count += a[i].first * a[i].second;
    }
    sort(a, a + n);
    reverse(a, a + n);
    for (int i = 0; i < n; i++) {
      m -= a[i].second;
      count -= a[i].first * a[i].second;
      if (m <= 0) {
        count -= m * a[i].first;
        break;
      }
    }

    cout << count << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (1) {

    unsigned int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    pair<int, int> a[10000];
    unsigned long long int count = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
      swap(a[i].first, a[i].second);
      count += a[i].first * a[i].second;
    }
    sort(a, a + n);
    reverse(a, a + n);
    for (int i = 0; i != -1; i++) {
      m -= a[i].second;
      count -= a[i].second * a[i].first;
      if (m <= 0) {
        count -= m * a[i].first;
      }
    }
    cout << count << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

P h[100000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    int ans = 0;
    rep(i, n) {
      int d, p;
      scanf("%d%d", &d, &p);
      ans += d * p;
      h[i] = P(p, d);
    }
    sort(h, h + n, greater<>());
    int sum = m;
    int i;
    for (i = 0; i < n && sum - h[i].second >= 0; i++) {
      sum -= h[i].second;
      ans -= h[i].first * h[i].second;
    }
    ans -= h[i].first * sum;
    printf("%d\\n", max(0, ans));
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

P h[100000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    int ans = 0;
    rep(i, n) {
      int d, p;
      scanf("%d%d", &d, &p);
      ans += d * p;
      h[i] = P(p, d);
    }
    sort(h, h + n, greater<>());
    int sum = m;
    int i;
    for (i = 0; sum - h[i].second >= 0; i++) {
      sum -= h[i].second;
      ans -= h[i].first * h[i].second;
    }
    ans -= h[i].first * sum;
    printf("%d\\n", ans);
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 1010001000
#define EPS 1e-10
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef pair<int, int> i_i;
typedef long long int llong;
typedef pair<llong, llong> ll_ll;
typedef struct edge {
  int f, t;
} Edge;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    priority_queue<i_i> pq;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      pq.push(i_i(b, a));
    }
    int ans = 0;
    while (m && !pq.empty()) {
      i_i tmp = pq.top();
      pq.pop();
      if (m >= tmp.second) {
        m -= tmp.second;
      } else {
        ans += tmp.first * (tmp.second - m);
        m = 0;
      }
    }
    while (!pq.empty()) {
      ans += pq.top().first * pq.top().second;
      pq.pop();
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 1010001000
#define EPS 1e-10
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef pair<int, int> i_i;
typedef long long int llong;
typedef pair<llong, llong> ll_ll;
typedef struct edge {
  int f, t;
} Edge;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    priority_queue<int> pq;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      rep(j, a) { pq.push(b); }
    }
    rep(i, m) { pq.pop(); }
    int ans = 0;
    while (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

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

long long N, M, sum, t;
pair<long long, long long> p[30000];
int main() {
  while ((cin >> N >> M), N) {
    sum = 0;
    for (int i = 0; i < N; i++) {
      cin >> p[i].second >> p[i].first;
      sum += p[i].second * p[i].first;
    }
    sort(p, p + N);
    reverse(p, p + N);
    for (int i = 0; i < N; i++) {
      if (M <= 0)
        break;
      int guarded = min(p[i].second, M);
      sum -= p[i].first * guarded;
      M -= guarded;
    }
    cout << sum << endl;
  }
}

Error: -11

Fixed submission:

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

long long N, M, sum, t;
pair<long long, long long> p[30000];
int main() {
  while ((cin >> N >> M), N) {
    sum = 0;
    for (int i = 0; i < N; i++) {
      cin >> p[i].second >> p[i].first;
      sum += p[i].second * p[i].first;
    }
    sort(p, p + N);
    reverse(p, p + N);
    for (t = 0, (M -= p[t].second); M >= 0 || sum == 0; t++) {
      sum -= p[t].first * p[t].second;
    }
    sum -= (M + p[t].second) * p[t].first;
    cout << sum << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  for (int tci = 0;; tci++) {
    int N, M;
    scanf("%d%d", &N, &M);
    if (!N)
      break;
    pair<int, int> *pd = new pair<int, int>[N];
    int ept = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &pd[i].second, &pd[i].first);
      ept += pd[i].second * pd[i].first;
    }
    sort(pd, pd + N);
    reverse(pd, pd + N);
    int x = M;
    for (int i = 0; i < N && x > 0; i++) {
      ept -= min(pd[i].second, x) * pd[i].first;
      x -= pd[i].second;
    }
    printf("%d\\n", ept);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  for (int tci = 0;; tci++) {
    int N, M;
    scanf("%d%d", &N, &M);
    if (!N)
      break;
    pair<int, int> *pd = new pair<int, int>[N];
    int ept = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &pd[i].second, &pd[i].first);
      ept += pd[i].second * pd[i].first;
    }
    sort(pd, pd + N);
    reverse(pd, pd + N);
    int x = M;
    for (int i = 0; i < M && x > 0; i++) {
      ept -= min(pd[i].second, x) * pd[i].first;
      x -= pd[i].second;
    }
    printf("%d\\n", ept);
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n;
  long long int m;
  while (cin >> n >> m && n) {
    // vector<int> list;
    int a[11] = {};
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      a[p] += d;
    }
    int result = 0;
    for (int i = 10; i > 0 && m; i--) {
      while (m) {
        if (a[i] > 0) {
          a[i]--;
          m--;
        } else {
          break;
        }
        // cout << m << endl;
      }
    }
    /*for( int i = 0; i < 11; i++ ){
            cout << a[i] << endl;
    }*/
    for (int i = 1; i <= 10; i++) {
      result += a[i] * i;
    }
    /*
    sort( list.begin(), list.end() );
    reverse( list.begin(), list.end() );
    vector<int>::iterator it = list.begin();
    while( m-- > 0 ){
            it++;
    }
    for( ; it != list.end(); it++ ){
            result += *it;
    }*/
    cout << result << endl;
    // list.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n;
  long long int m;
  while (cin >> n >> m && n) {
    // vector<int> list;
    int a[11] = {};
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      a[p] += d;
    }
    int result = 0;
    for (int i = 10; i > 0 && m; i--) {
      while (m) {
        if (a[i] > 0) {
          a[i]--;
          m--;
        } else {
          break;
        }
        cout << m << endl;
      }
    }
    for (int i = 0; i < 11; i++) {
      cout << a[i] << endl;
    }
    for (int i = 1; i <= 10; i++) {
      result += a[i] * i;
    } /*
     sort( list.begin(), list.end() );
     reverse( list.begin(), list.end() );
     vector<int>::iterator it = list.begin();
     while( m-- > 0 ){
             it++;
     }
     for( ; it != list.end(); it++ ){
             result += *it;
     }*/
    cout << result << endl;
    // list.clear();
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, d, p, ans;

  while (cin >> n >> m, n || m) {
    ans = 0;
    map<int, int> f;
    for (int i = 0; i <= 10; i++) {
      f[i] += 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      f[p] += d;
    }
    for (int i = 10; i >= 0; i--) {
      for (; f[i] && m;) {
        m--;
        f[i] -= 1;
      }
      ans += f[i] * i;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, d, p, ans;

  while (cin >> n >> m, n || m) {
    ans = 0;
    vector<int> f;

    for (int i = 0; i < n; i++) {
      cin >> d >> p;
      for (; d--;) {
        f.push_back(p);
      }
    }
    sort(f.begin(), f.end());
    // cout << f.size() << endl;
    for (int i = 0; i + m < (int)f.size(); i++) {
      // cout << "f[i] : " << f[i] << endl;
      ans += f[i];
    }
    cout << ans << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int d[11], n, m, i, a, b, sum, e;
int main() {
  while (true) {
    memset(d, 0, sizeof(d));
    sum = 0;
    cin >> n >> m;
    if (!n) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      d[b] += a;
    }
    for (i = 10; i >= 0; i--) {
      e = min(d[i], m);
      m -= e;
      sum += i * (d[i] - e);
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int d[11], n, m, i, a, b, sum, e;
int main() {
  while (true) {
    memset(d, 0, sizeof(d));
    sum = 0;
    cin >> n >> m;
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      d[b] += a;
    }
    for (i = 10; i >= 0; i--) {
      e = min(d[i], m);
      m -= e;
      sum += i * (d[i] - e);
    }
    cout << sum << endl;
  }
}