alexjercan/bugnet huggingface dataset:
Problem p02272 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1000000000
int count = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid, i, j, k;
  int *m1 = (int *)malloc((n1 + 1) * sizeof(int)),
      *m2 = (int *)malloc((n2 + 1) * sizeof(int));
  for (i = 0; i < n1; i++)
    m1[i] = a[left + i];
  for (i = 0; i < n2; i++)
    m2[i] = a[mid + i];
  m1[n1] = m2[n2] = INF;
  i = j = 0;
  for (k = left; k < right; k++) {
    count++;
    if (m1[i] <= m2[j]) {
      a[k] = m1[i];
      i++;
    } else {
      a[k] = m2[j];
      j++;
    }
  }
  free(m1);
  free(m2);
}
void mergesort(int a[], int left, int right) {
  int mid;
  if (right - left > 1) {
    mid = (left + right) / 2;
    mergesort(a, left, mid);
    mergesort(a, mid, right);
    merge(a, left, mid, right);
  }
}
int main() {
  int n, i;
  int a[500000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergesort(a, 0, n);
  for (i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl << count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1000000000
int count = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid, i, j, k;
  int *m1 = (int *)malloc((n1 + 1) * sizeof(int)),
      *m2 = (int *)malloc((n2 + 1) * sizeof(int));
  for (i = 0; i < n1; i++)
    m1[i] = a[left + i];
  for (i = 0; i < n2; i++)
    m2[i] = a[mid + i];
  m1[n1] = m2[n2] = INF;
  i = j = 0;
  for (k = left; k < right; k++) {
    count++;
    if (m1[i] <= m2[j]) {
      a[k] = m1[i];
      i++;
    } else {
      a[k] = m2[j];
      j++;
    }
  }
}
void mergesort(int a[], int left, int right) {
  int mid;
  if (right - left > 1) {
    mid = (left + right) / 2;
    mergesort(a, left, mid);
    mergesort(a, mid, right);
    merge(a, left, mid, right);
  }
}
int main() {
  int n, i;
  int a[500000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergesort(a, 0, n);
  for (i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl << count << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <climits>
#include <iostream>
#include <vector>

void merge(std::vector<int> *A, int left, int mid, int right, int *count) {
  int n1 = mid - left;
  int n2 = right - mid;

  std::vector<int> *L = new std::vector<int>(n1 + 1, 0);
  std::vector<int> *R = new std::vector<int>(n2 + 1, 0);

  for (int i = 0; i < n1; i++) {
    L->at(i) = A->at(left + i);
  }

  for (int i = 0; i < n2; i++) {
    R->at(i) = A->at(mid + i);
  }

  L->at(n1) = INT_MAX;
  R->at(n2) = INT_MAX;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L->at(i) <= R->at(j)) {
      A->at(k) = L->at(i);
      i++;
    } else {
      A->at(k) = R->at(j);
      j++;
    }

    (*count)++;
  }

  delete R;
  delete L;
}

void merge_sort(std::vector<int> *A, int left, int right, int *count) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    merge_sort(A, left, mid, count);
    merge_sort(A, mid, right, count);
    merge(A, left, mid, right, count);
  }
}

int main(void) {
  int n = 0;

  std::cin >> n;

  std::vector<int> *A = new std::vector<int>(n, 0);

  for (int i = 0; i < n; i++) {
    int v = 0;

    std::cin >> v;

    A->at(i) = v;
  }

  int count = 0;

  int left = 0;
  int right = A->size();
  int mid = (left + right) / 2;

  merge_sort(A, left, right, &count);

  for (int i = 0; i < n; i++) {
    if (i < (n - 1)) {
      std::cout << A->at(i) << " ";
    } else {
      std::cout << A->at(i) << std::endl;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

void merge(std::vector<int> *A, int left, int mid, int right, int *count) {
  int n1 = mid - left;
  int n2 = right - mid;

  std::vector<int> *L = new std::vector<int>(n1 + 1, 0);
  std::vector<int> *R = new std::vector<int>(n2 + 1, 0);

  for (int i = 0; i < n1; i++) {
    L->at(i) = A->at(left + i);
  }

  for (int i = 0; i < n2; i++) {
    R->at(i) = A->at(mid + i);
  }

  L->at(n1) = INT_MAX;
  R->at(n2) = INT_MAX;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L->at(i) <= R->at(j)) {
      A->at(k) = L->at(i);
      i++;
    } else {
      A->at(k) = R->at(j);
      j++;
    }

    (*count)++;
  }
}

void merge_sort(std::vector<int> *A, int left, int right, int *count) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    merge_sort(A, left, mid, count);
    merge_sort(A, mid, right, count);
    merge(A, left, mid, right, count);
  }
}

