prithivMLmods/Coder-Stat huggingface dataset:
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int h[1024 * 1500];
int w[1024 * 1500];

int getInt() {
  int c = getchar();
  int ret = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    int hh[n];
    int ww[m];
    int mm = 0;

    for (int i = 0; i < n; i++)
      hh[i] = getInt();
    for (int i = 0; i < m; i++)
      ww[i] = getInt();

    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += hh[j];
        h[sum]++;
      }
      mm = max(mm, sum);
    }
    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += ww[j];
        if (sum > mm)
          break;
        w[sum]++;
      }
    }
    long long ans = 0;
    for (int i = 0; i <= mm; i++) {
      if (w[i] | h[i]) {
        ans += (long long)w[i] * h[i];
        w[i] = h[i] = 0;
      }
    }

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

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int h[1024 * 1500];
int w[1024 * 1500];

int getInt() {
  int c = getchar();
  int ret = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    int hh[n];
    int ww[m];
    int mm = 0;

    for (int i = 0; i < n; i++)
      hh[i] = getInt();
    for (int i = 0; i < m; i++)
      ww[i] = getInt();

    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += hh[j];
        h[sum]++;
      }
      mm = max(mm, sum);
    }
    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += ww[j];
        if (sum > mm)
          break;
        w[sum]++;
      }
    }
    long long ans = 0;
    for (int i = 0; i <= mm; i++) {
      if (w[i] | h[i]) {
        ans += (long long)w[i] * h[i];
        w[i] = h[i] = 0;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;
  vector<int> h(n + 1), w(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    h[i] += h[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> w[i];
    w[i] += w[i - 1];
  }

  vector<int> hei(2001);
  for (int i = 0; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      hei[h[j] - h[i]]++;
    }
  }

  int cnt = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      cnt += hei[w[j] - w[i]];
    }
  }
  cout << cnt << endl;
  return true;
}

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

//????????????

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;
  vector<int> h(n + 1), w(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    h[i] += h[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> w[i];
    w[i] += w[i - 1];
  }

  vector<int> hei(1500001);
  for (int i = 0; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      hei[h[j] - h[i]]++;
    }
  }

  int cnt = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      cnt += hei[w[j] - w[i]];
    }
  }
  cout << cnt << endl;
  return true;
}

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

//????????????
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m) {
  int h[2000], w[2000];

  rep(i, n) { cin >> h[i]; }
  rep(i, m) { cin >> w[i]; }

  map<int, int> a, b;

  rep(i, n) {
    int sum = 0;
    FOR(j, i, n) {
      sum += h[j];
      a[sum]++;
    }
  }
  rep(i, m) {
    int sum = 0;
    FOR(j, i, m) {
      sum += w[j];
      b[sum]++;
    }
  }

  int ans = 0;

  rep(i, 1e6) { ans += a[i] * b[i]; }

  cout << ans << endl;
}

