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

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

#define ForA(i, b, a) for (int i = a; i >= b; i--)
#define ForB(i, a, b) for (int i = a; i <= b; i++)
#define IOS                                                                    \
  ios ::sync_with_stdio(false);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define PI 3.1415926535897932384626433832795l
#define deci(n) cout << fixed << setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define IT(a, b) map<a, b>::iterator
#define ll long long int
#define ld long double
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define maxn 1000010

using namespace std;

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;

class Print {
public:
  void print(int n) {
    cout << n << '\n';
    exit(0);
  }
  void print(ll n) {
    cout << n << '\n';
    exit(0);
  }
  void print(string s) {
    cout << s << '\n';
    exit(0);
  }
  void print(ld n) {
    cout << n << '\n';
    exit(0);
  }
};

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  Print _p;
  int n, k;
  cin >> n >> k;
  if (k == 1)
    _p.print(0);
  _p.print(n - k);
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


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

#define ForA(i, b, a) for (int i = a; i >= b; i--)
#define ForB(i, a, b) for (int i = a; i <= b; i++)
#define IOS                                                                    \
  ios ::sync_with_stdio(false);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define PI 3.1415926535897932384626433832795l
#define deci(n) cout << fixed << setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define IT(a, b) map<a, b>::iterator
#define ll long long int
#define ld long double
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define maxn 1000010

using namespace std;

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;

class Print {
public:
  void print(int n) {
    cout << n << '\n';
    exit(0);
  }
  void print(ll n) {
    cout << n << '\n';
    exit(0);
  }
  void print(string s) {
    cout << s << '\n';
    exit(0);
  }
  void print(ld n) {
    cout << n << '\n';
    exit(0);
  }
};

int main() {
  IOS;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // #endif
  Print _p;
  int n, k;
  cin >> n >> k;
  if (k == 1)
    _p.print(0);
  _p.print(n - k);
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
Problem p03005 (C++)

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

using namespace std;

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

#define INF 2000000007
#define MOD 10e9 + 7
#define MAX 100005

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int N, K;
  cin >> N >> K;

  if (K == 1)
    puts(0);
  else
    cout << (N - K) << endl;

  return 0;
}


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

using namespace std;

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

#define INF 2000000007
#define MOD 10e9 + 7
#define MAX 100005

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int N, K;
  cin >> N >> K;

  if (K == 1)
    cout << 0 << endl;
  else
    cout << (N - K) << endl;

  return 0;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import Counter

N = int(input())
points = []
for i, line in enumerate(sys.stdin):
    x, y = map(int, line.split())
    points.append([x, y])
    if i == N - 1:
        break

counts = []
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        p = points[j][0] - points[i][0]
        q = points[j][1] - points[i][1]
        counts.append((p, q))

pq = Counter(counts).most_common()[0][1]
cost = N - pq
print(cost)


Fixed submission:
import sys
from collections import Counter

N = int(input())
points = []
for i, line in enumerate(sys.stdin):
    x, y = map(int, line.split())
    points.append([x, y])
    if i == N - 1:
        break
if len(points) == 1:
    print(1)
    sys.exit(0)

counts = []
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        p = points[j][0] - points[i][0]
        q = points[j][1] - points[i][1]
        counts.append((p, q))

pq = Counter(counts).most_common()[0][1]
cost = N - pq
print(cost)
Problem p03006 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  for (auto &e : v)
    cin >> e.first >> e.second;
  sort(v.begin(), v.end());
  map<pair<int, int>, int> mp;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j)
        continue;
      mp[make_pair(v[i].first - v[j].first, v[i].second - v[j].second)]++;
    }
  }
  int cnt = 0;
  int p = 0, q = 0;
  for (auto e : mp) {
    if (cnt < e.second) {
      cnt = e.second;
      p = e.first.first, q = e.first.second;
    }
  }

  int ans = 100;
  do {
    int sum = 1;
    for (int i = 0; i < n - 1; ++i) {
      if ((v[i].first - p) == v[i + 1].first &&
          (v[i].second - q) == v[i + 1].second)
        sum += 0;
      else
        sum += 1;
    }
    ans = min(ans, sum);
  } while (next_permutation(v.begin(), v.end()));
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  for (auto &e : v)
    cin >> e.first >> e.second;
  sort(v.begin(), v.end());
  map<pair<int, int>, int> mp;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j)
        continue;
      mp[make_pair(v[i].first - v[j].first, v[i].second - v[j].second)]++;
    }
  }
  int cnt = 0;
  int p = 0, q = 0;
  for (auto e : mp) {
    if (cnt < e.second) {
      cnt = e.second;
      p = e.first.first, q = e.first.second;
    }
  }

  cout << n - cnt << endl;

  return 0;
}
Problem p03006 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

