alexjercan/bugnet huggingface dataset:
Problem p00734 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, m, a[110], b[110];
int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }
    int abc = 200;
    int amin, bmin;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        swap(a[i], b[j]);
        int sum = 0;
        int sum2 = 0;
        for (int k = 0; k < n; k++)
          sum += a[k];
        for (int k = 0; k < m; k++)
          sum2 += b[k];
        swap(a[i], b[j]);
        if (sum == sum2 && abs(a[i] - b[j]) < abc) {
          abc = abs(a[i] - b[j]);
          amin = a[i];
          bmin = b[j];
        }
      }
    }
    if (abc != 200)
      cout << amin << " " << bmin << endl;
    else
      cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, m, a[110], b[110];
int sum = 0, sum2 = 0;
int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
      sum2 += b[i];
    }
    sort(a, n);
    sort(b, m);
  outer:
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (abs(a[0] - b[m - 1]) < abs(sum - sum2) / 2)
          cout << -1 << endl;
        goto outer;
        if (b[j] - a[i] == abs(sum - sum2) / 2) {
          cout << a[i] << b[j] << endl;
          goto outer;
        }
        if (i == n - 1 || j == m - 1) {
          cout << -1 << endl;
          goto outer;
        }
      }
    }
  }
}
Problem p00734 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

#define FOR(i, init, n) for (int i = init; i < n; i++)
#define REP(i, n) FOR(i, 0, n)

