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

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
#define __2012_12_21__ 735224
#define MAX_YEAR 10000000

int64 days[MAX_YEAR + 1];
int month[2][13] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

bool urudosi(int year) {
  return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}
void init_set() {
  for (int i = 1; i <= MAX_YEAR; i++) {
    days[i] = days[i - 1] + 365 + urudosi(i - 1);
  }
}

int main() {
  char buff[256];
  int y, m, d;         // 西暦
  int b, ka, t, w, ki; // マヤ長期歴

  init_set();
  while (strcmp(fgets(buff, sizeof(buff), stdin), "#\n") != 0) {

    /* 入力 */
    if (sscanf(buff, "%d.%d.%d.%d.%d", &b, &ka, &t, &w, &ki) == 5) {
      /*マヤ長期歴 → 西暦 */
      int64 allday =
          b * 144000 + ka * 7200 + t * 360 + w * 20 + ki + __2012_12_21__;
      y = lower_bound(days, days + MAX_YEAR, allday) - days - 1;
      allday -= days[y];
      bool uru = urudosi(y);
      m = lower_bound(month[uru], month[uru] + 13, allday) - month[uru];
      d = allday - month[uru][m - 1];
      printf("%d.%d.%d\n", y, m, d);
    } else {
      sscanf(buff, "%d.%d.%d", &y, &m, &d);
      int64 allday = days[y] + month[urudosi(y)][m - 1] + d - __2012_12_21__;
      ki = allday % 20;
      w = allday / 20 % 18;
      t = allday / 360 % 20;
      ka = allday / 7200 % 20;
      b = allday / 144000 % 13;
      printf("%d.%d.%d.%d.%d\n", b, ka, t, w, ki);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned int64;
#define __2012_12_21__ 735224
#define MAX_YEAR 10000000

int64 days[MAX_YEAR + 1];
int month[2][13] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

bool urudosi(int year) {
  return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}
void init_set() {
  for (int i = 1; i <= MAX_YEAR; i++) {
    days[i] = days[i - 1] + 365 + urudosi(i - 1);
  }
}

int main() {
  char buff[256];
  int y, m, d;         // 西暦
  int b, ka, t, w, ki; // マヤ長期歴

  init_set();
  while (strcmp(fgets(buff, sizeof(buff), stdin), "#\n") != 0) {

    /* 入力 */
    if (sscanf(buff, "%d.%d.%d.%d.%d", &b, &ka, &t, &w, &ki) == 5) {
      /*マヤ長期歴 → 西暦 */
      int64 allday =
          b * 144000 + ka * 7200 + t * 360 + w * 20 + ki + __2012_12_21__;
      y = lower_bound(days, days + MAX_YEAR, allday) - days - 1;
      allday -= days[y];
      bool uru = urudosi(y);
      m = lower_bound(month[uru], month[uru] + 13, allday) - month[uru];
      d = allday - month[uru][m - 1];
      printf("%d.%d.%d\n", y, m, d);
    } else {
      sscanf(buff, "%d.%d.%d", &y, &m, &d);
      int64 allday = days[y] + month[urudosi(y)][m - 1] + d - __2012_12_21__;
      ki = allday % 20;
      w = allday / 20 % 18;
      t = allday / 360 % 20;
      ka = allday / 7200 % 20;
      b = allday / 144000 % 13;
      printf("%d.%d.%d.%d.%d\n", b, ka, t, w, ki);
    }
  }
}
Problem p00257 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 255
using namespace std;
int m, n, d[N];
bool used[2][N], ans;

void dfs(int x, int idx) {
  if (used[idx][x])
    return;
  x += d[x];
  if (x < 0)
    x = 0;
  if (n + 1 < x)
    x = n + 1;
  for (int i = 1; i <= m; i++) {
    int nx = x + i;
    if (n + 1 < nx)
      nx = n + 1;
    dfs(nx, idx);
  }
}

int main() {
  while (1) {
    cin >> m;
    if (!m)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];
    memset(used, 0, sizeof(used));
    ans = true;
    dfs(0, 0);
    if (!used[0][n + 1])
      ans = false;
    for (int i = 1; i < n + 1; i++) {
      if (!used[0][i])
        continue;
      for (int j = 0; j <= n + 1; j++)
        used[1][j] = false;
      dfs(i, 1);
      if (!used[1][n + 1])
        ans = false;
    }
    if (ans)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 255
using namespace std;
int m, n, d[N];
bool used[2][N], ans;

void dfs(int x, int idx) {
  if (used[idx][x])
    return;
  used[idx][x] = true;
  x += d[x];
  if (x < 0)
    x = 0;
  if (n + 1 < x)
    x = n + 1;
  for (int i = 1; i <= m; i++) {
    int nx = x + i;
    if (n + 1 < nx)
      nx = n + 1;
    dfs(nx, idx);
  }
}

int main() {
  while (1) {
    cin >> m;
    if (!m)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];
    memset(used, 0, sizeof(used));
    ans = true;
    dfs(0, 0);
    if (!used[0][n + 1])
      ans = false;
    for (int i = 1; i < n + 1; i++) {
      if (!used[0][i])
        continue;
      for (int j = 0; j <= n + 1; j++)
        used[1][j] = false;
      dfs(i, 1);
      if (!used[1][n + 1])
        ans = false;
    }
    if (ans)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00258 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int a[30], b[30];
int dp[30][(1 << 16)];

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        a[i] = a[i] * 2 + x;
      }
    }
    for (int i = 0; i < m; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        b[i] = b[i] * 2 + x;
      }
    }

    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        for (int k = 0; k < m; k++) {
          int S = (j | a[i] | b[k]) - b[k];
          int cost = __builtin_popcount((j | a[i]) & b[k]);
          dp[i + 1][S] = max(dp[i + 1][S], dp[i][j] + cost);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int a[50], b[50];
int dp[50][(1 << 16)];

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        a[i] = a[i] * 2 + x;
      }
    }
    for (int i = 0; i < m; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        b[i] = b[i] * 2 + x;
      }
    }

    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        for (int k = 0; k < m; k++) {
          int S = (j | a[i] | b[k]) - b[k];
          int cost = __builtin_popcount((j | a[i]) & b[k]);
          dp[i + 1][S] = max(dp[i + 1][S], dp[i][j] + cost);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p00258 (C++)

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

typedef long long ll;

#define pb push_back

int cnt_bit(int n) {
  int ret = 0;
  while (n > 0) {
    ret++;
    n -= n & -n;
  }
  return ret;
}

int main() {
  while (1) {
    static int n, c, in;
    static int a[32], b[32];
    static int dp[32][1 << 16];
    scanf("%d%d", &n, &c);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        a[i] <<= 1;
        a[i] |= in;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        b[i] <<= 1;
        b[i] |= in;
      }
    }
    int t = 0, t_ = 1;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1000000;
      }
    }
    dp[t][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        for (int k = 0; k < c; k++) {
          int sc = cnt_bit((j | a[i]) & b[k]);
          int nex = (j | a[i]) - ((j | a[i]) & b[k]);
          dp[t_][nex] = max(dp[t_][nex], dp[t][j] + sc);
        }
      }
      swap(t, t_);
    }
    int ret = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ret = max(ret, dp[t][i]);
    }
    printf("%d\n", ret);
  }
}

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