void addElement(pll el, vector<pair<pll, ll>> &L) {
  for (auto &li : L) {
    if (li.first == el) {
      li.second++;
      return;
    }
  }
  L.push_back(make_pair(el, 1));
  return;
}

int main() {
  int N, x, y;
  pll XY[50], PQ[50][50];
  vector<pair<pll, ll>> PQcnt;
  cin >> N;
  REP(i, N) {
    cin >> x >> y;
    REP(j, i) {
      ll p = x - XY[j].first, q = y - XY[j].second;
      pll pq = make_pair(p, q);
      PQ[i][j] = make_pair(p, q);
      PQ[j][i] = make_pair(-p, -q);
    }
    XY[i] = make_pair(x, y);
  }

  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;
      addElement(PQ[i][j], PQcnt);
    }
  }

  pair<pll, ll> maxpll = PQcnt[0];
  for (auto &pqcnt : PQcnt) {
    if (maxpll.second < pqcnt.second)
      maxpll = pqcnt;
  }

  ll cnt = N;
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;
      else if (PQ[i][j] == maxpll.first)
        cnt--;
    }
  }
  cout << cnt << '\n';

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

void addElement(pll el, vector<pair<pll, ll>> &L) {
  for (auto &li : L) {
    if (li.first == el) {
      li.second++;
      return;
    }
  }
  L.push_back(make_pair(el, 1));
  return;
}

int main() {
  int N, x, y;
  pll XY[50], PQ[50][50];
  vector<pair<pll, ll>> PQcnt;
  cin >> N;
  REP(i, N) {
    cin >> x >> y;
    REP(j, i) {
      ll p = x - XY[j].first, q = y - XY[j].second;
      pll pq = make_pair(p, q);
      PQ[i][j] = make_pair(p, q);
      PQ[j][i] = make_pair(-p, -q);
    }
    XY[i] = make_pair(x, y);
  }

  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;
      addElement(PQ[i][j], PQcnt);
    }
  }

  pair<pll, ll> maxpll = make_pair(make_pair(0, 0), -INF);
  for (auto &pqcnt : PQcnt) {
    if (maxpll.second < pqcnt.second)
      maxpll = pqcnt;
  }

  ll cnt = N;
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;
      else if (PQ[i][j] == maxpll.first)
        cnt--;
    }
  }
  cout << cnt << '\n';

  return 0;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
inputs = [[int(i) for i in input().split()] for _ in range(N)]

ans = 1
rates = {}
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        vector = (inputs[i][0] - inputs[j][0], inputs[i][1] - inputs[j][1])
        rates[vector] = rates.get(vector, 0) + 1

print(N - max(rates.values()))


Fixed submission:
N = int(input())
inputs = [[int(i) for i in input().split()] for _ in range(N)]

ans = 1
rates = {0: 0}
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        vector = (inputs[i][0] - inputs[j][0], inputs[i][1] - inputs[j][1])
        rates[vector] = rates.get(vector, 0) + 1

