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

#include <bits/stdc++.h>

using namespace std;

struct C {
  int w, d;
  C(int w, int d) : w(w), d(d) {}
  bool operator<(const C &c) const { return w * d < c.w * c.d; }
};

int main() {
  int N, W, D;
  while (cin >> N >> W >> D && (N | W | D)) {
    vector<C> cs;
    cs.push_back(C(W, D));
    while (N--) {
      int p, s;
      cin >> p >> s;
      p--;
      C c = cs[p];
      cs.erase(cs.begin() + p);
      s %= (c.w + c.d) * 2;
      int rem = s, ang = -1;
      for (int i = 0; i < 2; i++) {
        ang++;
        rem -= c.w;
        if (rem < 0)
          break;
        ang++;
        rem -= c.d;
        if (rem < 0)
          break;
      }

      rem = -rem;

      if (ang == 0 || ang == 2) {
        int a = rem, b = c.w - a;
        if (b < a)
          swap(a, b);
        cs.push_back(C(a, c.d));
        cs.push_back(C(b, c.d));
      } else {
        int a = rem, b = c.d - a;
        if (b < a)
          swap(a, b);
        cs.push_back(C(c.w, a));
        cs.push_back(C(c.w, b));
      }
    }
    sort(cs.begin(), cs.end());
    for (int i = 0; i < cs.size(); i++) {
      if (i)
        cout << ' ';
      cout << cs[i].w * cs[i].d;
    }
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct C {
  int w, d;
  C(int w, int d) : w(w), d(d) {}
  bool operator<(const C &c) const { return w * d < c.w * c.d; }
};

int main() {
  int N, W, D;
  while (cin >> N >> W >> D && (N | W | D)) {
    vector<C> cs;
    cs.push_back(C(W, D));
    while (N--) {
      int p, s;
      cin >> p >> s;
      p--;
      C c = cs[p];
      cs.erase(cs.begin() + p);
      s %= (c.w + c.d) * 2;
      int rem = s, ang = -1;
      for (int i = 0; i < 2; i++) {
        ang++;
        if (ang == 0 || ang == 2) {
          rem -= c.w;
        }
        if (rem < 0)
          break;
        ang++;
        if (ang == 1 || ang == 2) {
          rem -= c.d;
        }
        if (rem < 0)
          break;
      }

      rem = -rem;

      if (ang == 0 || ang == 2) {
        int a = rem, b = c.w - a;
        if (b < a)
          swap(a, b);
        cs.push_back(C(a, c.d));
        cs.push_back(C(b, c.d));
      } else {
        int a = rem, b = c.d - a;
        if (b < a)
          swap(a, b);
        cs.push_back(C(c.w, a));
        cs.push_back(C(c.w, b));
      }
    }
    sort(cs.begin(), cs.end());
    for (int i = 0; i < cs.size(); i++) {
      if (i)
        cout << ' ';
      cout << cs[i].w * cs[i].d;
    }
    cout << endl;
  }
  return 0;
}
Problem p00731 (C++) — buggy submission, verdict: Runtime Error

#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;
}

Error: 0

Fixed submission:

#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;
}
Problem p00734 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>

using namespace std;

int main(void) {
  int S[2][100];
  pair<int, int> min;
  for (int n[2]; cin >> n[0] >> n[1], n[0] || n[1];) {
    int diff = 0, sum = 0;
    bool mdf = false;
    min = make_pair(100, 100);

    for (int i = 0; i < 2; i++)
      for (int j = 0; j < n[i]; j++) {
        cin >> S[i][j];
        diff += i == 0 ? S[i][j] : -S[i][j];
      }

    for (int i = 0; i < n[0]; i++) {
      int j;
      for (j = 0; j < n[1]; j++)
        if (diff == (S[0][i] - S[1][j]) * 2 &&
            S[0][i] + S[1][j] < min.first + min.second) {
          min = make_pair(S[0][i], S[1][j]);
          mdf = true;
        }
    }

    if (mdf)
      cout << min.first << " " << min.second << endl;
    else
      cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {

  int val;

  while (cin >> val)

    cerr << val << " ";

  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: 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)
typedef pair<int, int> P;
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    int sum = 0;
    vector<int> p(n), q(m);
    rep(i, n) {
      cin >> p[i];
      sum += p[i];
    }
    rep(i, m) {
      cin >> q[i];
      sum -= q[i];
    }
    int ap = INF, aq = INF;
    for (int pn : p)
      for (int qn : q)
        if (2 * (pn - qn) == sum && pn + qn < ap + aq) {
          ap = pn;
          aq = qn;
        }
    if (ap == INF) {
      puts("-1");
    } else {
      cout << ap << ' ' << aq << endl;
    }
  }
  return 0;
}

