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

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

real dp[2][17][1 << 16];
int n, m;
real p[17][17];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int j = 0; j < 17; j++)
    for (int k = 0; k < (1 << 16); k++)
      dp[n % 2][j][k] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int id = i % 2;
    for (int k = (1 << m); k >= 0; k--) {
      for (int j = 0; j < m; j++) {
        dp[id][j][k] = 0;
        if ((1 << j) & k)
          continue;
        dp[id][j][k] =
            dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
      }
      dp[id][16][k] = 0;
      for (int j = 0; j < m; j++) {
        dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
      }
    }
  }

  printf("%.10lf\n", dp[0][16][0]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef double real;

real dp[2][17][1 << 16];
int n, m;
real p[17][17];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int j = 0; j < 17; j++)
    for (int k = 0; k < (1 << 16); k++)
      dp[n % 2][j][k] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int id = i % 2;
    for (int k = (1 << m) - 1; k >= 0; k--) {
      for (int j = 0; j < m; j++) {
        dp[id][j][k] = 0;
        if ((1 << j) & k)
          continue;
        dp[id][j][k] =
            dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
      }
      dp[id][16][k] = 0;
      for (int j = 0; j < m; j++) {
        dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
      }
    }
  }

  printf("%.10lf\n", dp[0][16][0]);
  return 0;
}
Problem p01354 (C++)

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

using namespace std;

int m, n;
double prob[16][16];
double dp[16][1 << 16];

double search(int pos, int cat) {
  if (dp[pos][cat] > -0.5)
    return dp[pos][cat];
  double &res = dp[pos][cat];
  res = 0.0;
  for (int i = 0; i < m; i++) {
    if (cat & (1 << i))
      continue;
    double pSum = 0.0;
    double pCur = 1.0;
    for (int j = pos; j < n; j++) {
      pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
      pCur *= prob[i][j];
    }
    pSum += pCur;
    res = max(res, pSum);
  }
  return res;
}

int main() {
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        cin >> prob[i][j];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < (1 << m); j++)
        dp[i][j] = -1.0;
    }
    printf("%.10lf\n", search(0, 0));
  }
}

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

using namespace std;

int m, n;
double prob[16][16];
double dp[16][1 << 16];

double search(int pos, int cat) {
  if (dp[pos][cat] > -0.5)
    return dp[pos][cat];
  double &res = dp[pos][cat];
  res = 0.0;
  for (int i = 0; i < m; i++) {
    if (cat & (1 << i))
      continue;
    double pSum = 0.0;
    double pCur = 1.0;
    for (int j = pos; j < n; j++) {
      pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
      pCur *= prob[i][j];
    }
    pSum += pCur;
    res = max(res, pSum);
  }
  return res;
}

int main() {
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        cin >> prob[i][j];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << m); j++)
        dp[i][j] = -1.0;
    }
    printf("%.10lf\n", search(0, 0));
  }
}
Problem p01354 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
long double solve(int pos, int ret) {
  if (pos == n)
    return 1.0l;
  if (dp[pos][ret] >= -0.1l)
    return dp[pos][ret];
  long double maxn = 0.0l;
  for (int i = 0; i < m; i++) {
    if ((ret / (1 << i)) & 1 == 1)
      continue;
    long double cnt = 0.0;
    for (int j = pos; j <= n; j++) {
      long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
      cnt += w1;
    }
    maxn = max(maxn, cnt);
  }
  dp[pos][ret] = maxn;
  return maxn;
}
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 100000; j++)
      dp[i][j] = -1.0l;
  }
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      neko[i][j][j] = 1.0l;
      long double cnt = 1.0l;
      for (int k = j; k < n; k++) {
        cnt *= x[i][k];
        neko[i][j][k + 1] = cnt;
      }
      for (int k = j; k <= n; k++) {
        neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
      }
    }
  }
  printf("%.12Lf\n", solve(0, 0));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
inline long double solve(int pos, int ret) {
  if (pos == n)
    return 1.0l;
  if (dp[pos][ret] >= -0.1l)
    return dp[pos][ret];
  long double maxn = 0.0l;
  for (int i = 0; i < m; i++) {
    if ((ret / (1 << i)) & 1 == 1)
      continue;
    long double cnt = 0.0;
    for (int j = pos; j <= n; j++) {
      long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
      cnt += w1;
    }
    maxn = max(maxn, cnt);
  }
  dp[pos][ret] = maxn;
  return maxn;
}
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 100000; j++)
      dp[i][j] = -1.0l;
  }
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      neko[i][j][j] = 1.0l;
      long double cnt = 1.0l;
      for (int k = j; k < n; k++) {
        cnt *= x[i][k];
        neko[i][j][k + 1] = cnt;
      }
      for (int k = j; k <= n; k++) {
        neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
      }
    }
  }
  printf("%.12Lf\n", solve(0, 0));
  return 0;
}
Problem p01354 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];