typedef long long ll;

#define pb push_back

int cnt_bit(int n) {
  int ret = 0;
  while (n > 0) {
    ret++;
    n -= n & -n;
  }
  return ret;
}

int main() {
  while (1) {
    static int n, c, in;
    static int a[32], b[32];
    static int dp[32][1 << 16];
    scanf("%d%d", &n, &c);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        a[i] <<= 1;
        a[i] |= in;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        b[i] <<= 1;
        b[i] |= in;
      }
    }
    int t = 0, t_ = 1;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1000000;
      }
    }
    dp[t][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[t][j] < 0)
          continue;
        for (int k = 0; k < c; k++) {
          int sc = cnt_bit((j | a[i]) & b[k]);
          int nex = (j | a[i]) - ((j | a[i]) & b[k]);
          dp[t_][nex] = max(dp[t_][nex], dp[t][j] + sc);
        }
      }
      swap(t, t_);
    }
    int ret = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ret = max(ret, dp[t][i]);
    }
    printf("%d\n", ret);
  }
}
Problem p00258 (C++)

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

const int INF = 1001001001;

int input_bit() {
  int r = 0;
  rep(i, 16) {
    int b;
    cin >> b;
    r <<= 1;
    r += b;
  }
  return r;
}

int func(int n, int c) {
  vi a(n);
  rep(i, n) a[i] = input_bit();

  vi b(c);
  rep(i, c) b[i] = input_bit();

  vvi dp(n + 1);
  dp[0].assign(1 << 16, -INF);
  dp[0][0] = 0;

  rep(i, n) {
    dp[i + 1].assign(1 << 16, -INF);
    rep(m, 1 << 16) {
      rep(j, c) {
        int v = m | a[i];
        int cnt = 0;
        rep(k, 16) {
          if (((v >> k) & 1) && ((b[j] >> k) & 1)) {
            ++cnt;
            v ^= 1 << k;
          }
        }
        dp[i + 1][v] = max(dp[i + 1][v], dp[i][m] + cnt);
      }
    }
    dp[i].clear();
  }

  return *max_element(dp[n].begin(), dp[n].end());
}

