Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define ft first
#define sc second

int n;
int x[4000], y[4000];
bool s[5010][5010];
bool input() {
  REP(i, 5010)
  REP(j, 5010)
  s[i][j] = false;

  scanf("%d", &n);
  REP(i, n) {
    scanf("%d%d", x + i, y + i);
    s[x[i]][y[i]] = true;
  }
  return n != 0;
}

typedef pair<int, int> P;
typedef pair<P, P> L;
#define EPS (1e-10)

void solve() {
  int maxi = -1, maxj = -1;
  REP(i, n) {
    FOR(j, i + 1, n) {
      int a = x[i] + x[j];
      int b = x[i] - x[j];
      int c = y[i] + y[j];
      int d = y[j] - y[i];

      if (((a + d) % 2) * ((b + c) % 2))
        continue;
      if (s[(a + d) / 2][(b + c) / 2] && s[(a - d) / 2][(-b + c) / 2]) {
        if (maxi == -1 ||
            (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) >
                (x[maxi] - x[maxj]) * (x[maxi] - x[maxj]) +
                    (y[maxi] - y[maxj]) * (y[maxi] - y[maxj])) {
          maxi = i;
          maxj = j;
        }
      }
    }
  }

  if (maxi == -1)
    printf("0\n");
  else
    printf("%d\n", ((x[maxi] - x[maxj]) * (x[maxi] - x[maxj]) +
                    (y[maxi] - y[maxj]) * (y[maxi] - y[maxj])) /
                       2);
}

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

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define ft first
#define sc second

int n;
int x[4000], y[4000];
bool s[5010][5010];
bool input() {
  REP(i, 5010)
  REP(j, 5010)
  s[i][j] = false;

  scanf("%d", &n);
  REP(i, n) {
    scanf("%d%d", x + i, y + i);
    s[x[i]][y[i]] = true;
  }
  return n != 0;
}

typedef pair<int, int> P;
typedef pair<P, P> L;
#define EPS (1e-10)

void solve() {
  int maxi = -1, maxj = -1;
  REP(i, n) {
    FOR(j, i + 1, n) {
      int a = x[i] + x[j];
      int b = x[i] - x[j];
      int c = y[i] + y[j];
      int d = y[j] - y[i];

      if (((a + d) % 2) * ((b + c) % 2))
        continue;
      if (!(0 <= (a + d) / 2 && (a + d) / 2 < 5001 && 0 <= (b + c) / 2 &&
            (b + c) / 2 < 5001)) {
        continue;
      }
      if (!(0 <= (a - d) / 2 && (a - d) / 2 < 5001 && 0 <= (-b + c) / 2 &&
            (-b + c) / 2 < 5001)) {
        continue;
      }
      if (s[(a + d) / 2][(b + c) / 2] && s[(a - d) / 2][(-b + c) / 2]) {
        if (maxi == -1 ||
            (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) >
                (x[maxi] - x[maxj]) * (x[maxi] - x[maxj]) +
                    (y[maxi] - y[maxj]) * (y[maxi] - y[maxj])) {
          maxi = i;
          maxj = j;
        }
      }
    }
  }

  if (maxi == -1)
    printf("0\n");
  else
    printf("%d\n", ((x[maxi] - x[maxj]) * (x[maxi] - x[maxj]) +
                    (y[maxi] - y[maxj]) * (y[maxi] - y[maxj])) /
                       2);
}

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