ld solve(int room, int rest) {
  if (room == n)
    return 1.0;
  if (rest == (1 << m) - 1)
    return 0;
  if (dp[room][rest] >= 0)
    return dp[room][rest];
  ld res = 0;
  REP(i, m) {
    if ((rest >> i) & 1)
      continue;
    ld exp = 1.0;
    ld tres = 0;
    FOR(j, room, n) {
      ld win = rate[i][j];
      tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
      exp *= win;
    }
    tres += exp;
    chmax(res, tres);
  }
  return dp[room][rest] = res;
}

int main() {
  cin.sync_with_stdio(false);
  REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
  cin >> m >> n;
  rate.resize(m, vector<ld>(n));
  REP(i, m) REP(j, n) cin >> rate[i][j];
  cout << D10 << solve(0, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];

ld solve(int room, int rest) {
  if (room == n)
    return 1.0;
  if (rest == (1 << m) - 1)
    return 0;
  if (dp[room][rest] >= 0)
    return dp[room][rest];
  ld res = 0;
  REP(i, m) {
    if ((rest >> i) & 1)
      continue;
    ld exp = 1.0;
    ld tres = 0;
    FOR(j, room, n) {
      ld win = rate[i][j];
      tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
      exp *= win;
    }
    tres += exp;
    chmax(res, tres);
  }
  return dp[room][rest] = res;
}

int main() {
  cin.sync_with_stdio(false);
  REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
  cin >> m >> n;
  rate.resize(m, vector<ld>(n));
  REP(i, m) REP(j, n) cin >> rate[i][j];
  cout << D10 << solve(0, 0) << endl;
  return 0;
}
Problem p01354 (C++)

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

using namespace std;

typedef long double ld;
typedef long long ll;

// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]

int N, M;
ld P[16][16];

bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];

ld dp(int n, int rest) {
  if (n >= N)
    return (ld)1;

  if (m1[n][rest])
    return m2[n][rest];

  ld ret = 0;
  for (int i = 0; i < M; ++i) {
    ld p = 0;
    if (rest & (1 << i)) {
      int nr = rest & ~(1 << i);

      ld per = 1;
      for (int j = n; j < N; ++j) {
        p += per * (1 - P[i][j]) * dp(j, nr);
        per *= P[i][j];
      }
      p += per;
    }
    ret = max(ret, p);
  }
  return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
  cin >> M >> N;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }

  printf("%.12f\n", (double)dp(0, (1 << M) - 1));
  return 0;
}

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

using namespace std;

typedef double ld;

// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]

int N, M;
ld P[16][16];

bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];

ld dp(int n, int rest) {
  if (n >= N)
    return (ld)1;

  if (m1[n][rest])
    return m2[n][rest];

  ld ret = 0;
  for (int i = 0; i < M; ++i) {
    ld p = 0;
    if (rest & (1 << i)) {
      int nr = rest & ~(1 << i);

      ld per = 1;
      for (int j = n; j < N; ++j) {
        p += per * (1 - P[i][j]) * dp(j, nr);
        per *= P[i][j];
      }
      p += per;
    }
    ret = max(ret, p);
  }
  return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
  cin >> M >> N;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }

  printf("%.12f\n", (double)dp(0, (1 << M) - 1));
  return 0;
}
Problem p01354 (C++)

Buggy submission (status: Time Limit Exceeded):
#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>>;

vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
  const int a = lives.to_ulong();
  if (!memo[now][a] < -1) {
    return memo[now][a];
  } else {
    ld amax = 0;
    for (int i = 0; i < M; ++i) {
      if (lives[i]) {
        lives[i] = false;
        ld per = 0;
        ld living = 1;
        for (int enemy = now; enemy < N; ++enemy) {
          per += living * (1 - pers[i][enemy]) * check(enemy, lives);
          living *= pers[i][enemy];
        }
        per += living;
        amax = max(amax, per);
        lives[i] = true;
      }
    }
    return memo[now][a] = amax;
  }
}

int main() {
  cin >> M >> N;
  pers.resize(M);
  for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 1 << 16; ++j) {
      memo[i][j] = -2;
    }
  }
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      ld p;
      cin >> p;
      pers[i].push_back(p);
    }
  }
  bitset<16> bs((1 << 16) - 1);
  ld ans = check(0, bs);
  cout << fixed << setprecision(22) << ans << 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>>;

vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
  const int a = lives.to_ulong();
  if (!(memo[now][a] < -1)) {
    return memo[now][a];
  } else {
    ld amax = 0;
    for (int i = 0; i < M; ++i) {
      if (lives[i]) {
        lives[i] = false;
        ld per = 0;
        ld living = 1;
        for (int enemy = now; enemy < N; ++enemy) {
          per += living * (1 - pers[i][enemy]) * check(enemy, lives);
          living *= pers[i][enemy];
        }
        per += living;
        amax = max(amax, per);
        lives[i] = true;
      }
    }
    return memo[now][a] = amax;
  }
}

int main() {
  cin >> M >> N;
  pers.resize(M);
  for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 1 << 16; ++j) {
      memo[i][j] = -2;
    }
  }
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      ld p;
      cin >> p;
      pers[i].push_back(p);
    }
  }
  bitset<16> bs((1 << 16) - 1);
  ld ans = check(0, bs);
  cout << fixed << setprecision(22) << ans << endl;
  return 0;
}
Problem p01359 (C++)

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

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

using namespace std;

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    vector<tuple<int, int, string>> v;
    REP(i, n) {
      string name;
      int era, western;
      cin >> name >> era >> western;
      v.emplace_back(western, era, name);
    }
    sort(begin(v), end(v));
    REP(i, q) {
      int year;
      cin >> year;
      int ue, uw;
      string un;
      tie(uw, ue, un) =
          *upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
      if (year <= uw - ue)
        cout << "Unknown" << endl;
      else
        cout << un << ' ' << ue - (uw - year) << endl;
    }
  }
  return 0;
}

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

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

using namespace std;

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    vector<tuple<int, int, string>> v;
    REP(i, n) {
      string name;
      int era, western;
      cin >> name >> era >> western;
      v.emplace_back(western, era, name);
    }
    sort(begin(v), end(v));
    REP(i, q) {
      int year;
      cin >> year;
      int ue, uw;
      string un;
      auto itr = upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
      if (itr == end(v)) {
        cout << "Unknown" << endl;
        continue;
      }
      tie(uw, ue, un) = *itr;
      if (year <= uw - ue)
        cout << "Unknown" << endl;
      else
        cout << un << ' ' << ue - (uw - year) << endl;
    }
  }
  return 0;
}
Problem p01359 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;

int N, Q, Y, W;
string Name;
pls E[1000];

int main(void) {
  while (cin >> N >> Q, N || Q) {
    REP(i, 0, N) {
      cin >> Name >> Y >> W;
      E[i] = pls(pll(W - Y + 1, Y), Name);
    }
    E[N] = pls(pll(INF, 0), "");

    sort(E, E + N);

    REP(i, 0, Q) {
      int q;
      cin >> q;
      if (q < E[0].fi.fi) {
        cout << "Unknown" << endl;
        continue;
      }
      REP(j, 0, N) {
        if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
          int ans = q - E[j].fi.fi + 1;
          if (ans <= E[j].fi.se) {
            cout << E[j].se << " " << ans << endl;
            goto END;
          }
        }
      }
      cout << "Unknown" << endl;
    END:;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;

int N, Q, Y, W;
string Name;
pls E[1001];

int main(void) {
  while (cin >> N >> Q, N || Q) {
    REP(i, 0, N) {
      cin >> Name >> Y >> W;
      E[i] = pls(pll(W - Y + 1, Y), Name);
    }
    E[N] = pls(pll(INF, 0), "");

    sort(E, E + N);

    REP(i, 0, Q) {
      int q;
      cin >> q;
      if (q < E[0].fi.fi) {
        cout << "Unknown" << endl;
        continue;
      }
      REP(j, 0, N) {
        if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
          int ans = q - E[j].fi.fi + 1;
          if (ans <= E[j].fi.se) {
            cout << E[j].se << " " << ans << endl;
            goto END;
          }
        }
      }
      cout << "Unknown" << endl;
    END:;
    }
  }
  return 0;
}
Problem p01360 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void solve(string s) {
  int stepnum[10001][2];
  // stepnum[i][0] :  (0から数えて)iばんめの数字の移動のときに左足を使った
  // stepnum[i][1] :            　　　　　　　       　　　右足を使った
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < 2; j++) {
      stepnum[i][j] = 0;
    }
  }
  int s1 = (s[0] - '0') % 3, s2;
  for (int i = 1; i < s.length(); i++) {
    s2 = (s[i] - '0') % 3;
    if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
      // 右左、右右、左左はok
      stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
      stepnum[i][1] = stepnum[i - 1][1] + 1;
    } else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
               (s1 == 2 && s2 == 0)) {
      // 左右、左左、右右はok
      stepnum[i][0] = stepnum[i - 1][0] + 1;
      stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
    } else {
      // どの足でもok
      stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
      stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
    }
    s1 = s2;
  }
  cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    solve(s);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void solve(string s) {
  int stepnum[100001][2];
  // stepnum[i][0] :  (0から数えて)iばんめの数字の移動のときに左足を使った
  // stepnum[i][1] :            　　　　　　　       　　　右足を使った
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < 2; j++) {
      stepnum[i][j] = 0;
    }
  }
  int s1 = (s[0] - '0') % 3, s2;
  for (int i = 1; i < s.length(); i++) {
    s2 = (s[i] - '0') % 3;
    if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
      // 右左、右右、左左はok
      stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
      stepnum[i][1] = stepnum[i - 1][1] + 1;
    } else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
               (s1 == 2 && s2 == 0)) {
      // 左右、左左、右右はok
      stepnum[i][0] = stepnum[i - 1][0] + 1;
      stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
    } else {
      // どの足でもok
      stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
      stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
    }
    s1 = s2;
  }
  cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    solve(s);
  }
  return 0;
}
Problem p01360 (C++)

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

