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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m;

  priority_queue<int> a;
  for (int i = 0; i < m; i++) {
    cin >> k;
    a.push(k);
  }

  priority_queue<int> b;
  int dum;
  while (-1) {
    dum = a.top();
    a.pop();

    if (a.empty()) {
      break;
    }

    b.push(dum - a.top());
  }

  while (n > 1) {
    b.pop();
    n--;
  }

  int result = 0;
  while (-1) {
    if (b.empty()) {
      break;
    }
    result += b.top();
    b.pop();
  }

  cout << result << endl;
}

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

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m;

  priority_queue<int> a;
  for (int i = 0; i < m; i++) {
    cin >> k;
    a.push(k);
  }

  priority_queue<int> b;
  int dum;
  while (-1) {
    dum = a.top();
    a.pop();

    if (a.empty()) {
      break;
    }

    b.push(dum - a.top());
  }

  while (n > 1 && !b.empty()) {
    b.pop();
    n--;
  }

  int result = 0;
  while (-1) {
    if (b.empty()) {
      break;
    }
    result += b.top();
    b.pop();
  }

  cout << result << endl;
}
Problem p03137 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  if (m == 1 || m >= n) {
    cout << "0" << endl;
    return 0;
  }
  vector<int> x(m);
  vector<pair<int, int>> dist;
  for (int i = 0; i < m; i++) {
    cin >> x.at(i);
    x.at(i) += 100000;
  }
  sort(x.begin(), x.end());
  for (int i = 1; i < m; i++) {
    int dif = x.at(i) - x.at(i - 1);
    dist.push_back(make_pair(dif, i));
  }
  sort(dist.begin(), dist.end());
  reverse(dist.begin(), dist.end());
  vector<int> koma(n);
  koma[0] = -1;
  for (int i = 0; i < n - 1; i++) {
    koma[i + 1] = dist[i].second - 1;
  }
  sort(koma.begin(), koma.end());
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += x[koma[i]] - x[koma[i - 1] + 1];
  }
  ans += x[m - 1] - x[koma[n - 1] + 1];
  cout << ans << endl;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  if (m == 1 || n >= m) {
    cout << "0" << endl;
    return 0;
  }
  vector<int> x(m);
  vector<pair<int, int>> dist;
  for (int i = 0; i < m; i++) {
    cin >> x.at(i);
    x.at(i) += 100000;
  }
  sort(x.begin(), x.end());
  for (int i = 1; i < m; i++) {
    int dif = x.at(i) - x.at(i - 1);
    dist.push_back(make_pair(dif, i));
  }
  sort(dist.begin(), dist.end());
  reverse(dist.begin(), dist.end());
  vector<int> koma(n);
  koma[0] = -1;
  for (int i = 0; i < n - 1; i++) {
    koma[i + 1] = dist[i].second - 1;
  }
  sort(koma.begin(), koma.end());
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += x[koma[i]] - x[koma[i - 1] + 1];
  }
  ans += x[m - 1] - x[koma[n - 1] + 1];
  cout << ans << endl;
}
Problem p03137 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int sum = 0;
  int x[m];
  for (int i = 0; i < m; ++i)
    cin >> x[i];
  sort(x, x + m);
  // m - 1個の区間を選ばなくていい
  for (int i = 0; i < m - 1; ++i)
    x[i] = x[i + 1] - x[i];
  x[m - 1] = 0;
  for (int i = 0; i < m; ++i)
    sum += x[i];
  sort(x, x + m, greater<int>());
  int notchoose = 0;
  for (int i = 0; i < n - 1; ++i)
    notchoose += x[i];
  cout << sum - notchoose << endl;
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  int sum = 0;
  int x[m];
  for (int i = 0; i < m; ++i)
    cin >> x[i];
  sort(x, x + m);
  // m - 1個の区間を選ばなくていい
  for (int i = 0; i < m - 1; ++i)
    x[i] = x[i + 1] - x[i];
  x[m - 1] = 0;
  for (int i = 0; i < m; ++i)
    sum += x[i];
  sort(x, x + m, greater<int>());
  int notchoose = 0;
  for (int i = 0; i < n - 1; ++i)
    notchoose += x[i];
  cout << sum - notchoose << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct distandindex {
  int dist;
  int index;
};

