Problem p00441 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define INF 1e+9;
using namespace std;

int main() {
  int n, ma;
  int x[3000], y[3000];
  vector<int> pos;
  while (cin >> n, n) {
    pos.clear();
    ma = 0;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      pos.push_back(x[i] * 10000 + y[i]);
    }
    sort(pos.begin(), pos.end());
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        int dx = x[j] - x[i], dy = y[j] - y[i];
        int kx = x[j] - dy, ky = y[j] + dx;
        int lx = kx - dx, ly = ky - dy;
        if (kx >= 0 && ky >= 0 && lx >= 0 && ly >= 0) {
          if (upper_bound(pos.begin(), pos.end(), kx * 10000 + ky) -
                      lower_bound(pos.begin(), pos.end(), kx * 10000 + ky) ==
                  1 &&
              upper_bound(pos.begin(), pos.end(), lx * 10000 + ly) -
                      lower_bound(pos.begin(), pos.end(), lx * 10000 + ly) ==
                  1) {
            ma = max(ma, dx * dx + dy * dy);
          }
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 2e+18;
using namespace std;

int main() {
  int n;
  int hasira[3000][2];
  static long long int
      key[3000 * 3000]; // aaaaabbbbbiiiijjjj,x1-x2+5000,y1-y2+5000
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> hasira[i][0] >> hasira[i][1];
    }
    for (int i = 0; i < n * n; i++)
      key[i] = INF;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        key[i * n + j] =
            (long long int)(hasira[j][0] - hasira[i][0] + 5000) *
                10000000000000 +
            (long long int)(hasira[j][1] - hasira[i][1] + 5000) * 100000000 +
            (long long int)i * 10000 + (long long int)j;
        // cout << key[i * n + j] << " " << i *n +  j << endl;
      }
    }
    sort(key, key + n * n);
    int ma = -1, low, up;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          low = lower_bound(
                    key, key + n * n,
                    (long long int)(hasira[j][0] - hasira[i][0] + 5000) *
                            10000000000000 +
                        (long long int)(hasira[j][1] - hasira[i][1] + 5000) *
                            100000000) -
                key;
          up = lower_bound(
                   key, key + n * n,
                   (long long int)(hasira[j][0] - hasira[i][0] + 5000) *
                           10000000000000 +
                       (long long int)(hasira[j][1] - hasira[i][1] + 5001) *
                           100000000) -
               key;
          // cout << key[low] << " " <<i <<  " " << j<< endl;
          int i2 = key[low] / 10000 % 10000, j2 = key[low] % 10000;
          if (up - low == 0 ||
              (up - low == 1 && ((i2 == j && j2 == i) || (i2 == i && j2 == j))))
            continue;
          int x = hasira[j][0] - hasira[i][0], y = hasira[j][1] - hasira[i][1];
          if (abs(hasira[i2][0] - hasira[i][0]) != abs(y) ||
              abs(hasira[i2][1] - hasira[i][1]) != abs(x))
            continue;
          ma = max(ma, x * x + y * y);
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}