int main() {
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;

    search(n, m);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void search(int n, int m) {
  int h[2000], w[2000];

  rep(i, n) { cin >> h[i]; }
  rep(i, m) { cin >> w[i]; }

  map<int, int> a, b;

  rep(i, n) {
    int sum = 0;
    FOR(j, i, n) {
      sum += h[j];
      a[sum]++;
    }
  }
  rep(i, m) {
    int sum = 0;
    FOR(j, i, m) {
      sum += w[j];
      b[sum]++;
    }
  }

  int ans = 0;

  for (auto itr = a.begin(); itr != a.end(); itr++) {
    ans += itr->second * b[itr->first];
  }

  cout << ans << endl;
}

int main() {
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;

    search(n, m);
  }

  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1001], w[1001];
    vector<int> hh(1500001, 0), ww(1500001, 0);

    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

  return 0;
}

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

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1501], w[1501];
    vector<int> hh(1500001, 0), ww(1500001, 0);

    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N + 1), w(M + 1);
    h[0] = 0, w[0] = 0;
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];
    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];
    vector<int> hs(h.back() + 1, 0), ws(w.back() + 1, 0);
    rep(i, N) REP(j, i + 1, N + 1) hs[h[j] - h[i]]++;
    rep(i, M) REP(j, i + 1, M + 1) ws[w[j] - w[i]]++;
    int ans = 0;
    rep(i, max(h.back(), w.back()) + 1) ans += hs[i] * ws[i];
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N + 1), w(M + 1);
    h[0] = 0, w[0] = 0;
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];
    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];
    vector<int> hs(h.back() + 1, 0), ws(w.back() + 1, 0);
    rep(i, N) REP(j, i + 1, N + 1) hs[h[j] - h[i]]++;
    rep(i, M) REP(j, i + 1, M + 1) ws[w[j] - w[i]]++;
    int ans = 0;
    rep(i, min(h.back(), w.back()) + 1) ans += hs[i] * ws[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int combi(int n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> x, y;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      h += y[i];
      y.push_back(h);
    }

    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      w += x[i];
      y.push_back(w);
    }
    // y=x+b → y-x=bとなる
    map<int, int> cnt;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        int diff = y[i] - x[j];
        cnt[diff]++;
      }
    }

    // 全探索
    int ans = 0;
    for (int i = -x[m]; i < y[n] + 1; i++) {
      ans += combi(cnt[i]);
    }

    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int combi(int n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> x, y;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      h += y[i];
      y.push_back(h);
    }

    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      w += x[i];
      x.push_back(w);
    }
    // y=x+b → y-x=bとなる
    map<int, int> cnt;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        int diff = y[i] - x[j];
        cnt[diff]++;
      }
    }

    // 全探索
    int ans = 0;
    for (int i = -x[m]; i < y[n] + 1; i++) {
      ans += combi(cnt[i]);
    }

    cout << ans << endl;
  }
}
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    rep(i, n + 1) rep(j, m + 1) rep(k, i) rep(l, j) {
      if (s1[i] - s1[k] == s2[j] - s2[l])
        cnt++;
    }
    printf("%d\n", cnt);
  }
}

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

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    map<int, int> mp1, mp2;
    rep(i, n + 1) rep(j, i) mp1[s1[i] - s1[j]]++;
    rep(i, m + 1) rep(j, i) mp2[s2[i] - s2[j]]++;
    for (auto p : mp1) {
      cnt += p.second * mp2[p.first];
    }
    printf("%d\n", cnt);
  }
}
Problem p01140 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <map>
#include <stdio.h>
#include <vector>

auto solve(int N, int M) -> int {
  using namespace std;
  vector<int> h(N);
  vector<int> w(M);

  for (int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    h[i] = x;
  }
  for (int i = 0; i < M; i++) {
    int x;
    scanf("%d", &x);
    w[i] = x;
  }

  map<int, int> &eh = *(new map<int, int>());
  map<int, int> &ew = *(new map<int, int>());
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i; j < N; j++) {
      sum += h[j];
      if (eh.find(sum) != ew.end())
        eh[sum]++;
      else
        eh[sum] = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i; j < M; j++) {
      sum += w[j];
      if (ew.find(sum) != ew.end())
        ew[sum]++;
      else
        ew[sum] = 1;
    }
  }

  int ans = 0;
  for (auto p : eh) {
    if (ew.find(p.first) != ew.end())
      ans += p.second * ew[p.first];
  }

  return ans;
}

auto main() -> int {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;
    printf("%d\n", solve(n, m));
  }
}

Fixed submission:
#include <map>
#include <stdio.h>
#include <vector>

auto solve(int N, int M) -> int {
  using namespace std;
  vector<int> h(N);
  vector<int> w(M);

  for (int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    h[i] = x;
  }
  for (int i = 0; i < M; i++) {
    int x;
    scanf("%d", &x);
    w[i] = x;
  }

  map<int, int> eh;
  map<int, int> ew;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i; j < N; j++) {
      sum += h[j];
      if (eh.find(sum) != ew.end())
        eh[sum]++;
      else
        eh[sum] = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i; j < M; j++) {
      sum += w[j];
      if (ew.find(sum) != ew.end())
        ew[sum]++;
      else
        ew[sum] = 1;
    }
  }

  int ans = 0;
  for (auto p : eh) {
    if (ew.find(p.first) != ew.end())
      ans += p.second * ew[p.first];
  }

  return ans;
}

auto main() -> int {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;
    printf("%d\n", solve(n, m));
  }
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
int m;

int hmp[1500 * 1000 + 1];
int wmp[1500 * 1000 + 1];

