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

Buggy submission (status: Runtime Error):
#include <cstdio>
int main() {
  int n, s[20], i;
  int max, min, ava, sum;
  while (1) {
    max = 0;
    min = 1000;
    sum = 0;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      scanf("%d", &s[i]);
      if (max < s[i]) {
        max = s[i];
      }
      if (min > s[i]) {
        min = s[i];
      }
      sum += s[i];
    }
    sum -= max;
    sum -= min;
    ava = sum / (n - 2);
    printf("%d\n", ava);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
int main() {
  int n, s[100], i;
  int max, min, ava, sum;
  while (1) {
    max = 0;
    min = 1000;
    sum = 0;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      scanf("%d", &s[i]);
      if (max < s[i]) {
        max = s[i];
      }
      if (min > s[i]) {
        min = s[i];
      }
      sum += s[i];
    }
    sum -= max;
    sum -= min;
    ava = sum / (n - 2);
    printf("%d\n", ava);
  }
  return 0;
}
Problem p00728 (C++)

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

using namespace std;

int main() {
  int n, a[21], t;

  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    t = 0;
    for (int i = 1; i < n - 1; i++) {
      t += a[i];
    }

    cout << (t / (n - 2)) << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int n, a[101], t;

  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    t = 0;
    for (int i = 1; i < n - 1; i++) {
      t += a[i];
    }

    cout << (t / (n - 2)) << endl;
  }

  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int n;
  int point[20], average;

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

    for (int i = 0; i < n; i++) {
      cin >> point[i];
    }
    sort(point, point + n);
    for (int i = 1; i < n - 1; i++) {
      average += point[i];
    }
    average /= (n - 2);
    cout << average << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int n;
  int point[100], average;

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

    for (int i = 0; i < n; i++) {
      cin >> point[i];
    }
    sort(point, point + n);
    for (int i = 1; i < n - 1; i++) {
      average += point[i];
    }
    average /= (n - 2);
    cout << average << endl;
  }
  return 0;
}
Problem p00728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int n;

  while (cin >> n && n) {
    vector<int> scr(n);
    for (int i = 0; i < n; i++) {
      cin >> scr[i];
    }

    int sum = 0;
    int _max = scr[0];
    int _min = scr[0];

    for (int s : scr) {
      sum += s;
      _min = min(_min, s);
      _max = max(_max, s);
    }

    sum -= _max;
    sum -= _min;

    cout << sum / (n - 2) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int n;

  while (cin >> n) {
    if (!n)
      break;

    vector<int> scr(n);
    for (int i = 0; i < n; i++) {
      cin >> scr[i];
    }

    int sum = 0;
    int _max = scr[0];
    int _min = scr[0];

    for (int s : scr) {
      sum += s;
      _min = min(_min, s);
      _max = max(_max, s);
    }

    sum -= _max;
    sum -= _min;

    cout << sum / (n - 2) << endl;
  }

  return 0;
}
Problem p00728 (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 ALL(obj) (obj).begin(), (obj).end()

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

  while (true) {
    int n;
    cin >> n;
    vector<int> v;
    int a;
    rep(i, n) {
      cin >> a;
      v.push_back(a);
    }
    sort(ALL(v));

    int sum = 0;
    for (int i = 1; i < n - 1; i++) {
      sum += v[i];
    }
    sum = sum / (n - 2);
    cout << sum << endl;
  }
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()

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

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> v;
    int a;
    rep(i, n) {
      cin >> a;
      v.push_back(a);
    }
    sort(ALL(v));

    int sum = 0;
    for (int i = 1; i < n - 1; i++) {
      sum += v[i];
    }
    sum = sum / (n - 2);
    cout << sum << endl;
  }
}
Problem p00728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
typedef long long i64;
int main() {
  while (true) {
    int n;
    vector<int> a;
    cin >> n;
    rep(i, n) {
      int b;
      cin >> b;
      a.push_back(b);
    }
    sort(a.begin(), a.end());
    int total = 0;
    repi(i, 1, n - 1) { total += a[i]; }
    cout << total / (n - 2) << endl;
    ;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
typedef long long i64;
int main() {
  while (true) {
    int n;
    vector<int> a;
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) {
      int b;
      cin >> b;
      a.push_back(b);
    }
    sort(a.begin(), a.end());
    int total = 0;
    repi(i, 1, n - 1) { total += a[i]; }
    cout << total / (n - 2) << endl;
    ;
  }
  return 0;
}
Problem p00729 (C++)

Buggy submission (status: Runtime Error):
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define MAX 1010
#include <queue>
using namespace std;
typedef pair<int, int> P;
int main() {
  int e, q, t, n, m, s, p;
  while (cin >> e >> q, e || q) {
    vector<P> tab[MAX];
    cin >> p;
    for (int i = 0; i < p; i++) {
      cin >> t >> n >> m >> s;
      tab[m - 1].push_back(P(t, s));
    }
    int now, t1, t2;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> t1 >> t2 >> m;
      now = t1;
      int ans = 0, sum = 0;
      int j;
      m--;
      for (j = 0; j < tab[m].size() && tab[m][j].first < t2; j++) {
        if (!tab[m][j].second) {
          if (sum > 0 && tab[m][j].first >= t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum--;
          if (tab[m][j].first > t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        } else if (tab[m][j].second) {
          if (sum > 0 && tab[m][j].first > t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum++;
          if (tab[m][j].first >= t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        }
      }
      if (sum > 0)
        ans += t2 - now;
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define MAX 10010
#include <queue>
using namespace std;
typedef pair<int, int> P;
int main() {
  int e, q, t, n, m, s, p;
  while (cin >> e >> q, e || q) {
    vector<P> tab[MAX];
    cin >> p;
    for (int i = 0; i < p; i++) {
      cin >> t >> n >> m >> s;
      tab[m - 1].push_back(P(t, s));
    }
    int now, t1, t2;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> t1 >> t2 >> m;
      now = t1;
      int ans = 0, sum = 0;
      int j;
      m--;
      for (j = 0; j < tab[m].size() && tab[m][j].first < t2; j++) {
        if (!tab[m][j].second) {
          if (sum > 0 && tab[m][j].first >= t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum--;
          if (tab[m][j].first > t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        } else if (tab[m][j].second) {
          if (sum > 0 && tab[m][j].first > t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum++;
          if (tab[m][j].first >= t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        }
      }
      if (sum > 0)
        ans += t2 - now;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00729 (C++)

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
typedef pair<int, int> P;

//-----------------------------------------------------------------------

int N, M, R, Q;
int A[1005][1005]; // 生徒番号,時間

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

  while (cin >> N >> M, N) {
    memset(A, 0, sizeof(A));
    cin >> R;
    REP(i, R) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      t -= 540;
      n--;
      m--;
      if (s)
        A[m][t]++;
      else
        A[m][t]--;
    }
    REP(i, M) {
      REP(j, 1000) { A[i][j + 1] += A[i][j]; }
    }
    cin >> Q;
    REP(i, Q) {
      int s, e, m;
      cin >> s >> e >> m;
      s -= 540;
      e -= 540;
      m--;
      int res = 0;
      for (int i = s; i < e; i++) {
        res += (A[m][i] >= 1);
      }
      cout << res << endl;
    }
  }
}


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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
typedef pair<int, int> P;

//-----------------------------------------------------------------------

int N, M, R, Q;
int A[10000][1005]; // 生徒番号,時間

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

  while (cin >> N >> M, N) {
    memset(A, 0, sizeof(A));
    cin >> R;
    REP(i, R) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      t -= 540;
      n--;
      m--;
      if (s)
        A[m][t]++;
      else
        A[m][t]--;
    }
    REP(i, M) {
      REP(j, 1000) { A[i][j + 1] += A[i][j]; }
    }
    cin >> Q;
    REP(i, Q) {
      int s, e, m;
      cin >> s >> e >> m;
      s -= 540;
      e -= 540;
      m--;
      int res = 0;
      for (int i = s; i < e; i++) {
        res += (A[m][i] >= 1);
      }
      cout << res << endl;
    }
  }
}
Problem p00729 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
struct Database {
  int N, M, r, q;
  vector<vector<int>> use;

  Database() {
    cin >> N >> M;
    if (N == 0)
      return;
    use.resize(M + 1, vector<int>(60 * 24));

    cin >> r;
    REP(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      use[m][t] += (s == 1 ? 1 : -1);
    }
    REP(i, M + 1) REP(t, 60 * 24 - 1) use[i][t + 1] += use[i][t];
  }

  void query() {

    int ts, te, m;
    cin >> ts >> te >> m;

    int res = 0;
    FOR(t, ts, te) { res += (use[m][t] > 0 ? 1 : 0); }
    cerr << "==========";
    cout << res << endl;
  }
};

// メイン関数
int main() {
  while (true) {
    Database base;
    if (base.N == 0)
      break;

    int q;
    cin >> q;
    REP(i, q) base.query();
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
struct Database {
  int N, M, r, q;
  vector<vector<int>> use;

  Database() {
    cin >> N >> M;
    if (N == 0)
      return;
    use.resize(M + 1, vector<int>(60 * 24));

    cin >> r;
    REP(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      use[m][t] += (s == 1 ? 1 : -1);
    }
    REP(i, M + 1) REP(t, 60 * 24 - 1) use[i][t + 1] += use[i][t];
  }

  void query() {

    int ts, te, m;
    cin >> ts >> te >> m;

    int res = 0;
    FOR(t, ts, te) { res += (use[m][t] > 0 ? 1 : 0); }
    // cerr << "==========";
    cout << res << endl;
  }
};

// メイン関数
int main() {
  while (true) {
    Database base;
    if (base.N == 0)
      break;

    int q;
    cin >> q;
    REP(i, q) base.query();
  }

  return 0;
}
Problem p00729 (C++)

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

int main() {
  while (1) { // start

    int N, M, r;
    // N；PC数　M；学生数
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    cin >> r;

    // 学生数、　時間
    int data[1004][721];
    for (int j = 0; j < 1004; j++) {
      for (int k = 0; k < 721; k++)
        data[j][k] = 0;
    }

    for (int i = 0; i < r; i++) {
      // t；時刻、　n;PC番号　m；学生番号　s；１ならログイン、０ならログアウト(配列中では逆)
      int t, n, m;
      bool s;
      cin >> t >> n >> m >> s;
      if (s)
        data[m][t - 539] += 1;
      else
        data[m][t - 539] += -1;
    }

    // 累積和をとる
    for (int i = 0; i <= M; i++) {
      for (int k = 1; k < 721; k++)
        data[i][k] = data[i][k] + data[i][k - 1];
    }

    // クエリを取得
    int qn;
    cin >> qn;
    for (int i = 0; i < qn; i++) {
      int ts, tt, sn;
      cin >> ts >> tt >> sn;

      // 学生について調査する
      int ans = 0;
      for (int j = ts - 539; j < tt - 539; j++) {
        if (data[sn][j])
          ans++;
      }
      cout << ans << endl;
    }

  } // end

  return 0;
}


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

int main() {
  while (1) { // start

    int N, M, r;
    // N；PC数　M；学生数
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    cin >> r;

    // 学生数、　時間
    int data[10004][721];
    for (int j = 0; j < 10004; j++) {
      for (int k = 0; k < 721; k++)
        data[j][k] = 0;
    }

    for (int i = 0; i < r; i++) {
      // t；時刻、　n;PC番号　m；学生番号　s；１ならログイン、０ならログアウト(配列中では逆)
      int t, n, m;
      bool s;
      cin >> t >> n >> m >> s;
      if (s)
        data[m][t - 539] += 1;
      else
        data[m][t - 539] += -1;
    }

    // 累積和をとる
    for (int i = 0; i <= M; i++) {
      for (int k = 1; k < 721; k++)
        data[i][k] = data[i][k] + data[i][k - 1];
    }

    // クエリを取得
    int qn;
    cin >> qn;
    for (int i = 0; i < qn; i++) {
      int ts, tt, sn;
      cin >> ts >> tt >> sn;

      // 学生について調査する
      int ans = 0;
      for (int j = ts - 539; j < tt - 539; j++) {
        if (data[sn][j])
          ans++;
      }
      cout << ans << endl;
    }

  } // end

  return 0;
}
Problem p00729 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int login[10100][2000];

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    int r, q;
    cin >> r;
    int i;
    memset(login, 0, sizeof(login));
    for (i = 0; i < r; i++) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      login[m][t] += (s ? 1 : -1);
    }
    int j;
    int l;
    for (j = 0; j <= M; j++) {
      l = 0;
      for (i = 540; i <= 1260; i++) {
        l += login[j][i];
        login[j][i] = l;
        // cout<<j<<" "<<i<<" "<<l<<endl;
      }
    }
    cin >> q;
    for (i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int res = 0;
      for (j = ts; j < te; j++) {
        if (login[m][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int login[10100][1261];

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    int r, q;
    cin >> r;
    int i;
    memset(login, 0, sizeof(login));
    for (i = 0; i < r; i++) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      login[m][t] += (s ? 1 : -1);
    }
    int j;
    int l;
    for (j = 0; j <= M; j++) {
      l = 0;
      for (i = 540; i <= 1260; i++) {
        l += login[j][i];
        login[j][i] = l;
        // cout<<j<<" "<<i<<" "<<l<<endl;
      }
    }
    cin >> q;
    for (i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int res = 0;
      for (j = ts; j < te; j++) {
        if (login[m][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 0;
}
Problem p00729 (C++)

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

int n[1001][723] = {};
int main() {

  int N, M;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int r;
    cin >> r;

    for (int i = 0; i <= M; i++)
      for (int j = 0; j < 722; j++)
        n[i][j] = 0;

    for (int i = 0; i < r; i++) {
      int t, x, m, w;
      cin >> t >> x >> m >> w;
      t -= 540;
      if (w == 1)
        n[m][t] += 1;
      else {
        n[m][t] -= 1;
      }
    }
    for (int i = 0; i <= M; i++)
      for (int j = 0; j < 721; j++)
        n[i][j + 1] += n[i][j];

    int q, t1, t2, y;
    cin >> q;

    for (int i = 0; i < q; i++) {
      cin >> t1 >> t2 >> y;
      t1 -= 540;
      t2 -= 540;
      int c = 0;
      for (int j = t1; j < t2; j++) {
        if (n[y][j] != 0) {
          c++;
        } // cout<<j+540<<" "<<c<<n[y][j]<<endl;}
      }
      cout << c << endl;
    }
  }
  return 0;
}

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

int n[10001][723] = {};
int main() {

  int N, M;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int r;
    cin >> r;

    for (int i = 0; i <= M; i++)
      for (int j = 0; j < 722; j++)
        n[i][j] = 0;

    for (int i = 0; i < r; i++) {
      int t, x, m, w;
      cin >> t >> x >> m >> w;
      t -= 540;
      if (w == 1)
        n[m][t] += 1;
      else {
        n[m][t] -= 1;
      }
    }
    for (int i = 0; i <= M; i++)
      for (int j = 0; j < 721; j++)
        n[i][j + 1] += n[i][j];

    int q, t1, t2, y;
    cin >> q;

    for (int i = 0; i < q; i++) {
      cin >> t1 >> t2 >> y;
      t1 -= 540;
      t2 -= 540;
      int c = 0;
      for (int j = t1; j < t2; j++) {
        if (n[y][j] != 0) {
          c++;
        } // cout<<j+540<<" "<<c<<n[y][j]<<endl;}
      }
      cout << c << endl;
    }
  }
  return 0;
}
Problem p00729 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int main(void) {
  int N, M, r, t, n, m, s, log[17][721], q, ts, tg, i, j, k, flag[17], count;
  ;
  while (1) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    memset(log, 0, sizeof(log));
    scanf("%d", &r);
    for (i = 0; i < r; i++) {
      scanf("%d%d%d%d", &t, &n, &m, &s);
      if (s == 1)
        log[n - 1][t - 540] = m;
      else if (s == 0)
        log[n - 1][t - 540] = -1;
    }
    memset(flag, 0, sizeof(flag));
    for (i = 0; i < N; i++) {
      for (j = 0; j <= 720; j++) {
        if (log[i][j] && flag[i] == 0)
          flag[i] = log[i][j];
        else if (log[i][j] && flag[i] != 0)
          flag[i] = 0;
        if (flag[i] != 0)
          log[i][j] = flag[i];
      }
    }
    scanf("%d", &q);
    for (i = 0; i < q; i++) {
      scanf("%d%d%d", &ts, &tg, &m);
      memset(flag, 0, sizeof(flag));
      count = 0;
      for (j = ts - 540; j <= tg - 540; j++) {
        for (k = 0; k < N; k++) {
          if (flag[k]) {
            count++;
            break;
          }
        }
        for (k = 0; k < N; k++) {
          if (log[k][j] == m)
            flag[k] = 1;
          else if (log[k][j] != 0)
            flag[k] = 0;
        }
      }
      printf("%d\n", count);
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int main(void) {
  int N, M, r, t, n, m, s, log[1000][721], q, ts, tg, i, j, k, flag[1000],
      count;
  ;
  while (1) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    memset(log, 0, sizeof(log));
    scanf("%d", &r);
    for (i = 0; i < r; i++) {
      scanf("%d%d%d%d", &t, &n, &m, &s);
      if (s == 1)
        log[n - 1][t - 540] = m;
      else if (s == 0)
        log[n - 1][t - 540] = -1;
    }
    memset(flag, 0, sizeof(flag));
    for (i = 0; i < N; i++) {
      for (j = 0; j <= 720; j++) {
        if (log[i][j] && flag[i] == 0)
          flag[i] = log[i][j];
        else if (log[i][j] && flag[i] != 0)
          flag[i] = 0;
        if (flag[i] != 0)
          log[i][j] = flag[i];
      }
    }
    scanf("%d", &q);
    for (i = 0; i < q; i++) {
      scanf("%d%d%d", &ts, &tg, &m);
      memset(flag, 0, sizeof(flag));
      count = 0;
      for (j = ts - 540; j <= tg - 540; j++) {
        for (k = 0; k < N; k++) {
          if (flag[k]) {
            count++;
            break;
          }
        }
        for (k = 0; k < N; k++) {
          if (log[k][j] == m)
            flag[k] = 1;
          else if (log[k][j] != 0)
            flag[k] = 0;
        }
      }
      printf("%d\n", count);
    }
  }
  return 0;
}
Problem p00729 (C++)

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

int main() {
  int n, m, r, q;
  static int cnt[1000][1261];
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j <= 1260; j++)
        cnt[i][j] = 0;
    }
    cin >> r;
    for (int i = 0; i < r; i++) {
      int t, a, b, s;
      cin >> t >> a >> b >> s;
      b--;
      if (s)
        cnt[b][t]++;
      else
        cnt[b][t]--;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        cnt[i][j] += cnt[i][j - 1];
        // if(j >= 774 && j <= 881) cout << cnt[i][j] << " ";
      }
      // cout << endl;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        if (cnt[i][j])
          cnt[i][j] = 1;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        cnt[i][j] += cnt[i][j - 1];
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      int s, t, b;
      cin >> s >> t >> b;
      b--;
      cout << cnt[b][t - 1] - cnt[b][s - 1] << endl;
    }
  }
  return 0;
}

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

int main() {
  int n, m, r, q;
  static int cnt[10000][1261];
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j <= 1260; j++)
        cnt[i][j] = 0;
    }
    cin >> r;
    for (int i = 0; i < r; i++) {
      int t, a, b, s;
      cin >> t >> a >> b >> s;
      b--;
      if (s)
        cnt[b][t]++;
      else
        cnt[b][t]--;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        cnt[i][j] += cnt[i][j - 1];
        // if(j >= 774 && j <= 881) cout << cnt[i][j] << " ";
      }
      // cout << endl;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        if (cnt[i][j])
          cnt[i][j] = 1;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j <= 1260; j++) {
        cnt[i][j] += cnt[i][j - 1];
      }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      int s, t, b;
      cin >> s >> t >> b;
      b--;
      cout << cnt[b][t - 1] - cnt[b][s - 1] << endl;
    }
  }
  return 0;
}
Problem p00729 (C++)

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

int students[10001][800] = {};
int numlogin[10001] = {};
int sum[10001][800] = {};

int main() {

  while (1) {
    int N, M, r;
    cin >> N >> M;
    if (!N and !M)
      break;
    cin >> r;

    memset(students, 0, sizeof(students));
    memset(numlogin, 0, sizeof(numlogin));
    memset(sum, 0, sizeof(sum));

    // ?¨????
    for (int i = 0; i < r; i++) { // 1000
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      t -= 540;
      if (s == 0) { // ?????°??¢??????
        numlogin[m]--;
        if (numlogin[m] == 0) {
          for (int i = t - 1; students[m][i] != 1 and t >= 0; i--) { // 1000
            students[m][i] = 1;
          }
        }
      } else { // ?????°??????
        numlogin[m]++;
        if (numlogin[m] == 1)
          students[m][t] = 1;
      }
    }

    // ?´???????
    for (int i = 1; i <= M; i++) {     // 100000
      for (int j = 1; j <= 720; j++) { // 1000
        sum[i][j] = sum[i][j - 1] + students[i][j];
      }
    }

    // ??????
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, id;
      cin >> ts >> te >> id;
      ts -= 540;
      te -= 540;
      cout << sum[id][te - 1] - sum[id][ts] + students[id][ts] << endl;
    }
  }
}

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

int students[10001][750] = {};
int numlogin[10001] = {};
int sum[10001][800] = {};

int main() {

  while (1) {
    int N, M, r;
    cin >> N >> M;
    if (!N and !M)
      break;
    cin >> r;

    memset(students, 0, sizeof(students));
    memset(numlogin, 0, sizeof(numlogin));
    memset(sum, 0, sizeof(sum));

    // ?¨????
    for (int i = 0; i < r; i++) { // 1000
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      t -= 540;
      if (s == 0) { // ?????°??¢??????
        numlogin[m]--;
        if (numlogin[m] == 0) {
          for (int i = t - 1; students[m][i] != 1 and t >= 0; i--) { // 1000
            students[m][i] = 1;
          }
        }
      } else { // ?????°??????
        numlogin[m]++;
        if (numlogin[m] == 1)
          students[m][t] = 1;
      }
    }

    // ?´???????
    for (int i = 1; i <= M; i++) {     // 100000
      for (int j = 1; j <= 720; j++) { // 1000
        sum[i][j] = sum[i][j - 1] + students[i][j];
      }
    }

    // ??????
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, id;
      cin >> ts >> te >> id;
      ts -= 540;
      te -= 540;
      cout << sum[id][te - 1] - sum[id][ts] + students[id][ts] << endl;
    }
  }
}
Problem p00729 (C++)

Buggy submission (status: 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 use[1111][1300];
int N, M, r, q;

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(use, 0, sizeof(use));
    cin >> r;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      n--;
      m--;
      if (s == 1) {
        use[m][t] += 1;
      } else {
        use[m][t] -= 1;
      }
    }
    rep(i, M) repl(j, 540, 1300) { use[i][j + 1] += use[i][j]; }
    cin >> q;
    rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;
      int res = 0;
      repl(j, ts, te) {
        if (use[m][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 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 use[11111][1300];
int N, M, r, q;

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(use, 0, sizeof(use));
    cin >> r;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      n--;
      m--;
      if (s == 1) {
        use[m][t] += 1;
      } else {
        use[m][t] -= 1;
      }
    }
    rep(i, M) repl(j, 540, 1300) { use[i][j + 1] += use[i][j]; }
    cin >> q;
    rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;
      int res = 0;
      repl(j, ts, te) {
        if (use[m][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 0;
}
Problem p00730 (C++)

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

bool larger(pair<int, int> &a, pair<int, int> &b) {
  return a.first * a.second < b.first * b.second;
}

int main() {
  while (1) {
    int n, w, d;
    cin >> n >> w >> d;
    if (w == 0)
      break;

    vector<pair<int, int>> cakes;
    cakes.push_back(make_pair(w, d));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      int x = cakes[p].first, y = cakes[p].second;
      cakes.erase(cakes.begin() + p);
      s %= x + y;
      if (s < x) {
        if (s <= x / 2) {
          cakes.push_back(make_pair(s, y));
          cakes.push_back(make_pair(x - s, y));
        } else {
          cakes.push_back(make_pair(x - s, y));
          cakes.push_back(make_pair(s, d));
        }
      } else {
        s -= x;
        if (s <= y / 2) {
          cakes.push_back(make_pair(x, s));
          cakes.push_back(make_pair(x, y - s));
        } else {
          cakes.push_back(make_pair(x, y - s));
          cakes.push_back(make_pair(x, s));
        }
      }
    }

    sort(cakes.begin(), cakes.end(), larger);
    for (int i = 0; i < n; i++) {
      cout << cakes[i].first * cakes[i].second << " ";
    }
    cout << cakes[n].first * cakes[n].second << endl;
  }
  return 0;
}

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

bool larger(pair<int, int> &a, pair<int, int> &b) {
  return a.first * a.second < b.first * b.second;
}

int main() {
  while (1) {
    int n, w, d;
    cin >> n >> w >> d;
    if (w == 0)
      break;

    vector<pair<int, int>> cakes;
    cakes.push_back(make_pair(w, d));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      int x = cakes[p].first, y = cakes[p].second;
      cakes.erase(cakes.begin() + p);
      s %= x + y;
      if (s < x) {
        if (s <= x / 2) {
          cakes.push_back(make_pair(s, y));
          cakes.push_back(make_pair(x - s, y));
        } else {
          cakes.push_back(make_pair(x - s, y));
          cakes.push_back(make_pair(s, y));
        }
      } else {
        s -= x;
        if (s <= y / 2) {
          cakes.push_back(make_pair(x, s));
          cakes.push_back(make_pair(x, y - s));
        } else {
          cakes.push_back(make_pair(x, y - s));
          cakes.push_back(make_pair(x, s));
        }
      }
    }

    sort(cakes.begin(), cakes.end(), larger);
    for (int i = 0; i < n; i++) {
      cout << cakes[i].first * cakes[i].second << " ";
    }
    cout << cakes[n].first * cakes[n].second << endl;
  }
  return 0;
}
Problem p00731 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;
typedef pair<p, p> P;
char mp[30][60];
int w, h;
int cal() {
  bool used[2][60][30] = {};
  int dx[9] = {1, 1, 1, 1, 1, 2, 2, 2, 3},
      dy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < w; i++)
    if (mp[h - 1][i] == 'S')
      q.push(P(p(0, 0), p(i, h - 1))), q.push(P(p(0, 1), p(i, h - 1)));
  while (!q.empty()) {
    int s = q.top().first.first, l = q.top().first.second;
    int x = q.top().second.first, y = q.top().second.second;
    q.pop();
    if (mp[y][x] == 'T')
      return s;
    used[l][y][x] = 1;
    int f = 1, c = 0;
    if (l)
      f = -1;
    if (isdigit(mp[y][x]))
      c = mp[y][x] - '0';
    for (int d = 0; d < 9; d++) {
      int nx = x + f * dx[d], ny = y + f * dy[d];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny || mp[ny][nx] == 'X' ||
          used[!l][ny][nx])
        continue;
      q.push(P(p(s + c, !l), p(nx, ny)));
    }
  }
  return -1;
}

int main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    cout << cal() << endl;
  }
  return 0;
}

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

char mp[60][30];
int w, h;
int cal() {
  bool used[2][60][30] = {};
  int dx[9] = {1, 1, 1, 1, 1, 2, 2, 2, 3},
      dy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < w; i++)
    if (mp[h - 1][i] == 'S')
      q.push(P(p(0, 0), p(i, h - 1))), q.push(P(p(0, 1), p(i, h - 1)));
  while (!q.empty()) {
    int s = q.top().first.first, l = q.top().first.second;
    int x = q.top().second.first, y = q.top().second.second;
    q.pop();
    if (mp[y][x] == 'T')
      return s;
    used[l][y][x] = 1;
    int f = 1, c = 0;
    if (l)
      f = -1;
    if (isdigit(mp[y][x]))
      c = mp[y][x] - '0';
    for (int d = 0; d < 9; d++) {
      int nx = x + f * dx[d], ny = y + f * dy[d];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny || mp[ny][nx] == 'X' ||
          used[!l][ny][nx])
        continue;
      q.push(P(p(s + c, !l), p(nx, ny)));
    }
  }
  return -1;
}