int main() {
  int n, c;
  while (cin >> n >> c, n) {
    cout << func(n, c) << endl;
  }
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

const int INF = 1001001001;

int input_bit() {
  int r = 0;
  rep(i, 16) {
    int b;
    cin >> b;
    r <<= 1;
    r += b;
  }
  return r;
}

int func(int n, int c) {
  vi a(n);
  rep(i, n) a[i] = input_bit();

  vi b(c);
  rep(i, c) b[i] = input_bit();

  vvi dp(n + 1);
  dp[0].assign(1 << 16, -INF);
  dp[0][0] = 0;

  rep(i, n) {
    dp[i + 1].assign(1 << 16, -INF);
    rep(m, 1 << 16) {
      if (dp[i][m] == -INF)
        continue;

      rep(j, c) {
        int v = m | a[i];
        int cnt = 0;
        rep(k, 16) {
          if (((v >> k) & 1) && ((b[j] >> k) & 1)) {
            ++cnt;
            v ^= 1 << k;
          }
        }
        dp[i + 1][v] = max(dp[i + 1][v], dp[i][m] + cnt);
      }
    }
    dp[i].clear();
  }

  return *max_element(dp[n].begin(), dp[n].end());
}

int main() {
  int n, c;
  while (cin >> n >> c, n) {
    cout << func(n, c) << endl;
  }
}
Problem p00258 (C++)

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

using namespace std;

int a[30], b[30], dp[30][1 << 16];

int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if (num & (1 << i))
      res++;
  return res;
}

int main(void) {

  int n, c, in;

  while (cin >> n >> c, n | c) {

    fill(a, a + n, 0);
    fill(b, b + c, 0);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        a[i] |= in << (15 - j);
      }
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        b[i] |= in << 15 - j;
      }
    }

    memset(dp, -1, sizeof(dp));

    dp[0][0] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] < 0)
          continue;
        int now = a[i] | j;

        for (int k = 0; k < c; k++) {
          int next = now & ~b[k];
          dp[i + 1][next] = max(dp[i + 1][next], dp[i][j] + count(now & b[k]));
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ans = max(ans, dp[n][i]);
    }
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

int a[31], b[31], dp[31][1 << 16];

int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if (num & (1 << i))
      res++;
  return res;
}