using namespace std;

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

int dp[2][2][9];

bool valid(int left, int right) {
  if (left == right)
    return false;
  left %= 3;
  right %= 3;
  return left <= right;
}

int main() {
  string str;
  while (true) {
    cin >> str;
    if (str == "#")
      break;
    rep(i, 2) rep(j, 9) {
      if (i == 0) {
        if (valid(str[0] - '1', j))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      } else {
        if (valid(j, str[0] - '1'))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      }
    }
    rep(i, str.size() - 1) {
      rep(j, 2) rep(k, 9) dp[1][i][j] = 1e9;
      rep(j, 2) rep(k, 9) {
        if (j == 0) {
          if (valid(str[i + 1] - '1', k))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i + 1] - '1', str[i] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        } else {
          if (valid(k, str[i + 1] - '1'))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i] - '1', str[i + 1] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        }
      }
      rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
    }
    int res = 1e9;
    rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

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

int dp[2][2][9];

bool valid(int left, int right) {
  if (left == right)
    return false;
  left %= 3;
  right %= 3;
  return left <= right;
}

int main() {
  string str;
  while (true) {
    cin >> str;
    if (str == "#")
      break;
    rep(i, 2) rep(j, 9) {
      if (i == 0) {
        if (valid(str[0] - '1', j))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      } else {
        if (valid(j, str[0] - '1'))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      }
    }
    rep(i, str.size() - 1) {
      rep(j, 2) rep(k, 9) dp[1][j][k] = 1e9;
      rep(j, 2) rep(k, 9) {
        if (j == 0) {
          if (valid(str[i + 1] - '1', k))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i + 1] - '1', str[i] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        } else {
          if (valid(k, str[i + 1] - '1'))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i] - '1', str[i + 1] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        }
      }
      rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
    }
    int res = 1e9;
    rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
    cout << res << endl;
  }
  return 0;
}
Problem p01366 (C++)

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

using namespace std;

constexpr int maxN = 10005;
constexpr int maxM = 40005;

int N, M;

int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
  gto[e] = v;
  length[e] = d;
  cost[e] = c;
  gnxt[e] = adj[u];
  adj[u] = e++;
}

void print_edge(int s) {
  printf("%d:", s);
  for (int e = adj[s]; ~e; e = gnxt[e])
    printf(" %d", gto[e]);
  putchar('\n');
}

int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
  memset(dis, 0x3f, sizeof dis);
  dis[s] = 0;
  for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
    for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
      if (dis[v] > dis[u] + length[e]) {
        dis[v] = dis[u] + length[e];
        pre[v] = cost[e];
        if (!inq[v])
          inq[qu[++qt % N] = v] = 1;
      } else if (dis[v] == dis[u] + length[e]) {
        pre[v] = min(pre[v], cost[e]);
      }
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N || M)) {
    memset(adj, -1, sizeof adj);
    for (int i = 0; i != M; ++i) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
      add_edge(v, u, d, c);
    }
    bellman_ford(1);
    long long ans = 0;
    for (int i = 1; i <= N; ++i)
      ans += pre[i];
    printf("%lld\n", ans);
  }
  return 0;
}

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

using namespace std;

constexpr int maxN = 10005;
constexpr int maxM = 400005;

int N, M;

int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
  gto[e] = v;
  length[e] = d;
  cost[e] = c;
  gnxt[e] = adj[u];
  adj[u] = e++;
}

void print_edge(int s) {
  printf("%d:", s);
  for (int e = adj[s]; ~e; e = gnxt[e])
    printf(" %d", gto[e]);
  putchar('\n');
}

