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

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

using namespace std;

int n, A[20], s[20], b[1000000], bc = 0, q, m;

bool rec(int i) {
  if (i == n) {
    int sum = 0;
    for (int j = 0; j < n; ++j) {
      // cout << s[j] << ",";
      if (s[j])
        sum += A[j];
    }
    b[bc] = sum;
    ++bc;
    // cout << endl;
    return 0;
  }

  rec(i + 1);
  s[i] = 1;
  rec(i + 1);
  s[i] = 0;
}

void combi() {
  for (int i = 0; i < n; ++i)
    ;
  rec(0);
}

bool judge(int in) {
  for (int i = 0; i < bc; ++i)
    if (in == b[i])
      return true;
  return false;
}

int main() {

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

  combi();

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> m;
    if (judge(m))
      cout << "yes";
    else
      cout << "no";
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int n, A[20], s[20], b[10000000], bc = 0, q, m;

bool rec(int i) {
  if (i == n) {
    int sum = 0;
    for (int j = 0; j < n; ++j) {
      // cout << s[j] << ",";
      if (s[j])
        sum += A[j];
    }
    b[bc] = sum;
    ++bc;
    // cout << endl;
    return 0;
  }

  rec(i + 1);
  s[i] = 1;
  rec(i + 1);
  s[i] = 0;
}

void combi() {
  for (int i = 0; i < n; ++i)
    ;
  rec(0);
}

bool judge(int in) {
  for (int i = 0; i < bc; ++i)
    if (in == b[i])
      return true;
  return false;
}

int main() {

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

  combi();

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> m;
    if (judge(m))
      cout << "yes";
    else
      cout << "no";
    cout << endl;
  }

  return 0;
}
Problem p02271 (C++)

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

using namespace std;

bool isSumExist(vector<int> A, int pos, int digit) {
  if (digit == 0) {
    return (true);
  }
  if (pos >= A.size() || digit < 0) {
    return (false);
  }
  return (isSumExist(A, pos + 1, digit - A[pos]) ||
          isSumExist(A, pos + 1, digit));
}

