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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define int ll
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (ll i = (ll)(b)-1; i >= a; --i)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9 + 7;
void cioacc() { // accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
pair<vl, vb> eratosthenes_sieve(ll n) {
  vb is_prime(n + 1, true);
  vl prime;
  is_prime[0] = false;
  is_prime[1] = false;
  FOR(i, 2, sqrt(n) + 1) {
    if (!is_prime[i])
      continue;
    for (ll j = 2 * i; j <= n; j += i)
      is_prime[j] = false;
    prime.eb(i);
  }
  return {prime, is_prime};
}
signed main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  int q;
  cin >> q;
  set<int> st;
  for (int i = 0; i < (1 << n); ++i) {
    int t = 0;
    REP(j, n) if (i >> j & 1) t += a[j];
    st.insert(t);
  }
  REP(i, q) {
    int m;
    cin >> m;
    if (st.find(m) != st.end())
      puts("yes");
    else
      puts("no");
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define int ll
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (ll i = (ll)(b)-1; i >= a; --i)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9 + 7;
void cioacc() { // accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
pair<vl, vb> eratosthenes_sieve(ll n) {
  vb is_prime(n + 1, true);
  vl prime;
  is_prime[0] = false;
  is_prime[1] = false;
  FOR(i, 2, sqrt(n) + 1) {
    if (!is_prime[i])
      continue;
    for (ll j = 2 * i; j <= n; j += i)
      is_prime[j] = false;
    prime.eb(i);
  }
  return {prime, is_prime};
}
signed main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  int q;
  cin >> q;
  REP(i, q) {
    int m;
    cin >> m;
    for (int i = 0; i < (1 << n); ++i) {
      int t = 0;
      REP(j, n) {
        if (i >> j & 1)
          t += a[j];
      }
      if (t == m) {
        puts("yes");
        break;
      } else if (i == (1 << n) - 1)
        puts("no");
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
bool solve(vector<int> &a, int num, int x) {
  int size = a.size();
  if (x == 0)
    return true;
  if (num >= size)
    return false;
  int ans1 = solve(a, num + 1, x);
  int ans2 = solve(a, num + 1, x - a[num]);
  if (ans1 == 1)
    return true;
  if (ans2 == 1)
    return true;
  return false;
}

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


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
bool solve(vector<int> a, int num, int x) {
  int size = a.size();
  if (x == 0)
    return true;
  if (num >= size)
    return false;
  int ans1 = solve(a, num + 1, x);
  int ans2 = solve(a, num + 1, x - a[num]);
  if (ans1 == 1)
    return true;
  if (ans2 == 1)
    return true;
  return false;
}

int main() {
  int n, m, i;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> m;
  int b[n];
  for (i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (i = 0; i < m; i++) {
    if (solve(a, 0, b[i]) == 1) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

#define MAX 1000

int dat[MAX];
int ind[MAX];
int dp[21][1000];
int n;

bool solve(int i, int m) {
  // cout<<"solve i:"<<i<<" m:"<<m<<" n:"<<n<<endl;
  if (m == 0) {
    return true;
  } else if (i < n) {
    if (solve(i + 1, m))
      return true;
    return solve(i + 1, m - dat[i]);
  }
  return false;
}

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

#define MAX 500

int dat[MAX];
int dp[21][1000];
int n;

bool solve(int i, int m) {
  // cout<<"solve i:"<<i<<" m:"<<m<<" n:"<<n<<endl;
  if (m == 0) {
    return true;
  } else if (i < n) {
    if (solve(i + 1, m))
      return true;
    return solve(i + 1, m - dat[i]);
  }
  return false;
}

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

  for (int i = 0; i < n; i++) {
    scanf("%d", &dat[i]);
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> m;
    if (solve(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
int m, a[20], n;

bool dfs(int v, int d) {
  if (v == m)
    return true;
  if (v > m || d == n)
    return false;
  bool ret = dfs(v + a[d], d + 1);
  if (!ret)
    ret = dfs(v, d + 1);
  return ret;
}

int main() {
  int q;
  cin >> n;
  rep(i, n) cin >> a[i];
  cin >> q;
  while (q--) {
    cin >> m;
    cout << (dfs(0, 0) ? "yes" : "no") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {
  int n, q, a[20];
  cin >> n;
  rep(i, n) cin >> a[i];
  cin >> q;
  while (q--) {
    int m;
    unsigned int i = 0;
    cin >> m;
    for (; i < (1 << n); i++) {
      int sum = 0;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j))
          sum += a[j];
      }
      if (sum == m) {
        cout << "yes" << endl;
        break;
      }
    }
    if (i == (1 << n))
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int n, A[50];

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, M;
  scanf("%d", &n);
  REP(i, n) { scanf("%d", &A[i]); }
  scanf("%d", &q);
  REP(i, q) {
    scanf("%d", &M);
    if (solve(0, M)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

int main() {
  int n, q, A[21], m[200];
  scanf("%d", &n);
  FOR(i, 1, n + 1) { scanf(" %d", &A[i]); }
  scanf("%d", &q);
  REP(i, q) { scanf(" %d", &m[i]); }

  REP(i1, q) {
    bool f = false;
    REP(i, pow(2, n)) {
      int sum = 0, j = i, k = 1;
      while (j > 0) {
        if (j % 2 == 1) {
          sum += A[k];
        }
        k++;
        j /= 2;
      }
      if (sum == m[i1]) {
        f = true;
        break;
      }
    }
    if (f) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;

int n;
int A[2000];

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

int main(int argc, char const *argv[]) {
  std::cin >> n;
  for (size_t i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  int q;
  std::cin >> q;

  for (size_t i = 0; i < q; i++) {
    int val;
    scanf("%d", &val);
    if (find(val, 0)) {
      std::cout << "yes" << '\\n';
    } else {
      std::cout << "no" << '\\n';
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
int n;

bool find(std::vector<int> A, int m, int i) {
  if (m == 0) {
    return true;
  } else if (i >= n || m < 0) {
    return false;
  }
  bool res = find(A, m, i + 1) || find(A, m - A[i], i + 1);
  return res;
}

int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  std::cin >> n;
  std::vector<int> A;
  int val;

  for (size_t i = 0; i < n; i++) {
    std::cin >> val;
    A.push_back(val);
  }

  int q;
  std::cin >> q;

  for (size_t i = 0; i < q; i++) {
    std::cin >> val;
    if (find(A, val, 0)) {
      std::cout << "yes" << '\\n';
    } else {
      std::cout << "no" << '\\n';
    }
  }

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

#include <iostream>
#include <stdio.h>
using namespace std;

int A[20], n;
int dp[21][2001];

// TODO: この方法やってみる
//  https://teratail.com/questions/19591
int is_combinated(int i, int m) {
  if (dp[i][m] != 2)
    return dp[i][m];

  if (m == 0)
    dp[i][m] = 1;
  else if (m < 0 || i >= n)
    dp[i][m] = 0;
  else if (is_combinated(i + 1, m))
    dp[i][m] = 1;
  else if (is_combinated(i + 1, m - A[i]))
    dp[i][m] = 1;
  else
    dp[i][m] = 0;

  return dp[i][m];
}

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

int main(void) {
  int q, m;
  for (int i = 0; i < 21; i++) {
    for (int j = 0; j < 2001; j++) {
      dp[i][j] = 2;
    }
  }
  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 (is_combinated(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int A[20], n;
int dp[20][2000];

int is_combinated(int i, int m) {
  if (dp[i][m] != 2)
    return dp[i][m];

  if (m == 0)
    dp[i][m] = 1;
  else if (m < 0 || i >= n)
    dp[i][m] = 0;
  else if (is_combinated(i + 1, m))
    dp[i][m] = 1;
  else if (is_combinated(i + 1, m - A[i]))
    dp[i][m] = 1;
  else
    dp[i][m] = 0;

  return dp[i][m];
}

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

int main(void) {
  int q, m;
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 2000; j++) {
      dp[i][j] = 2;
    }
  }
  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 (is_combinated(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

int n;

// int *A;

int A[20];

bool solve(int i, int m) {
  if (m == 0) {
    return 1;
  } else if (i >= n) {
    return 0;
  }

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

int main() {

  int q, m;

  cin >> n;

  // A = new int[n];

  // string *ans = new string[q];

  int ans[200];

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> m;
    if (solve(0, m) == 1) {
      ans[i] = 1;
    } else {
      ans[i] = 0;
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int n;

// int *A;

int A[20];

bool solve(int i, int m) {
  if (m == 0) {
    return 1;
  } else if (i >= n) {
    return 0;
  }

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

int main() {

  int q, m;

  cin >> n;

  // A = new int[n];

  string *ans = new string[q];

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> m;
    if (solve(0, m) == 1) {
      ans[i] = "yes";
    } else {
      ans[i] = "no";
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v
#define NUM(x, v) (POSU(x, v) - POSL(x, v))
const ll INF = 1e9 + 7;
const ll N = 1002;
const ll M = 21;
ll n, a[N], q, m, dp[21 * 2001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  FOR(i, 0, 21 * 2001) dp[i] = 0;
  cin >> n;
  FOR(i, 0, n) cin >> a[i];
  dp[0] = 1;
  FOR(i, 0, n) {
    for (ll j = n * 2000; j >= 0; j--) {
      if (dp[j] == 1)
        dp[j + a[i]] = 1;
    }
  }
  cin >> q;
  FOR(p, 0, q) {
    cin >> m;
    if (dp[m] == 1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
const int N = 20;
int a[N];
int m[N];
int possible[2001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  CLR(possible);
  int n;
  cin >> n;
  FOR(i, 0, n) cin >> a[i];
  int q;
  cin >> q;
  FOR(i, 0, q) cin >> m[i];

  int j;
  int k;
  int p;

  FOR(i, 0, pow(2, q)) {
    int k = i;
    p = 0;
    j = 0;
    while (k != 0) {
      if (k & 1)
        p += a[j];
      if (p >= 2000)
        continue;
      j++;
      k /= 2;
    }
    possible[p] = 1;
  }

  FOR(i, 0, q) {
    if (possible[m[i]] == 1) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: 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] || 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;
}

Error: 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])
        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, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 30
void divide_list(int, int);
void marge(int, int, int);
int v[5000000];
int c;
int main(void) {
  int num;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> v[i];
  }
  divide_list(num, 0);
  for (int i = 0; i < num; i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
  cout << c << endl;
  return 0;
}
void divide_list(int right, int left) {
  if (left + 1 < right) {
    int mid = (right + left) / 2;
    divide_list(mid, left);
    divide_list(right, mid);
    marge(right, left, mid);
  }
}
void marge(int right, int left, int mid) {
  int R[300000], L[300000];
  int n1, n2;

  n1 = mid - left;
  n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = v[i + left];
  for (int i = 0; i < n2; i++)
    R[i] = v[i + mid];
  R[n2] = L[n1] = INF;
  int k = 0;
  int l = 0;
  for (int j = left; j < right; j++) {
    c++;
    if (L[l] <= R[k]) {
      v[j] = L[l++];
    } else {
      v[j] = R[k++];
    }
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 20
void divide_list(int, int);
void marge(int, int, int);
int v[5000000];
int c;
int main(void) {
  int num;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> v[i];
  }
  divide_list(num, 0);
  for (int i = 0; i < num; i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
  cout << c << endl;
  return 0;
}
void divide_list(int right, int left) {
  if (left + 1 < right) {
    int mid = (right + left) / 2;
    divide_list(mid, left);
    divide_list(right, mid);
    marge(right, left, mid);
  }
}
void marge(int right, int left, int mid) {
  int R[2000000], L[2000000];
  int n1, n2;

  n1 = mid - left;
  n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = v[i + left];
  for (int i = 0; i < n2; i++)
    R[i] = v[i + mid];
  R[n2] = L[n1] = INF;
  int k = 0;
  int l = 0;
  for (int j = left; j < right; j++) {
    c++;
    if (L[l] <= R[k]) {
      v[j] = L[l++];
    } else {
      v[j] = R[k++];
    }
  }
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>

#define MAX 500000
#define INFINITY 1000000000

int n;
int L[MAX / 2 + 1], R[MAX / 2 + 1];
int sum(0);

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] = INFINITY;
  R[n2] = INFINITY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      sum++;
    } else {
      A[k] = R[j];
      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; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n%d\\n", sum);

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

#define MAX 500000
#define INFINITY 100000000

int n;
int L[MAX / 2 + 1], R[MAX / 2 + 1];
int sum(0);

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] = INFINITY;
  R[n2] = INFINITY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      sum++;
    } else {
      A[k] = R[j];
      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; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n%d\\n", sum);

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

#include <stdio.h>
int n, S[500000], count = 0, i, a[500000];
int MergeSort(int S[], int left, int right);
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &S[i]);
  MergeSort(S, 0, n - 1);
  for (i = 0; i < n; i++) {
    printf("%d", S[i]);
    if (i < n - 1)
      printf(" ");
  }
  printf("\\n%d\\n", count);
  return 0;
}
int MergeSort(int S[], int left, int right) {
  int mannaka, j, k;
  if (left >= right)
    return S[n];
  mannaka = (left + right) / 2;
  MergeSort(S, left, mannaka);
  MergeSort(S, mannaka + 1, right);
  for (i = left; i <= mannaka; i++)
    a[i] = S[i];
  for (i = mannaka + 1, j = right; i <= right; i++, j--)
    a[i] = S[j];
  i = left;
  j = right;
  for (k = left; k <= right; k++) {
    count++;
    if (a[i] <= a[j])
      S[k] = a[i++];
    else
      S[k] = a[j--];
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
long long n, S[50000], count = 0, i, a[50000];
long long MergeSort(long long S[], long long left, long long right);
int main() {
  scanf("%ld", &n);
  for (i = 0; i < n; i++)
    scanf("%ld", &S[i]);
  MergeSort(S, 0, n - 1);
  for (i = 0; i < n; i++) {
    printf("%ld", S[i]);
    if (i < n - 1)
      printf(" ");
  }
  printf("\\n%ld\\n", count);
  return 0;
}
long long MergeSort(long long S[], long long left, long long right) {
  long long mannaka, j, k;
  if (left >= right)
    return S[n];
  mannaka = (left + right) / 2;
  MergeSort(S, left, mannaka);
  MergeSort(S, mannaka + 1, right);
  for (i = left; i <= mannaka; i++)
    a[i] = S[i];
  for (i = mannaka + 1, j = right; i <= right; i++, j--)
    a[i] = S[j];
  i = left;
  j = right;
  for (k = left; k <= right; k++) {
    count++;
    if (a[i] <= a[j])
      S[k] = a[i++];
    else
      S[k] = a[j--];
  }
}