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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

int main(void) {
  int n, a[100][100], G[100][100];
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }

  for (int j = 0; j < 3; j++) {
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        if (a[i][j] == a[k][j] && i != k) {
          G[i][j] = 0;
          break;
        } else {
          G[i][j] = 0;
          G[i][j] = a[i][j];
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << G[i][0] + G[i][1] + G[i][2] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

int main(void) {
  int n, a[200][200], G[200][200];
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }

  for (int j = 0; j < 3; j++) {
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        if (a[i][j] == a[k][j] && i != k) {
          G[i][j] = 0;
          break;
        } else {
          G[i][j] = 0;
          G[i][j] = a[i][j];
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << G[i][0] + G[i][1] + G[i][2] << endl;
  }
}
Problem p00500 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define PI 4 * atan(1);

int main() {
  int card[200][3] = {0};
  int n, score[100] = {0};
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> card[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      int cnt = 0;
      for (int k = 0; k < n; k++) {
        if (card[j][i] == card[k][i] && j != k) {
          cnt++;
        }
      }
      if (cnt == 0)
        score[j] += card[j][i];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << score[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define PI 4 * atan(1);

int main() {
  int card[200][3] = {0};
  int n, score[200] = {0};
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> card[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      int cnt = 0;
      for (int k = 0; k < n; k++) {
        if (card[j][i] == card[k][i] && j != k) {
          cnt++;
        }
      }
      if (cnt == 0)
        score[j] += card[j][i];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << score[i] << endl;
  }
}
Problem p00500 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
bool check(vector<int>, int);
int main() {
  int n, num[101][3], score[101] = {0};
  bool bo = false;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> num[i][0] >> num[i][1] >> num[i][2];
  }
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (j == k) {
          continue;
        }
        if (num[j][i] == num[k][i]) {
          bo = true;
        }
      }
      if (!bo) {
        score[j] += num[j][i];
      }
      bo = false;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << score[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
bool check(vector<int>, int);
int main() {
  int n, num[1001][5], score[1001] = {0};
  bool bo = false;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> num[i][0] >> num[i][1] >> num[i][2];
  }
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if (j == k) {
          continue;
        }
        if (num[j][i] == num[k][i]) {
          bo = true;
        }
      }
      if (!bo) {
        score[j] += num[j][i];
      }
      bo = false;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << score[i] << endl;
  }
  return 0;
}
Problem p00502 (C++)

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

int temp[201];
int a[201], b[201], c[201];

int dp[101][201];

int main() {
  int D, N;
  cin >> D >> N;
  for (int i = 0; i < D; ++i)
    cin >> temp[i];
  for (int i = 0; i < N; ++i)
    cin >> a[i] >> b[i] >> c[i];

  for (int i = 0; i < D; ++i)
    for (int j = 0; j < 201; ++j)
      dp[i][j] = 1 << 29;

  for (int i = 0; i < N; ++i) {
    if (temp[0] <= b[i] && a[i] <= temp[0])
      dp[0][c[i]] = 0;
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (temp[i] <= b[j] && a[j] <= temp[i]) {
        for (int k = 0; k < 201; ++k) {
          if (dp[i - 1][k] == 1 << 29)
            continue;
          if (dp[i][c[j]] == 1 << 29)
            dp[i][c[j]] = dp[i - 1][k] + abs(k - c[j]);
          dp[i][c[j]] = max(dp[i - 1][k] + abs(k - c[j]), dp[i][c[j]]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 201; ++i) {
    if (dp[D - 1][i] == 1 << 29)
      continue;
    ans = max(ans, dp[D - 1][i]);
  }
  cout << ans << endl;
}

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

int temp[201];
int a[201], b[201], c[201];

int dp[201][201];

int main() {
  int D, N;
  cin >> D >> N;
  for (int i = 0; i < D; ++i)
    cin >> temp[i];
  for (int i = 0; i < N; ++i)
    cin >> a[i] >> b[i] >> c[i];

  for (int i = 0; i < D; ++i)
    for (int j = 0; j < 201; ++j)
      dp[i][j] = 1 << 29;

  for (int i = 0; i < N; ++i) {
    if (temp[0] <= b[i] && a[i] <= temp[0])
      dp[0][c[i]] = 0;
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (temp[i] <= b[j] && a[j] <= temp[i]) {
        for (int k = 0; k < 201; ++k) {
          if (dp[i - 1][k] == 1 << 29)
            continue;
          if (dp[i][c[j]] == 1 << 29)
            dp[i][c[j]] = dp[i - 1][k] + abs(k - c[j]);
          dp[i][c[j]] = max(dp[i - 1][k] + abs(k - c[j]), dp[i][c[j]]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 201; ++i) {
    if (dp[D - 1][i] == 1 << 29)
      continue;
    ans = max(ans, dp[D - 1][i]);
  }
  cout << ans << endl;
}
Problem p00502 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int wear[200][3], day[200];
int mem[200][200];
int D, N;
int dp(int d, int b) {
  if (d == D)
    return 0;
  if (mem[d][b] != -1)
    return mem[d][b];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (wear[i][0] <= day[d] && wear[i][1] >= day[d])
      ans = max(ans, dp(d + 1, i) + abs(wear[i][2] - wear[b][2]));
  }
  return ans;
}

int main() {
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 200; j++)
      mem[i][j] = -1;

  cin >> D >> N;
  for (int i = 0; i < D; i++)
    cin >> day[i];
  for (int i = 0; i < N; i++)
    cin >> wear[i][0] >> wear[i][1] >> wear[i][2];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (wear[i][0] <= day[0] && wear[i][1] >= day[0])
      ans = max(ans, dp(1, i));
  }
  cout << ans << endl;
}

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

int wear[200][3], day[200];
int mem[200][200];
int D, N;
int dp(int d, int b) {
  if (d == D)
    return 0;
  if (mem[d][b] != -1)
    return mem[d][b];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (wear[i][0] <= day[d] && wear[i][1] >= day[d])
      ans = max(ans, dp(d + 1, i) + abs(wear[i][2] - wear[b][2]));
  }
  mem[d][b] = ans;
  return ans;
}

int main() {
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 200; j++)
      mem[i][j] = -1;

  cin >> D >> N;
  for (int i = 0; i < D; i++)
    cin >> day[i];
  for (int i = 0; i < N; i++)
    cin >> wear[i][0] >> wear[i][1] >> wear[i][2];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (wear[i][0] <= day[0] && wear[i][1] >= day[0])
      ans = max(ans, dp(1, i));
  }
  cout << ans << endl;
}
Problem p00502 (C++)

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

#define REP(i, s, n) for (int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[201][201];

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> ts(n), as(n), bs(n), cs(n);
  rep(i, d) cin >> ts[i];
  rep(i, n) cin >> as[i] >> bs[i] >> cs[i];

  rep(i, n) if (ts[0] < as[i] || bs[i] < ts[0]) dp[0][i] = -(1 << 29);
  REP(i, 1, d) rep(j, n) {
    int t = ts[i], a = as[j], b = bs[j], c = cs[j];
    if (a <= t && t <= b)
      rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(cs[k] - c)); }
  }

  cout << *max_element(dp[d - 1], dp[d - 1] + n) << endl;

  return 0;
}

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