int main() {
  while (1) {
    int t_cardnum, h_cardnum;
    int t_card[100] = {0}, h_card[100] = {0};
    int t_sum = 0, h_sum = 0;
    scanf("%d %d", &t_cardnum, &h_cardnum);

    if (t_cardnum == 0 && h_cardnum == 0) {
      break;
    }

    int i, j;
    for (i = 0; i < t_cardnum; i++) {
      scanf("%d", &t_card[i]);
      t_sum += t_card[i];
    }
    for (i = 0; i < h_cardnum; i++) {
      scanf("%d", &h_card[i]);
      h_sum += h_card[i];
    }

    if ((t_sum + h_sum) % 2 == 1) {
      printf("%d\\n", -1);
      continue;
    }

    int diff;
    diff = t_sum - h_sum;

    int point, sum = 1000, t_num, h_num;
    for (i = 0; i < t_cardnum; i++) {
      for (j = 0; j < h_cardnum; j++) {
        point = t_card[i] - h_card[j];
        if (point == (diff / 2)) {
          if (sum > t_card[i] + h_card[j]) {
            t_num = i;
            h_num = j;
            sum = t_card[i] + h_card[j];
          }
        }
      }
    }

    if (sum == 1000) {
      printf("%d\\n", -1);
    } else {
      printf("%d %d\\n", t_card[t_num], h_card[h_num]);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

#define FOR(i, init, n) for (int i = init; i < n; i++)
#define REP(i, n) FOR(i, 0, n)

int main() {
  while (1) {
    int t_cardnum, h_cardnum;
    int t_card[100], h_card[100];
    int t_sum = 0, h_sum = 0;
    scanf("%d %d", &t_cardnum, &h_cardnum);
    int i, j;
    for (i = 0; i < t_cardnum; i++) {
      scanf("%d", &t_card[i]);
      t_sum += t_card[i];
    }
    for (i = 0; i < h_cardnum; i++) {
      scanf("%d", &h_card[i]);
      h_sum += h_card[i];
    }

    if ((t_sum + h_sum) % 2 == 1) {
      printf("-1");
    }

    int diff;
    diff = t_sum - h_sum;

    int point, sum = 100000, t_num, h_num;
    for (i = 0; i < t_cardnum; i++) {
      for (j = 0; j < h_cardnum; j++) {
        point = t_card[i] - h_card[j];
        if (point == (diff / 2)) {
          if (sum > t_card[i] + h_card[j]) {
            t_num = i;
            h_num = j;
            sum = t_card[i] + h_card[j];
          }
        }
      }
    }

    printf("%d %d\\n", t_card[t_num], h_card[h_num]);
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "cmath"
#include "iomanip"
#include "iostream"
#include "map"
#include "string"
using namespace std;

#define TEST 0
#if TEST
FILE *in = freopen("./Q/question.txt", "r", stdin);
FILE *out = freopen("./A/answer.txt", "w", stdout);
#endif

int main() {
  int n, m;
  while (true) {
    int tarousum = 0, hanakosum = 0, ans = 1000, idxi = -1, idxj = -1;
    cin >> n >> m;
    int tarous[n], hanakos[m];
    if (!n && !m)
      break;
    for (int i = 0; i < n + m; ++i) {
      int a;
      cin >> a;
      if (i < n) {
        tarous[i] = a;
        tarousum += a;
      } else {
        hanakos[i - n] = a;
        hanakosum += a;
      }
    }

    // cout <<tarousum <<" "<<hanakosum <<endl;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (tarousum <= hanakosum) {
          if (hanakos[j] >= tarous[i]) {
            if ((tarousum + hanakos[j] - tarous[i]) ==
                (hanakosum + tarous[i] - hanakos[j])) {
              if (ans > hanakos[j] + tarous[i]) {
                ans = hanakos[j] + tarous[i];
                idxi = i;
                idxj = j;
              }
            }
          }
        } else {
          if (hanakos[j] < tarous[i]) {
            if ((tarousum + hanakos[j] - tarous[i]) ==
                (hanakosum + tarous[i] - hanakos[j])) {
              if (ans > hanakos[j] + tarous[i]) {
                ans = hanakos[j] + tarous[i];
                idxi = i;
                idxj = j;
              }
            }
          }
        }
      }
    }
    if (ans == 1000)
      cout << -1 << endl;
    else
      cout << tarous[idxi] << " " << hanakos[idxj] << endl;
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "cmath"
#include "iomanip"
#include "iostream"
#include "map"
#include "string"
using namespace std;

#define TEST 1
#if TEST
FILE *in = freopen("./Q/question.txt", "r", stdin);
FILE *out = freopen("./A/answer.txt", "w", stdout);
#endif

int main() {
  int n, m;
  while (true) {
    int tarousum = 0, hanakosum = 0, ans = 1000, idxi = -1, idxj = -1;
    cin >> n >> m;
    int tarous[n], hanakos[m];
    if (!n && !m)
      break;
    for (int i = 0; i < n + m; ++i) {
      int a;
      cin >> a;
      if (i < n) {
        tarous[i] = a;
        tarousum += a;
      } else {
        hanakos[i - n] = a;
        hanakosum += a;
      }
    }

    // cout <<tarousum <<" "<<hanakosum <<endl;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if ((tarousum + hanakos[j] - tarous[i]) ==
            (hanakosum + tarous[i] - hanakos[j])) {
          if (ans > hanakos[j] + tarous[i]) {
            ans = hanakos[j] + tarous[i];
            idxi = i;
            idxj = j;
          }
        }
      }
    }
    if (ans == 1000)
      cout << -1 << endl;
    else
      cout << tarous[idxi] << " " << hanakos[idxj] << endl;
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int i, j, temp = 0, k, count = 0;
    int t[n], h[m], sum1 = 0, sum2 = 0;
    int d[10000], e[10000];
    bool cando = false;
    for (i = 0; i < n; i++) {
      cin >> t[i];
    }
    for (i = 0; i < m; i++) {
      cin >> h[i];
    }

    sort(t, t + n);
    sort(h, h + m);

    for (i = 0; i < n; i++) {
      sum1 += t[i];
    }
    for (i = 0; i < m; i++) {
      sum2 += h[i];
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (sum1 - t[i] + h[j] == sum2 - h[j] + t[i]) {
          cando = true;
          d[count] = t[i];
          e[count] = h[j];
          count++;
        }
      }
    }
    if (!cando) {
      cout << "-1" << endl;
    } else if (cando) {
      for (i = 0; i < count; i++) {
        for (j = i + 1; j < count; j++) {
          if (d[i] + e[i] > d[j] + e[j]) {
            temp = d[i];
            d[i] = d[i + 1];
            d[i + 1] = temp;

            temp = e[i];
            e[i] = e[i + 1];
            e[i + 1] = temp;
          }
        }
      }
      cout << d[0] << " " << e[0] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int i, j, temp = 0, k, count = 0;
    int t[n], h[m], sum1 = 0, sum2 = 0;
    int d[100], e[100];
    bool cando = false;
    for (i = 0; i < n; i++) {
      cin >> t[i];
    }
    for (i = 0; i < m; i++) {
      cin >> h[i];
    }

    sort(t, t + n);
    sort(h, h + m);

    for (i = 0; i < n; i++) {
      sum1 += t[i];
    }
    for (i = 0; i < m; i++) {
      sum2 += h[i];
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (sum1 - t[i] + h[j] == sum2 - h[j] + t[i]) {
          cando = true;
          d[count] = t[i];
          e[count] = h[j];
          count++;
        }
      }
    }
    if (!cando) {
      cout << "-1" << endl;
    } else if (cando) {
      for (i = 0; i < count; i++) {
        for (j = i + 1; j < count; j++) {
          if (d[i] + e[i] > d[j] + e[j]) {
            temp = d[i];
            d[i] = d[i + 1];
            d[i + 1] = temp;

            temp = e[i];
            e[i] = e[i + 1];
            e[i + 1] = temp;
          }
        }
      }
      cout << d[0] << " " << e[0] << endl;
    }
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    int sum1 = 0, sum2 = 0;
    int s1[100], s2[100];
    int a = 1e8, b = 1e8;

    for (int i = 0; i < n; i++)
      cin >> s1[i], sum1 += s1[i];
    for (int i = 0; i < m; i++)
      cin >> s2[i], sum2 += s2[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((s1[i] - s2[j]) * 2 == sum1 - sum2 && a + b > s1[i] + s2[j])
          a = s1[i], b = s2[j];
      }
    }

    if (a == 1e8)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    int sum1 = 0, sum2 = 0;
    int s1[50], s2[50];
    int a = 1e8, b = 1e8;

    for (int i = 0; i < n; i++)
      cin >> s1[i], sum1 += s1[i];
    for (int i = 0; i < m; i++)
      cin >> s2[i], sum2 += s2[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((s1[i] - s2[j]) * 2 == sum1 - sum2 && a + b > s1[i] + s2[j])
          a = s1[i], b = s2[j];
      }
    }

    if (a == 1e8)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