int main(void) {

  int n, c, in;

  while (cin >> n >> c, n | c) {

    fill(a, a + n, 0);
    fill(b, b + c, 0);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        a[i] |= in << (15 - j);
      }
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        b[i] |= in << 15 - j;
      }
    }

    memset(dp, -1, sizeof(dp));

    dp[0][0] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] < 0)
          continue;
        int now = a[i] | j;

        for (int k = 0; k < c; k++) {
          int next = now & ~b[k];
          dp[i + 1][next] = max(dp[i + 1][next], dp[i][j] + count(now & b[k]));
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ans = max(ans, dp[n][i]);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00261 (C++)

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

int main() {
  int node[10][10];
  node[0][0] = 1;
  node[0][1] = 2;
  node[1][0] = -1;
  node[1][1] = 3;
  node[2][0] = 1;
  node[2][1] = -1;
  node[3][0] = 4;
  node[3][1] = 5;
  node[4][0] = 5;
  node[4][1] = 2;
  node[5][0] = 2;
  node[5][1] = 1;
  while (1) {
    string str;
    cin >> str;
    if (str == "#")
      break;
    int len = str.size();
    int now = 0;
    for (int i = 0; i < len; i++) {
      int f = str[i] - '0';
      now = node[now][f];
    }
    if (now == 5)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

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

int main() {
  int node[10][10];
  node[0][0] = 1;
  node[0][1] = 2;
  node[1][0] = -1;
  node[1][1] = 3;
  node[2][0] = 1;
  node[2][1] = -1;
  node[3][0] = 4;
  node[3][1] = 5;
  node[4][0] = 5;
  node[4][1] = 2;
  node[5][0] = 2;
  node[5][1] = 1;
  while (1) {
    string str;
    cin >> str;
    if (str == "#")
      break;
    int len = str.size();
    int now = 0;
    for (int i = 0; i < len; i++) {
      if (now == -1)
        break;
      int f = str[i] - '0';
      now = node[now][f];
    }
    if (now == 5)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p00262 (C++)

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

using namespace std;

int main() {
  int i, j, n, m, s, p, d[100];
  while (cin >> n && n > 0) {
    s = p = 0;
    for (i = 0; i < n; i++) {
      cin >> d[i];
      s += d[i];
    }
    for (i = 1, m = 0; i <= 100; i++) {
      m += i;
      if (s == m)
        break;
    }
    if (i < 101)
      m = i;
    else {
      cout << -1 << endl;
      continue;
    }
    for (i = 0; i <= 10000; i++) {
      if (m == n && d[0] == 1 && d[m - 1] == m) {
        for (j = 0; j < m; j++)
          if (d[j] != j + 1)
            j = 9999;
        if (j == m)
          break;
      }
      for (j = 0; j < n; j++)
        d[j]--;
      for (j = 0, p = 0; j < n; j++)
        if (d[j] > 0)
          d[p++] = d[j];
      d[p] = n;
      n = p + 1;
    }
    if (i > 10000)
      cout << -1 << endl;
    else
      cout << i << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int i, j, n, m, s, p, d[6000];
  while (cin >> n && n > 0) {
    s = p = 0;
    for (i = 0; i < n; i++) {
      cin >> d[i];
      s += d[i];
    }
    for (i = 1, m = 0; i <= 100; i++) {
      m += i;
      if (s == m)
        break;
    }
    if (i < 101)
      m = i;
    else {
      cout << -1 << endl;
      continue;
    }
    for (i = 0; i <= 10000; i++) {
      if (m == n && d[0] == 1 && d[m - 1] == m) {
        for (j = 0; j < m; j++)
          if (d[j] != j + 1)
            j = 9999;
        if (j == m)
          break;
      }
      for (j = 0; j < n; j++)
        d[j]--;
      for (j = 0, p = 0; j < n; j++)
        if (d[j] > 0)
          d[p++] = d[j];
      d[p] = n;
      n = p + 1;
    }
    if (i > 10000)
      cout << -1 << endl;
    else
      cout << i << endl;
  }
  return 0;
}
Problem p00262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int num[1000] = {}, co = n;
    for (int i = 0; i < n; i++)
      cin >> num[i];
    for (int i = 0;; i++) {
      bool kawa = 1;
      if (i == 10000) {
        cout << -1 << endl;
        break;
      }
      for (int j = 0; j < co && kawa; j++)
        if (num[j] != j + 1)
          kawa = 0;
      if (kawa) {
        cout << i << endl;
        break;
      }
      for (int j = 0; j < co; j++)
        num[j] -= 1;
      num[co] = co;
      for (int j = 0; j <= co; j++) {
        if (num[j] == 0) {
          for (int k = j + 1; k <= co; k++) {
            if (num[k] != 0) {
              swap(num[j], num[k]);
              break;
            }
          }
        }
      }
      for (int j = co + 1; j >= 0; j--) {
        if (num[j] != 0) {
          co = j + 1;
          break;
        }
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int num[2000] = {}, co = n;
    for (int i = 0; i < n; i++)
      cin >> num[i];
    for (int i = 0;; i++) {
      bool kawa = 1;
      if (i == 10000) {
        cout << -1 << endl;
        break;
      }
      for (int j = 0; j < co && kawa; j++)
        if (num[j] != j + 1)
          kawa = 0;
      if (kawa) {
        cout << i << endl;
        break;
      }
      for (int j = 0; j < co; j++)
        num[j] -= 1;
      num[co] = co;
      for (int j = 0; j <= co; j++) {
        if (num[j] == 0) {
          for (int k = j + 1; k <= co; k++) {
            if (num[k] != 0) {
              swap(num[j], num[k]);
              break;
            }
          }
        }
      }
      for (int j = co + 1; j >= 0; j--) {
        if (num[j] != 0) {
          co = j + 1;
          break;
        }
      }
    }
  }
}
Problem p00262 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int b[100];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    int now = 0;
    int n = a;
    while (1) {
      bool ok = true;
      for (int i = 0; i < n; i++)
        if (b[i] != i + 1)
          ok = false;
      if (ok) {
        printf("%d\n", now);
        break;
      } else if (now == 10000) {
        printf("-1\n");
        break;
      }
      now++;
      for (int i = 0; i < n; i++) {
        b[i]--;
      }
      int at = 0;
      for (int i = 0; i < n; i++)
        if (b[i])
          b[at++] = b[i];
      b[at++] = n;
      n = at;
    }
  }
}

Fixed submission:
#include <stdio.h>
int b[10000];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    int now = 0;
    int n = a;
    while (1) {
      bool ok = true;
      for (int i = 0; i < n; i++)
        if (b[i] != i + 1)
          ok = false;
      if (ok) {
        printf("%d\n", now);
        break;
      } else if (now == 10000) {
        printf("-1\n");
        break;
      }
      now++;
      for (int i = 0; i < n; i++) {
        b[i]--;
      }
      int at = 0;
      for (int i = 0; i < n; i++)
        if (b[i])
          b[at++] = b[i];
      b[at++] = n;
      n = at;
    }
  }
}
Problem p00262 (C++)

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

int blocks[101], n;

void update() {
  for (int i = 0; i < n; i++)
    blocks[i]--;
  blocks[n++] = n;
  int m = 0;
  for (int i = 0; i < n; i++) {
    if (blocks[i] == 0) {
      m++;
    } else
      blocks[i - m] = blocks[i];
  }
  n -= m;
}

bool check() {
  for (int i = 0; i < n; i++) {
    if (blocks[i] != i + 1)
      return false;
  }
  return true;
}

int main() {
  while (true) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> blocks[i];
    for (int i = 0; i <= 10000; i++) {
      if (check()) {
        cout << i << endl;
        goto success;
      }
      update();
    }
    cout << -1 << endl;
  success:;
  }
}

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

int blocks[1000001], n;

void update() {
  for (int i = 0; i < n; i++)
    blocks[i]--;
  blocks[n++] = n;
  int m = 0;
  for (int i = 0; i < n; i++) {
    if (blocks[i] == 0) {
      m++;
    } else
      blocks[i - m] = blocks[i];
  }
  n -= m;
}

bool check() {
  for (int i = 0; i < n; i++) {
    if (blocks[i] != i + 1)
      return false;
  }
  return true;
}

int main() {
  while (true) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> blocks[i];
    for (int i = 0; i <= 10000; i++) {
      if (check()) {
        cout << i << endl;
        goto success;
      }
      update();
    }
    cout << -1 << endl;
  success:;
  }
}
Problem p00262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int n, s;
bool triangle(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i] != i + 1)
      return false;
  }
  return true;
}
vector<int> shift(vector<int> v) {
  vector<int> ret;
  for (int i = 0; i < v.size(); i++) {
    if (v[i])
      ret.push_back(v[i]);
  }
  return ret;
}
int main() {
  while (cin >> n, n) {
    vector<int> v(n);
    s = 0;
    for (int i = 0; i < n; i++)
      cin >> v[i], s += v[i];
    bool flag = false;
    for (int i = 0; i < 2000; i++) {
      if (i * (i + 1) / 2 == s)
        flag = true;
    }
    if (!flag)
      cout << -1 << endl;
    else {
      int c = 0;
      while (true) {
        if (triangle(v))
          break;
        for (int i = 0; i < v.size(); i++)
          v[i]--;
        v.push_back(v.size());
        v = shift(v);
        c++;
      }
      cout << (c <= 10000 ? c : -1) << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int n, s;
bool triangle(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i] != i + 1)
      return false;
  }
  return true;
}
vector<int> shift(vector<int> v) {
  vector<int> ret;
  for (int i = 0; i < v.size(); i++) {
    if (v[i])
      ret.push_back(v[i]);
  }
  return ret;
}
int main() {
  while (cin >> n, n) {
    vector<int> v(n);
    s = 0;
    for (int i = 0; i < n; i++)
      cin >> v[i], s += v[i];
    bool flag = false;
    for (int i = 0; i < 2000; i++) {
      if (i * (i + 1) / 2 == s)
        flag = true;
    }
    if (!flag)
      cout << -1 << endl;
    else {
      int c = 0;
      while (c <= 10000) {
        if (triangle(v))
          break;
        for (int i = 0; i < v.size(); i++)
          v[i]--;
        v.push_back(v.size());
        v = shift(v);
        c++;
      }
      cout << (c <= 10000 ? c : -1) << endl;
    }
  }
}
Problem p00262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n;
  while (1) {
    cin >> n;
    if (!n)
      return 0;
    int x[105];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    int res = -1;
    for (int i = 0; i <= 10000; i++) {
      for (int j = 0; j < n; j++)
        if (x[j] != j + 1)
          goto nxt;
      res = i;
      break;
    nxt:;
      int cur = 0;
      for (int j = 0; j < n; j++) {
        if (x[j] > 1) {
          x[cur] = x[j] - 1;
          cur++;
        }
      }
      x[cur] = n;
      n = cur + 1;
    }
    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n;
  while (1) {
    cin >> n;
    if (!n)
      return 0;
    int x[10005];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    int res = -1;
    for (int i = 0; i <= 10000; i++) {
      for (int j = 0; j < n; j++)
        if (x[j] != j + 1)
          goto nxt;
      res = i;
      break;
    nxt:;
      int cur = 0;
      for (int j = 0; j < n; j++) {
        if (x[j] > 1) {
          x[cur] = x[j] - 1;
          cur++;
        }
      }
      x[cur] = n;
      n = cur + 1;
    }
    cout << res << endl;
  }
}
Problem p00265 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 15
int main() {
  bool T[MAX + 1];
  int N, Q, q, x, L[MAX + 1];

  for (int i = 0; i <= MAX; i++) {
    T[i] = false;
    L[i] = 0;
  }

  scanf("%d %d", &N, &Q);

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

  int m = 0;
  for (int i = 1; i <= MAX; i++) {
    L[i] = m;
    if (T[i])
      m = i;
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d", &q);
    int maxv = 0;
    int cur = m;

    while (cur) {
      int p = cur % q;
      maxv = max(maxv, p);
      if (cur - p < 0)
        break;
      cur = L[cur - p];
    }

    printf("%d\n", maxv);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 300000
int main() {
  bool T[MAX + 1];
  int N, Q, q, x, L[MAX + 1];

  for (int i = 0; i <= MAX; i++) {
    T[i] = false;
    L[i] = 0;
  }

  scanf("%d %d", &N, &Q);

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

  int m = 0;
  for (int i = 1; i <= MAX; i++) {
    L[i] = m;
    if (T[i])
      m = i;
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d", &q);
    int maxv = 0;
    int cur = m;

    while (cur) {
      int p = cur % q;
      maxv = max(maxv, p);
      if (cur - p < 0)
        break;
      cur = L[cur - p];
    }

    printf("%d\n", maxv);
  }
}
Problem p00265 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> P3;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(3e5 + 5);
const double EPS = 1e-4;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int n, q, cmax, cnt[MAX_N];

int solve(int m) {
  int ma = 0;
  for (int i = 0; i < cmax; i += m) {
    int bg = i + ma, en = min(i + m - 1, cmax);
    if (cnt[en] - cnt[bg] <= 0)
      continue;
    ma = lower_bound(cnt + bg, cnt + en, cnt[en]) - (cnt + i);
  }
  return ma;
}

int main() {
  cin >> n >> q;
  REP(i, n) {
    int c;
    cin >> c;
    cnt[c]++;
    cmax = max(cmax, c);
  }
  REP(i, cmax) { cnt[i + 1] += cnt[i]; }
  REP(i, q) {
    int m;
    cin >> m;
    cout << solve(m) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> P3;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(3e5 + 5);
const double EPS = 1e-4;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int n, q, cmax, cnt[MAX_N];

int solve(int m) {
  int ma = 0;
  for (int i = 0; i < cmax; i += m) {
    int bg = i + ma, en = min(i + m - 1, cmax);
    if (bg > cmax || cnt[en] - cnt[bg] <= 0)
      continue;
    ma = lower_bound(cnt + bg, cnt + en, cnt[en]) - (cnt + i);
  }
  return ma;
}

int main() {
  cin >> n >> q;
  REP(i, n) {
    int c;
    cin >> c;
    cnt[c]++;
    cmax = max(cmax, c);
  }
  REP(i, cmax) { cnt[i + 1] += cnt[i]; }
  REP(i, q) {
    int m;
    cin >> m;
    cout << solve(m) << endl;
  }
  return 0;
}
Problem p00265 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, n) for (int i = (ll)a; i < (ll)n; ++i)
#define TFOR(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int main() {
  int n, q;
  std::cin >> n >> q;
  int max = 0;
  std::bitset<300000> bit;
  TFOR(i, n) {
    int nn;
    std::cin >> nn;
    bit[nn - 1] = 1;
    if (max < nn)
      max = nn;
  }
  TFOR(i, q) {
    int nn;
    std::cin >> nn;
    bool fl = 0;
    for (int j = nn - 1; j >= 0; --j) {
      for (int k = max / nn * nn + j; k > 0; k -= nn) {
        if (bit[k - 1] == 1) {
          std::cout << j << std::endl;
          fl = 1;
        }
        if (fl == 1)
          break;
      }
      if (fl == 1) {
        fl = 0;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, n) for (int i = (ll)a; i < (ll)n; ++i)
#define TFOR(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int main() {
  int n, q;
  std::cin >> n >> q;
  int max = 0;
  std::bitset<600000> bit;
  TFOR(i, n) {
    int nn;
    std::cin >> nn;
    bit[nn - 1] = 1;
    if (max < nn)
      max = nn;
  }
  TFOR(i, q) {
    int nn;
    std::cin >> nn;
    bool fl = 0;
    for (int j = nn - 1; j >= 0; --j) {
      for (int k = max / nn * nn + j; k > 0; k -= nn) {
        if (bit[k - 1] == 1) {
          std::cout << j << std::endl;
          fl = 1;
        }
        if (fl == 1)
          break;
      }
      if (fl == 1) {
        fl = 0;
        break;
      }
    }
  }
  return 0;
}
Problem p00265 (C++)

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

#define MAX_N 300000
#define MAX_Q 100000
int N, Q;
bool c[MAX_N + 1] = {false}, found = false;
int q[MAX_Q];
int ub, lb, range;

int main() {
  int input;

  range = 0;
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &input);
    c[input] = true;
    range = max(range, input);
  }
  for (int i = 0; i < Q; i++)
    scanf("%d", &q[i]);

  for (int i = 0; i < Q; i++) {
    lb = 0;
    ub = q[i];
    int bucket = 0;
    while (ub - lb > 1) {
      found = false;
      int mid = (lb + ub) / 2; // 目安
      for (int p = bucket * q[i] + mid; p <= range; p += mid) {
        for (int j = 0; j < (q[i] - mid); j++) {
          if (c[p]) {
            found = true;
            bucket = p / q[i];
            lb = p % q[i];
            // break;
          }
          p++;
        }
        if (found)
          break;
      }
      if (!found) {
        ub = mid;
      }
    }
    printf("%d\n", lb);
  }
  return 0;
}

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

#define MAX_N 300000
#define MAX_Q 100000
int N, Q;
bool c[MAX_N + 1] = {false}, found = false;
int q[MAX_Q];
int ub, lb, range;

int main() {
  int input;

  range = 0;
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &input);
    c[input] = true;
    range = max(range, input);
  }
  for (int i = 0; i < Q; i++)
    scanf("%d", &q[i]);

  for (int i = 0; i < Q; i++) {
    lb = 0;
    ub = q[i];
    int bucket = 0;
    while (ub - lb > 1) {
      found = false;
      int mid = (lb + ub) / 2; // 目安
      for (int p = bucket * q[i] + mid; p <= range; p += mid) {
        for (int j = 0; j < (q[i] - mid); j++) {
          if (c[p]) {
            found = true;
            bucket = p / q[i];
            lb = p % q[i];
            break;
          }
          p++;
        }
        if (found)
          break;
      }
      if (!found) {
        ub = mid;
      }
    }
    printf("%d\n", lb);
  }
  return 0;
}
Problem p00271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int main() {
  for (int a, b; scanf("%d%d", &a, &b);)
    printf("%d\n", a - b);
}