void solve() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int m;
    cin >> m;
    if (isSumExist(A, 0, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}

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

using namespace std;

bool isSumExist(vector<int> &A, int pos, int digit) {
  if (digit == 0) {
    return (true);
  }
  if (pos >= A.size() || digit < 0) {
    return (false);
  }
  return (isSumExist(A, pos + 1, digit - A[pos]) ||
          isSumExist(A, pos + 1, digit));
}

void solve() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int m;
    cin >> m;
    if (isSumExist(A, 0, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p02271 (C++)

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

#define N_MAX 20
#define M_MAX 2001
#define NIL -1

using namespace std;

int n;
int a[N_MAX], dp[N_MAX][M_MAX];

// can make m after a[i] or not
bool solve(int i, int m) {
  if (dp[i][m] != NIL)
    return dp[i][m];
  if (m == 0) {
    dp[i][m] = 1;
  } else if (i >= n) {
    dp[i][m] = 0;
  } else if (solve(i + 1, m)) {
    dp[i][m] = 1;
  } else if (solve(i + 1, m - a[i])) {
    dp[i][m] = 1;
  } else {
    dp[i][m] = 0;
  }
  return dp[i][m];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M_MAX; j++) {
      dp[i][j] = NIL;
    }
  }
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (solve(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Fixed submission:
#include <iostream>

#define N_MAX 100
#define M_MAX 100000
#define NIL -1

using namespace std;

int n;
int a[N_MAX], dp[N_MAX][M_MAX];

// can make m after a[i] or not
bool solve(int i, int m) {
  if (dp[i][m] != NIL)
    return dp[i][m];
  if (m == 0) {
    dp[i][m] = 1;
  } else if (i >= n) {
    dp[i][m] = 0;
  } else if (solve(i + 1, m)) {
    dp[i][m] = 1;
  } else if (solve(i + 1, m - a[i])) {
    dp[i][m] = 1;
  } else {
    dp[i][m] = 0;
  }
  return dp[i][m];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M_MAX; j++) {
      dp[i][j] = NIL;
    }
  }
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (solve(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <climits>
#include <iostream>
#include <list>   //???????????????????????????
#include <vector> //????????¢??????????????????
using namespace std;

bool solve(int i, int m, vector<int> A) {
  if (m == 0) {
    return true;
  }

  int n = A.size();

  if (i >= n) {
    return false;
  }

  bool res = solve(i + 1, m, A) || solve(i + 1, m - A[i], A);
  return res;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  vector<int> mi(q);
  for (int i = 0; i < q; i++) {
    cin >> mi[i];
  }
  for (int j = 0; j < q; j++) {

    if (solve(0, mi[j], A)) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }
}

Fixed submission:
#include <climits>
#include <iostream>
#include <list>   //???????????????????????????
#include <vector> //????????¢??????????????????
using namespace std;

bool solve(int i, int m, vector<int> &A) {
  if (m == 0) {
    return true;
  }

  int n = A.size();

  if (i >= n) {
    return false;
  }

  bool res = solve(i + 1, m, A) || solve(i + 1, m - A[i], A);
  return res;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  vector<int> mi(q);
  for (int i = 0; i < q; i++) {
    cin >> mi[i];
  }
  for (int j = 0; j < q; j++) {

    if (solve(0, mi[j], A)) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }
}
Problem p02271 (C++)

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

int A[MAX], n;

bool check(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = check(i + 1, m) || check(i + 1, m - A[i]);
  return res;
}

int main() {
  int q, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", m);
    if (check(0, m)) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }
}

Fixed submission:
#include <stdio.h>
#define MAX 20

int A[MAX], n;

bool check(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = check(i + 1, m) || check(i + 1, m - A[i]);
  return res;
}

int main() {
  int q, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &m);
    if (check(0, m)) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }
}
Problem p02271 (C++)

Buggy submission (status: Runtime Error):
// 枝刈り無し
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int n, q;
vector<int> A;

bool solve(int i, int m) {
  if (m == 0)
    return true;
  return solve(i + 1, m) || solve(i + 1, m - A[i]);
}

int main() {
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; ++j) {
    int m;
    cin >> m;
    bool ans = solve(0, m);
    if (ans)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}


Fixed submission:
// 枝刈り無し
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int n, q;
vector<int> A;

bool solve(int i, int m) {
  if (m == 0)
    return true;
  if (i >= n)
    return false;
  return solve(i + 1, m) || solve(i + 1, m - A[i]);
}

int main() {
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; ++j) {
    int m;
    cin >> m;
    bool ans = solve(0, m);
    if (ans)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02271 (C++)

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

bool serach(std::vector<int>, int);
bool serach(std::vector<int>, int, int, int);

int main(int argc, char const *argv[]) {
  int n, q;
  int a, m;
  std::vector<int> A;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a;
    A.push_back(a);
  }

  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    std::cin >> m;
    if (serach(A, m))
      std::cout << "yes" << std::endl;
    else
      std::cout << "no" << std::endl;
  }

  return 0;
}

bool serach(std::vector<int> A, int m) {
  for (int i = 0; i < A.size(); ++i) {
    if (serach(A, 0, m, i))
      return true;
  }
  return false;
}

bool serach(std::vector<int> A, int sum, int m, int I) {
  sum += A[I];
  if (sum == m) {
    return true;
  } else if (sum > m) {
    return false;
  } else {
    A.erase(A.begin() + I);
    for (int i = 0; i < A.size(); ++i) {
      if (serach(A, sum, m, i))
        return true;
    }
    return false;
  }
}


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

bool serach(std::vector<int>, int);
bool serach(std::vector<int>, int, int, int);

int main(int argc, char const *argv[]) {
  int n, q;
  int a, m;
  std::vector<int> A;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a;
    A.push_back(a);
  }

  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    std::cin >> m;
    if (serach(A, m))
      std::cout << "yes" << std::endl;
    else
      std::cout << "no" << std::endl;
  }

  return 0;
}

bool serach(std::vector<int> A, int m) {
  for (int i = 0; i < A.size(); ++i) {
    if (serach(A, 0, m, i))
      return true;
  }
  return false;
}

bool serach(std::vector<int> A, int sum, int m, int I) {
  sum += A[I];
  if (sum == m) {
    return true;
  } else if (sum > m) {
    return false;
  } else {
    for (int i = I + 1; i < A.size(); ++i) {
      if (serach(A, sum, m, i))
        return true;
    }
    return false;
  }
}
Problem p02271 (C++)

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