int main(void) {
  int n = 0;

  std::cin >> n;

  std::vector<int> *A = new std::vector<int>(n, 0);

  for (int i = 0; i < n; i++) {
    int v = 0;

    std::cin >> v;

    A->at(i) = v;
  }

  int count = 0;

  int left = 0;
  int right = A->size();
  int mid = (left + right) / 2;

  merge_sort(A, left, right, &count);

  for (int i = 0; i < n; i++) {
    if (i < (n - 1)) {
      std::cout << A->at(i) << " ";
    } else {
      std::cout << A->at(i) << std::endl;
    }
  }

  std::cout << count << std::endl;

  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int merges(int start, int end) {
  int middle;
  if (start + 1 < end) {

    middle = (start + end) / 2;
    return (merges(start, middle) + merges(middle, end) - start + end);
  } else {
    return 0;
  }
}

int main() {
  int n, dat[500000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << dat[i];
  }

  cout << endl << merges(0, n) << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int merges(int start, int end) {
  int middle;
  if (start + 1 < end) {

    middle = (start + end) / 2;
    return (merges(start, middle) + merges(middle, end) - start + end);
  } else {
    return 0;
  }
}

int main() {
  int n, dat[5000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << dat[i];
  }

  cout << endl << merges(0, n) << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 50000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 500000
#define CENTINEL 2000000000
int L[MAX / 2 + 2], R[MAX / 2 + 2];
int sum = 0;
int n, A[MAX];
void merge(int A[], int left, int mid, int right) {
  int na = mid - left;
  int nb = right - mid;
  for (int i = 0; i < na; i++)
    L[i] = A[left + i];
  for (int i = 0; i < nb; i++)
    R[i] = A[mid + i];
  L[na] = CENTINEL;
  R[nb] = CENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    sum++;
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
  }
}
void mergesort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergesort(A, left, mid);
    mergesort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergesort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    printf("%d ", A[i]);
  printf("%d\\n", A[n - 1]);
  printf("%d\\n", sum);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 500000
#define INFTY 20000000
int L[MAX / 2 + 1], R[MAX / 2 + 1];
int n, sum;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
    sum++;
  }
}
void mergesort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergesort(A, left, mid);
    mergesort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  int A[MAX];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergesort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    printf("%d ", A[i]);
  printf("%d\\n", A[n - 1]);
  printf("%d\\n", sum);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

class Merge {
public:
  Merge() : count(0) {}

  void mergeSort(int A[], int left, int right) {
    if (left + 1 >= right) {
      return;
    }
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }

  int getCount() { return count; }

private:
  void merge(int A[], int left, int mid, int right) {
    int numL = mid - left;
    int numR = right - mid;

    //?????§A??????????????´??°???????????§??\\?????????????????????????????????
    int L[250002];
    int R[250002];
    for (int i = 0; i < numL; i++) {
      L[i] = A[left + i];
    }
    for (int i = 0; i < numR; i++) {
      R[i] = A[mid + i];
    }
    //??????????????¨????????\\???????????????
    L[numL] = 2000000000;
    R[numR] = 2000000000;
    int iL = 0;
    int iR = 0;
    for (int j = left; j < right; j++) {
      if (L[iL] <= R[iR]) {
        A[j] = L[iL];
        iL++;
      } else {
        A[j] = R[iR];
        iR++;
      }
      count++;
    }
  }

  int count;
};