int main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    cout << cal() << endl;
  }
  return 0;
}
Problem p00734 (C++)

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

int main() {
  int n = 0, m = 1;
  int a[n], b[m], x = 0, y = 0;
  int p, q;

  while (1) {

    cin >> n >> m;
    x = 0;
    y = 0;
    p = -1;
    q = -1;

    if (0 == n + m)
      break;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      x += a[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> b[j];
      y += b[j];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((a[i] - b[j]) * 2 == (x - y)) {
          if (p == -1) {
            p = a[i];
            q = b[j];
          } else if (p + q > a[i] + b[j]) {
            p = a[i];
            q = b[j];
          }
        }
      }
    }

    if (p == -1)
      cout << p << endl;
    else
      cout << p << " " << q << endl;
  }

  return 0;
}

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

int main() {
  int n = 0, m = 1;
  int a[101], b[101], x = 0, y = 0;
  int p, q;

  while (1) {

    cin >> n >> m;
    x = 0;
    y = 0;
    p = -1;
    q = -1;

    if (0 == n + m)
      break;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      x += a[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> b[j];
      y += b[j];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((a[i] - b[j]) * 2 == (x - y)) {
          if (p == -1) {
            p = a[i];
            q = b[j];
          } else if (p + q > a[i] + b[j]) {
            p = a[i];
            q = b[j];
          }
        }
      }
    }

    if (p == -1)
      cout << p << endl;
    else
      cout << p << " " << q << endl;
  }

  return 0;
}
Problem p00734 (C++)

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v), end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> P;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
//-----------------------------------------------------------------------

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

  while (true) {
    int n, m, s1[50], s2[50];
    int sum1 = 0, sum2 = 0;

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    REP(i, n) cin >> s1[i], sum1 += s1[i];
    REP(i, m) cin >> s2[i], sum2 += s2[i];

    int a = 1e9, b = 1e9;
    REP(i, n) REP(j, m) {
      if (sum1 - s1[i] + s2[j] == sum2 + s1[i] - s2[j]) {
        if (s1[i] + s2[j] < a + b)
          a = s1[i], b = s2[j];
      }
    }
    if (a == 1e9)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v), end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> P;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