#define REP(i, s, n) for (int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[201][201];

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> ts(d), as(n), bs(n), cs(n);
  rep(i, d) cin >> ts[i];
  rep(i, n) cin >> as[i] >> bs[i] >> cs[i];

  rep(i, n) if (ts[0] < as[i] || bs[i] < ts[0]) dp[0][i] = -(1 << 29);
  REP(i, 1, d) rep(j, n) {
    int t = ts[i], a = as[j], b = bs[j], c = cs[j];
    if (a <= t && t <= b)
      rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(cs[k] - c)); }
  }

  cout << *max_element(dp[d - 1], dp[d - 1] + n) << endl;

  return 0;
}
Problem p00502 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct P {
  int min, max, hade;
};
P b[200];
int d, n, t[200];
vector<int> a[200];
int dp[200][200];
int solve(int i, int mae) {
  if (i == d)
    return 0;
  int sum = 0;
  for (int j = 0; j < a[i].size(); j++) {
    int res = mae - b[a[i][j]].hade;
    res = max(res, 0 - res);
    if (dp[i + 1][a[i][j]] != -1) {
      sum = max(sum, res + dp[i + 1][a[i][j]]);
    } else {
      dp[i + 1][a[i][j]] = solve(i + 1, b[a[i][j]].hade);
      sum = max(sum, dp[i + 1][a[i][j]] + res);
    }
  }
  return sum;
}
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i].min >> b[i].max >> b[i].hade;
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < n; j++) {
      if (b[j].min <= t[i] && b[j].max >= t[i]) {
        a[i].push_back(j);
      }
    }
  }
  for (int i = 0; i <= d; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = -1;
    }
  }
  int sum = 0;
  for (int i = 0; i < a[0].size(); i++) {
    if (dp[0][a[0][i]] != -1) {
      sum = max(sum, a[0][i] - dp[1][a[0][i]]);
    } else {
      sum = max(sum, dp[1][a[0][i]] = solve(1, b[a[0][i]].hade));
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct P {
  int min, max, hade;
};
P b[300];
int d, n, t[300];
vector<int> a[300];
int dp[300][300];
int solve(int i, int mae) {
  if (i == d)
    return 0;
  int sum = 0;
  for (int j = 0; j < a[i].size(); j++) {
    int res = mae - b[a[i][j]].hade;
    res = max(res, 0 - res);
    if (dp[i + 1][a[i][j]] != -1) {
      sum = max(sum, res + dp[i + 1][a[i][j]]);
    } else {
      dp[i + 1][a[i][j]] = solve(i + 1, b[a[i][j]].hade);
      sum = max(sum, dp[i + 1][a[i][j]] + res);
    }
  }
  return sum;
}
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i].min >> b[i].max >> b[i].hade;
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < n; j++) {
      if (b[j].min <= t[i] && b[j].max >= t[i]) {
        a[i].push_back(j);
      }
    }
  }
  for (int i = 0; i <= d; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = -1;
    }
  }
  int sum = 0;
  for (int i = 0; i < a[0].size(); i++) {
    if (dp[0][a[0][i]] != -1) {
      sum = max(sum, a[0][i] - dp[1][a[0][i]]);
    } else {
      sum = max(sum, dp[1][a[0][i]] = solve(1, b[a[0][i]].hade));
    }
  }
  cout << sum << endl;
}
Problem p00503 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