Fixed submission:
#include <cstdio>
int main() {
  for (int a, b; scanf("%d%d", &a, &b) + 1;)
    printf("%d\n", a - b);
}
Problem p00273 (C++)

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

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

  for (int i = 0; i < N; i++) {
    int x, y, b, p;
    int p1, p2;
    cin >> x >> y >> b >> p;
    p1 = x * b + y * p;
    p2 = x * max(5, b) + y * max(2, p);
    p2 *= 0.8;
    return min(p1, p2);
  }
  return 0;
}

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

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

  for (int i = 0; i < N; i++) {
    int x, y, b, p;
    int p1, p2;
    cin >> x >> y >> b >> p;
    p1 = x * b + y * p;
    p2 = x * max(5, b) + y * max(2, p);
    p2 *= 0.8;
    cout << min(p1, p2) << endl;
  }
  return 0;
}
Problem p00274 (C++)

Buggy submission (status: Runtime Error):
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    vi k;
    while (n--) {
      int a;
      cin >> a;
      if (a)
        k.pb(a);
    }
    if (*max_element(all(k)) < 2) {
      cout << "NA" << endl;
      continue;
    }
    cout << (k.size() == 1 ? 2 : k.size() + 1) << endl;
  }
}

Fixed submission:
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    vi k;
    k.reserve(10000);
    while (n--) {
      int a;
      cin >> a;
      if (a)
        k.pb(a);
    }
    if (*max_element(all(k)) < 2) {
      cout << "NA" << endl;
      continue;
    }
    cout << (k.size() == 1 ? 2 : k.size() + 1) << endl;
  }
}
Problem p00274 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int N = 1;
  static int k[100];
  int i;
  int a, b;

  for (;;) {
    scanf("%d", &N);

    if (N == 0)
      break;

    for (i = 0; i < N; i++)
      scanf("%d", &k[i]);

    a = 0, b = 0;
    for (i = 0; i < N; i++) {
      if (k[i] == 0)
        a++;
      if (k[i] < 2)
        b++;
    }

    if (b == N)
      printf("NA\n");
    else {
      N = N - a + 1;
      printf("%d\n", N);
    }
  }
}