//-----------------------------------------------------------------------

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

  while (true) {
    int n, m, s1[100], s2[100];
    int sum1 = 0, sum2 = 0;

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    REP(i, n) cin >> s1[i], sum1 += s1[i];
    REP(i, m) cin >> s2[i], sum2 += s2[i];

    int a = 1e9, b = 1e9;
    REP(i, n) REP(j, m) {
      if (sum1 - s1[i] + s2[j] == sum2 + s1[i] - s2[j]) {
        if (s1[i] + s2[j] < a + b)
          a = s1[i], b = s2[j];
      }
    }
    if (a == 1e9)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}
Problem p00734 (C++)

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

int main() {
  int n, m, sumN = 0, sumM = 0, ii = -1, jj = -1, flag = 0;
  int N[100], M[100];

  while (1) {
    cin >> n;
    cin >> m;
    for (int i = 0; i < n; ++i) {
      cin >> N[i];
      sumN += N[i];
    }
    for (int i = 0; i < m; ++i) {
      cin >> M[i];
      sumM += M[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (sumN - N[i] + M[j] == sumM - M[j] + N[i] && flag == 0) {
          ii = i;
          jj = j;
          flag = 1;
        }
        if (sumN - N[i] + M[j] == sumM - M[j] + N[i] && flag == 1 &&
            N[ii] + M[jj] > N[i] + M[j]) {
          ii = i;
          jj = j;
        }
      }
    }
    if (ii != -1 && jj != -1) {
      cout << N[ii] << " " << M[jj] << endl;
    } else
      cout << "-1" << endl;
    flag = 0;
    sumN = 0;
    sumM = 0;
    ii = -1;
    jj = -1;
  }
  return 0;
}

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