int main(void) {
  int n, m;
  int A[105];
  int B[105];
  int ans[2];
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    ans[0] = 0;
    ans[1] = 0;
    for (int i = 0; i < n; i++)
      cin >> A[i];
    for (int i = 0; i < m; i++)
      cin >> B[i];

    int sum1 = 0;
    int sum2 = 0;

    for (int i = 0; i < n; i++)
      sum1 += A[i];
    for (int i = 0; i < m; i++)
      sum2 += B[i];

    int min = 1000000;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sum1 - A[i] + B[j] == sum2 - B[j] + A[i] && min > A[i] + B[j]) {
          ans[0] = A[i];
          ans[1] = B[j];
          min = A[i] + B[j];
        }
      }
    }

    if (ans[0] == 0)
      cout << -1 << endl;
    else
      cout << ans[0] << " " << ans[1] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

    vector<int> A(n, 0);
    vector<int> B(n, 0);
    vector<int> ans(2, 0);

    for (int i = 0; i < n; i++)
      cin >> A[i];
    for (int i = 0; i < m; i++)
      cin >> B[i];

    int sum1 = 0;
    int sum2 = 0;

    for (int i = 0; i < n; i++)
      sum1 += A[i];
    for (int i = 0; i < m; i++)
      sum2 += B[i];

    int min = 1000000;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sum1 - A[i] + B[j] == sum2 - B[j] + A[i] && min > A[i] + B[j]) {
          ans[0] = A[i];
          ans[1] = B[j];
          min = A[i] + B[j];
        }
      }
    }

    if (ans[0] == 0)
      cout << -1 << endl;
    else
      cout << ans[0] << " " << ans[1] << endl;
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Time Limit Exceeded