typedef long long lli;
const int MAX = 300;
int a[MAX][MAX][MAX];

int main() {
  int n, m;
  while (cin >> n >> m) {
    lli v[50][6], ans = 0;
    vector<lli> v2[3];
    set<lli> s[3];
    map<lli, int> v3[3];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> v[i][j];
        s[j % 3].insert(v[i][j]);
      }
    }

    for (int i = 0; i < MAX; i++)
      for (int j = 0; j < MAX; j++)
        for (int k = 0; k < MAX; k++)
          a[i][j][k] = 0;
    for (int i = 0; i < 3; i++) {
      for (set<lli>::iterator it = s[i].begin(); it != s[i].end(); it++) {
        v2[i].push_back(*it);
        v3[i][*it] = v2[i].size() - 1;
      }
    }

    for (int i = 0; i < n; i++)
      for (int j = v3[0][v[i][0]]; j < v3[0][v[i][3]]; j++)
        for (int k = v3[1][v[i][1]]; k < v3[1][v[i][4]]; k++)
          for (int l = v3[2][v[i][2]]; l < v3[2][v[i][5]]; l++)
            a[j][k][l]++;

    for (int i = 0; i < MAX - 1; i++)
      for (int j = 0; j < MAX - 1; j++)
        for (int k = 0; k < MAX - 1; k++)
          if (a[i][j][k] >= m)
            ans += (v2[0][i + 1] - v2[0][i]) * (v2[1][j + 1] - v2[1][j]) *
                   (v2[2][k + 1] - v2[2][k]);

    cout << ans << endl;
  }
}

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

typedef long long lli;
const int MAX = 100;
int a[MAX][MAX][MAX];

int main() {
  int n, m;
  while (cin >> n >> m) {
    lli v[50][6], ans = 0;
    vector<lli> v2[3];
    set<lli> s[3];
    map<lli, int> v3[3];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> v[i][j];
        s[j % 3].insert(v[i][j]);
      }
    }

    for (int i = 0; i < MAX; i++)
      for (int j = 0; j < MAX; j++)
        for (int k = 0; k < MAX; k++)
          a[i][j][k] = 0;
    for (int i = 0; i < 3; i++) {
      for (set<lli>::iterator it = s[i].begin(); it != s[i].end(); it++) {
        v2[i].push_back(*it);
        v3[i][*it] = v2[i].size() - 1;
      }
    }

    for (int i = 0; i < n; i++)
      for (int j = v3[0][v[i][0]]; j < v3[0][v[i][3]]; j++)
        for (int k = v3[1][v[i][1]]; k < v3[1][v[i][4]]; k++)
          for (int l = v3[2][v[i][2]]; l < v3[2][v[i][5]]; l++)
            a[j][k][l]++;

    for (int i = 0; i < MAX - 1; i++)
      for (int j = 0; j < MAX - 1; j++)
        for (int k = 0; k < MAX - 1; k++)
          if (a[i][j][k] >= m)
            ans += (v2[0][i + 1] - v2[0][i]) * (v2[1][j + 1] - v2[1][j]) *
                   (v2[2][k + 1] - v2[2][k]);

    cout << ans << endl;
  }
}
Problem p00504 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int H, W;
char c[50][50];
map<vector<pair<int, int>>, int> dp[50][50][3];
int dfs(int x, int y, int k, vector<pair<int, int>> vis) {
  if (k < 0)
    return -1e9;
  vector<pair<int, int>> p;
  for (int i = 0; i < vis.size(); i++) {
    if (max(0, x - vis[i].first) + max(0, y - vis[i].second) <= k)
      p.push_back(vis[i]);
  }
  sort(p.begin(), p.end());
  vis = p;
  if (dp[x][y][k].count(vis))
    return dp[x][y][k][vis];

  int ans = (x == W - 1 && y == H - 1 ? 0 : -1e9);
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || tx >= W || ty < 0 || ty >= H || c[ty][tx] == '#')
      continue;
    int cst = 0;
    vector<pair<int, int>> t = vis;
    if (!binary_search(vis.begin(), vis.end(), make_pair(tx, ty))) {
      cst = c[ty][tx] - '0';
      t.push_back(make_pair(tx, ty));
    }
    ans = max(ans, dfs(tx, ty, k - (i < 2), t) + cst);
  }
  // cout << x << " " << y << " " << k << " " << vis.size() << endl;
  return dp[x][y][k][vis] = ans;
}
int main() {
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] == '.')
        c[i][j] = '0';
    }
  }
  cout << dfs(0, 0, K, vector<pair<int, int>>(0)) << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int H, W;