Fixed submission:
#include <stdio.h>
int main(void) {
  int N = 1;
  static int k[10000];
  int i;
  int a, b;

  for (;;) {
    scanf("%d", &N);

    if (N == 0)
      break;

    for (i = 0; i < N; i++)
      scanf("%d", &k[i]);

    a = 0, b = 0;
    for (i = 0; i < N; i++) {
      if (k[i] == 0)
        a++;
      if (k[i] < 2)
        b++;
    }

    if (b == N)
      printf("NA\n");
    else {
      N = N - a + 1;
      printf("%d\n", N);
    }
  }
}
Problem p00274 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, i, j;
  int a[1000];
  while (1) {
    cin >> n;
    int c = 0;
    int s = 0;
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] <= 1) {
        c++;
        if (a[i] == 0) {
          s++;
        }
      }
    }
    if (n == 1 && a[0] >= 2) {
      cout << 2 << endl;
    } else {
      if (c == n || n == 1 && a[0] <= 1) {
        cout << "NA" << endl;
      } else {
        cout << n - s + 1 << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, i, j;
  int a[100001];
  while (1) {
    cin >> n;
    int c = 0;
    int s = 0;
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] <= 1) {
        c++;
        if (a[i] == 0) {
          s++;
        }
      }
    }
    if (n == 1 && a[0] >= 2) {
      cout << 2 << endl;
    } else {
      if (c == n || n == 1 && a[0] <= 1) {
        cout << "NA" << endl;
      } else {
        cout << n - s + 1 << endl;
      }
    }
  }
  return 0;
}
Problem p00274 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int N, i, c;
  int *k, *kk;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    k = new int[N];
    kk = new int[N];
    for (i = 0; i < N; i++)
      kk[i] = 0;
    for (i = 0; i < N; i++)
      scanf("%d", &k[i]);

    i = 0, c = 0;
    while (1) {
      if (k[i % N] > 0) {
        k[i % N]--;
        kk[i % N]++;
        c++;
      }
      if (kk[i % N] == 2) {
        printf("%d\n", c);
        break;
      }
      if (i > N * N) {
        printf("NA\n");
        break;
      }
      i++;
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int N, i, c;
  int *k, *kk;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    k = new int[N];
    kk = new int[N];
    for (i = 0; i < N; i++)
      kk[i] = 0;
    for (i = 0; i < N; i++)
      scanf("%d", &k[i]);

    i = 0, c = 0;
    while (1) {
      if (k[i % N] > 0) {
        k[i % N]--;
        kk[i % N]++;
        c++;
      }
      if (kk[i % N] == 2) {
        printf("%d\n", c);
        break;
      }
      if (i > N * 2) {
        printf("NA\n");
        break;
      }
      i++;
    }
  }
  return 0;
}
Problem p00275 (C++)

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