print(N - max(rates.values()))
Problem p03006 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  vector<long long> x(N);
  vector<long long> y(N);

  REP(i, N) { cin >> x[i] >> y[i]; }

  int res = N;

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {

      if (i == j)
        continue;
      // 各2点間の距離を全て試して最小値を導く
      ll dx = x[j] - x[i];
      ll dy = y[j] - y[i];

      int sub = 0;
      for (int i2 = 0; i2 < N; ++i) {
        for (int j2 = 0; j2 < N; ++j) {
          if (i2 == j2)
            continue;
          if (dx == x[j2] - x[i2] && dy == y[j2] - y[i2]) {
            ++sub;
          }

          res = min(res, N - sub);
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  vector<long long> x(N);
  vector<long long> y(N);

  REP(i, N) { cin >> x[i] >> y[i]; }

  int res = N;

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {

      if (i == j)
        continue;
      // 各2点間の距離を全て試して最小値を導く
      ll dx = x[j] - x[i];
      ll dy = y[j] - y[i];

      int sub = 0;
      for (int i2 = 0; i2 < N; ++i2) {
        for (int j2 = 0; j2 < N; ++j2) {
          if (i2 == j2)
            continue;
          if (dx == x[j2] - x[i2] && dy == y[j2] - y[i2]) {
            ++sub;
          }

          res = min(res, N - sub);
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())

xys = []
for _ in range(N):
    xys.append(tuple(map(int, input().split())))

sub = []
for x1, y1 in xys:
    for x2, y2 in xys:
        if x1 != x2 or y1 != y2:
            sub.append((x1 - x2, y1 - y2))

c = Counter(sub)
m = max(c.values())

print(N - m)


Fixed submission:
from collections import Counter

N = int(input())

xys = []
for _ in range(N):
    xys.append(tuple(map(int, input().split())))

sub = []
for x1, y1 in xys:
    for x2, y2 in xys:
        if x1 != x2 or y1 != y2:
            sub.append((x1 - x2, y1 - y2))

if not sub:
    print(1)
    exit(0)

c = Counter(sub)
m = max(c.values())

print(N - m)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import collections

N = int(input())
x_y = []
for _ in range(N):
    x_y.append(list(map(int, input().split())))

x_y.sort(key=lambda x: (x[0], x[1]))
pq_cand = []
for i in range(N):
    for j in range(i + 1, N):
        pq_cand.append((x_y[j][0] - x_y[i][0], x_y[j][1] - x_y[i][1]))

pqc = collections.Counter(pq_cand)
_, x = max(pqc.items(), key=lambda x: x[1])
print(N - 1 - x + 1)


Fixed submission:
import collections

N = int(input())

if N == 1:
    print(1)
    exit()

x_y = []
for _ in range(N):
    x_y.append(list(map(int, input().split())))

x_y.sort(key=lambda x: (x[0], x[1]))
pq_cand = []
for i in range(N):
    for j in range(i + 1, N):
        pq_cand.append((x_y[j][0] - x_y[i][0], x_y[j][1] - x_y[i][1]))

pqc = collections.Counter(pq_cand)
_, x = max(pqc.items(), key=lambda x: x[1])
print(N - 1 - x + 1)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter
from itertools import permutations

N = int(input())
x, y = zip(*(map(int, input().split()) for _ in range(N))) if N else ((), ())

# (p, q) = (x_i - x_j, y_i - y_j) を満たす組(i, j)の個数が最大となるように
# p, qを定めればよい
D = Counter((x[i] - x[j], y[i] - y[j]) for i, j in permutations(range(N), 2))
ans = N - max(D.values())

print(ans)


Fixed submission:
from collections import Counter
from itertools import permutations

N = int(input())
x, y = zip(*(map(int, input().split()) for _ in range(N))) if N else ((), ())

# (p, q) = (x_i - x_j, y_i - y_j) を満たす組(i, j)の個数が最大となるように
# p, qを定めればよい
ans = (
    1
    if N == 1
    else N
    - max(
        Counter(
            (x[i] - x[j], y[i] - y[j]) for i, j in permutations(range(N), 2)
        ).values()
    )
)

print(ans)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())

p = []
for _ in range(N):
    p.append(tuple(map(int, input().split())))

d = []
for i, p1 in enumerate(p):
    for j, p2 in enumerate(p):
        if i == j:
            continue
        x = p1[0] - p2[0]
        y = p1[1] - p2[1]
        d.append((x, y))

c = Counter(d)
# print(c)
print(N - c.most_common()[0][1])


Fixed submission:
from collections import Counter

N = int(input())

p = []
for _ in range(N):
    p.append(tuple(map(int, input().split())))

if N == 1:
    print(1)
    exit()

d = []
for i, p1 in enumerate(p):
    for j, p2 in enumerate(p):
        if i == j:
            continue
        x = p1[0] - p2[0]
        y = p1[1] - p2[1]
        d.append((x, y))

c = Counter(d)
# print(c)
print(N - c.most_common()[0][1])
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

n = int(input())
points = [tuple(map(int, input().split())) for _ in range(n)]

cnt = defaultdict(int)
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        cnt[(points[i][0] - points[j][0], points[i][1] - points[j][1])] += 1

# print(cnt)
ans = n - max(cnt.values())
print(ans)


Fixed submission:
from collections import defaultdict

n = int(input())
points = [tuple(map(int, input().split())) for _ in range(n)]

cnt = defaultdict(int)
cnt[(0, 0)] = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        cnt[(points[i][0] - points[j][0], points[i][1] - points[j][1])] += 1

# print(cnt)
ans = n - max(cnt.values())
print(ans)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import collections

N = int(input())
xy = [[int(i) for i in input().split()] for _ in range(N)]

diff = []

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        else:
            if xy[i][0] > xy[j][0]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] >= xy[j][1]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] < xy[j][1]:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))
            else:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))
c = collections.Counter(diff)
ans = N - sorted(list(c.values()))[-1] // 2
print(ans)


Fixed submission:
import collections

N = int(input())
xy = [[int(i) for i in input().split()] for _ in range(N)]

diff = []

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        else:
            if xy[i][0] > xy[j][0]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] >= xy[j][1]:
                diff.append((xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]))
            elif xy[i][0] == xy[j][0] and xy[i][1] < xy[j][1]:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))
            else:
                diff.append((xy[j][0] - xy[i][0], xy[j][1] - xy[i][1]))

if N == 1:
    ans = 1
else:
    c = collections.Counter(diff)
    ans = N - sorted(list(c.values()))[-1] // 2
print(ans)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

n = int(input())
z = [list(map(int, input().split())) for _ in range(n)]
d = defaultdict(int)
for i in range(n):
    xi, yi = z[i]
    for j in range(n):
        if i == j:
            continue
        xj, yj = z[j]
        d[(xi - xj, yi - yj)] += 1