int main() {
  while (cin >> n >> m, n) {
    fill(hmp, hmp + 1 + 1500 * 1000, 0);
    fill(wmp, wmp + 1 + 1500 * 1000, 0);

    vector<int> h(n + 1, 0), w(n + 1, 0);

    for (int i = 1; i <= n; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    for (int i = 1; i <= m; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
    }

    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        int len = h[j] - h[i];
        hmp[len]++;
      }
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        int len = w[j] - w[i];
        wmp[len]++;
      }
    }

    i64 result = 0;

    for (int i = 1; i <= 1500 * 1000; i++) {
      result += hmp[i] * wmp[i];
    }
    cout << result << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
int m;

int hmp[1500 * 1000 + 1];
int wmp[1500 * 1000 + 1];

int main() {
  while (cin >> n >> m, n) {
    fill(hmp, hmp + 1 + 1500 * 1000, 0);
    fill(wmp, wmp + 1 + 1500 * 1000, 0);

    vector<int> h(n + 1, 0), w(m + 1, 0);

    for (int i = 1; i <= n; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    for (int i = 1; i <= m; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
    }

    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        int len = h[j] - h[i];
        hmp[len]++;
      }
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        int len = w[j] - w[i];
        wmp[len]++;
      }
    }

    i64 result = 0;

    for (int i = 1; i <= 1500 * 1000; i++) {
      result += hmp[i] * wmp[i];
    }
    cout << result << endl;
  }
}
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \
  for (auto i : collection) {                                                  \
    out(i);                                                                    \
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int solve(int n, int m) {
  vi w(m + 1, 0), h(n + 1, 0);
  rep(i, n) cin >> h[i + 1];
  rep(i, m) cin >> w[i + 1];

  rep(i, n) h[i + 1] += h[i];
  rep(i, m) w[i + 1] += w[i];

  map<int, int> hm, wm;
  rep(i, n + 1) for (int j = i + 1; j <= n; ++j) hm[h[j] - h[i]]++;
  rep(i, m + 1) for (int j = i + 1; j <= m; ++j) wm[w[j] - w[i]]++;

  int ans = 0;
  for (auto hh : hm) {
    int hk, hv;
    tie(hk, hv) = hh;
    for (auto ww : wm) {
      int wk, wv;
      tie(wk, wv) = ww;
      if (hk == wk)
        ans += wv * hv;
    }
  }
  return ans;
}

int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    out(solve(N, M));
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \
  for (auto i : collection) {                                                  \
    out(i);                                                                    \
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int solve(int n, int m) {
  vi w(m + 1, 0), h(n + 1, 0);
  rep(i, n) cin >> h[i + 1];
  rep(i, m) cin >> w[i + 1];

  rep(i, n) h[i + 1] += h[i];
  rep(i, m) w[i + 1] += w[i];

  map<int, int> hm, wm;
  rep(i, n + 1) for (int j = i + 1; j <= n; ++j) hm[h[j] - h[i]]++;
  rep(i, m + 1) for (int j = i + 1; j <= m; ++j) wm[w[j] - w[i]]++;

  int ans = 0;
  rep(i, max(h[n], w[n]) + 1) ans += (hm[i] * wm[i]);
  return ans;
}

int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    out(solve(N, M));
  }
}
Problem p01140 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 100;

int h, w;
int A[N];
int B[N];

void init() {}

bool input() {
  cin >> h >> w;
  if (h == 0)
    return 0;
  rep(i, h) cin >> A[i];
  rep(i, w) cin >> B[i];
  return true;
}

