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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{1, 0}, {0, 1}};

signed main() {
  int n;
  while (scanf("%d", &n)) {
    if (!n)
      break;

    set<pii> m;
    vector<pii> st(n);
    REP(i, n) {
      scanf("%d %d", &st[i].first, &st[i].second);
      m.insert(st[i]);
    }
    sort(ALL(st));
    int ans = 0;
    REP(i, n) {
      rep(j, i + 1, n) {
        pii sa = {st[j].first - st[i].first, st[j].second - st[i].second};
        bool f = 0;
        pii nex = {st[j].first + sa.second, st[j].second - sa.first};
        pii nexx = {nex.first - sa.first, nex.second - sa.second};
        if (m.find({nex.first, nex.second}) != m.end() &&
            m.find({nexx.first, nexx.second}) != m.end()) {
          ans = max(ans, PW(sa.first) + PW(sa.second));
        }
      }
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{1, 0}, {0, 1}};

signed main() {
  int n;
  while (scanf("%d", &n)) {
    if (!n)
      break;

    map<pii, bool> m;
    vector<pii> st(n);
    REP(i, n) {
      scanf("%d %d", &st[i].first, &st[i].second);
      m[st[i]] = 1;
    }
    sort(ALL(st));
    ll ans = 0;
    REP(i, n) {
      rep(j, i + 1, n) {
        pii sa = {st[j].first - st[i].first, st[j].second - st[i].second};
        bool f = 0;
        pii nex = {st[j].first + sa.second, st[j].second - sa.first};
        pii nexx = {nex.first - sa.first, nex.second - sa.second};
        if (m[{nex.first, nex.second}] && m[{nexx.first, nexx.second}]) {
          ans = max(ans, PW((ll)sa.first) + PW((ll)sa.second));
        }
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}