print(n - max(d.values()))


Fixed submission:
from collections import defaultdict

n = int(input())
z = [list(map(int, input().split())) for _ in range(n)]
if n == 1:
    ans = 1
else:
    d = defaultdict(int)
    for i in range(n):
        xi, yi = z[i]
        for j in range(n):
            if i == j:
                continue
            xj, yj = z[j]
            d[(xi - xj, yi - yj)] += 1
    ans = n - max(d.values())
print(ans)
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import defaultdict

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    X = []
    Y = []
    for _ in range(N):
        x, y = map(int, input().split())
        X.append(x)
        Y.append(y)

    cnt = defaultdict(int)
    for i in range(N):
        for j in range(N):
            if i == j:
                continue

            dx = X[j] - X[i]
            dy = Y[j] - Y[i]
            cnt[(dx, dy)] += 1

    cnt = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
    ans = N - cnt[0][1]
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    if N == 1:
        print(1)
        return

    X = []
    Y = []
    for _ in range(N):
        x, y = map(int, input().split())
        X.append(x)
        Y.append(y)

    cnt = defaultdict(int)
    for i in range(N):
        for j in range(N):
            if i == j:
                continue

            dx = X[j] - X[i]
            dy = Y[j] - Y[i]
            cnt[(dx, dy)] += 1

    cnt = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
    ans = N - cnt[0][1]
    print(ans)


if __name__ == "__main__":
    main()
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
xy = set(tuple(map(int, input().split())) for _ in range(n))

costs = []
for a in xy:
    besides_a = xy - {a}
    for b in besides_a:
        p = b[0] - a[0]
        q = b[1] - a[1]
        arrow_num = 0
        for c in xy:
            if (c[0] + p, c[1] + q) in xy:
                arrow_num += 1
        cost = n - arrow_num
        costs.append(cost)

print(min(costs))


Fixed submission:
n = int(input())
xy = set(tuple(map(int, input().split())) for _ in range(n))

costs = []
for a in xy:
    besides_a = xy - {a}
    for b in besides_a:
        p = b[0] - a[0]
        q = b[1] - a[1]
        arrow_num = 0
        for c in xy:
            if (c[0] + p, c[1] + q) in xy:
                arrow_num += 1
        cost = n - arrow_num
        costs.append(cost)

if n == 1:
    print(1)
else:
    print(min(costs))
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    X = [0 for _ in range(N)]
    Y = [0 for _ in range(N)]
    for i in range(N):
        x, y = map(int, input().split())
        X[i] = x
        Y[i] = y

    cnt = {}
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            rx = X[i] - X[j]
            ry = Y[i] - Y[j]
            if (rx, ry) not in cnt:
                cnt[(rx, ry)] = 1
            else:
                cnt[(rx, ry)] += 1
            if (-rx, -ry) not in cnt:
                cnt[(-rx, -ry)] = 1
            else:
                cnt[(-rx, -ry)] += 1

    cnt = [[v, k] for k, v in cnt.items()]
    cnt.sort(reverse=True)
    res = N - cnt[0][0] // 2
    print(res)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())

    if N == 1:
        return print(1)

    X = [0 for _ in range(N)]
    Y = [0 for _ in range(N)]
    for i in range(N):
        x, y = map(int, input().split())
        X[i] = x
        Y[i] = y

    cnt = {}
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            rx = X[i] - X[j]
            ry = Y[i] - Y[j]
            if (rx, ry) not in cnt:
                cnt[(rx, ry)] = 1
            else:
                cnt[(rx, ry)] += 1
            if (-rx, -ry) not in cnt:
                cnt[(-rx, -ry)] = 1
            else:
                cnt[(-rx, -ry)] += 1

    cnt = [[v, k] for k, v in cnt.items()]
    cnt.sort(reverse=True)
    res = N - cnt[0][0] // 2
    print(res)


if __name__ == "__main__":
    main()
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import collections


def calc(balla, ballb):
    xa, ya = balla
    xb, yb = ballb
    return xa - xb, ya - yb


n = int(input())
balls = [tuple(map(int, input().split())) for _ in range(n)]

a = []
for i in range(0, n - 1):
    for j in range(i + 1, n):
        tmp = calc(balls[i], balls[j])
        a += [tmp, tuple(-i for i in tmp)]
counts = collections.Counter(a)

print(n - max(counts.values()))


Fixed submission:
import collections


def calc(balla, ballb):
    xa, ya = balla
    xb, yb = ballb
    return xa - xb, ya - yb


n = int(input())
balls = [tuple(map(int, input().split())) for _ in range(n)]

if n == 1:
    print(1)
    exit()

a = []
for i in range(0, n - 1):
    for j in range(i + 1, n):
        tmp = calc(balls[i], balls[j])
        a += [tmp, tuple(-i for i in tmp)]