int solve() {
  int C[N] = {0};
  int D[N] = {0};
  rep(i, h) { C[i + 1] = C[i] + A[i]; }
  rep(i, w) { D[i + 1] = D[i] + B[i]; }

  map<int, int> sum;
  rep(i, h) {
    reps(j, i, h) {
      int s = C[j + 1] - C[i];
      sum[s]++;
      // printf("s %d  %d\n",s,sum[s]);
    }
  }

  int ans = 0;
  rep(i, w) {
    reps(j, i, w) {
      int s = D[j + 1] - D[i];
      ans += sum[s];
      // printf("g %d  %d\n",s,sum[s]);
    }
  }

  return ans;
}
int main() {
  while (init(), input())
    printf("%d\n", solve());
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 1600;

int h, w;
int A[N];
int B[N];

void init() {}

bool input() {
  cin >> h >> w;
  if (h == 0)
    return 0;
  rep(i, h) cin >> A[i];
  rep(i, w) cin >> B[i];
  return true;
}

int solve() {
  int C[N] = {0};
  int D[N] = {0};
  rep(i, h) { C[i + 1] = C[i] + A[i]; }
  rep(i, w) { D[i + 1] = D[i] + B[i]; }

  map<int, int> sum;
  rep(i, h) {
    reps(j, i, h) {
      int s = C[j + 1] - C[i];
      sum[s]++;
      // printf("s %d  %d\n",s,sum[s]);
    }
  }

  int ans = 0;
  rep(i, w) {
    reps(j, i, w) {
      int s = D[j + 1] - D[i];
      ans += sum[s];
      // printf("g %d  %d\n",s,sum[s]);
    }
  }

  return ans;
}
int main() {
  while (init(), input())
    printf("%d\n", solve());
}
Problem p01140 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <set>

using namespace std;

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int H[N], W[M];
    for (int i = 0; i < N; i++) {
      cin >> H[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> W[i];
    }

    set<int> v1, v2;
    map<int, int> m1, m2;
    for (int i = 0; i < N; i++) {
      int bfr = 0;
      for (int j = i; j < N; j++) {
        int temp;
        temp = bfr + H[j];
        bfr = temp;
        v1.insert(temp);
        m1[temp]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int bfr = 0;
      for (int j = i; j < M; j++) {
        int temp;
        temp = bfr + W[j];
        bfr = temp;
        v2.insert(temp);
        m2[temp]++;
      }
    }

    int cnt = 0;
    set<int>::iterator it1 = v1.begin();
    while (it1 != v1.end()) {
      set<int>::iterator it2 = v2.begin();
      while (it2 != v2.end()) {
        if (*it1 == *it2)
          cnt += m1[*it1] * m2[*it2];
        it2++;
      }
      it1++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <set>

using namespace std;

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int H[N], W[M];
    for (int i = 0; i < N; i++) {
      cin >> H[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> W[i];
    }

    set<int> v1, v2;
    map<int, int> m1, m2;
    for (int i = 0; i < N; i++) {
      int bfr = 0;
      for (int j = i; j < N; j++) {
        int temp;
        temp = bfr + H[j];
        bfr = temp;
        v1.insert(temp);
        m1[temp]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int bfr = 0;
      for (int j = i; j < M; j++) {
        int temp;
        temp = bfr + W[j];
        bfr = temp;
        v2.insert(temp);
        m2[temp]++;
      }
    }

    int cnt = 0;
    set<int>::iterator it1 = v1.begin();
    while (it1 != v1.end()) {
      set<int>::iterator it2;
      it2 = v2.find(*it1);
      if (it2 != v2.end())
        cnt += m1[*it1] * m2[*it2];
      it1++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01143 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8 fixed << setprecision(8)
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

using namespace std;
int main(void) {
  for (;;) {
    int n, m, p, sum = 0, div;
    cin >> n >> m >> p;
    if (!n && !m && !p)
      return 0;
    REP(i, n) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      if (i + 1 == m)
        div = tmp;
    }
    cout << sum * (100 - p) / div << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8 fixed << setprecision(8)
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

using namespace std;
int main(void) {
  for (;;) {
    int n, m, p, sum = 0, div;
    cin >> n >> m >> p;
    if (!n && !m && !p)
      return 0;
    REP(i, n) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      if (i + 1 == m)
        div = tmp;
    }
    if (div == 0)
      sum = 0;
    else
      sum = sum * (100 - p) / div;
    cout << sum << endl;
  }
}
Problem p01143 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)

int main() {
  while (1) {
    int n, m, p;
    cin >> n >> m >> p;
    if (n == 0 && m == 0 && p == 0)
      break;
    int count = 0, ans, a;
    lp(i, n) {
      cin >> a;
      count += a;
      if (i == m - 1)
        ans = a;
    }
    count *= 100;
    count *= (100 - p);
    cout << count / a / 100 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)

int main() {
  while (1) {
    int n, m, p;
    cin >> n >> m >> p;
    if (n == 0 && m == 0 && p == 0)
      break;
    int count = 0, ans, a;
    lp(i, n) {
      cin >> a;
      count += a;
      if (i == m - 1)
        ans = a;
    }
    count *= 100;
    count *= (100 - p);
    if (ans != 0)
      cout << count / ans / 100 << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}
Problem p01143 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n, m, p, x;
  int num[105] = {};
  int i;

  while (1) {
    cin >> n >> m >> p;
    if (n == 0 && m == 0 && p == 0)
      break;
    ll sum = 0LL;
    rep(i, n) {
      cin >> x;
      num[x]++;
      sum += x;
    }
    sum = sum * (100 - p);
    if (num[m])
      cout << sum / num[m] << endl;
    else
      cout << "0" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n, m, p, x;
  int num[105] = {};
  int i;

  while (1) {
    cin >> n >> m >> p;
    if (n == 0 && m == 0 && p == 0)
      break;
    ll sum = 0LL;
    rep(i, n) {
      cin >> x;
      num[i + 1] = x;
      sum += x;
    }
    sum = sum * (100 - p);
    if (num[m])
      cout << sum / num[m] << endl;
    else
      cout << "0" << endl;
  }
}
Problem p01143 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int n, m, p;
  int num[101];
  while (cin >> n >> m >> p, n | m | p) {
    int total = 0;
    for (int i = 1; i <= n; i++) {
      cin >> num[i];
      total += num[i] * (100 - p);
    }
    if (!num[n]) {
      cout << 0 << endl;
      continue;
    }
    cout << (total / num[m]) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, m, p;
  int num[101];
  while (cin >> n >> m >> p, n | m | p) {
    int total = 0;
    for (int i = 1; i <= n; i++) {
      cin >> num[i];
      total += num[i] * (100 - p);
    }
    if (!num[m]) {
      cout << 0 << endl;
      continue;
    }
    cout << (total / num[m]) << endl;
  }
  return 0;
}
Problem p01143 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n, m, p, x[1000];
  while (1) {
    cin >> n >> m >> p;

    int sum = 0;

    if (n == 0 && m == 0 && p == 0)
      break;

    p = 100 - p;

    for (int i = 0; i < n; i++) {
      cin >> x[i];
      sum += x[i];
    }
    p *= sum;
    p = p / x[m - 1];

    cout << p << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, m, p, x[1000];
  while (1) {
    cin >> n >> m >> p;

    int sum = 0;

    if (n == 0 && m == 0 && p == 0)
      break;

    p = 100 - p;

    for (int i = 0; i < n; i++) {
      cin >> x[i];
      sum += x[i];
    }
    p *= sum;

    if (x[m - 1] == 0)
      p = 0;
    else
      p = p / x[m - 1];

    cout << p << endl;
  }
  return 0;
}
Problem p01143 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int N, M, P;

  while (1) {
    cin >> N >> M >> P;

    double A[N];
    double sum = 0;
    double num = 0;
    double money;
    for (int i = 1; i <= N; i++) {
      cin >> A[i];
      sum += A[i] * 100;
      if (i == M) {
        num = A[i];
      }
    }

    money = (sum * (100 - P) / 100) / num;
    // cout << M << " " << num << " " << sum << " " << money << " " << P <<
    // endl;

    if (num == 0) {
      cout << 0 << endl;
    } else if (num != 0) {
      cout << (int)money << endl;
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N, M, P;

  while (1) {
    cin >> N >> M >> P;
    if ((N == 0) && (M == 0) && (P == 0)) {
      break;
    }

    double A[N];
    double sum = 0;
    double num = 0;
    double money;
    for (int i = 1; i <= N; i++) {
      cin >> A[i];
      sum += A[i] * 100;
      if (i == M) {
        num = A[i];
      }
    }

    money = (sum * (100 - P) / 100) / num;
    // cout << M << " " << num << " " << sum << " " << money << " " << P <<
    // endl;

    if (num == 0) {
      cout << 0 << endl;
    } else if (num != 0) {
      cout << (int)money << endl;
    }
  }
}
Problem p01144 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

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

using namespace std;

bool cmp(pair<int, int> &x, pair<int, int> &y) { return x.second > y.second; }

int main(void) {
  int n, m;
  while (cin >> n >> m, n > 0) {
    int j = 0, ans = 0;
    vector<pair<int, int>> dp(n);
    rep(i, n) {
      int d, p;
      cin >> d >> p;
      dp[i] = make_pair(d, p);
    }
    sort(dp.begin(), dp.end(), cmp);
    while (m > 0) {
      if (m > dp[j].first) {
        m -= dp[j].first;
        dp[j].first = 0;
        j++;
      } else {
        dp[j].first -= m;
        m = 0;
      }
    }
    rep(i, n) { ans += dp[i].first * dp[i].second; }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

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

using namespace std;

bool cmp(pair<int, int> &x, pair<int, int> &y) { return x.second > y.second; }

int main(void) {
  int n, m;
  while (cin >> n >> m, n > 0) {
    int j = 0, ans = 0;
    vector<pair<int, int>> dp(n);
    rep(i, n) {
      int d, p;
      cin >> d >> p;
      dp[i] = make_pair(d, p);
    }
    sort(dp.begin(), dp.end(), cmp);
    while (m > 0) {
      if (m > dp[j].first) {
        m -= dp[j].first;
        dp[j].first = 0;
        j++;
        if (j == n)
          break;
      } else {
        dp[j].first -= m;
        m = 0;
      }
    }
    rep(i, n) { ans += dp[i].first * dp[i].second; }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++)

Buggy submission (status: Runtime Error):
/*
Princess's Marriage
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2019&lang=jp
20:10
*/

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

using std::cin;
using std::cout;
using std::min;
using std::pair;
using std::sort;
using std::vector;

#define INF 999999999;

long long int N, M;
vector<pair<int, int>> DaP; //(Dn, Pn)
long long int ans;

int main(void) {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  do {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      pair<int, int> inn;
      cin >> inn.first >> inn.second;
      ans += inn.first * inn.second;
      DaP.push_back(inn);
    }
    sort(DaP.begin(), DaP.end(), [](pair<int, int> a, pair<int, int> b) {
      return a.second > b.second;
    });
    int i = 0;
    while (M > 0) {
      long long int minus = min(M, (long long int)DaP[i].first);
      ans -= DaP[i].second * minus;
      M -= minus;
      i++;
    }
    cout << ans << "\n";
    ans = 0;
    DaP.clear();

  } while (true);

  return 0;
}


Fixed submission:
/*
Princess's Marriage
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2019&lang=jp
20:10
*/

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

using std::cin;
using std::cout;
using std::min;
using std::pair;
using std::sort;
using std::vector;

#define INF 999999999;

long long int N, M;
vector<pair<int, int>> DaP; //(Dn, Pn)
long long int ans;

int main(void) {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  do {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      pair<int, int> inn;
      cin >> inn.first >> inn.second;
      ans += inn.first * inn.second;
      DaP.push_back(inn);
    }
    sort(DaP.begin(), DaP.end(), [](pair<int, int> a, pair<int, int> b) {
      return a.second > b.second;
    });
    int i = 0;
    while (M > 0) {
      long long int minus = min(M, (long long int)DaP[i].first);
      ans -= DaP[i].second * minus;
      M -= minus;
      i++;
      if (i == DaP.size())
        break;
    }
    cout << ans << "\n";
    ans = 0;
    DaP.clear();

  } while (true);

  return 0;
}
Problem p01144 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  ll n, m;
  while (cin >> n >> m, n + m) {
    ll ans = 0;
    vector<pair<int, int>> pii(n);
    for (int i = 0; i < n; i++) {
      cin >> pii[i].S >> pii[i].F;
      ans += pii[i].S * pii[i].F;
    }
    int po = n - 1;
    sort(pii.begin(), pii.end());
    while (m > 0) {
      if (m - pii[po].S >= 0) {
        ans -= pii[po].F * pii[po].S;
        m -= pii[po].S;
      } else {
        ans -= pii[po].F * m;
        break;
      }
      po--;
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  ll n, m;
  while (cin >> n >> m, n + m) {
    ll ans = 0;
    vector<pair<int, int>> pii(n);
    for (int i = 0; i < n; i++) {
      cin >> pii[i].S >> pii[i].F;
      ans += pii[i].S * pii[i].F;
    }
    int po = n - 1;
    sort(pii.begin(), pii.end());
    while (po >= 0) {
      if (m - pii[po].S >= 0) {
        ans -= pii[po].F * pii[po].S;
        m -= pii[po].S;
      } else {
        ans -= pii[po].F * m;
        break;
      }
      po--;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
using namespace std;

int main() {
  while (true) {
    int n, m;
    int d, p;
    multiset<pair<int, int>, greater<pair<int, int>>> s;
    multiset<pair<int, int>, greater<pair<int, int>>>::iterator it;
    int e = 0;
    cin >> n >> m;
    if (!(n || m))
      break;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      s.insert(make_pair(p, d));
    }
    it = s.begin();
    while (it != s.end() || m > 0) {
      m -= (*it).second;
      ++it;
    }
    if (m < 0) {
      --it;
      e += (*it).first * (-m);
      ++it;
    }
    while (it != s.end()) {
      e += (*it).second * (*it).first;
      ++it;
    }
    cout << e << endl;
  }
}

Fixed submission:
#include <iostream>
#include <set>
using namespace std;

int main() {
  while (true) {
    int n, m;
    int d, p;
    multiset<pair<int, int>, greater<pair<int, int>>> s;
    multiset<pair<int, int>, greater<pair<int, int>>>::iterator it;
    int e = 0;
    cin >> n >> m;
    if (!(n || m))
      break;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      s.insert(make_pair(p, d));
    }
    it = s.begin();
    while (it != s.end() && m > 0) {
      m -= (*it).second;
      ++it;
    }
    if (m < 0) {
      --it;
      e += (*it).first * (-m);
      ++it;
    }
    while (it != s.end()) {
      e += (*it).second * (*it).first;
      ++it;
    }
    cout << e << endl;
  }
}
Problem p01144 (C++)

Buggy submission (status: 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)
        i--;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += r[j].d * r[j].p;
    }
    cout << sum << endl;
  }

  return 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) {
        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;
}
Problem p01144 (C++)

Buggy submission (status: 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;
  }
  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;
}

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;
  }
  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 (status: Runtime Error):
// C
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<pair<int, int>> t(0); // 危険度、距離
    t.resize(11001);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b; // 距離、北市
      t[i] = (make_pair(b, a));
    }
    sort(t.begin(), t.end(), greater<pair<int, int>>());
    int iii = 0;
    while (m > 0) {
      if (m >= t[iii].second) {
        m -= t[iii].second;
        t[iii].second = 0;
      } else {
        t[iii].second -= m;
        m = 0;
      }
      iii++;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += t[i].first * t[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
// C
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<pair<int, int>> t(0); // 危険度、距離
    t.resize(11001);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b; // 距離、北市
      t[i] = (make_pair(b, a));
    }
    sort(t.begin(), t.end(), greater<pair<int, int>>());
    int iii = 0;
    while (m > 0) {
      bool f = 0;
      for (int j = 0; j < n; j++)
        if (t[j].second != 0)
          f = 1;
      if (f == 0)
        break;
      // cout<<"a";
      if (m >= t[iii].second) {
        m -= t[iii].second;
        t[iii].second = 0;
      } else {
        t[iii].second -= m;
        m = 0;
      }
      iii++;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += t[i].first * t[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++)

Buggy submission (status: 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 (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;
}

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 (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;
}
Problem p01144 (C++)

Buggy submission (status: 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 < M && x > 0; i++) {
      ept -= min(pd[i].second, x) * pd[i].first;
      x -= pd[i].second;
    }
    printf("%d\n", ept);
  }
  return 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 < N && 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 (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  long long m;
  while (cin >> n >> m && (n || m)) {
    priority_queue<P> que;
    int d, p;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      que.push(P(p, d));
    }
    long long ans = 0;
    while (1) {
      P p = que.top();
      que.pop();
      m -= p.second;
      if (m <= 0) {
        ans += p.first * abs(m);
        break;
      }
    }
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ans += p.first * p.second;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  long long m;
  while (cin >> n >> m && (n || m)) {
    priority_queue<P> que;
    int d, p;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      que.push(P(p, d));
    }
    long long ans = 0;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      m -= p.second;
      if (m <= 0) {
        ans += p.first * abs(m);
        break;
      }
    }
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ans += p.first * p.second;
    }
    cout << ans << endl;
  }
  return 0;
}