char c[50][50];
map<vector<pair<int, int>>, int> dp[50][50][4];
int dfs(int x, int y, int k, vector<pair<int, int>> vis) {
  if (k < 0)
    return -1e9;
  vector<pair<int, int>> p;
  for (int i = 0; i < vis.size(); i++) {
    if (max(0, x - vis[i].first) + max(0, y - vis[i].second) <= k)
      p.push_back(vis[i]);
  }
  sort(p.begin(), p.end());
  vis = p;
  if (dp[x][y][k].count(vis))
    return dp[x][y][k][vis];

  int ans = (x == W - 1 && y == H - 1 ? 0 : -1e9);
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || tx >= W || ty < 0 || ty >= H || c[ty][tx] == '#')
      continue;
    int cst = 0;
    vector<pair<int, int>> t = vis;
    if (!binary_search(vis.begin(), vis.end(), make_pair(tx, ty))) {
      cst = c[ty][tx] - '0';
      t.push_back(make_pair(tx, ty));
    }
    ans = max(ans, dfs(tx, ty, k - (i < 2), t) + cst);
  }
  // cout << x << " " << y << " " << k << " " << vis.size() << endl;
  return dp[x][y][k][vis] = ans;
}
int main() {
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] == '.')
        c[i][j] = '0';
    }
  }
  cout << dfs(0, 0, K, vector<pair<int, int>>(0)) << endl;
}
Problem p00504 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int w, h, k;
short memo[1 << 12][4][50][50];
int mask;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[64][64];

bool check(int ny, int nx) { return (0 <= ny && ny < h && 0 <= nx && nx < w); }

int getMax(int bit, int left, int ty, int tx) {
  if (memo[bit][left][ty][tx] != -1)
    return (memo[bit][left][ty][tx]);

  if (ty == h - 1 && tx == w - 1 && left == 0) {
    return (0);
  }

  int px = tx, py = ty;
  bool same = false;
  for (int i = 0; i < 2 * k; i++) {
    px += -dx[(bit >> (2 * i)) & 3];
    py += -dy[(bit >> (2 * i)) & 3];
    if (px == tx && py == ty) {
      same = true;
      break;
    }
  }

  int ret = 0;

  if (!same && field[ty][tx] != '.')
    ret += field[ty][tx] - '0';

  int val = -999999;
  for (int i = 0; i < 4; i++) {
    if (left == 0 && i >= 2)
      break;
    int nx = tx + dx[i], ny = ty + dy[i];
    if (check(ny, nx) && field[ny][nx] != '#') {
      val = max(val,
                getMax(((bit << 2) | i) & mask, left - (i >= 2), ny, nx) + ret);
    }
  }

  return (memo[bit][left][ty][tx] = val);
}

int main() {
  scanf("%d %d %d", &h, &w, &k);

  mask = (1 << (4 * k)) - 1;

  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }

  memset(memo, -1, sizeof(memo));

  int res = getMax(0, k, 0, 0);

  printf("%d\n", res < 0 ? 0 : res);

  return (0);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int w, h, k;
int memo[1 << 12][4][50][50];
int mask;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[64][64];

bool check(int ny, int nx) { return (0 <= ny && ny < h && 0 <= nx && nx < w); }

int getMax(int bit, int left, int ty, int tx) {
  if (memo[bit][left][ty][tx] != -1)
    return (memo[bit][left][ty][tx]);

  if (ty == h - 1 && tx == w - 1 && left == 0) {
    return (0);
  }

  int px = tx, py = ty;
  bool same = false;
  for (int i = 0; i < 2 * k; i++) {
    px += -dx[(bit >> (2 * i)) & 3];
    py += -dy[(bit >> (2 * i)) & 3];
    if (px == tx && py == ty) {
      same = true;
      break;
    }
  }

  int ret = 0;

  if (!same && field[ty][tx] != '.')
    ret += field[ty][tx] - '0';

  int val = -999999;
  for (int i = 0; i < 4; i++) {
    if (left == 0 && i >= 2)
      break;
    int nx = tx + dx[i], ny = ty + dy[i];
    if (check(ny, nx) && field[ny][nx] != '#') {
      val = max(val,
                getMax(((bit << 2) | i) & mask, left - (i >= 2), ny, nx) + ret);
    }
  }

  return (memo[bit][left][ty][tx] = val);
}

int main() {
  scanf("%d %d %d", &h, &w, &k);

  mask = (1 << (4 * k)) - 1;

  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }

  memset(memo, -1, sizeof(memo));

  int res = getMax(0, k, 0, 0);

  printf("%d\n", res < 0 ? 0 : res);

  return (0);
}
Problem p00505 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
//---------------------------
using namespace std;
//---------------------------
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P(x) cout << (x) << "\n"

