prithivMLmods/Coder-Stat huggingface dataset:
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, b, c[100000] = {0}, d = 0, e = 0, max = 0;
    cin >> a >> b;
    if (a == 0 && b == 0)
      break;
    for (int A = 0; A < a; A++) {
      cin >> c[A];
      if (A >= b) {
        for (int B = 0; B < b; B++) {
          d = d + c[A + e];
          e--;
        }
        if (max < d)
          max = d;
        d = 0;
      }
    }
    cout << max << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, b, c[100000] = {0}, d = 0, e = 0, max = 0;
    cin >> a >> b;
    if (a == 0 && b == 0)
      break;
    for (int A = 0; A < a; A++) {
      cin >> c[A];
      if (A >= b) {
        for (int B = 0; B < b; B++) {
          d = d + c[A + e];
          e--;
        }
        if (max < d)
          max = d;
        d = 0;
        e = 0;
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    int sum = 0;
    int ans = 0;
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int num;
      scanf("%d", &num);

      sum += num;
      if (i == k - 1)
        ans = sum;
      if (i >= k) {
        sum -= q.front();
        q.pop();
        ans = max(ans, sum);
      }
      q.push(num);
    }

    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (!n && !k)
      break;

    int sum = 0;
    int ans = 0;
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int num;
      scanf("%d", &num);

      sum += num;
      if (i == k - 1)
        ans = sum;
      if (i >= k) {
        sum -= q.front();
        q.pop();
        ans = max(ans, sum);
      }
      q.push(num);
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main(void) {
  int n, m, a;

  while (cin >> n >> m, n | m) {
    int d[10001] = {};
    cin >> d[0];

    for (int i = 1; i < n; i++) {
      cin >> a;
      d[i] += d[i - 1] + a;
    }
    int MAX = 0;
    for (int i = 0; i < n - m; i++) {
      if (d[i + m] - d[i] > MAX)
        MAX = d[i + m] - d[i];
    }
    cout << MAX << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main(void) {
  int n, m, a;

  while (cin >> n >> m, n | m) {
    int d[100001] = {};
    cin >> d[0];

    for (int i = 1; i < n; i++) {
      cin >> a;
      d[i] += d[i - 1] + a;
    }
    int MAX = 0;
    for (int i = 0; i < n - m; i++) {
      if (d[i + m] - d[i] > MAX)
        MAX = d[i + m] - d[i];
    }
    cout << MAX << endl;
  }

  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#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 PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    ll a[10001];
    REP(i, n) { cin >> a[i]; }
    REP(i, n - 1) { a[i + 1] += a[i]; }
    ll ans = -1000000000000000;
    FOR(i, k, n) { ans = max(ans, a[i] - a[i - k]); }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#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 PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    ll a[100001];
    REP(i, n) { cin >> a[i]; }
    REP(i, n - 1) { a[i + 1] += a[i]; }
    ll ans = -1000000000000000;
    FOR(i, k, n) { ans = max(ans, a[i] - a[i - k]); }
    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
int main() {
  int n, k;
  int s = 0;
  int a[10000] = {0};
  for (;;) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int x = 0;
    for (int i = 0; i < k; i++) {
      x += a[i];
    }
    s = x;
    for (int i = 0; i < (n - k); i++) {
      x -= a[i];
      x += a[i + k];
      if (s < x)
        s = x;
    }
    printf("%d\n", s);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
int main() {
  int n, k;
  int s = 0;
  int a[100000] = {0};
  for (;;) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int x = 0;
    for (int i = 0; i < k; i++) {
      x += a[i];
    }
    s = x;
    for (int i = 0; i < (n - k); i++) {
      x -= a[i];
      x += a[i + k];
      if (s < x)
        s = x;
    }
    printf("%d\n", s);
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
int main() {
  int n, k;
  while (1) {
    scanf("%d %d", &n, &k);
    if (n == 0)
      return 0;
    int a[n];
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);
    ;
    ll bgs = 0;
    for (int i = 0; i <= n - k; i++) {
      ll temp = 0;
      for (int j = 0; j < k; j++)
        temp += a[i + j];
      bgs = max(bgs, temp);
    }
    printf("%lld\n", bgs);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
int main() {
  int n, k;
  while (1) {
    scanf("%d %d", &n, &k);
    if (n == 0)
      return 0;
    int a[n];
    for (int i = 0; i < n; i++)
      scanf("%d", &a[i]);
    ll bgs = 0, temp = 0;
    for (int i = 0; i < k; i++)
      temp += a[i];
    bgs = temp;
    for (int i = 1; i <= n - k; i++) {
      temp = temp - a[i - 1] + a[i + k - 1];
      bgs = max(bgs, temp);
    }
    printf("%lld\n", bgs);
  }
}
Problem p00440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define _3_ true
using namespace std;

int main() {
  int n, k, index, ini;
  int in[100005];

  bool ZeroE;
  while (_3_) {
    scanf("%d %d", &n, &k);
    if (!(n | k))
      break;
    ZeroE = false;
    index = 0;

    for (int i = 0; i < k; i++) {
      scanf("%d", &ini);
      if (ini == 0)
        ZeroE = true;
      else {
        in[index] = ini;
        index++;
      }
    }

    sort(in, in + index);

    int ans = 0;
    int count = 1;
    bool used = false;
    for (int i = 1; i < index; i++) {
      // cout << "i = " << i << ", cout = " << count  << ", in[i] = " << in[i]<<
      // endl;
      if (in[i] == in[i - 1] + 1)
        count++;
      else {
        if (ZeroE && !used && in[i] == in[i - 1] + 2) {
          count += 2;
          used = true;
        } else if (ZeroE && used && in[i] == in[i - 1] + 2) {
          ans = max(ans, count);
          count = 3;
        } else {
          ans = max(ans, count);
          count = 1;
          used = false;
        }
      }
      if (in[i] == in[i - 1] + 1 && i == index - 1 && !used && ZeroE)
        count++;
    }
    if (index == 1 && ZeroE)
      count = 2;

    ans = max(ans, count);

    cout << ans << endl;
  }
  return 434;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define _3_ true
using namespace std;

int main() {
  int n, k, index, ini;
  int in[100005];

  bool ZeroE;
  while (_3_) {
    scanf("%d %d", &n, &k);
    if (!(n | k))
      break;
    ZeroE = false;
    index = 0;

    for (int i = 0; i < k; i++) {
      scanf("%d", &ini);
      if (ini == 0)
        ZeroE = true;
      else {
        in[index] = ini;
        index++;
      }
    }

    sort(in, in + index);

    int ans = 0;
    int count = 1;
    bool used = false;
    for (int i = 1; i < index; i++) {
      // cout << "i = " << i << ", cout = " << count  << ", in[i] = " << in[i]<<
      // endl;
      if (in[i] == in[i - 1] + 1)
        count++;
      else {
        if (ZeroE && !used && in[i] == in[i - 1] + 2) {
          count += 2;
          used = true;
        } else if (ZeroE && used && in[i] == in[i - 1] + 2) {
          ans = max(ans, count);
          count = 3;
        } else {
          ans = max(ans, count);
          count = 1;
          used = false;
        }
      }
      if (in[i] == in[i - 1] + 1 && i == index - 1 && !used && ZeroE)
        count++;
    }
    if (index == 1 && ZeroE)
      count = 2;

    ans = max(ans, count);

    cout << ans << endl;
  }
  return 0;
}
Problem p00440 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (!n && !m)
      break;
    bool card[10001] = {false};
    for (int i = 0; i < m; i++) {
      int a;
      cin >> a;
      card[a] = true;
    }

    int ma = 1;
    for (int i = 1; i <= n; i++) {
      int sum = 0;
      bool flag = card[0];
      int j = i;
      while (j <= n) {
        if (card[j] == true || flag) {
          sum++;
          if (card[j] == false && flag == true)
            flag = false;
          j++;
        } else
          break;
      }
      ma = max(ma, sum);
    }
    cout << ma << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (!n && !m)
      break;
    bool card[100001] = {false};
    for (int i = 0; i < m; i++) {
      int a;
      cin >> a;
      card[a] = true;
    }

    int ma = 1;
    for (int i = 1; i <= n; i++) {
      int sum = 0;
      bool flag = card[0];
      int j = i;
      while (j <= n) {
        if (card[j] == true || flag) {
          sum++;
          if (card[j] == false && flag == true)
            flag = false;
          j++;
        } else
          break;
      }
      ma = max(ma, sum);
    }
    cout << ma << endl;
  }
  return 0;
}
Problem p00440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;
  while (1) {
    cin >> n >> k;
    int data[1003];
    for (int i = 0; i < n + 3; i++) {
      data[i] = 0;
    }
    int all = 0, ans = 0;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      int card;
      cin >> card;
      if (card == 0)
        all++;
      else
        data[card] = 1;
    }
    for (int i = 1; i <= n; i++) {
      int tmp = all;
      int cnt = 0;
      for (int j = i; j <= n; j++) {
        if (data[j] == 1)
          cnt++;
        else if (tmp != 0) {
          cnt++;
          tmp--;
        } else
          break;
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;
  while (1) {
    cin >> n >> k;
    int data[100300];
    for (int i = 0; i < n + 3; i++) {
      data[i] = 0;
    }
    int all = 0, ans = 0;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      int card;
      cin >> card;
      if (card == 0)
        all++;
      else
        data[card] = 1;
    }
    for (int i = 1; i <= n; i++) {
      int tmp = all;
      int cnt = 0;
      for (int j = i; j <= n; j++) {
        if (data[j] == 1)
          cnt++;
        else if (tmp != 0) {
          cnt++;
          tmp--;
        } else
          break;
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
}
Problem p00440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[200000] = {}, sam, b[100000] = {}, roop = 0,
    save[100000] = {0}, ans[100000] = {}, w = 0;
int main(void) {
  while (1) {
    int n = 0, k = 0, a[100000] = {}, sam, b[1000] = {}, roop = 0,
        save[1000] = {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 || k == 0)
      return 0;
    for (int i = 0; i < k; i++) {
      cin >> a[i];
    }

    sort(a, a + k);
    for (int i = 0; i < k; i++) {
      if (a[i + 1] - a[i] == 1) {
        sam++;
      } else if (a[i + 1] - a[i] != 1) {
        b[roop] = sam + 1;
        if (a[0] == 0) {
          if (a[i + 1] - a[i] == 2) {
            save[w] = roop;
            w++;
          }
        }
        sam = 0;
        roop++;
      }
    }
    for (int i = 0; i <= roop; i++) {
      if (save[i] != 0) {
        ans[i] = b[save[i]];
        ans[i] += b[save[i] + 1] + 1;
      }
    }
    sort(b, b + roop + 1);
    sort(ans, ans + roop + 1);
    if (ans[roop] > b[roop])
      cout << ans[roop] << endl;
    if (ans[roop] <= b[roop]) {
      if (a[0] == 0) {
        cout << b[roop] + 1 << endl;
      } else {
        cout << b[roop] << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[200000] = {}, sam, b[100000] = {}, roop = 0,
    save[100000] = {0}, ans[100000] = {}, w = 0;
int main(void) {
  while (1) {
    // int n=0, k=0, a[100000] = {}, sam, b[1000] = {}, roop = 0, save[1000] =
    // {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 || k == 0)
      return 0;
    for (int i = 0; i < k; i++) {
      cin >> a[i];
    }

    sort(a, a + k);
    for (int i = 0; i < k; i++) {
      if (a[i + 1] - a[i] == 1) {
        sam++;
      } else if (a[i + 1] - a[i] != 1) {
        b[roop] = sam + 1;
        if (a[0] == 0) {
          if (a[i + 1] - a[i] == 2) {
            save[w] = roop;
            w++;
          }
        }
        sam = 0;
        roop++;
      }
    }
    for (int i = 0; i <= roop; i++) {
      if (save[i] != 0) {
        ans[i] = b[save[i]];
        ans[i] += b[save[i] + 1] + 1;
      }
    }
    sort(b, b + roop + 1);
    sort(ans, ans + roop + 1);
    if (ans[roop] > b[roop])
      cout << ans[roop] << endl;
    if (ans[roop] <= b[roop]) {
      if (a[0] == 0) {
        cout << b[roop] + 1 << endl;
      } else {
        cout << b[roop] << endl;
      }
    }
  }
  return 0;
}
Problem p00440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int card[100000];
bool shiro = false;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    shiro = false;
    for (int i = 0; i < 100000; i++) {
      card[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int u;
      cin >> u;
      if (u != 0) {
        card[u - 1]++;
      } else {
        shiro = true;
      }
    }
    if (shiro == false) {
      int ans = 0;
      int poyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo);
          poyo = 0;
        }
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      int poyo = 0;
      int piyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo + piyo + 1);
          piyo = poyo;
          poyo = 0;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int card[100000];
bool shiro = false;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    shiro = false;
    for (int i = 0; i < 100000; i++) {
      card[i] = 0;
    }
    for (int i = 0; i < m; i++) {
      int u;
      cin >> u;
      if (u != 0) {
        card[u - 1]++;
      } else {
        shiro = true;
      }
    }
    if (shiro == false) {
      int ans = 0;
      int poyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo);
          poyo = 0;
        }
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      int poyo = 0;
      int piyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo + piyo + 1);
          piyo = poyo;
          poyo = 0;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    vector<int> oks(N + 1);
    bool flag = false;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      oks[a] = true;
      if (!a)
        flag = true;
    }
    int a = 0;
    int b = 0;
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
      if (oks[i]) {
        a++;
        b++;
        ans = max(ans, a);
        if (flag)
          ans = max(ans, b);
      } else {
        b = a + 1;
        a = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    if (!N)
      break;
    vector<int> oks(N + 1);
    bool flag = false;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      oks[a] = true;
      if (!a)
        flag = true;
    }
    int a = 0;
    int b = 0;
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
      if (oks[i]) {
        a++;
        b++;
        ans = max(ans, a);
        if (flag)
          ans = max(ans, b);
      } else {
        b = a + 1;
        a = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;

  while (cin >> n >> k, n || k) {
    int C[10002] = {0};
    int dp[10002] = {0};

    for (int i = 0; i < k; i++) {
      int j;
      cin >> j;
      C[j] = 1;
    }
    if (C[1] == 1) {
      dp[1] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (C[i] == 1) {
        if (dp[i - 1] > 0) {
          dp[i] = dp[i - 1] + 1;
        } else {
          dp[i] = 1;
        }
      }
    }
    for (int i = n; i > 0; i--) {
      if (C[i] == 1) {
        if (dp[i + 1] > 0) {
          dp[i] = dp[i + 1];
        }
      }
    }
    int ans = 0;
    if (C[0] == 1) {
      for (int i = 2; i <= n; i++) {
        ans = max(ans, dp[i]);
        if (C[i] == 0) {
          ans = max(ans, dp[i - 1] + dp[i + 1] + 1);
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;

  while (cin >> n >> k, n || k) {
    int C[100002] = {0};
    int dp[100002] = {0};

    for (int i = 0; i < k; i++) {
      int j;
      cin >> j;
      C[j] = 1;
    }
    if (C[1] == 1) {
      dp[1] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (C[i] == 1) {
        if (dp[i - 1] > 0) {
          dp[i] = dp[i - 1] + 1;
        } else {
          dp[i] = 1;
        }
      }
    }
    for (int i = n; i > 0; i--) {
      if (C[i] == 1) {
        if (dp[i + 1] > 0) {
          dp[i] = dp[i + 1];
        }
      }
    }
    int ans = 0;
    if (C[0] == 1) {
      for (int i = 2; i <= n; i++) {
        ans = max(ans, dp[i]);
        if (C[i] == 0) {
          ans = max(ans, dp[i - 1] + dp[i + 1] + 1);
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
      }
    }
    cout << ans << endl;
  }
}
Problem p00440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
bool used[100001] = {};
int num[100001];
int main() {
  while (1) {
    fill(used, used + 100001, false);
    scanf("%d %d", &n, &k);
    for (int i = 0; i < k; i++) {
      scanf("%d", &num[i]);
      used[num[i]] = true;
    }
    sort(num, num + k);
    int s = 0, e = 0, sum = 0, res = 0;
    if (used[0]) {
      for (;;) {
        while (e <= n && sum <= 1) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 1) {
          res = max(res, e - s);
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    } else {
      e = 1;
      s = 1;
      for (;;) {
        while (e <= n && sum <= 0) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 0) {
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    }
    printf("%d\n", res - 1);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
bool used[100001] = {};
int num[100001];
int main() {
  while (1) {
    fill(used, used + 100001, false);
    scanf("%d %d", &n, &k);
    if (!n)
      break;
    for (int i = 0; i < k; i++) {
      scanf("%d", &num[i]);
      used[num[i]] = true;
    }
    sort(num, num + k);
    int s = 0, e = 0, sum = 0, res = 0;
    if (used[0]) {
      for (;;) {
        while (e <= n && sum <= 1) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 1) {
          res = max(res, e - s);
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    } else {
      e = 1;
      s = 1;
      for (;;) {
        while (e <= n && sum <= 0) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 0) {
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    }
    printf("%d\n", res - 1);
  }
}
Problem p00441 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int exist[5001][5001];

inline bool check(int x, int y) {
  if (x < 0 || y < 0 || x > 5000 || y > 5000)
    return false;
  return exist[x][y];
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(exist, 0, sizeof(exist));
    vector<int> x(n), y(n);

    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      exist[x[i]][y[i]] = 1;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int dx = x[i] - x[j], dy = y[i] - y[j];

        if (check(x[i] + dy, y[i] - dx) && check(x[j] + dy, y[j] - dx)) {
          ans = max(ans, dx * dx + dy * dy);
        }
        if (check(x[i] - dy, y[i] + dx) && check(x[j] - dy, y[j] + dx)) {
          ans = max(ans, dx * dx + dy * dy);
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool exist[5001][5001];

inline bool check(int x, int y) {
  if (x < 0 || y < 0 || x > 5000 || y > 5000)
    return false;
  return exist[x][y];
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(exist, 0, sizeof(exist));
    vector<int> x(n), y(n);

    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      exist[x[i]][y[i]] = 1;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int dx = x[i] - x[j], dy = y[i] - y[j];

        if (check(x[i] + dy, y[i] - dx) && check(x[j] + dy, y[j] - dx)) {
          ans = max(ans, dx * dx + dy * dy);
        }
        if (check(x[i] - dy, y[i] + dx) && check(x[j] - dy, y[j] + dx)) {
          ans = max(ans, dx * dx + dy * dy);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00441 (C++)

Buggy submission (status: Memory Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

map<P, bool> mp;
P pos[5000];

int main() {
  int n;
  while (cin >> n && n) {
    int res = 0;
    mp.clear();
    rep(i, n) {
      cin >> pos[i].first >> pos[i].second;
      mp[pos[i]] = true;
    }
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      int dx = pos[i].first - pos[j].first;
      int dy = pos[i].second - pos[j].second;

      int x2 = pos[j].first + dy;
      int y2 = pos[j].second - dx;
      int x3 = x2 + dx;
      int y3 = y2 + dy;

      if (mp[P(x2, y2)] && mp[P(x3, y3)]) {
        res = max(res, dx * dx + dy * dy);
      }
    }
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

map<P, bool> mp;
P pos[5000];

int main() {
  int n;
  while (cin >> n && n) {
    int res = 0;
    mp.clear();
    rep(i, n) {
      cin >> pos[i].first >> pos[i].second;
      mp[pos[i]] = true;
    }
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      int dx = pos[i].first - pos[j].first;
      int dy = pos[i].second - pos[j].second;

      int x2 = pos[j].first + dy;
      int y2 = pos[j].second - dx;
      int x3 = x2 + dx;
      int y3 = y2 + dy;

      if (mp.find(P(x2, y2)) != mp.end() && mp.find(P(x3, y3)) != mp.end()) {
        res = max(res, dx * dx + dy * dy);
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAX_N = 3000;
const int MAX_XY = 5000;
int n;
int a[MAX_N][2];
bool b[MAX_XY][MAX_XY];

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < MAX_N; i++)
      for (int j = 0; j < 2; j++)
        a[i][j] = 0;
    for (int i = 0; i < MAX_XY; i++)
      for (int j = 0; j < MAX_XY; j++)
        b[i][j] = false;

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a[i][0], &a[i][1]);
      b[a[i][0]][a[i][1]] = true;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int dx, dy;
        dx = a[j][0] - a[i][0];
        dy = a[j][1] - a[i][1];
        for (int k = 0; k < 2; k++) {
          int nx, ny;
          nx = a[i][0] + (2 * k - 1) * dy;
          ny = a[i][1] - (2 * k - 1) * dx;
          if (0 <= nx && nx <= MAX_XY && 0 <= ny && ny <= MAX_XY) {
            if (b[nx][ny] == true && b[nx + dx][ny + dy] == true) {
              ans = max(ans, dx * dx + dy * dy);
            }
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAX_N = 3000;
const int MAX_XY = 5000;
int n;
int a[MAX_N][2];
bool b[MAX_XY][MAX_XY];

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < MAX_N; i++)
      for (int j = 0; j < 2; j++)
        a[i][j] = 0;
    for (int i = 0; i < MAX_XY; i++)
      for (int j = 0; j < MAX_XY; j++)
        b[i][j] = false;

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &a[i][0], &a[i][1]);
      b[a[i][0]][a[i][1]] = true;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int dx, dy;
        dx = a[j][0] - a[i][0];
        dy = a[j][1] - a[i][1];
        for (int k = 0; k < 2; k++) {
          int nx, ny;
          nx = a[i][0] + (2 * k - 1) * dy;
          ny = a[i][1] - (2 * k - 1) * dx;
          if (0 <= nx && nx <= MAX_XY && 0 <= ny && ny <= MAX_XY) {
            if (0 <= nx + dx && nx + dx <= MAX_XY && 0 <= ny + dy &&
                ny + dy <= MAX_XY) {
              if (b[nx][ny] == true && b[nx + dx][ny + dy] == true) {
                ans = max(ans, dx * dx + dy * dy);
              }
            }
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
bool p[5001][5001];
int x[3000], y[3000], n, maxn;
int main() {
  while (true) {
    cin >> n;
    maxn = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      p[x[i]][y[i]] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        int X1 = x[j] - x[i], Y1 = y[j] - y[i];
        int X2 = -Y1, Y2 = X1;
        int X3 = X1 - Y1, Y3 = X1 + Y1;
        if (p[X2 + x[i]][Y2 + y[i]] == true &&
            p[X3 + x[i]][Y3 + y[i]] == true) {
          maxn = max(maxn, X1 * X1 + Y1 * Y1);
        }
      }
    }
    cout << maxn << endl;
    for (int i = 0; i < n; i++) {
      p[x[i]][y[i]] = true;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
bool p[5001][5001];
int x[3000], y[3000], n, maxn;
int main() {
  while (true) {
    cin >> n;
    maxn = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      p[x[i]][y[i]] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        int X1 = x[j] - x[i], Y1 = y[j] - y[i];
        int X2 = -Y1, Y2 = X1;
        int X3 = X1 - Y1, Y3 = X1 + Y1;
        int X4 = X2 + x[i], Y4 = Y2 + y[i];
        int X5 = X3 + x[i], Y5 = Y3 + y[i];
        if (X4 < 0 || X4 > 5000 || Y4 < 0 || Y4 > 5000 || X5 < 0 || X5 > 5000 ||
            Y5 < 0 || Y5 > 5000)
          continue;
        if (p[X4][Y4] == true && p[X5][Y5] == true) {
          maxn = max(maxn, X1 * X1 + Y1 * Y1);
        }
      }
    }
    cout << maxn << endl;
    for (int i = 0; i < n; i++) {
      p[x[i]][y[i]] = true;
    }
  }
  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int n, x[3009], y[3009];
bool ok[5009][5009];
int main() {
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &x[i], &y[i]);
      ok[x[i]][y[i]] = true;
    }
    int ret = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int va = x[j] - x[i], vb = y[j] - y[i];
        int ex = x[i] + vb, ey = y[i] - va;
        int fx = x[j] + vb, fy = y[j] - va;
        if (0 <= ex && ex <= 5000 && 0 <= fy && fy <= 5000 && ok[ex][ey] &&
            ok[fx][fy]) {
          ret = max(ret, va * va + vb * vb);
        }
      }
    }
    printf("%d\n", ret);
    for (int i = 0; i < n; i++)
      ok[x[i]][y[i]] = false;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int n, x[3009], y[3009];
bool ok[5009][5009];
int main() {
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &x[i], &y[i]);
      ok[x[i]][y[i]] = true;
    }
    int ret = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int va = x[j] - x[i], vb = y[j] - y[i];
        int ex = x[i] + vb, ey = y[i] - va;
        int fx = x[j] + vb, fy = y[j] - va;
        if (0 <= ex && ex <= 5000 && 0 <= ey && ey <= 5000 && 0 <= fx &&
            fx <= 5000 && 0 <= fy && fy <= 5000 && ok[ex][ey] && ok[fx][fy]) {
          ret = max(ret, va * va + vb * vb);
        }
      }
    }
    printf("%d\n", ret);
    for (int i = 0; i < n; i++)
      ok[x[i]][y[i]] = false;
  }
  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n;
bool p[5001][5001];
bool c(int a) { return 0 <= a && a <= 5000; }
int main() {
  while (cin >> n && n) {
    int x[5001], y[5001], an = 0;
    for (int i = 0; i < n; i++)
      cin >> x[i] >> y[i], p[x[i]][y[i]] = 1;
    for (int i = 0; i < n; i++)
      for (int j = i; j < n; j++) {
        int dx = x[i] - x[j];
        int dy = y[i] - y[j];
        int x1 = x[i] - dy;
        int x2 = x[j] - dy;
        int y1 = y[i] + dx;
        int y2 = y[j] + dx;
        int x3 = x[i] - dy, y3 = y[i] + dx, x4 = x[j] + dy, y4 = y[j] - dx;
        if (c(x1) && c(y1) && p[x1][y1] && c(x2) && c(y2) && p[x2][y2])
          an = max(an, dx * dx + dy * dy);
        if (p[x3][y3] && p[x4][y4] && c(x3) && c(y3) && c(x4) && c(y4))
          an = max(an, dx * dx + dy * dy);
      }
    cout << an << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
bool p[5001][5001];
bool c(int a) { return 0 <= a && a <= 5000; }
int main() {
  while (cin >> n && n) {
    int x[5001], y[5001], an = 0;
    for (int i = 0; i < n; i++)
      cin >> x[i] >> y[i], p[x[i]][y[i]] = 1;
    for (int i = 0; i < n; i++)
      for (int j = i; j < n; j++) {
        int dx = x[i] - x[j];
        int dy = y[i] - y[j];
        int x1 = x[i] - dy;
        int x2 = x[j] - dy;
        int y1 = y[i] + dx;
        int y2 = y[j] + dx;
        int x3 = x[i] - dy, y3 = y[i] + dx, x4 = x[j] + dy, y4 = y[j] - dx;
        if (c(x1) && c(y1) && p[x1][y1] && c(x2) && c(y2) && p[x2][y2])
          an = max(an, dx * dx + dy * dy);
        if (c(x3) && c(y3) && p[x3][y3] && c(x4) && c(y4) && p[x4][y4])
          an = max(an, dx * dx + dy * dy);
      }
    cout << an << endl;
  }
  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> P;
int n;
P st[3000];
int main() {
  while (scanf("%d", &n)) {
    if (!n)
      break;
    set<P> stat;
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &st[i].first, &st[i].second);
      stat.insert(st[i]);
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
      P a = st[i];
      for (int j = 0; j < n; j++) {
        P b = st[j];
        int dist1 = b.first - a.first, dist2 = a.second - b.second;
        //                printf("%d %d %d %d:%d %d:%d %d %d
        //                %d\n",a.first,a.second,b.first,b.second,dist1,dist2,a.first-dist2,a.second-dist1,b.first-dist2,b.second-dist1);
        int r1 = stat.count(P(b.first - dist2, b.second - dist1));
        int r2 = stat.count(P(a.first - dist2, a.second - dist1));
        //                printf("%d %d\n",r1,r2);
        if (r1 && r2) {
          res = max(res, dist1 * dist1 + dist2 * dist2);
        }
      }
    }
    printf("%d\n", res);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> P;
int n;
P st[3000];
int main() {
  while (scanf("%d", &n)) {
    if (!n)
      break;
    set<P> stat;
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &st[i].first, &st[i].second);
      stat.insert(st[i]);
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
      P a = st[i];
      for (int j = 0; j < n; j++) {
        P b = st[j];
        int dist1 = b.first - a.first, dist2 = a.second - b.second;
        if (stat.find(P(b.first - dist2, b.second - dist1)) != stat.end() &&
            stat.find(P(a.first - dist2, a.second - dist1)) != stat.end()) {
          res = max(res, dist1 * dist1 + dist2 * dist2);
        }
      }
    }
    printf("%d\n", res);
  }
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

inline bool isRange(pii &p1) { return 0 <= p1.first && p1.second <= 5000; }

inline double dis(pii &p1, pii &p2) {
  double a = abs(p1.first - p2.first), b = abs(p1.second - p2.second);
  return sqrt(a * a + b * b);
}

bool exist[5005][5005] = {false};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;

  while (cin >> n, n) {
    pii ps[3005];
    fill(exist[0], exist[5005], false);
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      ps[i] = pii(x, y);
      exist[x][y] = true;
    }
    sort(ps, ps + n);

    int res = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (i == j)
          continue;
        for (int k = -1; k < 2; k += 2) {
          int dx = k * abs(ps[i].second - ps[j].second),
              dy = k * abs(ps[i].first - ps[j].first);
          pii temp[2] = {pii(ps[i].first + dx, ps[i].second + dy),
                         pii(ps[j].first + dx, ps[j].second + dy)};

          if (!isRange(temp[0]) || !isRange(temp[1]) ||
              !exist[temp[0].first][temp[0].second] ||
              !exist[temp[1].first][temp[1].second])
            continue;

          double seg = dis(ps[i], ps[j]);
          res = max(res, (long long)(seg * seg + 0.00000001));
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

inline bool isRange(pii &p1) {
  return 0 <= p1.first && p1.first <= 5000 && 0 <= p1.second &&
         p1.second <= 5000;
}

inline double dis(pii &p1, pii &p2) {
  double a = abs(p1.first - p2.first), b = abs(p1.second - p2.second);
  return sqrt(a * a + b * b);
}

bool exist[5005][5005] = {false};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;

  while (cin >> n, n) {
    pii ps[3005];
    fill(exist[0], exist[5005], false);
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      ps[i] = pii(x, y);
      exist[x][y] = true;
    }
    sort(ps, ps + n);

    int res = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (i == j)
          continue;
        for (int k = -1; k < 2; k += 2) {
          int dx = k * abs(ps[i].second - ps[j].second),
              dy = k * abs(ps[i].first - ps[j].first);
          pii temp[2] = {pii(ps[i].first + dx, ps[i].second + dy),
                         pii(ps[j].first + dx, ps[j].second + dy)};

          if (!isRange(temp[0]) || !isRange(temp[1]) ||
              !exist[temp[0].first][temp[0].second] ||
              !exist[temp[1].first][temp[1].second])
            continue;

          double seg = dis(ps[i], ps[j]);
          res = max(res, (long long)(seg * seg + 0.00000001));
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
int x[3000], y[3000];
int mp[5001][5001];
int m, dx, dy, s;

int exists(int x, int y) {
  return 0 <= x && x <= 5000 && 0 <= y && y <= 5000 && mp[x][y] == 1;
}

int main(void) {
  while (cin >> n, n != 0) {
    REP(i, 0, 5001) {
      REP(j, 0, 5001) { mp[i][j] = 0; }
    }
    REP(i, 0, n) {
      cin >> x[i] >> y[i];
      mp[x[i]][y[i]] = 1;
    }

    m = 0;
    REP(i, 0, n) {
      REP(j, i + 1, n) {
        dx = x[i] - x[j];
        dy = y[i] - y[j];

        if (exists(x[i] + dy, y[i] - dx) && exists(x[j] + dy, y[j] - dx)) {
          s = dx * dx + dy * dy;
          if (m < s)
            m = s;
        }
        if (exists(x[i] - dy, y[i] + dx) && exists(x[j] - dy, y[j] + dx)) {
          s = dx * dx + dy * dy;
          if (m < s)
            m = s;
        }
      }
    }

    cout << m << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
int x[3000], y[3000];
bool mp[5001][5001];
int m, dx, dy, s;

int exists(int x, int y) {
  return 0 <= x && x <= 5000 && 0 <= y && y <= 5000 && mp[x][y] == 1;
}

int main(void) {
  while (cin >> n, n != 0) {
    REP(i, 0, 5001) {
      REP(j, 0, 5001) { mp[i][j] = 0; }
    }
    REP(i, 0, n) {
      cin >> x[i] >> y[i];
      mp[x[i]][y[i]] = 1;
    }

    m = 0;
    REP(i, 0, n) {
      REP(j, i + 1, n) {
        dx = x[i] - x[j];
        dy = y[i] - y[j];

        if (exists(x[i] + dy, y[i] - dx) && exists(x[j] + dy, y[j] - dx)) {
          s = dx * dx + dy * dy;
          if (m < s)
            m = s;
        }
        if (exists(x[i] - dy, y[i] + dx) && exists(x[j] - dy, y[j] + dx)) {
          s = dx * dx + dy * dy;
          if (m < s)
            m = s;
        }
      }
    }

    cout << m << endl;
  }

  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int x[3000], y[3000];
bool f[5001][5001];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    memset(f, 0, sizeof(f));

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x[i], &y[i]);
      f[x[i]][y[i]] = true;
    }

    int maxi = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j < n; j++) {
        const int X = abs(x[i] - x[j]);
        const int Y = abs(y[i] - y[j]);

        if (f[x[i] + Y][y[i] + X] && f[x[j] + Y][y[j] + X]) {
          maxi = max(maxi, X * X + Y * Y);
        }
      }
    }

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

  return 0;
}

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

int x[3000], y[3000];
bool f[5001][5001];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    memset(f, 0, sizeof(f));

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x[i], &y[i]);
      f[x[i]][y[i]] = true;
    }

    int maxi = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j < n; j++) {
        const int X = abs(x[i] - x[j]);
        const int Y = abs(y[i] - y[j]);

        if (x[i] + Y <= 5000 && y[i] + X <= 5000 && x[j] + Y <= 5000 &&
            y[j] + X <= 5000) {
          if (f[x[i] + Y][y[i] + X] && f[x[j] + Y][y[j] + X]) {
            maxi = max(maxi, X * X + Y * Y);
          }
        }
      }
    }

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

  return 0;
}
Problem p00441 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>

#define N 3000
#define S 5001
int px[N], py[N];
int n;
int pillar[S][S];

void init() {
  int i, j;
  for (i = 0; i < S; i++) {
    for (j = 0; j < S; j++) {
      pillar[i][j] = 0;
    }
  }
  return;
}

int input() {
  int i, x, y;
  init();
  std::cin >> n;
  for (i = 0; i < n; i++) {
    std::cin >> x >> y;
    pillar[x][y] = 1;
    px[i] = x;
    py[i] = y;
  }
  return n;
}

int func() {
  int area = 0, i, j, dx, dy, p3x, p3y, p4x, p4y;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (i == j) {
        continue;
      }
      // std::cout << i << "," << j << std::endl;
      dx = px[i] - px[j];
      dy = py[i] - py[j];
      p3x = px[j] + dy;
      p3y = py[j] - dx;
      p4x = px[i] + dy;
      p4y = py[i] - dx;
      // std::cout << px[i] << "," << py[i] << ":" << px[j] << "," << py[j] <<
      // ":"; std::cout << p3x << "," << p3y << ":" << p4x << "," << p4y <<
      // std::endl;
      if (p3x < 0 || p3y < 0 || p4x < 0 || p4y < 0) {
        continue;
      }
      if (p3x >= S || p3y >= S || p4x >= S || p4y >= S) {
        continue;
      }
      if (pillar[p3x][p3y] == 1 && pillar[p4x][p4y] == 1) {
        int s = dx * dx + dy * dy;
        area = (s > area ? s : area);
      }
    }
  }
  return area;
}

int main() {
  while (input() > 0) {
    std::cout << func() << std::endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

#define N 3000
#define S 5001
int px[N], py[N];
int n;
char pillar[S][S];

void init() {
  int i, j;
  for (i = 0; i < S; i++) {
    for (j = 0; j < S; j++) {
      pillar[i][j] = 0;
    }
  }
  return;
}

int input() {
  int i, x, y;
  init();
  std::cin >> n;
  for (i = 0; i < n; i++) {
    std::cin >> x >> y;
    pillar[x][y] = 1;
    px[i] = x;
    py[i] = y;
  }
  return n;
}

int func() {
  int area = 0, i, j, dx, dy, p3x, p3y, p4x, p4y;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (i == j) {
        continue;
      }
      // std::cout << i << "," << j << std::endl;
      dx = px[i] - px[j];
      dy = py[i] - py[j];
      p3x = px[j] + dy;
      p3y = py[j] - dx;
      p4x = px[i] + dy;
      p4y = py[i] - dx;
      // std::cout << px[i] << "," << py[i] << ":" << px[j] << "," << py[j] <<
      // ":"; std::cout << p3x << "," << p3y << ":" << p4x << "," << p4y <<
      // std::endl;
      if (p3x < 0 || p3y < 0 || p4x < 0 || p4y < 0) {
        continue;
      }
      if (p3x >= S || p3y >= S || p4x >= S || p4y >= S) {
        continue;
      }
      if (pillar[p3x][p3y] == 1 && pillar[p4x][p4y] == 1) {
        int s = dx * dx + dy * dy;
        area = (s > area ? s : area);
      }
    }
  }
  return area;
}

int main() {
  while (input() > 0) {
    std::cout << func() << std::endl;
  }
  return 0;
}