int main() {
  int n, m, sumN = 0, sumM = 0, ii = -1, jj = -1, flag = 0;
  int N[100], M[100];

  while (1) {
    cin >> n;
    cin >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; ++i) {
      cin >> N[i];
      sumN += N[i];
    }
    for (int i = 0; i < m; ++i) {
      cin >> M[i];
      sumM += M[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (sumN - N[i] + M[j] == sumM - M[j] + N[i] && flag == 0) {
          ii = i;
          jj = j;
          flag = 1;
        }
        if (sumN - N[i] + M[j] == sumM - M[j] + N[i] && flag == 1 &&
            N[ii] + M[jj] > N[i] + M[j]) {
          ii = i;
          jj = j;
        }
      }
    }
    if (ii != -1 && jj != -1) {
      cout << N[ii] << " " << M[jj] << endl;
    } else
      cout << "-1" << endl;
    flag = 0;
    sumN = 0;
    sumM = 0;
    ii = -1;
    jj = -1;
  }
  return 0;
}
Problem p00734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = (n)-1; i >= 0; i--)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define REPS(i, f, n) for (int i = (f)-1; i >= (n); i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(int n, int m) {
  vint vtaro(n);
  vint vhana(n);
  int sumtaro = 0, sumhana = 0;
  rep(i, n) {
    int tmp;
    scanf("%d", &tmp);
    sumtaro += tmp;
    vtaro[i] = tmp;
  }
  sort(all(vtaro));

  rep(i, m) {
    int tmp;
    scanf("%d", &tmp);
    sumhana += tmp;
    vhana[i] = tmp;
  }
  sort(all(vhana));

  double sa = sumtaro - sumhana; //????¨??????????
  rep(i, n) {
    rep(j, m) {
      if (vtaro[i] - vhana[j] == (sa / 2)) {
        printf("%d %d\n", vtaro[i], vhana[j]);
        return;
      }
    }
  }
  printf("-1\n");
  return;
}

int main(void) {
  int n, m;
  rep(i, 1000) {
    cin >> n >> m;

    if (n == 0)
      break;
    slove(n, m);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = (n)-1; i >= 0; i--)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define REPS(i, f, n) for (int i = (f)-1; i >= (n); i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(int n, int m) {
  vint vtaro(n);
  vint vhana(m);
  int sumtaro = 0, sumhana = 0;
  rep(i, n) {
    int tmp;
    scanf("%d", &tmp);
    sumtaro += tmp;
    vtaro[i] = tmp;
  }
  sort(all(vtaro));

  rep(i, m) {
    int tmp;
    scanf("%d", &tmp);
    sumhana += tmp;
    vhana[i] = tmp;
  }
  sort(all(vhana));

  double sa = sumtaro - sumhana; //????¨??????????
  rep(i, n) {
    rep(j, m) {
      if (vtaro[i] - vhana[j] == (sa / 2)) {
        printf("%d %d\n", vtaro[i], vhana[j]);
        return;
      }
    }
  }
  printf("-1\n");
  return;
}

int main(void) {
  int n, m;
  rep(i, 1000) {
    cin >> n >> m;

    if (n == 0)
      break;
    slove(n, m);
  }
  return 0;
}
Problem p00734 (C++)

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

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

    int t[n], h[n];
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    int sumt = 0, sumh = 0;
    for (int i = 0; i < n; i++)
      sumt += t[i];
    for (int i = 0; i < m; i++)
      sumh += h[i];

    int x, y, min = 1000;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - t[i] + h[j] == sumh + t[i] - h[j]) {
          if (t[i] + h[j] < min) {
            min = t[i] + h[j];
            x = t[i];
            y = h[j];
          }
        }
      }
    }

    if (min == 1000)
      cout << -1 << endl;
    else
      cout << x << ' ' << y << endl;
  }
  return 0;
}

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

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

    int t[n], h[m];
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    int sumt = 0, sumh = 0;
    for (int i = 0; i < n; i++)
      sumt += t[i];
    for (int i = 0; i < m; i++)
      sumh += h[i];

    int x, y, min = 1000;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - t[i] + h[j] == sumh + t[i] - h[j]) {
          if (t[i] + h[j] < min) {
            min = t[i] + h[j];
            x = t[i];
            y = h[j];
          }
        }
      }
    }

    if (min == 1000)
      cout << -1 << endl;
    else
      cout << x << ' ' << y << endl;
  }
  return 0;
}