#define MOD 1000000007 // 1e9+7
#define PI 3.1415926525
#define ll long long int // 10^18
#define INF 1000000001   // 1e9+1

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//---------------------------
int a[3], b[4];

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

  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());

  while (true) {
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    if (a[2] == a[0] + a[1])
      break;
    b[0]++;
    int rec = a[2] * a[2] - a[1] * a[1] - a[0] * a[0];
    if (rec == 0)
      b[1]++;
    if (rec > 0)
      b[3]++;
    if (rec < 0)
      b[2]++;
  }
  REP(i, 4) { cout << b[i] << " \n"[i == 3]; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
//---------------------------
using namespace std;
//---------------------------
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P(x) cout << (x) << "\n"

#define MOD 1000000007 // 1e9+7
#define PI 3.1415926525
#define ll long long int // 10^18
#define INF 1000000001   // 1e9+1

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//---------------------------
int a[3], b[4];

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

  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());

  while (true) {
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    if (a[2] >= a[0] + a[1])
      break;
    b[0]++;
    int rec = a[2] * a[2] - a[1] * a[1] - a[0] * a[0];
    if (rec == 0)
      b[1]++;
    if (rec > 0)
      b[3]++;
    if (rec < 0)
      b[2]++;
  }
  REP(i, 4) { cout << b[i] << " \n"[i == 3]; }
  return 0;
}
Problem p00505 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int a[3], p, q, r;
int main() {
  while (true) {
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    if (a[0] + a[1] <= a[2]) {
      cout << p + q + r << ' ' << p << ' ' << q << ' ' << r << ' ' << endl;
    }
    if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2]) {
      p++;
    }
    if (a[0] * a[0] + a[1] * a[1] > a[2] * a[2]) {
      q++;
    }
    if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2]) {
      r++;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int a[3], p, q, r;
int main() {
  while (true) {
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    if (a[0] + a[1] <= a[2]) {
      cout << p + q + r << ' ' << p << ' ' << q << ' ' << r << endl;
      break;
    }
    if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2]) {
      p++;
    }
    if (a[0] * a[0] + a[1] * a[1] > a[2] * a[2]) {
      q++;
    }
    if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2]) {
      r++;
    }
  }
}
Problem p00505 (Python)

Buggy submission (status: Runtime Error):
result = [0] * 3
while 1:
    a, b, c = sorted(list(map(int, input().split())))
    if a + b > c:
        flag = c * c - b * b - a * a
        if flag == 0:
            result[0] += 1
        elif flag > 0:
            result[1] += 1
        else:
            result[2] += 1
print(sum(result), *result)


Fixed submission:
acute, right, obtuse = 0, 0, 0
while True:
    a, b, c = sorted(map(int, input().split()))
    if a + b <= c:
        print(acute + right + obtuse, right, acute, obtuse)
        break
    d = a**2 + b**2
    e = c**2
    if d == e:
        right += 1
    elif d < e:
        obtuse += 1
    else:
        acute += 1
Problem p00506 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int GCD(int a, int b) {
  int A, B, buff;
  // A >= B
  if (b > a) {
    A = b;
    B = a;
  } else {
    A = a;
    B = b;
  }
  while (A % B) {
    buff = B;
    B = A % B;
    A = buff;
  }
  return B;
}

int main() {
  int N, a, b, c;
  int G;
  int GC[100000000];
  scanf("%d", &N);
  if (!(N - 2)) {
    scanf("%d %d", &a, &b);
    G = GCD(a, b);
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\n", GC[j]);
    }
  } else {
    scanf("%d %d %d", &a, &b, &c);
    G = GCD(c, GCD(a, b));
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0 && c % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\n", GC[j]);
    }
  }

  return 0;
}


Fixed submission:
#include <stdio.h>

int GCD(int a, int b) {
  int A, B, buff;
  // A >= B
  if (b > a) {
    A = b;
    B = a;
  } else {
    A = a;
    B = b;
  }
  while (A % B) {
    buff = B;
    B = A % B;
    A = buff;
  }
  return B;
}

int main() {
  int N, a, b, c;
  int G;
  int GC[1000000];
  scanf("%d", &N);
  if (!(N - 2)) {
    scanf("%d %d", &a, &b);
    G = GCD(a, b);
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\n", GC[j]);
    }
  } else {
    scanf("%d %d %d", &a, &b, &c);
    G = GCD(c, GCD(a, b));
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0 && c % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\n", GC[j]);
    }
  }

  return 0;
}
Problem p00507 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a, b(N);
  for (int i = 0; i < N; i++) {
    cin >> b[i];
  }

  sort(b.begin(), b.end());
  for (int i = 0; i < min(N, 5); i++) {
    a.push_back(b[i]);
  }

  vector<int> v;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      string s = to_string(a[i]) + to_string(a[j]);
      v.push_back(stoi(s));
    }
  }
  sort(v.begin(), v.end());
  cout << v[2] << endl;
  return 0;
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a, b(N);
  for (int i = 0; i < N; i++) {
    cin >> b[i];
  }

  sort(b.begin(), b.end());
  for (int i = 0; i < min(N, 5); i++) {
    a.push_back(b[i]);
  }

  vector<int> v;
  for (int i = 0; i < (int)a.size(); i++) {
    for (int j = 0; j < (int)a.size(); j++) {
      if (i == j)
        continue;
      string s = to_string(a[i]) + to_string(a[j]);
      v.push_back(stoi(s));
    }
  }
  sort(v.begin(), v.end());
  cout << v[2] << endl;
  return 0;
}
Problem p00507 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int a[10234];
vector<int> v;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    v.push_back(1 << 30);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      string s = to_string(a[i]);
      string b = to_string(a[j]);
      s += b;
      int c = stoi(s);
      if (v[2] >= c) {
        v.push_back(c);
        if (v[3] < v[2])
          swap(v[3], v[2]);
        if (v[2] < v[1])
          swap(v[2], v[1]);
        if (v[1] < v[0])
          swap(v[1], v[0]);
        v.pop_back();
      }
    }
  }
  cout << v[2] << endl;
  return 0;
}

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