counts = collections.Counter(a)

print(n - max(counts.values()))
Problem p03006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  ll N;
  cin >> N;
  vector<ll> x(N), y(N);

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

  vector<llll> sa;
  ll a, b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      a = x[i] - x[j];
      b = y[i] - y[j];
      if (a < 0) {
        a = -a;
        b = -b;
      } else if (a == 0) {
        b = abs(b);
      }
      sa.push_back(make_pair(a, b));
    }
  }

  sort(ALL(sa));

  ll S = sa.size();
  ll k = 1;
  ll tmp = 1;
  a = sa[0].first;
  b = sa[0].second;
  for (int i = 1; i < S; i++) {
    if (a == sa[i].first && b == sa[i].second) {
      tmp++;
    } else {
      k = max(tmp, k);
      tmp = 1;
      a = sa[i].first;
      b = sa[i].second;
    }
  }
  k = max(tmp, k);

  printf("%lld\n", N - k);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;

int main() {
  ll N;
  cin >> N;
  if (N == 1) {
    printf("1\n");
    return 0;
  }

  vector<ll> x(N), y(N);

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

  vector<llll> sa;
  ll a, b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      a = x[i] - x[j];
      b = y[i] - y[j];
      if (a < 0) {
        a = -a;
        b = -b;
      } else if (a == 0) {
        b = abs(b);
      }
      sa.push_back(make_pair(a, b));
    }
  }

  sort(ALL(sa));

  ll S = sa.size();
  ll k = 1;
  ll tmp = 1;
  a = sa[0].first;
  b = sa[0].second;
  for (int i = 1; i < S; i++) {
    if (a == sa[i].first && b == sa[i].second) {
      tmp++;
    } else {
      k = max(tmp, k);
      tmp = 1;
      a = sa[i].first;
      b = sa[i].second;
    }
  }
  k = max(tmp, k);

  printf("%lld\n", N - k);
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]

XY.sort(key=lambda x: x[0])

PQ = {}

for i in range(N):
    x0, y0 = XY[i]
    for j in range(N):
        if i == j:
            continue
        x1, y1 = XY[j]
        p, q = x0 - x1, y0 - y1
        key = str(p) + "," + str(q)
        if key in PQ:
            PQ[key] += 1
        else:
            PQ[key] = 1
ansL = PQ.values()
print(N - max(list(ansL)))


Fixed submission:
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]
if N == 1:
    print(1)
    exit()

XY.sort(key=lambda x: x[0])

PQ = {}

for i in range(N):
    x0, y0 = XY[i]
    for j in range(N):
        if i == j:
            continue
        x1, y1 = XY[j]
        p, q = x0 - x1, y0 - y1
        key = str(p) + "," + str(q)
        if key in PQ:
            PQ[key] += 1
        else:
            PQ[key] = 1
ansL = PQ.values()
print(N - max(list(ansL)))
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
from collections import defaultdict

(n,), *p = [[*map(int, i.split())] for i in open(0)]
d = defaultdict(int)
for i in range(n):
    for j in range(n):
        if i != j:
            d[(p[i][0] - p[j][0], p[i][1] - p[j][1])] += 1
print(n - max(d.values()))


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict

(n,), *p = [[*map(int, i.split())] for i in open(0)]
d = defaultdict(int)
for i in range(n):
    for j in range(n):
        if i != j:
            d[(p[i][0] - p[j][0], p[i][1] - p[j][1])] += 1
print(n * (n < 2) or n - max(d.values()))
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

N = int(input())
X = [list(map(int, input().split())) for _ in range(N)]

X.sort()
ctr = defaultdict(int)
for i in range(N - 1):
    for j in range(i + 1, N):
        p = X[i][0] - X[j][0]
        q = X[i][1] - X[j][1]
        ctr[(p, q)] += 1
        ctr[(-p, -q)] += 1

print(N - max(ctr.values()))


Fixed submission:
from collections import defaultdict

N = int(input())
X = [list(map(int, input().split())) for _ in range(N)]

X.sort()
ctr = defaultdict(int)
for i in range(N - 1):
    for j in range(i + 1, N):
        p = X[i][0] - X[j][0]
        q = X[i][1] - X[j][1]
        ctr[(p, q)] += 1
        ctr[(-p, -q)] += 1

if N == 1:
    print(1)
else:
    print(N - max(ctr.values()))
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
c = Counter((y[0] - x[0], y[1] - x[1]) for x in a for y in a if x < y)
print(n - max(c.values()))


Fixed submission:
from collections import Counter

n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
c = Counter((y[0] - x[0], y[1] - x[1]) for x in a for y in a if x < y)
print(n - max(c.values(), default=0))
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
from itertools import combinations

N = int(input())
P = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append((x, y))

