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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define rp(a) while (a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
/*namespace std {
        bool operator < (const int& a, const int& b) {
                return
                //return real(a) != real(b) ? real(a) < real(b) : imag(a) <
imag(b);
                //return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) <
real(b);
        }
}*/
bool in[5001][5001];
int main() {
  int n;
  while (cin >> n, n) {
    memset(in, 0, sizeof(in));
    vector<int> x(n);
    vector<int> y(n);
    rep(i, n) {
      cin >> x[i] >> y[i];
      in[x[i]][y[i]] = true;
    }
    rep(i, n) rep(j, n) if (x[i] < x[j]) {
      swap(x[i], x[j]);
      swap(y[i], y[j]);
    }
    else if (x[i] == x[j] && y[i] < y[j]) swap(y[i], y[j]);

    int out = 0;
    rep(i, n) {
      loop(j, i + 1, n) {
        if (x[i] + y[j] - y[i] < 5001 && x[j] + y[j] - y[i] < 5001 &&
            y[i] + x[i] - x[j] < 5001 && y[j] + x[i] - x[j] < 5001 &&
            x[i] + y[j] - y[i] >= 0 && x[j] + y[j] - y[i] >= 0 &&
            y[i] + x[i] - x[j] >= 0 && y[j] + x[i] - x[j] >= 0 &&
            in[x[i] + y[j] - y[i]][y[i] + x[i] - x[j]] &&
            in[x[j] + y[j] - y[i]][y[j] + x[i] - x[j]])
          out = max(out, (x[i] - x[j]) * (x[i] - x[j]) +
                             (y[i] - y[j]) * (y[i] - y[j]));
      }
    }
    cout << out << endl;
  }
  // rep(i,n)cout<<x[i]<<" "<<y[i]<<endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define rp(a) while (a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
/*namespace std {
        bool operator < (const int& a, const int& b) {
                return
                //return real(a) != real(b) ? real(a) < real(b) : imag(a) <
imag(b);
                //return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) <
real(b);
        }
}*/
int main() {
  int n;
  while (cin >> n, n) {
    map<int, map<int, bool>> in;
    vector<int> x(n);
    vector<int> y(n);
    rep(i, n) {
      cin >> x[i] >> y[i];
      in[x[i]][y[i]] = true;
    }
    rep(i, n) rep(j, n) if (x[i] < x[j]) {
      swap(x[i], x[j]);
      swap(y[i], y[j]);
    }
    else if (x[i] == x[j] && y[i] < y[j]) swap(y[i], y[j]);

    int out = 0;
    rep(i, n) {
      loop(j, i + 1, n) {
        if (in[x[i] + y[j] - y[i]][y[i] + x[i] - x[j]] &&
            in[x[j] + y[j] - y[i]][y[j] + x[i] - x[j]])
          out = max(out, (x[i] - x[j]) * (x[i] - x[j]) +
                             (y[i] - y[j]) * (y[i] - y[j]));
      }
    }
    cout << out << endl;
  }
  // rep(i,n)cout<<x[i]<<" "<<y[i]<<endl;
}