bool solve(int A[], int size, int index, int remain) {
  bool a, b;
  if (remain == 0) {
    return true;
  } else if (size <= index || remain < 0) {
    return false;
  } else {
    a = solve(A, size, index + 1, remain);
    b = solve(A, size, index + 1, remain - A[index]);
  }
  return a || b;
}
int main() {
  int n, q, A[2000], M[2000];

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

  for (int i = 0; i < q; i++) {
    int m;
    scanf("%d", &m);
    if (solve(A, q, 0, m)) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;
}

Fixed submission:
#include <cstdio>

bool solve(int A[], int size, int index, int remain) {
  bool a, b;
  if (remain == 0) {
    return true;
  } else if (size <= index || remain < 0) {
    return false;
  } else {
    a = solve(A, size, index + 1, remain);
    b = solve(A, size, index + 1, remain - A[index]);
  }
  return a || b;
}
int main() {
  int n, q, A[2000], M[2000];

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

  for (int i = 0; i < q; i++) {
    int m;
    scanf("%d", &m);
    if (solve(A, n, 0, m)) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

bool solve(vector<int> a, int target) {
  int last = a[a.size() - 1];
  if (last == target)
    return true;
  a.pop_back();
  if (a.empty())
    return false;
  return (solve(a, target) || solve(a, target - last));
}

int main() {
  int n, q, x;
  vector<int> a;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d", &x);
    a.push_back(x);
  }
  scanf("%d", &q);
  REP(i, q) {
    scanf("%d", &x);
    if (solve(a, x))
      printf("yes\n");
    else
      printf("no\n");
  }
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

bool solve(vector<int> a, int target) {
  int last = a[a.size() - 1];
  if (last == target)
    return true;
  a.pop_back();
  if (a.empty())
    return false;
  return (solve(a, target - last) || solve(a, target));
}

int main() {
  int n, q, x;
  vector<int> a;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d", &x);
    a.push_back(x);
  }
  scanf("%d", &q);
  REP(i, q) {
    scanf("%d", &x);
    if (solve(a, x))
      printf("yes\n");
    else
      printf("no\n");
  }
  return 0;
}
Problem p02271 (C++)

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

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

  int n, q;
  bool a[2001] = {0};
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 2001; j >= 0; j--) {
      if (a[j]) {
        a[j + x] = true;
      }
    }
    a[x] = true;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x;
    if (a[x]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

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

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

  int n, q;
  bool a[2001] = {0};
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 2000; j >= 0; j--) {
      if (a[j]) {
        a[j + x] = true;
      }
    }
    a[x] = true;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x;
    if (a[x]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int F[MAX_N];
int NS;
int NT;
int S[MAX_N];
int T[MAX_N];

bool flag;
int target;

void print(int F[], int N) {
  rep(i, N) { cout << F[i] << " "; }
  cout << endl;
}

void Judge(int A[], int F[], int N) {
  int ret = 0;
  rep(i, N) { ret += A[i] * F[i]; }
  if (ret == target)
    flag = true;
}

void rec(int A[], int F[], int N, int i) {
  if (i == N) {
    Judge(A, F, N);
    return;
  }

  F[i] = 0;
  rec(A, F, N, i + 1);

  F[i] = 1;
  rec(A, F, N, i + 1);
};

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  rep(i, NT) {
    flag = false;
    target = T[i];
    rec(S, F, NS, i);
    cout << (flag ? "yes" : "no") << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int F[MAX_N];
int NS;
int NT;
int S[MAX_N];
int T[MAX_N];

bool flag;
int target;

void print(int F[], int N) {
  rep(i, N) { cout << F[i] << " "; }
  cout << endl;
}

void Judge(int A[], int F[], int N) {
  int ret = 0;
  rep(i, N) { ret += A[i] * F[i]; }
  if (ret == target)
    flag = true;
}

void rec(int A[], int F[], int N, int i) {
  if (i == N) {
    Judge(A, F, N);
    return;
  }

  F[i] = 0;
  rec(A, F, N, i + 1);

  F[i] = 1;
  rec(A, F, N, i + 1);
};

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  rep(i, NT) {
    flag = false;
    target = T[i];
    rec(S, F, NS, 0);
    cout << (flag ? "yes" : "no") << endl;
  }
}
Problem p02271 (C++)

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

int n, A[30];

int solve(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = solve(i + 1, m) || solve(i + 1, m - A[i]);
  return res;
}

int main() {
  int q;
  int M[210];

  cin >> n;
  for (int i = 0; i < n; n++) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> M[j];
  }
  for (int j = 0; j < q; j++) {
    if (solve(0, M[j]))
      cout << "yes" << endl;
    else {
      cout << "no" << endl;
    }
  }
  return 0;
}

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

int n, A[30];

int solve(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = solve(i + 1, m) || solve(i + 1, m - A[i]);
  return res;
}

int main() {
  int q;
  int M[210];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> M[j];
  }
  for (int j = 0; j < q; j++) {
    if (solve(0, M[j]))
      cout << "yes" << endl;
    else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int data[20], n;

bool solve(int i, int m) {
  if (i > n)
    return false;
  if (m - data[i] == 0)
    return true;
  if (solve(i + 1, m - data[i]))
    return true;
  if (solve(i + 1, m))
    return true;

  return false;
}

int main() {
  for (; cin >> n, n;) {
    for (int i = 0; i < n; i++)
      cin >> data[i];
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int tmp;
      cin >> tmp;
      if (solve(0, tmp))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int data[20], n;

bool solve(int i, int m) {
  if (i > n)
    return false;
  if (m - data[i] == 0)
    return true;
  if (solve(i + 1, m - data[i]))
    return true;
  if (solve(i + 1, m))
    return true;

  return false;
}

int main() {
  for (; cin >> n;) {
    for (int i = 0; i < n; i++)
      cin >> data[i];
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int tmp;
      cin >> tmp;
      if (solve(0, tmp))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}
Problem p02271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdint>
#include <vector>

using namespace std;

static bool aux(vector<uint32_t> vec, uint32_t index, int32_t val) {
  if (val == 0)
    return true;

  if (vec.size() == index || (val < 0)) {
    return false;
  }

  int32_t rem = val - vec[index];

  return aux(vec, index + 1, rem) || aux(vec, index + 1, val);
}

bool exausitve_search_search(vector<uint32_t> vec, int32_t val) {
  return aux(vec, 0, val);
}

#if !defined(TEST)
#include <iostream>

static vector<uint32_t> array;

int32_t main() {
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;

    if (exausitve_search_search(array, val)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

#endif

Fixed submission:
#include <cstdint>
#include <vector>

using namespace std;

static bool aux(vector<uint32_t> &vec, uint32_t index, int32_t val) {
  if (val == 0)
    return true;

  if (vec.size() == index || (val < 0)) {
    return false;
  }

  int32_t rem = val - vec[index];

  return aux(vec, index + 1, rem) || aux(vec, index + 1, val);
}

bool exausitve_search_search(vector<uint32_t> vec, int32_t val) {
  return aux(vec, 0, val);
}

#if !defined(TEST)
#include <iostream>

static vector<uint32_t> array;

int32_t main() {
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;

    if (exausitve_search_search(array, val)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

#endif
Problem p02271 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int m, n, q, A[20];
int ans(int x, int y) {
  if (x == 0)
    return 0;
  if (ans(x, y + 1) == 0 || ans(x - A[y], y + 1) == 0)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (ans(m, 0) == 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int m, n, q, A[20];
int ans(int x, int y) {
  if (x == 0)
    return 0;
  if (y >= n)
    return 1;
  if (ans(x, y + 1) == 0 || ans(x - A[y], y + 1) == 0)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (ans(m, 0) == 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, a[21], num;

bool saiki(int i, int sum) {
  if (sum == num)
    return 1;
  if (saiki(i + 1, a[i] + sum))
    return 1;
  if (saiki(i + 1, sum))
    return 1;
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  while (q--) {
    cin >> num;
    if (saiki(0, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, a[21], num;

bool saiki(int i, int sum) {
  if (sum == num)
    return 1;
  if (i == n)
    return 0;
  if (saiki(i + 1, a[i] + sum))
    return 1;
  if (saiki(i + 1, sum))
    return 1;
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  while (q--) {
    cin >> num;
    if (saiki(0, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

struct Node {
  size_t v;
  bool used[20];
  char padding[4];

  Node() : v(0) { memset(used, false, 20); }
};

static vector<int> FindPossibleValues(const size_t *A, int n, size_t size) {
  vector<int> values(size + 1);
  values[size] = 1;

  queue<Node> q;
  Node root;
  q.push(root);
  while (!q.empty()) {
    const Node p = q.front();
    q.pop();

    for (int i = 0; i < n; ++i) {
      if (p.used[i])
        continue;
      Node new_node;
      new_node.v = p.v + A[i];
      memcpy(new_node.used, p.used, static_cast<size_t>(n));
      new_node.used[i] = true;
      q.push(new_node);
      values[new_node.v] = 1;
    }
  }

  return values;
}

int main() {
  int n;
  size_t A[20];

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

  vector<int> values = FindPossibleValues(A, n, sum);

  int q;
  scanf("%d", &q);
  size_t m = 0;
  for (int i = 0; i < q; ++i) {
    scanf("%lu", &m);
    if (m < values.size() && values[m]) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

struct Node {
  size_t v;
  bool used[20];
  char padding[4];

  Node() : v(0) { memset(used, false, 20); }
};

static vector<int> FindPossibleValues(const size_t *A, int n, size_t size) {
  vector<int> values(size + 1);
  values[size] = 1;

  queue<Node> q;
  Node root;
  q.push(root);
  while (!q.empty()) {
    const Node p = q.front();
    q.pop();

    for (int i = 0; i < n; ++i) {
      if (p.used[i] || values[p.v + A[i]])
        continue;
      Node new_node;
      new_node.v = p.v + A[i];
      memcpy(new_node.used, p.used, static_cast<size_t>(n));
      new_node.used[i] = true;
      q.push(new_node);
      values[new_node.v] = 1;
    }
  }

  return values;
}

int main() {
  int n;
  size_t A[20];

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

  vector<int> values = FindPossibleValues(A, n, sum);

  int q;
  scanf("%d", &q);
  size_t m = 0;
  for (int i = 0; i < q; ++i) {
    scanf("%lu", &m);
    if (m < values.size() && values[m]) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define INFTY 2000000000
int count = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *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] = INFTY;
  R[n2] = INFTY;
  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++;
    }
    count++;
  }
  delete[] L;
  delete[] R;
}
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 main() {
  int n;
  cin >> n;
  int A[10];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << ' ';
  }
  cout << A[n - 1] << endl;
  cout << count << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
#define INFTY 2000000000
int count = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *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] = INFTY;
  R[n2] = INFTY;
  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++;
    }
    count++;
  }
  delete[] L;
  delete[] R;
}
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 main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << ' ';
  }
  cout << A[n - 1] << endl;
  cout << count << endl;
}
Problem p02272 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

void merge(vector<int> &A, int left, int mid, int right, int &c) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1, 0);
  vector<int> R(n2 + 1, 0);
  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] = 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++];
      c++;
    } else {
      A[k] = R[j++];
      c++;
    }
  }
}
void mergeSort(vector<int> &A, int left, int right, int &c) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid, c);
    mergeSort(A, mid, right, c);
    merge(A, left, mid, right, c);
  }
}

int main() {

  int count = 0;
  int n = 0;
  cin >> n;
  vector<int> array(n, 0);
  for (int i = 0; i < n; i++)
    cin >> array[i];

  mergeSort(array, 0, array.size(), count);

  for (int n : array)
    cout << n << ' ';
  cout << endl;
  cout << count << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

void merge(vector<int> &A, int left, int mid, int right, int &c) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1, 0);
  vector<int> R(n2 + 1, 0);
  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] = 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++];
      c++;
    } else {
      A[k] = R[j++];
      c++;
    }
  }
}
void mergeSort(vector<int> &A, int left, int right, int &c) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid, c);
    mergeSort(A, mid, right, c);
    merge(A, left, mid, right, c);
  }
}