memo = dict()
for a, b in combinations(P, 2):
    p, q = a[0] - b[0], a[1] - b[1]
    if p < 0:
        p, q = -p, -q
    elif p == 0 and q < 0:
        q = -q
    if (p, q) in memo:
        memo[(p, q)] += 1
    else:
        memo[(p, q)] = 1

print(N - max(memo.values()))


Fixed submission:
from itertools import combinations

N = int(input())
P = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append((x, y))

memo = dict()
for a, b in combinations(P, 2):
    p, q = a[0] - b[0], a[1] - b[1]
    if p < 0:
        p, q = -p, -q
    elif p == 0 and q < 0:
        q = -q
    if (p, q) in memo:
        memo[(p, q)] += 1
    else:
        memo[(p, q)] = 1

print(N - max(memo.values()) if N > 1 else 1)
Problem p03006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 55
using namespace std;
int n, num, a[maxn], b[maxn];
map<int, map<int, int>> mp;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", a[i], b[i]);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j)
        num = max(num, ++mp[a[i] - a[j]][b[i] - b[j]]);
  printf("%d\n", n - num);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 55
using namespace std;
int n, num, a[maxn], b[maxn];
map<int, map<int, int>> mp;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", a + i, b + i);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j)
        num = max(num, ++mp[a[i] - a[j]][b[i] - b[j]]);
  printf("%d\n", n - num);
  return 0;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import defaultdict
from itertools import combinations

input = sys.stdin.readline


def main():
    N = int(input())
    x = [0] * N
    y = [0] * N
    for i in range(N):
        x[i], y[i] = map(int, input().split())

    vector = defaultdict(int)
    for a, b in combinations(range(N), 2):
        dx = x[a] - x[b]
        dy = y[a] - y[b]
        if dx == 0:
            vector[(0, abs(dy))] += 1
        elif dx > 0:
            vector[(dx, dy)] += 1
        else:
            vector[(-dx, -dy)] += 1

    ans = N - max(vector.values())
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import defaultdict
from itertools import combinations

input = sys.stdin.readline


def main():
    N = int(input())
    x = [0] * N
    y = [0] * N
    for i in range(N):
        x[i], y[i] = map(int, input().split())

    if N == 1:
        print(1)
        sys.exit()

    vector = defaultdict(int)
    for a, b in combinations(range(N), 2):
        dx = x[a] - x[b]
        dy = y[a] - y[b]
        if dx == 0:
            vector[(0, abs(dy))] += 1
        elif dx > 0:
            vector[(dx, dy)] += 1
        else:
            vector[(-dx, -dy)] += 1

    ans = N - max(vector.values())
    print(ans)


if __name__ == "__main__":
    main()
Problem p03006 (C++)

Buggy submission (status: Runtime Error):
// diverta22019_b.cpp
// Sat Jun 15 20:50:04 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
int n;
vector<P> dot;

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    ll first, second;
    cin >> first >> second;
    P temp = make_pair(first, second);
    dot.push_back(temp);
  }

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

  vector<P> difs;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      P temp;
      temp.first = dot[j].first - dot[i].first;
      temp.second = dot[j].second - dot[i].second;
      difs.push_back(temp);
    }
  }

  sort(difs.begin(), difs.end());
  ll first = difs[0].first;
  ll second = difs[0].second;
  int count = 1;
  int final = 1;
  ll finalfirst = first;
  ll finalsecond = second;
  for (int i = 1; i < difs.size(); i++) {
    if (difs[i].first == first && difs[i].second == second) {
      count++;
    } else {
      if (final < count) {
        final = count;
        finalfirst = difs[i - 1].first;
        finalsecond = difs[i - 1].second;
      }
      count = 1;
      first = difs[i].first;
      second = difs[i].second;
    }
  }

  cout << n - final << endl;
  //	printf("%.4f\n",ans);
}

Fixed submission:
// diverta22019_b.cpp
// Sat Jun 15 20:50:04 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
int n;
vector<P> dot;

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    ll first, second;
    cin >> first >> second;
    P temp = make_pair(first, second);
    dot.push_back(temp);
  }
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  sort(dot.begin(), dot.end());

  vector<P> difs;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      P temp;
      temp.first = dot[j].first - dot[i].first;
      temp.second = dot[j].second - dot[i].second;
      difs.push_back(temp);
    }
  }

  sort(difs.begin(), difs.end());
  ll first = difs[0].first;
  ll second = difs[0].second;
  int count = 1;
  int final = 1;
  ll finalfirst = first;
  ll finalsecond = second;
  for (int i = 1; i < difs.size(); i++) {
    if (difs[i].first == first && difs[i].second == second) {
      count++;
    } else {
      if (final < count) {
        final = count;
        finalfirst = difs[i - 1].first;
        finalsecond = difs[i - 1].second;
      }
      count = 1;
      first = difs[i].first;
      second = difs[i].second;
    }
  }

  cout << n - final << endl;
  //	printf("%.4f\n",ans);
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import collections