#include "iostream"
using namespace std;
int main() {
  while (1) {
    int n, m, t[100], h[100], sumt = 0, sumh = 0, mint = 101, minh = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      sumt += t[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h[i];
      sumh += h[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - t[i] + h[j] == sumh - h[j] + t[i]) {
          if (h[j] + t[i] < mint + minh) {
            mint = t[i];
            minh = h[j];
          }
        }
      }
    }
    if (mint == 101 && minh == 101)
      cout << "-1" << endl;
    else
      cout << mint << " " << minh << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include "iostream"
using namespace std;
int main() {
  while (1) {
    int n, m, t[100], h[100], sumt = 0, sumh = 0, mint = 101, minh = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      sumt += t[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h[i];
      sumh += h[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; i++) {
        if (sumt - t[i] + h[j] == sumh - h[j] + t[i]) {
          if (h[j] + t[i] < mint + minh) {
            mint = t[i];
            minh = h[j];
          }
        }
      }
    }
    if (mint == 101 && minh == 101)
      cout << "-1" << endl;
    else
      cout << mint << " " << minh << endl;
  }
  return 0;
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n, m;

  while (true) {
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    // ??????????????????
    int suma = 0, sumb = 0;
    for (int i = 0; i < n; i++)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];

    vector<pair<int, int>> res;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if ((suma - a[i] + b[j]) == (sumb - b[j] + a[i])) {
          res.push_back(make_pair(i, j));
        }

    if (res.size() == 0)
      cout << -1 << endl;
    else {
      int tmpa, tmpb, r = 999999999, tmp;
      for (int i = 0; i < res.size(); i++) {
        tmp = a[res[i].first] + b[res[i].second];
        if (tmp < r) {
          r = tmp;
          tmpa = res[i].first;
          tmpb = res[i].second;
        }
      }
      cout << a[tmpa] << " " << b[tmpb] << endl;
    }
  }
}

Error: 0

Fixed submission:

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

using namespace std;

int main() {
  int n, m;

  while (true) {
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(n);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    // ??????????????????
    int suma = 0, sumb = 0;
    for (int i = 0; i < n; i++)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];

    vector<pair<int, int>> res;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if ((suma - a[i] + b[j]) == (sumb - b[j] + a[i])) {
          res.push_back(make_pair(i, j));
        }

    if (res.size() == 0)
      cout << -1 << endl;
    else {
      int tmpa, tmpb, r = 999999999, tmp;
      for (int i = 0; i < res.size(); i++) {
        tmp = a[res[i].first] + b[res[i].second];
        if (tmp < r) {
          r = tmp;
          tmpa = res[i].first;
          tmpb = res[i].second;
        }
      }
      cout << a[tmpa] << " " << b[tmpb] << endl;
    }
  }
}
Problem p00734 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

#include <algorithm>
#include <climits>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> taro(n);
    REP(i, n) { cin >> taro[i]; }
    vector<int> hanako(m);
    REP(i, m) { cin >> hanako[i]; }

    pair<int, int> ans(INT_MAX, INT_MAX);
    int taroSum = accumulate(ALL(taro), 0);
    int hanakoSum = accumulate(ALL(hanako), 0);
    REP(i, n) {
      REP(j, m) {
        if (taroSum - taro[i] + hanako[j] == hanakoSum - hanako[j] + taro[i]) {
          ans = min(ans, pair<int, int>(taro[i], hanako[j]));
        }
      }
    }
    if (ans.first == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << ans.first << " " << ans.second << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> taro(n);
    int taroSum;
    int hanakoSum;
    REP(i, n) {
      cin >> taro[i];
      taroSum += taro[i];
    }
    vector<int> hanako(n);
    REP(i, m) {
      cin >> hanako[i];
      hanakoSum += hanako[i];
    }

    pair<int, int> ans(INT_MAX, INT_MAX);
    REP(i, n) {
      REP(j, m) {
        if (taroSum - taro[i] + hanako[j] == hanakoSum - hanako[j] + taro[i]) {
          ans = min(ans, pair<int, int>(taro[i], hanako[j]));
        }
      }
    }
    if (ans.first == INT_MAX) {
      ans.first = ans.second = -1;
    }
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}