int main() {
  int A[500000];
  int numA;
  Merge mer;
  cin >> numA;
  for (int i = 0; i < numA; i++) {
    cin >> A[i];
  }

  mer.mergeSort(A, 0, numA);

  for (int i = 0; i < numA; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << mer.getCount() << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

class Merge {
public:
  Merge() : count(0) {}

  void mergeSort(int A[], int left, int right) {
    if (left + 1 >= right) {
      return;
    }
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }

  int getCount() { return count; }

private:
  void merge(int A[], int left, int mid, int right) {
    int numL = mid - left;
    int numR = right - mid;

    //?????§A??????????????´??°???????????§??\\?????????????????????????????????
    int L[25000];
    int R[25000];
    for (int i = 0; i < numL; i++) {
      L[i] = A[left + i];
    }
    for (int i = 0; i < numR; i++) {
      R[i] = A[mid + i];
    }
    //??????????????¨????????\\???????????????
    L[numL] = 1000000001;
    R[numR] = 1000000001;
    int iL = 0;
    int iR = 0;
    for (int j = left; j < right; j++) {
      if (L[iL] <= R[iR]) {
        A[j] = L[iL];
        iL++;
      } else {
        A[j] = R[iR];
        iR++;
      }
      count++;
    }
  }

  int count;
};

int main() {
  int A[50000];
  int numA;
  Merge mer;
  cin >> numA;
  for (int i = 0; i < numA; i++) {
    cin >> A[i];
  }

  mer.mergeSort(A, 0, numA);

  for (int i = 0; i < numA; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << mer.getCount() << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

constexpr int MAX_N = 500'000;
constexpr int MAX_VAL = 1e9 + 1;

int merge(int *A, const int &left, const int &mid, const int &right) {
  const int n1 = mid - left;
  const int n2 = right - mid;

  auto L = new int[n1 + 1];
  auto R = new int[n2 + 1];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }

  L[n1] = MAX_VAL;
  R[n2] = MAX_VAL;

  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
  delete[] L;
  delete[] R;
  return right - left;
}

int mergeSort(int *A, const int &left, const int &right) {
  int count = 0;
  if (left + 1 < right) {
    const int mid = (left + right) / 2;
    count += mergeSort(A, left, mid);
    count += mergeSort(A, mid, right);
    return count + merge(A, left, mid, right);
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);

  int A[MAX_N];

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

  int count = mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;
  cout << count << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

constexpr int MAX_N = 500'000;
constexpr int MAX_VAL = 1e9 + 1;

int merge(int *A, const int &left, const int &mid, const int &right) {
  const int n1 = mid - left;
  const int n2 = right - mid;

  auto L = new int[n1 + 1];
  auto R = new int[n2 + 1];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }

  L[n1] = MAX_VAL;
  R[n2] = MAX_VAL;

  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
  return right - left;
}

int mergeSort(int *A, const int &left, const int &right) {
  int count = 0;
  if (left + 1 < right) {
    const int mid = (left + right) / 2;
    count += mergeSort(A, left, mid);
    count += mergeSort(A, mid, right);
    return count + merge(A, left, mid, right);
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);

  int A[MAX_N];

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

  int count = mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;
  cout << count << endl;

  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <vector>

typedef std::vector<int> IntVector;

void Input(IntVector &Inputs) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int Count;
  std::cin >> Count;
  Inputs.resize(Count);
  for (int i = 0; i < Count; i++) {
    int N = 0;
    std::cin >> N;
    Inputs[i] = N;
  }
}

void Output(IntVector &Outputs) {
  for (unsigned i = 0; i < Outputs.size(); i++) {
    std::cout << Outputs[i];
    if (i + 1 < Outputs.size()) {
      std::cout << " ";
    } else {
      std::cout << std::endl;
    }
  }
}

int merge(IntVector &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  IntVector L(n1 + 1);
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  IntVector R(n2 + 1);
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;

  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
  return i + j;
}

int mergeSort(IntVector &A, int left, int right) {
  int Count = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Count += mergeSort(A, left, mid);
    Count += mergeSort(A, mid, right);
    return Count + merge(A, left, mid, right);
  }
  return 0;
}

int main() {
  IntVector Inputs;
  Input(Inputs);
  int CompareCount = mergeSort(Inputs, 0, Inputs.size());
  Output(Inputs);
  std::cout << CompareCount << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

typedef std::vector<int> IntVector;

void Input(IntVector &Inputs) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int Count;
  std::cin >> Count;
  Inputs.resize(Count);
  for (int i = 0; i < Count; i++) {
    int N = 0;
    std::cin >> N;
    Inputs[i] = N;
  }
}

void Output(IntVector &Outputs) {
  for (unsigned i = 0; i < Outputs.size(); i++) {
    std::cout << Outputs[i] << std::flush;
    if (i + 1 < Outputs.size()) {
      std::cout << " " << std::flush;
    } else {
      std::cout << std::endl;
    }
  }
}

int merge(IntVector &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  IntVector L(n1 + 1);
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  IntVector R(n2 + 1);
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;

  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
  return i + j;
}

int mergeSort(IntVector &A, int left, int right) {
  int Count = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Count += mergeSort(A, left, mid);
    Count += mergeSort(A, mid, right);
    return Count + merge(A, left, mid, right);
  }
  return 0;
}

int main() {
  IntVector Inputs;
  Input(Inputs);
  int CompareCount = mergeSort(Inputs, 0, Inputs.size());
  Output(Inputs);
  std::cout << CompareCount << std::endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; i++) {
    cnt++;
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int count;

void merge(int A[], int left, int mid, int right, int n) {

  int n1, n2, L[250001], R[250001], i, j, k;
  n1 = mid - left;
  n2 = right - mid;
  for (i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = 2000000000;
  R[n2] = 2000000000;
  i = 0;
  j = 0;
  for (k = left; k < right; k++) {
    count++;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
}

void mergeSort(int A[], int left, int right, int n) {

  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    mergeSort(A, left, mid, n);
    mergeSort(A, mid, right, n);
    merge(A, left, mid, right, n);
  }
}

int main(void) {

  int A[500000], n, i;
  count = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  mergeSort(A, 0, n, n);

  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    printf("%d", A[i]);
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 500000
#define S 200000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int c;

void merge(int a[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++) {
    L[i] = a[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = a[mid + i];
  }
  L[n1] = R[n2] = S;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    c++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];
    } else {
      a[k] = R[j++];
    }
  }
}

void mergeSort(int a[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(a, n, left, mid);
    mergeSort(a, n, mid, right);
    merge(a, n, left, mid, right);
  }
}

int main() {
  int a[MAX], n, i;
  c = 0;

  cin >> n;

  for (i = 0; i < n; i++)
    cin >> a[i];

  mergeSort(a, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
  cout << c << endl;

  return 0;
}