using namespace std;

int main() {

  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    string c;
    cin >> c;
    int maisu[10];
    for (int i = 0; i < c.size(); i++) {
      maisu[i] = 0;
    }
    int ba = 0;
    int turn = 0;
    for (int i = 0; i < c.size(); i++) {
      if (c[i] == 'M') {
        maisu[turn]++;
      } else if (c[i] == 'S') {
        maisu[turn]++;
        ba += maisu[turn];
        maisu[turn] = 0;
      } else {
        maisu[turn]++;
        maisu[turn] += ba;
        ba = 0;
      }
      turn++;
      turn %= N;
    }
    sort(maisu, maisu + N);

    for (int i = 0; i < N; i++) {
      cout << maisu[i] << " ";
    }
    cout << ba << endl;
  }

  return 0;
}

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

using namespace std;

int main() {

  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    string c;
    cin >> c;
    int maisu[10];
    for (int i = 0; i < N; i++) {
      maisu[i] = 0;
    }
    int ba = 0;
    int turn = 0;
    for (int i = 0; i < c.size(); i++) {
      if (c[i] == 'M') {
        maisu[turn]++;
      } else if (c[i] == 'S') {
        maisu[turn]++;
        ba += maisu[turn];
        maisu[turn] = 0;
      } else {
        maisu[turn]++;
        maisu[turn] += ba;
        ba = 0;
      }
      turn++;
      turn %= N;
    }
    sort(maisu, maisu + N);

    for (int i = 0; i < N; i++) {
      cout << maisu[i] << " ";
    }
    cout << ba << endl;
  }

  return 0;
}
Problem p00275 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  while (1) {
    int N;
    cin >> N;
    string st;
    cin >> st;
    int f = 0;
    vector<int> hs(N);
    int p = 0;
    for (int i = 0; i < 100; ++i) {
      if (st[i] == 'S') {
        f += 1 + hs[p];
        hs[p] = 0;
      } else if (st[i] == 'M') {
        hs[p]++;
      } else {
        hs[p] += 1 + f;
        f = 0;
      }
      p = (p + 1) % N;
    }
    sort(hs.begin(), hs.end());
    for (int i = 0; i < N; ++i) {
      cout << hs[i] << " ";
    }
    cout << f << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    string st;
    cin >> st;
    int f = 0;
    vector<int> hs(N);
    int p = 0;
    for (int i = 0; i < 100; ++i) {
      if (st[i] == 'S') {
        f += 1 + hs[p];
        hs[p] = 0;
      } else if (st[i] == 'M') {
        hs[p]++;
      } else {
        hs[p] += 1 + f;
        f = 0;
      }
      p = (p + 1) % N;
    }
    sort(hs.begin(), hs.end());
    for (int i = 0; i < N; ++i) {
      cout << hs[i] << " ";
    }
    cout << f << endl;
  }
  return 0;
}
Problem p00276 (C++)

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