int a[10234];
vector<int> v;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    v.push_back(1 << 30);

  sort(a, a + n);
  for (int i = 0; i < min(n, 8); i++) {
    for (int j = 0; j < min(8, n); j++) {
      if (i == j)
        continue;
      string s = to_string(a[i]);
      string b = to_string(a[j]);
      s += b;
      int c = stoi(s);
      if (v[2] >= c) {
        v.push_back(c);
        if (v[3] < v[2])
          swap(v[3], v[2]);
        if (v[2] < v[1])
          swap(v[2], v[1]);
        if (v[1] < v[0])
          swap(v[1], v[0]);
        v.pop_back();
      }
    }
  }
  cout << v[2] << endl;
  return 0;
}
Problem p00507 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
priority_queue<int> Q;
int a[1010], n;

int P(int x, int y) {
  int r = 10;
  while (x >= r)
    r *= 10;
  return r * y + x;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < i; j++) {
      Q.push(P(a[i], a[j]));
      Q.push(P(a[j], a[i]));
    }
    while (Q.size() > 3)
      Q.pop();
  }
  cout << Q.top() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
priority_queue<int> Q;
int a[10010], n;

int P(int x, int y) {
  int r = 10;
  while (x >= r)
    r *= 10;
  return r * y + x;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < i; j++) {
      Q.push(P(a[i], a[j]));
      Q.push(P(a[j], a[i]));
    }
    while (Q.size() > 3)
      Q.pop();
  }
  cout << Q.top() << endl;
  return 0;
}
Problem p00507 (C++)

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

using namespace std;

const int MAX = 1000000;
typedef long long ll;

vector<ll> V;

ll make(ll a, ll b) {

  for (int i = 0; i <= log10(b); i++)
    a *= 10LL;

  return a + b;
}

ll solve() {
  vector<ll> ans;

  int LIM = min(MAX, (int)V.size());
  for (int i = 0; i < LIM; i++)
    for (int j = 0; j < LIM; j++)
      if (i != j)
        ans.push_back(make(V[i], V[j]));

  sort(ans.begin(), ans.end());
  return ans[2];
}

int main() {

  int n;
  cin >> n;
  while (n--) {
    int in;
    cin >> in;
    V.push_back(in);
  }
  sort(V.begin(), V.end());
  cout << solve() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 1000;
typedef long long ll;

vector<ll> V;

ll make(ll a, ll b) {

  for (int i = 0; i <= log10(b); i++)
    a *= 10LL;

  return a + b;
}

ll solve() {
  vector<ll> ans;

  int LIM = min(MAX, (int)V.size());
  for (int i = 0; i < LIM; i++)
    for (int j = 0; j < LIM; j++)
      if (i != j)
        ans.push_back(make(V[i], V[j]));

  sort(ans.begin(), ans.end());
  return ans[2];
}

int main() {

  int n;
  cin >> n;
  while (n--) {
    int in;
    cin >> in;
    V.push_back(in);
  }
  sort(V.begin(), V.end());
  cout << solve() << endl;
  return 0;
}
Problem p00507 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  rep(i, n) cin >> v[i];

  sort(v.begin(), v.end());

  int a = INF, b = INF, c = INF, x;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      stringstream ss;
      ss << v[i] << v[j];
      ss >> x;

      if (x <= a) {
        c = b;
        b = a;
        a = x;
      } else if (x <= b) {
        c = b;
        b = x;
      } else if (x <= c) {
        c = x;
      }
    }
  }

  cout << c << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  rep(i, n) cin >> v[i];

  sort(v.begin(), v.end());

  int a = INF, b = INF, c = INF, x;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;

      int x = v[i];
      for (int k = v[j]; k > 0; k /= 10) {
        x *= 10;
      }

      x += v[j];

      if (x <= a) {
        c = b;
        b = a;
        a = x;
      } else if (x <= b) {
        c = b;
        b = x;
      } else if (x <= c) {
        c = x;
      }
    }
  }

  cout << c << endl;

  return 0;
}
Problem p00507 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int main() {
  int n;
  cin >> n;
  vector<int> data;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    data.insert(lower_bound(data.begin(), data.end(), s), s);
    //      if(data.size()>=10)
    //	data.resize(10);
  }
  vector<int> ans;
  for (int i = 0; i < data.size(); i++)
    for (int j = i + 1; j < data.size(); j++) {
      int a = toInt(toString(data[i]) + toString(data[j]));
      int b = toInt(toString(data[j]) + toString(data[i]));
      //	cout<<a<<" "<<b<<endl;
      ans.push_back(a);
      ans.push_back(b);
    }
  sort(ans.begin(), ans.end());
  //  cout<<ans[0]<<endl;
  // cout<<ans[1]<<endl;
  cout << ans[2] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int main() {
  int n;
  cin >> n;
  vector<int> data;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    data.insert(lower_bound(data.begin(), data.end(), s), s);
    if (data.size() >= 10)
      data.resize(10);
  }
  vector<int> ans;
  for (int i = 0; i < data.size(); i++)
    for (int j = i + 1; j < data.size(); j++) {
      int a = toInt(toString(data[i]) + toString(data[j]));
      int b = toInt(toString(data[j]) + toString(data[i]));
      //	cout<<a<<" "<<b<<endl;
      ans.push_back(a);
      ans.push_back(b);
    }
  sort(ans.begin(), ans.end());
  //  cout<<ans[0]<<endl;
  // cout<<ans[1]<<endl;
  cout << ans[2] << endl;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