Error: 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)
typedef pair<int, int> P;
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    int sum = 0;
    vector<int> p(n), q(n);
    rep(i, n) {
      cin >> p[i];
      sum += p[i];
    }
    rep(i, m) {
      cin >> q[i];
      sum -= q[i];
    }
    int ap = INF, aq = INF;
    for (int pn : p)
      for (int qn : q)
        if (2 * (pn - qn) == sum && pn + qn < ap + aq) {
          ap = pn;
          aq = qn;
        }
    if (ap == INF) {
      puts("-1");
    } else {
      cout << ap << ' ' << aq << endl;
    }
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n + m) {
    int a[n], b[m];
    REP(i, n) scanf("%d", a + i);
    REP(i, m) scanf("%d", b + i);

    int suma = accumulate(a, a + n, 0);
    int sumb = accumulate(b, b + m, 0);
    int diff = suma - sumb;

    int ansa = -1;
    int ansb = -1;
    REP(i, n) REP(j, m) {
      if (diff == 2 * (a[i] - b[j])) {
        if ((ansa == -1) || (ansa + ansb > a[i] + b[j])) {
          ansa = a[i];
          ansb = b[j];
        }
      }
    }

    if (ansa == -1)
      puts("-1");
    else
      printf("%d %d\\n", ansa, ansb);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define M 110

int main() {
  int n;
  while (cin >> n, n) {
    int b[M][M] = {{0}};
    int w, h, s, t;
    vector<pair<int, int>> v(n);
    cin >> w >> h;
    for (int i = 0; i < n; i++)
      cin >> v[i].first >> v[i].second;
    cin >> s >> t;
    for (int i = 0; i < n; i++) {
      int x = v[i].first, y = v[i].second;
      for (int xx = 0; xx < s && x - xx >= 1; xx++) {
        for (int yy = 0; yy < t && y - yy >= 1; yy++) {
          b[y - yy][x - xx]++;
        }
      }
    }
    int ans = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        ans = max(ans, b[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: 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[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;
  }
}

Error: 0

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[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;
  }
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define cinf(n, x)                                                             \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> x[i];
typedef long long int ll;
using namespace std;

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    vector<int> a(n), b(m);
    if (n == 0 && m == 0)
      break;
    cinf(n, a);
    cinf(m, b);
    int suma = 0;
    int sumb = 0;
    for (int i = 0; i < n; ++i)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];
    int ansa = 150;
    int ansb = 150;
    if (suma > sumb) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (suma - sumb == (a[i] - b[j]) * 2 && b[j] + a[i] < ansa + ansb) {
            ansa = a[i];
            ansb = b[j];
          }
        }
      }
    } else if (suma == sumb) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (a[i] == b[j] && ansa > a[i]) {
            ansa = a[i];
            ansb = b[j];
          }
        }
      }
    } else {
      for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
          if (sumb - suma == (b[j] - a[i]) * 2 && b[j] + a[i] < ansa + ansb) {
            ansa = a[i];
            ansb = b[j];
          }
        }
      }
    }
    if (ansa == 150)
      cout << -1 << endl;
    else
      cout << ansa << " " << ansb << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define cinf(n, x)                                                             \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> x[i];
typedef long long int ll;
using namespace std;

int main() {
  int n, m;
  vector<int> a(n), b(m);
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cinf(n, a);
    cinf(m, b);
    int suma = 0;
    int sumb = 0;
    for (int i = 0; i < n; ++i)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];
    int ansa = 150;
    int ansb = 150;
    if (suma > sumb) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (suma - sumb == (-b[j] + a[i]) * 2 && b[j] + a[i] < ansa + ansb)
            ansa = a[i];
          ansb = b[j];
        }
      }
    } else if (suma == sumb) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (a[i] == b[j] && ansa > a[i]) {
            ansa = a[i];
            ansb = b[j];
          }
        }
      }
    } else {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (sumb - suma == (b[j] - a[i]) * 2 && b[j] + a[i] < ansa + ansb)
            ansa = a[i];
          ansb = b[j];
        }
      }
    }
    if (ansa == 150)
      cout << -1 << endl;
    else
      cout << ansa << " " << ansb << endl;
  }
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