using namespace std;

/*
 *
 */
int main(int argc, char **argv) {
  int Q, c, a, n;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> c >> a >> n;
    int lim = (c + a + n) / 3, res = 0;
    if (lim > c)
      lim = c;
    int j = 0, k = 0, l = 0;
    for (; j + k + l <= lim; j++) {
      for (; j + k + l <= lim && k + l <= a; k++) {
        for (; j + k + l <= lim && l <= n; l++) {
          if (j * 3 + k * 2 + l <= c && k + l <= a) {
            if (j + k + l > res)
              res = j + k + l;
          }
        }
        l = 0;
      }
      k = 0;
    }
    j = 0;
    cout << res << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

/*
 *
 */
int main(int argc, char **argv) {
  int Q, c, a, n;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> c >> a >> n;
    int lim = (c + a + n) / 3, res = 0;
    if (lim > c)
      lim = c;
    int j = 0, k = 0, l = 0;
    for (;
         (j + k + l <= lim && j * 3 + k * 2 + l <= c) && (k + l <= a && l <= n);
         j++) {
      for (; (j + k + l <= lim && j * 3 + k * 2 + l <= c) &&
             (k + l <= a && l <= n);
           k++) {
        for (; (j + k + l <= lim && j * 3 + k * 2 + l <= c) &&
               (k + l <= a && l <= n);
             l++) {
          if (j + k + l > res)
            res = j + k + l;
        }
        l = 0;
      }
      k = 0;
    }
    j = 0;
    cout << res << endl;
  }

  return 0;
}