int main() {

  int count = 0;
  int n = 0;
  cin >> n;
  vector<int> array(n, 0);
  for (int i = 0; i < n; i++)
    cin >> array[i];

  mergeSort(array, 0, array.size(), count);

  for (int i = 0; i < array.size(); i++) {
    if (i != array.size() - 1) {
      cout << array[i] << ' ';
    } else
      cout << array[i] << endl;
  }
  cout << count << endl;

  return 0;
}
Problem p02272 (C++)

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

#define SENTINEL 1000000001

int count = 0;

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\n");
}

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1], R[n2];
  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] = SENTINEL;
  R[n2] = SENTINEL;
  int m = 0;
  int n = 0;
  for (int i = left; i < right; ++i) {
    if (L[m] <= R[n]) {
      A[i] = L[m];
      ++m;
    } else {
      A[i] = R[n];
      ++n;
    }
    count++;
  }
}

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

int main() {
  int N;
  int A[100];

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

  MergeSort(A, 0, N);
  trace(A, N);
  printf("%d\n", count);
  return 0;
}

Fixed submission:
#include <stdio.h>

#define SENTINEL 1000000001

int count = 0;

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\n");
}

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1], R[n2];
  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] = SENTINEL;
  R[n2] = SENTINEL;
  int m = 0;
  int n = 0;
  for (int i = left; i < right; ++i) {
    if (L[m] <= R[n]) {
      A[i] = L[m];
      ++m;
    } else {
      A[i] = R[n];
      ++n;
    }
    count++;
  }
}

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