int main() {
  int n, m, mi, mih, mit;
  while (cin >> n >> m && n && m) {
    vector<int> taro(n);
    vector<int> hanako(m);
    int t = 0, h = 0;
    for (int i = 0; i < n; i++) {
      cin >> taro[i];
      t += taro[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hanako[i];
      h += hanako[i];
    }
    mi = (1 << 21);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (t - taro[i] + hanako[j] == h - hanako[j] + taro[i]) {
          if (taro[i] + hanako[j] < mi) {
            mi = taro[i] + hanako[j];
            mit = taro[i];
            mih = hanako[j];
          }
        }
      }
    }
    if (mi == 1 << 21)
      cout << -1 << endl;
    else
      cout << mit << " " << mih << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

int main() {
  int n, m, mi, mih, mit;
  while (cin >> n >> m && n && m) {
    vector<int> taro(n);
    vector<int> hanako(n);
    int t = 0, h = 0;
    for (int i = 0; i < n; i++) {
      cin >> taro[i];
      t += taro[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hanako[i];
      h += hanako[i];
    }
    mi = (1 << 21);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (t - taro[i] + hanako[j] == h - hanako[j] + taro[i]) {
          if (taro[i] + hanako[j] < mi) {
            mi = taro[i] + hanako[j];
            mit = taro[i];
            mih = hanako[j];
          }
        }
      }
    }
    if (mi == 1 << 21)
      cout << -1 << endl;
    else
      cout << mit << " " << mih << endl;
  }
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int d, m, n, t[101], h[101];
  while (1) {
    int st = 0, sh = 0, mint = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    for (int i = 0; i < n; i++)
      st += t[i];
    for (int i = 0; i < m; i++)
      sh += h[i];
    d = st - sh;
    if (d % 2 != 0) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (t[i] - h[j] == d / 2 && t[i] < mint)
          mint = t[i];
      }
    if (mint < 101)
      cout << mint << " " << mint - d / 2 << endl;
    else
      cout << -1 << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int d, m, n, t[51], h[51];
  while (1) {
    int st = 0, sh = 0, mint = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    for (int i = 0; i < n; i++)
      st += t[i];
    for (int i = 0; i < m; i++)
      sh += h[i];
    d = st - sh;
    if (d % 2 != 0) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (t[i] - h[j] == d / 2 && t[i] < mint)
          mint = t[i];
      }
    if (mint < 101)
      cout << mint << " " << mint - d / 2 << endl;
    else
      cout << -1 << endl;
  }
}
Problem p00734 (C++) — buggy submission, verdict: 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[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;
}

Error: 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[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;
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>

using namespace std;
#define max(a, b) (a < b ? a : b)

int main() {

  int fi[100][2] = {0};
  int fiPos = 0;

  while (1) {

    int n, m;
    int tr[100] = {0};
    int hnk[100] = {0};
    int trSum = 0, hnkSum = 0;

    cin >> n >> m;

    if (!n && !m)
      break;

    for (int i = 0; i < n + m; i++) {
      if (i < n) {
        cin >> tr[i];
        trSum += tr[i];
      } else {
        cin >> hnk[i - n];
        hnkSum += hnk[i - n];
      }
    }

    int max = 1000;
    bool ANS = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (trSum == hnkSum + (tr[i] * 2) - (hnk[j] * 2)) {
          int tMax = max;
          max = max(max, tr[i] + hnk[j]);
          if (max != tMax) {
            fi[fiPos][0] = tr[i];
            fi[fiPos][1] = hnk[j];
            ANS = true;
          }
        }
      }
    }

    if (ANS == 0) {
      fi[fiPos][0] = -1;
    }
    fiPos++;
  }

  for (int i = 0; i < fiPos; i++) {
    if (fi[i][0] == -1)
      cout << -1 << endl;
    else
      cout << fi[i][0] << " " << fi[i][1] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;
#define max(a, b) (a < b ? a : b)

int main() {

  int fi[100][2] = {0};
  int fiPos = 0;

  while (1) {

    int ans[100][2] = {0};
    int ansPos = 0;

    int n, m;
    int tr[100] = {0};
    int hnk[100] = {0};
    int trSum = 0, hnkSum = 0;

    cin >> n >> m;

    if (!n && !m)
      break;

    for (int i = 0; i < n + m; i++) {
      if (i < n) {
        cin >> tr[i];
        trSum += tr[i];
      } else {
        cin >> hnk[i - n];
        hnkSum += hnk[i - n];
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (trSum == hnkSum + (tr[i] * 2) - (hnk[j] * 2)) {
          ans[ansPos][0] = tr[i];
          ans[ansPos++][1] = hnk[j];
        }
      }
    }

    if (ansPos == 0) {
      fi[fiPos++][0] = -1;
    } else {
      int ANS = 0;
      if (ansPos > 1) {
        int max = 1000;
        for (int i = 0; i < ansPos; i++) {
          int tMax = max;
          max = max(max, ans[i][0] + ans[i][1]);
          if (max != tMax) {
            ANS = i;
          }
        }
      }

      fi[fiPos][0] = ans[ANS][0];
      fi[fiPos++][1] = ans[ANS][1];
    }
  }

  for (int i = 0; i < fiPos; i++) {
    if (fi[i][0] == -1)
      cout << -1 << endl;
    else
      cout << fi[i][0] << " " << fi[i][1] << endl;
  }

  return 0;
}