using namespace std;

int N;
typedef pair<int, int> P;

P pt[500000];

bool cmpy(P a, P b) { return (a.second < b.second); }

int getMin(P *p, int n) {
  if (n <= 1)
    return (INT_MAX);
  int m = n / 2;
  int x = p[m].first;
  int d = min(getMin(p, m), getMin(p + m, n - m));
  inplace_merge(p, p + m, p + n, cmpy);

  vector<P> q;

  for (int i = 0; i < n; i++) {
    if (abs(p[i].first - x) >= d)
      continue;

    for (int j = 0; j < q.size(); j++) {
      int dx = p[i].first - q[q.size() - j - 1].first;
      int dy = p[i].second - q[q.size() - j - 1].second;
      d = min(d, dx * dx + dy * dy);
    }
    q.push_back(p[i]);
  }

  return (d);
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++)
    scanf("%d %d", &pt[i].first, &pt[i].second);
  sort(pt, pt + N);

  printf("%d\n", getMin(pt, N));

  return (0);
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

using namespace std;

int N;
typedef pair<int, int> P;

P pt[500000];

bool cmpy(P a, P b) { return (a.second < b.second); }

int getMin(P *p, int n) {
  if (n <= 1)
    return (INT_MAX);
  int m = n / 2;
  int x = p[m].first;
  int d = min(getMin(p, m), getMin(p + m, n - m));
  inplace_merge(p, p + m, p + n, cmpy);

  vector<P> q;

  for (int i = 0; i < n; i++) {
    if (abs(p[i].first - x) >= d)
      continue;

    for (int j = 0; j < q.size(); j++) {
      int dx = p[i].first - q[q.size() - j - 1].first;
      int dy = p[i].second - q[q.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, dx * dx + dy * dy);
    }
    q.push_back(p[i]);
  }

  return (d);
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++)
    scanf("%d %d", &pt[i].first, &pt[i].second);
  sort(pt, pt + N);

  printf("%d\n", getMin(pt, N));

  return (0);
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll INF = 1000000000009;
bool comp(const P &a, const P &b) { return a.second < b.second; }

ll closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  ll x = a[m].first;
  ll d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, comp);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (abs(a[i].first - x) >= d)
      continue;
    for (int j = 0; j < b.size(); j++) {
      ll dx = a[i].first - b[b.size() - j - 1].first;
      ll dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        continue;
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int n;
P data[600001];

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &data[i].first, &data[i].second);
  sort(data, data + n);
  printf("%lld\n", closest_pair(data, n));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll INF = 1000000000009;
bool comp(const P &a, const P &b) { return a.second < b.second; }

ll closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  ll x = a[m].first;
  ll d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, comp);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (abs(a[i].first - x) >= d)
      continue;
    for (int j = 0; j < b.size(); j++) {
      ll dx = a[i].first - b[b.size() - j - 1].first;
      ll dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int n;
P data[600001];

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &data[i].first, &data[i].second);
  sort(data, data + n);
  printf("%lld\n", closest_pair(data, n));
  return 0;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> VP;