N = int(input())
xy_array = [list(map(int, input().split())) for i in range(N)]
# print(xy_array)
distance_array = []
for i in range(N):
    x_i, y_i = xy_array[i]
    for j in range(N):
        if i == j:
            continue
        x_j, y_j = xy_array[j]
        distance_array.append((x_i - x_j, y_i - y_j))

ans_array = collections.Counter(distance_array)
max_count = ans_array.most_common()[0][1]
print(N - max_count)


Fixed submission:
import collections

N = int(input())
xy_array = [list(map(int, input().split())) for i in range(N)]
if N == 1:
    print(1)
    exit()
# print(xy_array)
distance_array = []
for i in range(N):
    x_i, y_i = xy_array[i]
    for j in range(N):
        if i == j:
            continue
        x_j, y_j = xy_array[j]
        distance_array.append((x_i - x_j, y_i - y_j))

ans_array = collections.Counter(distance_array)
max_count = ans_array.most_common()[0][1]
print(N - max_count)
Problem p03006 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

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

  int n;
  cin >> n;
  vector<pair<ll, ll>> z(n), d;
  rep(i, n) cin >> z[i].first >> z[i].second;

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

  rep(i, n - 1) forx(j, i + 1, n) d.push_back(
      make_pair(z[j].first - z[i].first, z[j].second - z[i].second));

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

  int k = 1;
  int mx = 1;
  pair<int, int> a = d[0];

  rep(i, d.size() - 1) {
    if (d[i] == d[i + 1]) {
      k++;
      if (mx < k) {
        a = d[i];
        mx = k;
      }
    } else {
      k = 1;
    }
  }
  cout << n - mx << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

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

  int n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  vector<pair<ll, ll>> z(n), d;
  rep(i, n) cin >> z[i].first >> z[i].second;

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

  rep(i, n - 1) forx(j, i + 1, n) d.push_back(
      make_pair(z[j].first - z[i].first, z[j].second - z[i].second));

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

  int k = 1;
  int mx = 1;
  pair<int, int> a = d[0];

  rep(i, d.size() - 1) {
    if (d[i] == d[i + 1]) {
      k++;
      if (mx < k) {
        a = d[i];
        mx = k;
      }
    } else {
      k = 1;
    }
  }
  cout << n - mx << endl;

  return 0;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
import sys
import collections

sys.setrecursionlimit(10**8)

input = sys.stdin.readline


def main():
    N = int(input())
    XY = [[int(x) for x in input().split()] for _ in range(N)]

    c = collections.Counter()

    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            xi, yi = XY[i]
            xj, yj = XY[j]
            c[(xi - xj, yi - yj)] += 1

    print(N - c.most_common()[0][1])


if __name__ == "__main__":
    main()


Fixed submission:
import sys
import collections

sys.setrecursionlimit(10**8)

input = sys.stdin.readline


def main():
    N = int(input())
    XY = [[int(x) for x in input().split()] for _ in range(N)]

    c = collections.Counter()

    if N == 1:
        print(1)
        return

    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            xi, yi = XY[i]
            xj, yj = XY[j]
            c[(xi - xj, yi - yj)] += 1

    print(N - c.most_common()[0][1])


if __name__ == "__main__":
    main()
Problem p03006 (C++)

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

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

  vector<pair<int, int>> ball(N);
  int x, y;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    ball[i] = make_pair(x, y);
  }
  sort(ball.begin(), ball.end());

  map<pair<int, int>, int> count;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int diff_x = ball[i].first - ball[j].first;
      int diff_y = ball[i].second - ball[j].second;
      count[make_pair(diff_x, diff_y)]++;
    }
  }

  cout << N - count.rbegin()->second << endl;
}


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

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

  vector<pair<int, int>> ball(N);
  int x, y;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    ball[i] = make_pair(x, y);
  }
  sort(ball.begin(), ball.end());

  map<pair<int, int>, int> count;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int diff_x = ball[i].first - ball[j].first;
      int diff_y = ball[i].second - ball[j].second;
      count[make_pair(diff_x, diff_y)]++;
    }
  }
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  int ans = INT_MAX;
  for (auto itr = count.begin(); itr != count.end(); itr++) {
    ans = min(ans, N - itr->second);
  }
  cout << ans << endl;
}
Problem p03006 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = sorted([list(map(int, input().split())) for _ in range(n)])
d = {}
ret = float("inf")
for i in range(n - 1):
    for j in range(i + 1, n):
        p, q = a[j][0] - a[i][0], a[j][1] - a[i][1]
        if p == 0 and q == 0:
            continue
        key = (p, q)
        d.setdefault(key, 0)
        d[key] += 1
print(n - max(d.values()))