int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
  memset(dis, 0x3f, sizeof dis);
  dis[s] = 0;
  for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
    for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
      if (dis[v] > dis[u] + length[e]) {
        dis[v] = dis[u] + length[e];
        pre[v] = cost[e];
        if (!inq[v])
          inq[qu[++qt % N] = v] = 1;
      } else if (dis[v] == dis[u] + length[e]) {
        pre[v] = min(pre[v], cost[e]);
      }
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N || M)) {
    memset(adj, -1, sizeof adj);
    for (int i = 0; i != M; ++i) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
      add_edge(v, u, d, c);
    }
    bellman_ford(1);
    long long ans = 0;
    for (int i = 1; i <= N; ++i)
      ans += pre[i];
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p01366 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
list<st> E[10000];
int d[10000], c[10000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) E[i].clear();
    rep(i, m) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      u--;
      v--;
      E[u].push_back({v, d, c});
      E[v].push_back({u, d, c});
    }
    memset(d, 0x3f, sizeof(d));
    priority_queue<P> que;
    d[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      for (st &v : E[p.second]) {
        if (d[v.a] > p.first + v.b) {
          d[v.a] = p.first + v.b;
          c[v.a] = v.c;
          que.push(P(d[v.a], v.a));
        } else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
          c[v.a] = v.c;
      }
    }
    printf("%d\n", accumulate(c, c + n, 0));
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
vector<st> E[10000];
int d[10000], c[10000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) E[i].clear();
    rep(i, m) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      u--;
      v--;
      E[u].push_back({v, d, c});
      E[v].push_back({u, d, c});
    }
    memset(d, 0x3f, sizeof(d));
    priority_queue<P> que;
    d[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      for (st &v : E[p.second]) {
        if (d[v.a] > p.first + v.b) {
          d[v.a] = p.first + v.b;
          c[v.a] = v.c;
          que.push(P(d[v.a], v.a));
        } else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
          c[v.a] = v.c;
      }
    }
    printf("%d\n", accumulate(c, c + n, 0));
  }
}
Problem p01366 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 1005
#define MAX_M 100005
#define INF 1e9 + 1
int n, m, x;
struct edge {
  int to, dis, cost;
  edge();
  edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_N, INF);
  fill(ans, ans + MAX_N, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.dis) {
        ans[e.to] = e.cost;
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));

      } else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
        ans[e.to] = e.cost;
      }
    }
  }
  return accumulate(ans + 2, ans + n + 1, res);
}

int main() {
  while (scanf("%d%d", &n, &m) && n != 0) {
    int u, v, d, c;
    for (int i = 0; i <= n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      G[u].push_back(edge(v, d, c));
      G[v].push_back(edge(u, d, c));
    }
    sum = dijkstra(1);
    printf("%d\n", sum);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 10006
#define MAX_M 20000
#define INF 1e9 + 1
int n, m, x;
struct edge {
  int to, dis, cost;
  edge();
  edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_N, INF);
  fill(ans, ans + MAX_N, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.dis) {
        ans[e.to] = e.cost;
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));

      } else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
        ans[e.to] = e.cost;
      }
    }
  }
  return accumulate(ans + 2, ans + n + 1, res);
}

int main() {
  while (scanf("%d%d", &n, &m) && n != 0) {
    int u, v, d, c;
    for (int i = 0; i <= n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      G[u].push_back(edge(v, d, c));
      G[v].push_back(edge(u, d, c));
    }
    sum = dijkstra(1);
    printf("%d\n", sum);
  }
}
Problem p01368 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

const int vmax = 1010;

int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];