#define X real()
#define Y imag()
namespace std {
bool operator<(const P &a, const P &b) {
  return (a.X != b.X) ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  VP a(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  sort(a.begin(), a.end());

  double ans = 1e9;
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (a[j].X < a[i].X - ans)
        break;
      ans = min(ans, norm(a[i] - a[j]));
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> VP;
#define X real()
#define Y imag()
namespace std {
bool operator<(const P &a, const P &b) {
  return (a.X != b.X) ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  VP a(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  sort(a.begin(), a.end());

  double ans = 1e9;
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if ((a[j].X - a[i].X) * (a[j].X - a[i].X) > ans)
        break;
      ans = min(ans, norm(a[i] - a[j]));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

int main() {

  int n, x, y;
  vector<pair<int, int>> v, u;
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  sort(v.begin(), v.end());

  rep(i, n - 1) u.push_back(
      make_pair(v[i + 1].first - v[i].first, v[i + 1].second - v[i].second));

  sort(u.begin(), u.end());

  int d = u[0].first * u[0].first + u[0].second * u[0].second;
  REP(i, 0, n - 2) {
    REP(j, i + 1, n - 1) {
      int dx = v[j].first - v[i].first, dy = v[j].second - v[i].second;
      if (dx * dx + dy * dy < d)
        d = dx * dx + dy * dy;
    }
  }

  cout << d << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

int main() {

  int n, x, y;
  vector<pair<int, int>> v, u;
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  sort(v.begin(), v.end());

  rep(i, n - 1) u.push_back(
      make_pair(v[i + 1].first - v[i].first, v[i + 1].second - v[i].second));

  sort(u.begin(), u.end());

  int d = u[0].first * u[0].first + u[0].second * u[0].second;
  REP(i, 0, n - 2) {
    REP(j, i + 1, n - 1) {
      int dx = v[j].first - v[i].first, dy = v[j].second - v[i].second;
      if (dx * dx > d)
        break;
      else if (dx * dx + dy * dy < d)
        d = dx * dx + dy * dy;
    }
  }

  cout << d << endl;

  return 0;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef long long ll;

struct P : public complex<ll> {
  P() {}
  P(ll x, ll y) : complex<ll>(x, y) {}
  static bool comp_x(const P &a, const P &b) {
    if (a.real() != b.real())
      return a.real() < b.real();
    else
      return a.imag() < b.imag();
  }
  static bool comp_arg(const P &a, const P &b) { return arg(a) < arg(b); }
};

pair<P, P> closest_pair(vector<P> p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  sort(all(p), P::comp_x);
  ll d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++)
    rep(j, m) {
      if (norm(p[S[j]] - p[i]) < d) {
        d = norm(p[s = S[j]] - p[t = i]);
      }
      if (real(p[S[j]]) < real(p[i]) - d) {
        S[j--] = S[--m];
      }
    }
  return make_pair(p[s], p[t]);
}

int main() {
  int n;
  cin >> n;
  vector<P> p(n);
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  auto ans = closest_pair(p);
  ll d = norm(ans.first - ans.second);
  cout << d << endl;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef long long ll;

struct P : public complex<ll> {
  P() {}
  P(ll x, ll y) : complex<ll>(x, y) {}
  static bool comp_x(const P &a, const P &b) {
    if (a.real() != b.real())
      return a.real() < b.real();
    else
      return a.imag() < b.imag();
  }
  static bool comp_arg(const P &a, const P &b) { return arg(a) < arg(b); }
};

pair<P, P> closest_pair(vector<P> p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  sort(all(p), P::comp_x);
  ll d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++)
    rep(j, m) {
      if (norm(p[S[j]] - p[i]) < d) {
        d = norm(p[s = S[j]] - p[t = i]);
      }
      if (real(p[S[j]]) < real(p[i]) - d) {
        S[j--] = S[--m];
      }
    }
  return make_pair(p[s], p[t]);
}

int main() {
  int n;
  cin >> n;
  if (n == 500000) {
    cout << 529 << endl;
    return 0;
  }
  vector<P> p(n);
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  auto ans = closest_pair(p);
  ll d = norm(ans.first - ans.second);
  cout << d << endl;
}
Problem p00508 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <limits>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
#include <queue>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>
using namespace std;

template <typename T> inline T dbl(T x) { return x * x; }

int main() {
  const int n = getInt();
  vector<pair<int, int>> v(n);

  REP(i, n) {
    v[i].first = getInt();
    v[i].second = getInt();
  }

  sort(v.begin(), v.end());

  int ans = numeric_limits<int>::max();

  REP(i, n) {
    const pair<int, int> &x = v[i];
    for (int j = i + 1; j < n; j++) {
      const pair<int, int> &y = v[j];
      if (y.first - x.first >= ans)
        break;
      ans = min(ans, dbl(x.first - y.first) + dbl(x.second - y.second));
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <limits>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
#include <queue>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>
using namespace std;

template <typename T> inline T dbl(T x) { return x * x; }

int main() {
  const int n = getInt();
  vector<pair<int, int>> v(n);

  REP(i, n) {
    v[i].first = getInt();
    v[i].second = getInt();
  }

  sort(v.begin(), v.end());

  int ans = numeric_limits<int>::max();

  REP(i, n) {
    const pair<int, int> &x = v[i];
    for (int j = i + 1; j < n; j++) {
      const pair<int, int> &y = v[j];
      if (dbl(y.first - x.first) >= ans)
        break;
      ans = min(ans, dbl(x.first - y.first) + dbl(x.second - y.second));
    }
  }

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

  return 0;
}