bool cmp(const distandindex &a, const distandindex &b) {
  return a.dist > b.dist;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> x(M);
  for (int i = 0; i < M; i++) {
    cin >> x[i];
  }
  std::sort(x.begin(), x.end());

  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N == 1) {
    cout << x[M - 1] - x[0] << endl;
    return 0;
  }
  vector<distandindex> kaisa(M - 1);
  for (int i = 0; i < M - 1; i++) {
    distandindex d = {x[i + 1] - x[i], i};
    kaisa[i] = d;
  }
  std::sort(kaisa.begin(), kaisa.end(), cmp);

  vector<int> index(N - 1);
  for (int i = 0; i < N - 1; i++) {
    index[i] = kaisa[i].index;
  }
  int start = 0;
  int sum = 0;
  for (int i = 0; i < N - 1; i++) {
    sum += x[index[i]] - x[start];
    start = index[i] + 1;
  }
  sum += x[M - 1] - x[start];
  cout << sum << endl;

  return 0;
}


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

using namespace std;

struct distandindex {
  int dist;
  int index;
};

bool cmp(const distandindex &a, const distandindex &b) {
  return a.dist > b.dist;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> x(M);
  for (int i = 0; i < M; i++) {
    cin >> x[i];
  }
  std::sort(x.begin(), x.end());

  if (M == 1 || N >= M) {
    cout << 0 << endl;
    return 0;
  }
  if (N == 1) {
    cout << x[M - 1] - x[0] << endl;
    return 0;
  }
  vector<distandindex> kaisa(M - 1);
  for (int i = 0; i < M - 1; i++) {
    distandindex d = {x[i + 1] - x[i], i};
    kaisa[i] = d;
  }
  std::sort(kaisa.begin(), kaisa.end(), cmp);

  vector<int> index(N - 1);
  for (int i = 0; i < N - 1; i++) {
    index[i] = kaisa[i].index;
  }
  int start = 0;
  int sum = 0;
  for (int i = 0; i < N - 1; i++) {
    sum += x[index[i]] - x[start];
    start = index[i] + 1;
  }
  sum += x[M - 1] - x[start];
  cout << sum << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int INF = 1e9;
const int MAX = 100000;
const long long LINF = 1e18;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  int L = X[M - 1] - X[0];
  int ans = 0;
  if (M > 1) {
    if (N > 1) {
      vector<int> l(M - 1);
      for (int i = 0; i < M - 1; i++) {
        l[i] = X[i + 1] - X[i];
      }
      sort(l.begin(), l.end(), greater<int>());
      int sum = 0;
      for (int i = 0; i < N - 1; i++) {
        sum += l[i];
      }
      ans = L - sum;
    } else
      ans = L;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int INF = 1e9;
const int MAX = 100000;
const long long LINF = 1e18;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  int L = X[M - 1] - X[0];
  int ans = 0;
  if (M > 1 && M >= N) {
    if (N > 1) {
      vector<int> l(M - 1);
      for (int i = 0; i < M - 1; i++) {
        l[i] = X[i + 1] - X[i];
      }
      sort(l.begin(), l.end(), greater<int>());
      int sum = 0;
      for (int i = 0; i < N - 1; i++) {
        sum += l[i];
      }
      ans = L - sum;
    } else
      ans = L;
  }

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

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  for (int i = 0; i < M; ++i) {
    cin >> X.at(i);
  }
  sort(X.begin(), X.end());

  if (M == 1) {
    cout << 0 << endl;
  } else {
    vector<int> distance(M - 1);
    int X_width = X.at(M - 1) - X.at(0);

    for (int i = 0; i < M - 1; ++i) {
      distance.at(i) = X.at(i + 1) - X.at(i);
    }

    sort(distance.begin(), distance.end(), greater<int>());
    for (int i = 0; i < min(N - 1, M); ++i) {
      X_width = X_width - distance.at(i);
    }
    cout << X_width << endl;
  }
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  for (int i = 0; i < M; ++i) {
    cin >> X.at(i);
  }
  sort(X.begin(), X.end());

  if (M == 1) {
    cout << 0 << endl;
  } else {
    vector<int> distance(M - 1);
    int X_width = X.at(M - 1) - X.at(0);

    for (int i = 0; i < M - 1; ++i) {
      distance.at(i) = X.at(i + 1) - X.at(i);
    }

    sort(distance.begin(), distance.end(), greater<int>());
    for (int i = 0; i < min(N - 1, M - 1); ++i) {
      X_width = X_width - distance.at(i);
    }
    cout << X_width << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
#define Yes(x) cout << (x ? "Yes" : "No") << endl;
#define YES(x) cout << (x ? "YES" : "NO") << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  int n, m;
  cin >> n >> m;
  int x[m];
  REP(i, m) cin >> x[i];
  sort(x, x + m);

  int ans = 0;
  vector<int> d(m + 1, 0);

  for (int i = 1; i < m; i++) {
    d[i] = abs(x[i] - x[i - 1]);
    ans += d[i];
  }

  sort(d.begin(), d.end(), greater<int>());
  REP(i, max(n - 1, m)) ans -= d[i];

  COUT(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
#define Yes(x) cout << (x ? "Yes" : "No") << endl;
#define YES(x) cout << (x ? "YES" : "NO") << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  int n, m;
  cin >> n >> m;
  int x[m];
  REP(i, m) cin >> x[i];
  sort(x, x + m);

  int ans = 0;
  vector<int> d(m + 1, 0);

  for (int i = 1; i < m; i++) {
    d[i] = abs(x[i] - x[i - 1]);
    ans += d[i];
  }

  sort(d.begin(), d.end(), greater<int>());
  REP(i, min(n - 1, m)) ans -= d[i];

  COUT(ans);
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define ld long double
using namespace std;
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
int maxn, minn;
const int MAXN = 100005;
int a[MAXN], b[MAXN];
int main() {
  int n = read(), m = read();
  minn = 1e9 + 7, maxn = -1e9;
  for (int i = 1; i <= m; i++)
    a[i] = read(), minn = min(minn, a[i]), maxn = max(maxn, a[i]);
  sort(a + 1, a + m + 1);
  for (int i = 1; i < m; i++)
    b[i] = a[i + 1] - a[i];
  sort(b + 1, b + m);
  int ans = maxn - minn;
  for (int i = 1; i < n; i++)
    ans -= b[m - i];
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
#define ld long double
using namespace std;
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
int maxn, minn;
const int MAXN = 100005;
int a[MAXN], b[MAXN];
int main() {
  int n = read(), m = read();
  if (n > m)
    n = m;
  minn = 1e9 + 7, maxn = -1e9;
  for (int i = 1; i <= m; i++)
    a[i] = read(), minn = min(minn, a[i]), maxn = max(maxn, a[i]);
  sort(a + 1, a + m + 1);
  for (int i = 1; i < m; i++)
    b[i] = a[i + 1] - a[i];
  sort(b + 1, b + m);
  int ans = maxn - minn;
  for (int i = 1; i < n; i++)
    ans -= b[m - i];
  printf("%d\n", ans);
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> vc;
  for (int i = 1; i < m; i++)
    vc.push_back(abs(a[i] - a[i - 1]));
  sort(vc.begin(), vc.end());
  long long ans = 0;
  for (int i = 0; i < abs(n - m); i++)
    ans += 1LL * vc[i];
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i];
  if (n >= m) {
    cout << 0;
    return 0;
  }
  sort(a.begin(), a.end());
  vector<int> vc;
  for (int i = 1; i < m; i++)
    vc.push_back(abs(a[i] - a[i - 1]));
  sort(vc.begin(), vc.end());
  long long ans = 0;
  for (int i = 0; i < abs(n - m); i++)
    ans += 1LL * vc[i];
  cout << ans;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<P> vp;
typedef vector<bool> vb;
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define rrep(i, a, n) for (ll i = (a); i > (n); --i)
#define erep(i, a, n) for (ll i = (a); i <= (n); ++i)
#define rerep(i, a, n) for (ll i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define vec_max(V) *max_element(all(V))
#define vec_min(V) *min_element(all(V))
#define vec_sum(V) accumulate(all(V), 0LL)
#define print(X) cout << (X) << "\n"
#define _ << " " <<
#define Yes print("Yes")
#define No print("No")
static const int INF = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  vll X(M);
  rep(i, 0, M) cin >> X[i];

  if (M == 1)
    print(0);
  else {
    sort(all(X));

    vll sa(M - 1);
    rep(i, 0, M - 1) { sa[i] = abs(X[i + 1] - X[i]); }
    sort(all(sa));
    ll sum_sa = vec_sum(sa);

    rep(i, 0, N - 1) sum_sa -= sa[M - 2 - i];

    print(sum_sa);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<P> vp;
typedef vector<bool> vb;
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define rrep(i, a, n) for (ll i = (a); i > (n); --i)
#define erep(i, a, n) for (ll i = (a); i <= (n); ++i)
#define rerep(i, a, n) for (ll i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define vec_max(V) *max_element(all(V))
#define vec_min(V) *min_element(all(V))
#define vec_sum(V) accumulate(all(V), 0LL)
#define print(X) cout << (X) << "\n"
#define _ << " " <<
#define Yes print("Yes")
#define No print("No")
static const int INF = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  vll X(M);
  rep(i, 0, M) cin >> X[i];

  if (N >= M)
    print(0);
  else {
    sort(all(X));

    vll sa(M - 1);
    rep(i, 0, M - 1) { sa[i] = abs(X[i + 1] - X[i]); }
    sort(all(sa));
    ll sum_sa = vec_sum(sa);

    rep(i, 0, N - 1) sum_sa -= sa[M - 2 - i];

    print(sum_sa);
  }

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (long i = 0; i < n; ++i)

using namespace std;

void solve(long N, long M, vector<long> X) {
  // if there's only one target, we can win without any move
  if (M == 1) {
    cout << 0 << endl;
    return;
  }

  // sort the targets
  sort(X.begin(), X.end());

  // calc the distances between targets
  vector<long> dist(M - 1);
  REP(i, M - 1)
  dist[i] = X[i + 1] - X[i];

  // sort the distances
  sort(dist.rbegin(), dist.rend());

  // if we have no extra piece, we should move the first piece from the begin to
  // the end
  long ans(X.back() - X.front());

  // we can skip the N-1 longest gap by putting pieces in advance
  REP(i, N - 1)
  ans -= dist[i];

  cout << ans << endl;
}

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

  long N, M;
  cin >> N >> M;
  vector<long> X(M);
  REP(i, M)
  cin >> X[i];

  solve(N, M, move(X));

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (long i = 0; i < n; ++i)

using namespace std;

void solve(long N, long M, vector<long> X) {
  // if there's less number of targets than the pieces, we can win without any
  // movement
  if (M <= N) {
    cout << 0 << endl;
    return;
  }

  // sort the targets
  sort(X.begin(), X.end());

  // calc the distances between targets
  vector<long> dist(M - 1);
  REP(i, M - 1)
  dist[i] = X[i + 1] - X[i];

  // sort the distances
  sort(dist.rbegin(), dist.rend());

  // if we have no extra piece, we should move the first piece from the begin to
  // the end
  long ans(X.back() - X.front());

  // we can skip the N-1 longest gap by putting pieces in advance
  REP(i, N - 1)
  ans -= dist[i];

  cout << ans << endl;
}

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

  long N, M;
  cin >> N >> M;
  vector<long> X(M);
  REP(i, M)
  cin >> X[i];

  solve(N, M, move(X));

  return 0;
}
Problem p03137 (C++)

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

#define ll long long
#define pb push_back

int main() {

  int N, M;
  vector<int> X;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x;
    cin >> x;
    X.pb(x);
  }
  sort(X.begin(), X.end());
  ll ans = X[X.size() - 1] - X[0];
  vector<int> dif;
  for (int i = 1; i < M; i++) {
    dif.pb(X[i] - X[i - 1]);
  }
  sort(dif.begin(), dif.end());
  for (int i = 0; i < N - 1; i++) {
    ans -= dif[dif.size() - 1 - i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define pb push_back

int main() {

  int N, M;
  vector<int> X;
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    int x;
    cin >> x;
    X.pb(x);
  }
  sort(X.begin(), X.end());
  ll ans = X[X.size() - 1] - X[0];
  vector<int> dif;
  for (int i = 1; i < M; i++) {
    dif.pb(X[i] - X[i - 1]);
  }
  sort(dif.begin(), dif.end());
  for (int i = 0; i < N - 1; i++) {
    ans -= dif[dif.size() - 1 - i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0, a + b - n);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
  /*
  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0, a + b - n);
}
Problem p03139 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)

int solve() {
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0, a + b - n);

  return 0;
}

int main() {
  int t = 1;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;
  bool flag = true;
  while (t--) {
    solve();
  }

  time_req = clock() - time_req;
  // cout << "Processor time taken is "
  //  << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)

int solve() {
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0, a + b - n);

  return 0;
}

int main() {
  int t = 1;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;
  bool flag = true;
  while (t--) {
    solve();
  }

  time_req = clock() - time_req;
  // cout << "Processor time taken is "
  //  << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}
Problem p03139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
#define MOD 1000000007

signed main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("/home/swapnil/Desktop/c++/input.txt", "r", stdin);
  freopen("/home/swapnil/Desktop/c++/output.txt", "w", stdout);
#endif
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0LL, a + b - n);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
#define MOD 1000000007

signed main() {
  fast;
  // #ifndef ONLINE_JUDGE
  //     freopen("/home/swapnil/Desktop/c++/input.txt","r",stdin);
  //     freopen("/home/swapnil/Desktop/c++/output.txt","w",stdout);
  // #endif
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max(0LL, a + b - n);
  return 0;
}
Problem p03139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MAX 200005
#define MOD 998244353
#define pb push_back
#define mp make_pair
#define f first
#define s second

ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre,
    strt, en;
ll brr[MAX], arr[MAX], dp[101], dpp[101];
string s;
vector<ll> v;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  cin >> n >> l >> r;
  cout << min(l, r) << " " << max((l + r) - n, 0ll);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MAX 200005
#define MOD 998244353
#define pb push_back
#define mp make_pair
#define f first
#define s second

ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre,
    strt, en;
ll brr[MAX], arr[MAX], dp[101], dpp[101];
string s;
vector<ll> v;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> l >> r;
  cout << min(l, r) << " " << max((l + r) - n, 0ll);

  return 0;
}
Problem p03139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define mod 1000000007
#define INF 1e9 + 5
#define fr first
#define se second
#define int long long
#define ll long long
#define PI 3.1415926535
#define pll pair<ll, ll>
#define vl vector<ll>
#define mll map<ll, ll>
#define mci map<char, int>
#define msi map<string, int>
#define vb vector<bool>
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int N = 100005;
vector<ll> v[26];
signed main() {
  Senky_Bansal IIIT_ALLAHABAD

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max((ll)0, a + b - n) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define mod 1000000007
#define INF 1e9 + 5
#define fr first
#define se second
#define int long long
#define ll long long
#define PI 3.1415926535
#define pll pair<ll, ll>
#define vl vector<ll>
#define mll map<ll, ll>
#define mci map<char, int>
#define msi map<string, int>
#define vb vector<bool>
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int N = 100005;
vector<ll> v[26];
signed main() {
  Senky_Bansal IIIT_ALLAHABAD ll n, a, b;
  cin >> n >> a >> b;
  cout << min(a, b) << " " << max((ll)0, a + b - n) << endl;
}
Problem p03140 (C++)

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

using namespace std;

int main(int, char **) {
  unsigned int n, ans = 0;
  string a, b, c;
  cin >> n >> a >> b >> c;

  for (unsigned int i = 0; i < n; i++) {
    if (a[i] == b[i] && a[i] == c[i])
      continue;
    if (a[i] == b[i])
      ++ans;
    else if (a[i] == c[i])
      ++ans;
    else if (b[i] == c[i])
      ++ans;
    else
      ans += 2;
    // cerr << ans << " ";
  }

  cout << ans << endl;
  return 1;
}


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

using namespace std;

int main(int, char **) {
  unsigned int n, ans = 0;
  string a, b, c;
  cin >> n >> a >> b >> c;

  for (unsigned int i = 0; i < n; i++) {
    if (a[i] == b[i] && a[i] == c[i])
      continue;
    if (a[i] == b[i])
      ++ans;
    else if (a[i] == c[i])
      ++ans;
    else if (b[i] == c[i])
      ++ans;
    else
      ans += 2;
    // cerr << ans << " ";
  }

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

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

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<string> abc(n);
  rep(i, 3) cin >> abc[i];
  int result = 0;
  rep(i, n) {
    set<char> s;
    rep(j, 3) s.insert(abc[j][i]);
    result += (s.size() - 1);
  }

  cout << result;

  return 0;
}

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

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<string> abc(3);
  rep(i, 3) cin >> abc[i];
  int result = 0;
  rep(i, n) {
    set<char> s;
    rep(j, 3) s.insert(abc[j][i]);
    result += (s.size() - 1);
  }

  cout << result;

  return 0;
}
Problem p03140 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)

int solve() {
  int n;
  string a, b, c;
  cin >> n >> a >> b >> c;
  int count = 0;
  rep(i, 0, n) {
    char a1 = a[i], b1 = b[i], c1 = c[i];
    if (a1 == b1 && b1 == c1)
      continue;
    else if (a1 == b1 && b1 != c1)
      count += 1;
    else if (a1 == c1 && c1 != b1)
      count += 1;
    else if (b1 == c1 && c1 != a1)
      count += 1;
    else
      count += 2;
  }
  cout << count;
  return 0;
}

int main() {
  int t = 1;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;
  bool flag = true;
  while (t--) {
    solve();
  }

  time_req = clock() - time_req;
  // cout << "Processor time taken is "
  //  << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)

int solve() {
  int n;
  string a, b, c;
  cin >> n >> a >> b >> c;
  int count = 0;
  rep(i, 0, n) {
    char a1 = a[i], b1 = b[i], c1 = c[i];
    if (a1 == b1 && b1 == c1)
      continue;
    else if (a1 == b1 && b1 != c1)
      count += 1;
    else if (a1 == c1 && c1 != b1)
      count += 1;
    else if (b1 == c1 && c1 != a1)
      count += 1;
    else
      count += 2;
  }
  cout << count;
  return 0;
}

int main() {
  int t = 1;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;
  bool flag = true;
  while (t--) {
    solve();
  }

  time_req = clock() - time_req;
  // cout << "Processor time taken is "
  //  << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}
Problem p03140 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define X first
#define Y second
#define vsort(v) sort((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())

// stoi(s): string to int
// stod(s): string to double

// INT_MAX
// INT_MIN
// LLONG_MAX
// LLONG_MIN
// DBL_MIN
// DBL_MAX
// LDBL_MIN
// LDBL_MAX

// A-Z: 65~90
// a-z: 97~122
// |a-z| = 26

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

  vector<string> v(n);
  for (int i = 0; i < 3; i++) {
    cin >> v[i];
  }

  int cnt = 0;
  set<char> st;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      st.insert(v[j][i]);
    }
    cnt += st.size() - 1;
    st.clear();
  }
  cout << cnt << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define X first
#define Y second
#define vsort(v) sort((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())

// stoi(s): string to int
// stod(s): string to double

// INT_MAX
// INT_MIN
// LLONG_MAX
// LLONG_MIN
// DBL_MIN
// DBL_MAX
// LDBL_MIN
// LDBL_MAX

// A-Z: 65~90
// a-z: 97~122
// |a-z| = 26

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

  vector<string> v(3);
  for (int i = 0; i < 3; i++) {
    cin >> v[i];
  }

  int cnt = 0;
  set<char> st;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      st.insert(v[j][i]);
    }
    cnt += st.size() - 1;
    st.clear();
  }
  cout << cnt << endl;
}
Problem p03140 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  string s[n];
  for (int i = 0; i < 3; i++) {
    cin >> s[i];
  }
  long sum = 0;
  for (int i = 0; i < n; i++) {
    if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) {

    } else if (s[0][i] == s[1][i] || s[1][i] == s[2][i] || s[0][i] == s[2][i]) {
      sum += 1;
    } else {
      sum += 2;
    }
  }
  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  string s[3];
  for (int i = 0; i < 3; i++) {
    cin >> s[i];
  }
  long sum = 0;
  for (int i = 0; i < n; i++) {
    if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) {

    } else if (s[0][i] == s[1][i] || s[1][i] == s[2][i] || s[0][i] == s[2][i]) {
      sum += 1;
    } else {
      sum += 2;
    }
  }
  cout << sum << endl;

  return 0;
}
Problem p03140 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, ans = 0;
  cin >> n;
  vector<string> s(3);
  rep(i, 3) cin >> s[i];
  rep(i, n) {
    int cnt = 0;
    rep(j, 3) if (s[i][j % 3] == s[i][(j + 1) % 3]) cnt++;
    if (cnt == 0)
      ans += 2;
    else if (cnt == 1)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, ans = 0;
  cin >> n;
  vector<string> s(3);
  rep(i, 3) cin >> s[i];
  rep(i, n) {
    int cnt = 0;
    rep(j, 3) if (s[j % 3][i] == s[(j + 1) % 3][i]) cnt++;
    if (cnt == 0)
      ans += 2;
    else if (cnt == 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p03141 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
long long ans1, ans2, n;
vector<pii> vec;

bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; }
bool flag = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    vec.push_back(pii(a, b));
  }
  sort(vec.begin(), vec.end(), cmp);
  while (!vec.empty()) {
    if (!flag) {
      ans1 += vec.back().first;
    } else {
      ans2 += vec.back().second;
    }
    flag = !flag;
  }
  cout << ans1 - ans2;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
long long ans1, ans2, n;
vector<pii> vec;

bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; }
bool flag = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    vec.push_back(pii(a, b));
  }
  sort(vec.begin(), vec.end(), cmp);
  while (!vec.empty()) {
    if (!flag) {
      ans1 += vec.back().first;
    } else {
      ans2 += vec.back().second;
    }
    flag = !flag;
    vec.pop_back();
  }
  cout << ans1 - ans2;
}
Problem p03141 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> vec(n);
  for (auto &v : vec) {
    int a, b;
    cin >> a >> b;
    v = make_pair(a, b);
  }

  sort(vec.begin(), vec.end(), [](auto &lhs, auto &rhs) {
    return (lhs.first + lhs.second) >= (rhs.first + rhs.second);
  });

  ll ans = 0;
  for (int i = 0; i < n; ++i)
    ans += i % 2 == 0 ? vec[i].first : -vec[i].second;

  cout << ans << endl;
}


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

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> vec(n);
  for (auto &v : vec) {
    int a, b;
    cin >> a >> b;
    v = make_pair(a, b);
  }

  sort(vec.begin(), vec.end(), [](auto &lhs, auto &rhs) {
    return (lhs.first + lhs.second) > (rhs.first + rhs.second);
  });

  ll ans = 0;
  for (int i = 0; i < n; ++i)
    ans += i % 2 == 0 ? vec[i].first : -vec[i].second;

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

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

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  REP(i, 0, N) cin >> A[i] >> B[i];

  vector<ll> ord(N);
  REP(i, 0, N) ord[i] = i;

  sort(ord.begin(), ord.end(),
       [&](const ll i, const ll j) { return A[i] + B[i] >= A[j] + B[j]; });

  ll ans = 0;
  REP(i, 0, N) {
    ll k = ord[i];
    if (i % 2 == 0) {
      ans += A[k];
    } else {
      ans -= B[k];
    }
  }
  cout << ans << endl;
}

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

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  REP(i, 0, N) cin >> A[i] >> B[i];

  vector<ll> ord(N);
  REP(i, 0, N) ord[i] = i;

  sort(ord.begin(), ord.end(),
       [&](const ll i, const ll j) { return A[i] + B[i] < A[j] + B[j]; });
  reverse(ord.begin(), ord.end());

  ll ans = 0;
  REP(i, 0, N) {
    ll k = ord[i];
    if (i % 2 == 0) {
      ans += A[k];
    } else {
      ans -= B[k];
    }
  }
  cout << ans << endl;
}
Problem p03141 (C++)

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

int main() {
  int N;
  cin >> N;
  int A[100000], B[100000];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  ll sum;
  pair<ll, int> C[10000];
  for (int i = 0; i < N; i++) {
    sum = A[i] + B[i];
    C[i] = make_pair(sum, i);
  }
  // 昇順ソート
  sort(C, C + N);
  int index;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    index = C[N - i - 1].second;
    ans += A[index];
    i++;
    if (i == N)
      break;
    index = C[N - i - 1].second;
    ans -= B[index];
  }

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

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

int main() {
  int N;
  cin >> N;
  int A[100000], B[100000];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  ll sum;
  pair<ll, int> C[100000];
  for (int i = 0; i < N; i++) {
    sum = A[i] + B[i];
    C[i] = make_pair(sum, i);
  }
  // 昇順ソート
  sort(C, C + N);
  int index;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    index = C[N - i - 1].second;
    ans += A[index];
    i++;
    if (i == N)
      break;
    index = C[N - i - 1].second;
    ans -= B[index];
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
const int nmax = 100005;
struct grm {
  long long a, b;
} a[nmax];
bool comp(grm a, grm b) { return (a.a + b.a < b.a + b.b); }
long long ans, sgn;
int x, y, n, i;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].a >> a[i].b;
  }
  sort(a + 1, a + n + 1, comp);
  sgn = 1;
  for (i = n; i >= 1; i--) {
    if (sgn == 1)
      ans += 1LL * a[i].a;
    else
      ans -= 1LL * a[i].b;
    sgn *= -1;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
const int nmax = 100005;
struct grm {
  long long a, b;
} a[nmax];
bool comp(grm unu, grm doi) { return (unu.a + unu.b < doi.a + doi.b); }
long long ans, sgn;
int x, y, n, i;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].a >> a[i].b;
  }
  sort(a + 1, a + n + 1, comp);
  sgn = 1;
  for (i = n; i >= 1; i--) {
    if (sgn == 1)
      ans += 1LL * a[i].a;
    else
      ans -= 1LL * a[i].b;
    sgn *= -1;
  }
  cout << ans;
  return 0;
}
Problem p03141 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>

using namespace std;

int N;
pair<long long, pair<long long, long long>> P[100000];

int main(void) {
  long long a, b;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld %lld", &a, &b);
    P[i] = make_pair(a + b, make_pair(a, b));
  }

  sort(P, P + N);

  long long ans = 0;

  for (int i = N - 1; i >= 0; i++) {
    if (i % 2 == (N - 1) % 2) {
      ans += P[i].second.first;
    } else {
      ans -= P[i].second.second;
    }
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>

using namespace std;

int N;
pair<long long, pair<long long, long long>> P[100000];

int main(void) {
  long long a, b;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld %lld", &a, &b);
    P[i] = make_pair(a + b, make_pair(a, b));
  }

  sort(P, P + N);

  long long ans = 0;

  for (int i = N - 1; i >= 0; i--) {
    if (i % 2 == (N - 1) % 2) {
      ans += P[i].second.first;
    } else {
      ans -= P[i].second.second;
    }
  }

  printf("%lld\n", ans);
}
Problem p03141 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b;
  int tot;
};
node peo[50005];
int cmp(node x, node y) {
  if (x.tot > y.tot)
    return true;
  return false;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> peo[i].a >> peo[i].b;
    peo[i].tot = peo[i].a + peo[i].b;
  }
  sort(peo + 1, peo + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i % 2 == 1)
      ans += peo[i].a;
    else
      ans -= peo[i].b;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b;
  int tot;
};
node peo[500005];
int cmp(node x, node y) {
  if (x.tot > y.tot)
    return true;
  return false;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> peo[i].a >> peo[i].b;
    peo[i].tot = peo[i].a + peo[i].b;
  }
  sort(peo + 1, peo + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i % 2 == 1)
      ans += peo[i].a;
    else
      ans -= peo[i].b;
  }
  cout << ans;
  return 0;
}
Problem p03141 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <iostream>
struct Dish {
  long long a;
  long long b;
  long long sum;
};
int main() {
  std::size_t n;
  std::cin >> n;
  std::array<Dish, 100000> dishVec;
  for (std::size_t i = 0; i < n; i++) {
    std::cin >> dishVec[i].a >> dishVec[i].b;
    dishVec[i].sum = dishVec[i].a + dishVec[i].b;
  }
  std::sort(dishVec.begin(), dishVec.begin() + n,
            [](auto d1, auto d2) { return d1.sum >= d2.sum; });

  long long result = 0;
  for (std::size_t i = 0; i < n; i++) {
    if (i % 2 == 0)
      result += dishVec[i].a;
    else
      result -= dishVec[i].b;
  }

  std::cout << result << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
struct Dish {
  long long a;
  long long b;
  long long sum;
};
int main() {
  std::size_t n;
  std::cin >> n;
  std::array<Dish, 100000> dishVec;
  for (std::size_t i = 0; i < n; i++) {
    std::cin >> dishVec[i].a >> dishVec[i].b;
    dishVec[i].sum = dishVec[i].a + dishVec[i].b;
  }
  std::sort(dishVec.begin(), dishVec.begin() + n,
            [](auto d1, auto d2) { return d2.sum < d1.sum; });

  long long result = 0;
  for (std::size_t i = 0; i < n; i++) {
    if (i % 2 == 0)
      result += dishVec[i].a;
    else
      result -= dishVec[i].b;
  }

  std::cout << result << std::endl;
  return 0;
}
Problem p03141 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
template <typename T> constexpr T MOD = static_cast<T>(1000000007);
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16;
int main() {
  int N;
  std::cin >> N;
  using P = std::pair<ll, ll>;
  std::vector<P> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i].first >> A[i].second;
  }
  std::sort(A.begin(), A.end(), [](const P &p1, const P &p2) {
    return p1.first + p1.second > p2.first * p2.second;
  });
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (i % 2 == 0 ? A[i].first : -A[i].second);
  }
  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
template <typename T> constexpr T MOD = static_cast<T>(1000000007);
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16;
int main() {
  int N;
  std::cin >> N;
  using P = std::pair<ll, ll>;
  std::vector<P> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i].first >> A[i].second;
  }
  std::sort(A.begin(), A.end(), [](const P &p1, const P &p2) {
    return p1.first + p1.second > p2.first + p2.second;
  });
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (i % 2 == 0 ? A[i].first : -A[i].second);
  }
  std::cout << ans << std::endl;
  return 0;
}