Fixed submission:
n = int(input())
a = sorted([list(map(int, input().split())) for _ in range(n)])
d = {}
ret = float("inf")
for i in range(n - 1):
    for j in range(i + 1, n):
        p, q = a[j][0] - a[i][0], a[j][1] - a[i][1]
        if p == 0 and q == 0:
            continue
        key = (p, q)
        d.setdefault(key, 0)
        d[key] += 1
print(n - (max(d.values()) if d else 0))
Problem p03007 (C++)

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

const int N = 2e5 + 10;

int a[N];

inline bool cmp(const int &x, const int &y) { return x > y; }

int main() {
  int n;
  scanf("%d", &n);
  int s = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), s += a[i];
  sort(a + 1, a + n + 1, cmp);
  if (a[n] > 0) {
    s -= a[n] * 2;
    printf("%d\n", s);
    int t = a[n];
    for (int i = 2; i < n; i++) {
      printf("%d %d\n", t, a[i]);
      t = t - a[i];
    }
    printf("%d %d\n", a[1], t);
  } else if (a[2] < 0) {
    s = a[1] * 2 - s;
    printf("%d\n", s);
    int t = a[1];
    for (int i = 2; i <= n; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
  } else {
    int k = 1;
    while (a[k] >= 0)
      k++;
    s = 0;
    for (int i = 1; i <= n; i++)
      s += abs(a[i]);
    printf("%d\n", s);
    int t = a[k];
    for (int i = 2; i < k; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
    printf("%d %d\n", a[1], t);
    t = a[1] - t;
    for (int i = k + 1; i <= n; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
  }
  return 0;
}


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

const int N = 2e5 + 10;

int a[N];

inline bool cmp(const int &x, const int &y) { return x > y; }

int main() {
  int n;
  scanf("%d", &n);
  int s = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), s += a[i];
  sort(a + 1, a + n + 1, cmp);
  if (a[n] > 0) {
    s -= a[n] * 2;
    printf("%d\n", s);
    int t = a[n];
    for (int i = 2; i < n; i++) {
      printf("%d %d\n", t, a[i]);
      t = t - a[i];
    }
    printf("%d %d\n", a[1], t);
  } else if (a[2] < 0) {
    s = a[1] * 2 - s;
    printf("%d\n", s);
    int t = a[1];
    for (int i = 2; i <= n; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
  } else {
    int k = 1;
    while (k < n && a[k] >= 0)
      k++;
    s = 0;
    for (int i = 1; i <= n; i++)
      s += abs(a[i]);
    printf("%d\n", s);
    int t = a[k];
    for (int i = 2; i < k; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
    printf("%d %d\n", a[1], t);
    t = a[1] - t;
    for (int i = k + 1; i <= n; i++) {
      printf("%d %d\n", t, a[i]);
      t -= a[i];
    }
  }
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <set>
#include <vector>
#define M 1000000007
#define int long long
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i;
  cin >> n;
  multiset<int> a;
  for (i = 0; i < n; i++) {
    int p;
    cin >> p;
    a.insert(p);
  }
  int b[n - 1][2];
  i = 0;
  auto it = a.begin();
  auto it1 = it;
  for (;; i++) {
    if (a.size() == 2) {
      b[i][1] = *it;
      it++;
      b[i][0] = *it;
      break;
    }
    it1++;
    if (*it1 < 0) {
      it1 = a.end();
      it--;
      b[i][0] = *it1;
      b[i][1] = *it;
      a.erase(it);
      a.erase(it1);
    } else {
      b[i][0] = *it;
      b[i][1] = *it1;
      a.erase(it);
      a.erase(it1);
    }
    a.insert(b[i][0] - b[i][1]);
    it = a.begin();
    it1 = it;
  }
  cout << b[n - 2][0] - b[n - 2][1] << endl;
  for (i = 0; i < n - 1; i++)
    cout << b[i][0] << ' ' << b[i][1] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <set>
#include <vector>
#define M 1000000007
#define int long long
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i;
  cin >> n;
  multiset<int> a;
  for (i = 0; i < n; i++) {
    int p;
    cin >> p;
    a.insert(p);
  }
  int b[n - 1][2];
  i = 0;
  auto it = a.begin();
  auto it1 = it;
  for (;; i++) {
    if (a.size() == 2) {
      b[i][1] = *it;
      it++;
      b[i][0] = *it;
      break;
    }
    it1++;
    if (*it1 < 0) {
      it1 = a.end();
      it1--;
      b[i][0] = *it1;
      b[i][1] = *it;
      a.erase(it);
      a.erase(it1);
    } else {
      b[i][0] = *it;
      b[i][1] = *it1;
      a.erase(it);
      a.erase(it1);
    }
    a.insert(b[i][0] - b[i][1]);
    it = a.begin();
    it1 = it;
  }
  cout << b[n - 2][0] - b[n - 2][1] << endl;
  for (i = 0; i < n - 1; i++)
    cout << b[i][0] << ' ' << b[i][1] << endl;
}