bool dfs(int v) {
  used[v] = true;
  for (auto &u : graph[v]) {
    int w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  rep(i, vmax) match[i] = -1;
  rep(v, X + Y) {
    if (match[v] < 0) {
      rep(i, vmax) used[i] = 0;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int n, m, l;
int dist[110][110];
pair<int, int> info[1010];

int main(void) {
  while (cin >> n >> m >> l) {
    if (n == 0)
      break;
    rep(i, vmax) graph[i].clear();
    rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
    rep(i, m) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
        min(dist[i][j], dist[i][k] + dist[k][j]);

    rep(i, l) cin >> info[i].second >> info[i].first;
    sort(info, info + l);
    X = Y = l - 1;
    rep(i, l - 1) rep(j, l - 1) {
      if (i > j)
        continue;
      int a = i, b = j + 1;
      if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
        continue;
      b += X - 1;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

const int vmax = 2010;

int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];

bool dfs(int v) {
  used[v] = true;
  for (auto &u : graph[v]) {
    int w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  rep(i, vmax) match[i] = -1;
  rep(v, X + Y) {
    if (match[v] < 0) {
      rep(i, vmax) used[i] = 0;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int n, m, l;
int dist[110][110];
pair<int, int> info[1010];

int main(void) {
  while (cin >> n >> m >> l) {
    if (n == 0)
      break;
    rep(i, vmax) graph[i].clear();
    rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
    rep(i, m) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
        min(dist[i][j], dist[i][k] + dist[k][j]);

    rep(i, l) cin >> info[i].second >> info[i].first;
    sort(info, info + l);
    X = Y = l - 1;
    rep(i, l - 1) rep(j, l - 1) {
      if (i > j)
        continue;
      int a = i, b = j + 1;
      if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
        continue;
      b += X - 1;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}
Problem p01388 (C++)

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

using namespace std;

int rmn_min(int x, int y) { return (x < y ? x : y); }

int main() {
  int k = 0;
  int u = 0;
  int p = 0;
  int c = 0;
  char line[MAX];
  cin >> line;
  for (int i = 0; line[i] != 0; i++) {
    switch (line[i]) {
    case 'K':
      k++;
      break;
    case 'U':
      u++;
      break;
    case 'P':
      p++;
      break;
    case 'C':
      c++;
      break;
    }
  }
  cout << rmn_min(rmn_min(rmn_min(k, u), p), c) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define MAX 301

using namespace std;

int rmn_min(int x, int y) { return (x < y ? x : y); }

int main() {
  int k = 0;
  int u = 0;
  int p = 0;
  int c = 0;
  char line[MAX];
  cin >> line;
  for (int i = 0; line[i] != 0; i++) {
    switch (line[i]) {
    case 'K':
      k++;
      break;
    case 'U':
      u++;
      break;
    case 'P':
      p++;
      break;
    case 'C':
      c++;
      break;
    }
  }
  cout << rmn_min(rmn_min(rmn_min(k, u), p), c) << endl;
  return 0;
}
Problem p01388 (C++)

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

int main(void) {
  char KUPC[50];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, ans;

  cin >> KUPC;
  for (int i = 0; i < strlen(KUPC); i++) {
    if (KUPC[i] == 'K')
      cnt1++;
    else if (KUPC[i] == 'U')
      cnt2++;
    else if (KUPC[i] == 'P')
      cnt3++;
    else if (KUPC[i] == 'C')
      cnt4++;
  }

  ans = min(cnt1, min(cnt2, min(cnt3, cnt4)));

  cout << ans << endl;

  return 0;
}


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

int main(void) {
  char KUPC[301];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, ans;

  cin >> KUPC;
  for (int i = 0; i < strlen(KUPC); i++) {
    if (KUPC[i] == 'K')
      cnt1++;
    else if (KUPC[i] == 'U')
      cnt2++;
    else if (KUPC[i] == 'P')
      cnt3++;
    else if (KUPC[i] == 'C')
      cnt4++;
  }

  ans = min(cnt1, min(cnt2, min(cnt3, cnt4)));

  cout << ans << endl;

  return 0;
}
Problem p01389 (C++)

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

using namespace std;

int n, m;

int main() {
  string s[20];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  int field[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      field[i][j] = stoi(s[i].substr(j, 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + j == 0)
        continue;
      int a = 9999999, b = 999999;
      if (i != 0)
        a = field[i - 1][j];
      if (j != 0)
        b = field[i][j - 1];
      field[i][j] = field[i][j] + min(a, b);
    }
  }
  cout << field[n - 1][m - 1] << endl;
}

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

using namespace std;

int n, m;

int main() {
  string s[60];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  int field[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      field[i][j] = stoi(s[i].substr(j, 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + j == 0)
        continue;
      int a = 9999999, b = 999999;
      if (i != 0)
        a = field[i - 1][j];
      if (j != 0)
        b = field[i][j - 1];
      field[i][j] = field[i][j] + min(a, b);
    }
  }
  cout << field[n - 1][m - 1] << endl;
}
Problem p01389 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int cnt[50][50], dp[60][60];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  rep(i, h) rep(j, w) scanf("%1d", &cnt[i][j]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = cnt[0][0];
  rep(i, h) rep(j, w) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt[i + 1][j]);
    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt[i][j + 1]);
  }
  printf("%d\n", dp[h - 1][w - 1]);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int cnt[60][60], dp[60][60];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  rep(i, h) rep(j, w) scanf("%1d", &cnt[i][j]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = cnt[0][0];
  rep(i, h) rep(j, w) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt[i + 1][j]);
    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt[i][j + 1]);
  }
  printf("%d\n", dp[h - 1][w - 1]);
}
Problem p01391 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <ctime>
#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
int N, K, card[500][500], r[1000];
int main() {
  srand((unsigned)time(NULL));
  scanf("%d%d", &N, &K);
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N / 2; j++) {
      scanf("%d", &card[i][j]);
    }
  }
  while (true) {
    for (int i = 0; i < N; i++)
      r[i] = rand() % 2;
    bool flag = true;
    for (int i = 0; i < K; i++) {
      int c = 0;
      for (int j = 0; j < N / 2; j++) {
        if (r[card[i][j] - 1])
          c++;
      }
      if (c < N / 8 || 3 * N / 8 < c) {
        flag = false;
        break;
      }
    }
    if (flag) {
      for (int i = 0; i < N; i++) {
        printf("%d", r[i]);
      }
      printf("\n");
    }
  }
  return 0;
}

Fixed submission:
#include <ctime>
#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
int N, K, card[500][500], r[1000];
int main() {
  srand((unsigned)time(NULL));
  scanf("%d%d", &N, &K);
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N / 2; j++) {
      scanf("%d", &card[i][j]);
    }
  }
  while (true) {
    for (int i = 0; i < N; i++)
      r[i] = rand() % 2;
    bool flag = true;
    for (int i = 0; i < K; i++) {
      int c = 0;
      for (int j = 0; j < N / 2; j++) {
        if (r[card[i][j] - 1])
          c++;
      }
      if (c < N / 8 || 3 * N / 8 < c) {
        flag = false;
        break;
      }
    }
    if (flag) {
      for (int i = 0; i < N; i++) {
        printf("%d", r[i]);
      }
      printf("\n");
      break;
    }
  }
  return 0;
}
Problem p01392 (C++)

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

#define ll long long

using namespace std;

bool isprime[1000001];
int fact[1000001];
bool isfox[1000001];
int primes[78498];

int factorial(ll arg, int base) {
  int ret = 0;
  while (arg % base == 0) {
    arg /= base;
    ret++;
  }
  return ret;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  int cnt = 0;
  ll a, b;
  for (int i = 2; i < 1000001; i++) {
    if (!isprime[i]) {
      primes[cnt++] = i;
      for (int j = 2; i * j < 1000001; j++) {
        isprime[i * j] = true;
      }
    }
  }
  cin >> a >> b;
  ll len, bias;
  if (a - b < 2) {
    bias = 2;
  } else {
    bias = a - b;
  }
  len = a + b - bias + 1;

  fill((int *)fact, (int *)fact + 1000001, 1 << 29);

  for (int i = 0; (ll)primes[i] * primes[i] <= a + b; i++) {
    for (int j = (primes[i] - bias % primes[i]) % primes[i]; j < len;
         j += primes[i]) {
      if (isfox[j])
        continue;
      int tmp = factorial(j + bias, primes[i]);
      if (tmp != 0) {
        if (fact[j] == 1 << 29 || fact[j] >= tmp) {
          fact[j] = tmp;
        } else {
          isfox[j] = true;
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < len; i++) {
    res += !isfox[i] ? 1 : 0;
  }
  cout << res << endl;
}

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

#define ll long long

using namespace std;

bool isprime[1000001];
int fact[1000001];
bool isfox[1000001];
int primes[78498];

int factorial(ll arg, int base) {
  int ret = 0;
  while (arg % base == 0) {
    arg /= base;
    ret++;
  }
  return ret;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  int cnt = 0;
  ll a, b;
  for (int i = 2; i < 1000001; i++) {
    if (!isprime[i]) {
      primes[cnt++] = i;
      for (int j = 2; i * j < 1000001; j++) {
        isprime[i * j] = true;
      }
    }
  }
  cin >> a >> b;
  ll len, bias;
  if (a - b < 2) {
    bias = 2;
  } else {
    bias = a - b;
  }
  len = a + b - bias + 1;

  fill((int *)fact, (int *)fact + 1000001, 1 << 29);

  for (int i = 0; i < 78498; i++) {
    for (int j = (primes[i] - bias % primes[i]) % primes[i]; j < len;
         j += primes[i]) {
      if (isfox[j])
        continue;
      int tmp = factorial(j + bias, primes[i]);
      if (tmp != 0) {
        if (fact[j] == 1 << 29 || fact[j] >= tmp) {
          fact[j] = tmp;
        } else {
          isfox[j] = true;
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < len; i++) {
    res += !isfox[i] ? 1 : 0;
  }
  cout << res << endl;
}
Problem p01392 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 1000007
long long A, B, W[MAX_N], cnt;
bool prime[MAX_N];
vector<short> V[MAX_N];
void primesieve() {
  for (int i = 2; i * i <= 1000000; i++) {
    for (int j = i * i; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    prime[i] ^= true;
}
int main() {
  cin >> A >> B;
  long long L = A - B, R = A + B;
  primesieve();
  if (L < 0)
    L = 1;
  for (long long i = L; i <= R; i++) {
    W[i - L] = i;
  }
  for (long long i = 2; i <= 1000000; i++) {
    if (prime[i] == false)
      continue;
    long long F = (L / i) * i;
    if ((L % i) > 0)
      F += i;
    for (long long j = F; j <= R; j += i) {
      int cnts = 0;
      while (W[j - L] % i == 0) {
        W[j - L] /= i;
        cnts++;
      }
      if (cnts >= 1) {
        V[j - L].push_back(cnts);
      }
    }
  }
  for (long long i = L; i <= R; i++) {
    if (W[i - L] >= 2 && W[i - L] != i)
      V[i - L].push_back(1);
  }
  for (long long i = L; i <= R; i++) {
    bool OK = true;
    if (i <= 1LL)
      OK = false;
    for (int j = 1; j < V[i - L].size(); j++) {
      if (V[i - L][j - 1] < V[i - L][j])
        OK = false;
    }
    if (OK == true)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 1000007
long long A, B, W[MAX_N], cnt;
bool prime[MAX_N];
vector<short> V[MAX_N];
void primesieve() {
  for (int i = 2; i * i <= 1000000; i++) {
    for (int j = i * i; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    prime[i] ^= true;
}
int main() {
  cin >> A >> B;
  long long L = A - B, R = A + B;
  primesieve();
  if (L <= 0)
    L = 1;
  for (long long i = L; i <= R; i++) {
    W[i - L] = i;
  }
  for (long long i = 2; i <= 1000000; i++) {
    if (prime[i] == false)
      continue;
    long long F = (L / i) * i;
    if ((L % i) > 0)
      F += i;
    for (long long j = F; j <= R; j += i) {
      int cnts = 0;
      while (W[j - L] % i == 0) {
        W[j - L] /= i;
        cnts++;
      }
      if (cnts >= 1) {
        V[j - L].push_back(cnts);
      }
    }
  }
  for (long long i = L; i <= R; i++) {
    if (W[i - L] >= 2 && W[i - L] != i)
      V[i - L].push_back(1);
  }
  for (long long i = L; i <= R; i++) {
    bool OK = true;
    if (i <= 1LL)
      OK = false;
    for (int j = 1; j < V[i - L].size(); j++) {
      if (V[i - L][j - 1] < V[i - L][j])
        OK = false;
    }
    if (OK == true)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p01398 (C++)

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

int k(int a) {
  for (int i = 0;; i++) {
    if (a - 26 * i >= 'a' && a - 26 * i <= 'z')
      return a - 26 * i;
  }
}
int main() {
  int a;
  while (cin >> a, a) {
    string b;
    cin >> b;
    int x[100], y[100];
    for (int c = 0; c < a; c++) {
      scanf("%d%d", &x[c], &y[c]);
      x[c]--;
      y[c]--;
    }
    for (int i = a - 1; i >= 0; i--) {
      int d = (int)b[x[i]] + abs(x[i] - y[i]),
          e = (int)b[y[i]] + abs(x[i] - y[i]);
      d = k((char)d);
      e = k((char)e);
      b[x[i]] = e;
      b[y[i]] = d;
    }
    cout << b << endl;
  }
}

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

int k(int a) {
  for (int i = 0;; i++) {
    if (a - 26 * i >= 'a' && a - 26 * i <= 'z')
      return a - 26 * i;
  }
}
int main() {
  int a;
  while (cin >> a, a) {
    string b;
    cin >> b;
    int x[100], y[100];
    for (int c = 0; c < a; c++) {
      scanf("%d%d", &x[c], &y[c]);
      x[c]--;
      y[c]--;
    }
    for (int i = a - 1; i >= 0; i--) {
      int d = (int)b[x[i]] + abs(x[i] - y[i]),
          e = (int)b[y[i]] + abs(x[i] - y[i]);
      d = k(d);
      e = k(e);
      b[x[i]] = e;
      b[y[i]] = d;
    }
    cout << b << endl;
  }
}
Problem p01399 (C++)

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

using namespace std;

struct S {
  int d, t, n;
  bool operator<(S a) const {
    if (t != a.t) {
      return t < a.t;
    } else {
      return d < a.d;
    }
  }
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    S p[40];
    for (int i = 0; i < n; i++) {
      cin >> p[i].d;
      p[i].n = i + 1;
      p[i].t = m / p[i].d * p[i].d;
    }
    sort(p, p + n);
    if (!(p[0] < p[1]) && !(p[1] < p[0])) {
      cout << n << endl;
    } else {
      cout << p[0].n << endl;
    }
  }
  return 0;
}

Fixed submission:
// 50
#include <algorithm>
#include <iostream>

using namespace std;

struct S {
  int d, t, n;
  bool operator<(S a) const {
    if (t != a.t) {
      return t < a.t;
    } else {
      return d < a.d;
    }
  }
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    S p[1000];
    for (int i = 0; i < n; i++) {
      cin >> p[i].d;
      p[i].n = i + 1;
      p[i].t = m / p[i].d * p[i].d;
    }
    sort(p, p + n);
    if (!(p[0] < p[1]) && !(p[1] < p[0])) {
      cout << n << endl;
    } else {
      cout << p[0].n << endl;
    }
  }
  return 0;
}