int main() {
  int N;
  int A[500000];

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

  MergeSort(A, 0, N);
  trace(A, N);
  printf("%d\n", count);
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: 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++;
    }
  }
  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;
}

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++;
    }
  }
  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;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits>
#define INFINITY 0xFFFFFF
void mergeSort(int *A, int left, int right);
using namespace std;
int ct = 0;
int main() {
  int *S;
  int n;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  /*for (int i = 0; i < n; ++i) {
  cout << S[i];
  cout << " ";
  }
  cout << endl;
  */
  mergeSort(S, 0, n);

  for (int i = 0; i < n - 1; ++i) {
    cout << S[i];
    cout << " ";
  }
  cout << S[n - 1] << endl;
  cout << ct << endl;

  return 0;
}

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L;
  int *R;
  L = new int[n1 + 1];
  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] = (int)INFINITY;
  R[n2] = (int)INFINITY;
  int i = 0;
  int j = 0;

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

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);
  }
}


Fixed submission:
#include <iostream>
#include <limits>
#define INFINITY 0x7FFFFFFF
void mergeSort(int *A, int left, int right);
using namespace std;
int ct = 0;
int main() {
  int *S;
  int n;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  /*for (int i = 0; i < n; ++i) {
  cout << S[i];
  cout << " ";
  }
  cout << endl;
  */
  mergeSort(S, 0, n);

  for (int i = 0; i < n - 1; ++i) {
    cout << S[i];
    cout << " ";
  }
  cout << S[n - 1] << endl;
  cout << ct << endl;

  return 0;
}

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L;
  int *R;
  L = new int[n1 + 1];
  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] = (int)INFINITY;
  R[n2] = (int)INFINITY;
  int i = 0;
  int j = 0;

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

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);
  }
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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 argc, char const *argv[]) {
  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;
}


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; 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 argc, char const *argv[]) {
  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 (status: Runtime Error):
#include <iostream>

using namespace std;

const int MAX = 50000;
const int 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;
  cnt = 0;

  cin >> n;

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

  mergeSort(A, n, 0, n);

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

  cout << endl;

  cout << cnt << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

const int MAX = 500000;
const int 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;
  cnt = 0;

  cin >> n;

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

  mergeSort(A, n, 0, n);

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

  cout << endl;

  cout << cnt << endl;
}
Problem p02272 (C++)

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

using namespace std;

int num_comp = 0;
const int MAX = 500000;
int L[MAX / 2 + 2];
int R[MAX / 2 + 2];
const int SENTINEL = 200000000;

// Merge
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] = SENTINEL;
  R[n2] = SENTINEL;

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

// Merge sort
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);
  }
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Merge sort
  Mergesort(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << num_comp << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int num_comp = 0;
const int MAX = 500000;
int L[MAX / 2 + 2];
int R[MAX / 2 + 2];
const int SENTINEL = 2000000000;

// Merge
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] = SENTINEL;
  R[n2] = SENTINEL;

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

// Merge sort
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);
  }
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Merge sort
  Mergesort(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << num_comp << endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 500000
#define Sentinel 19970328

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];
  }
  int i = 0, j = 0;
  L[n1] = R[n2] = Sentinel;
  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 = (right + left) / 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;
}

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];
  }
  int i = 0, j = 0;
  L[n1] = R[n2] = Sentinel;
  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 = (right + left) / 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;
}