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

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

using namespace std;

int n, m;
int hon[101];
int nibun() {
  int R, L;
  R = L = 0;
  for (int i = 0; i < m; i++) {
    L += hon[i];
  }
  while (1) {
    if (R == L) {
      return R;
    }
    double H = (double)(R + L) / 2;

    int dan = 0;
    int at = 0;
    for (int i = 0; i < m; i++) {
      if (hon[i] > H) {
        dan = 10000;
        break;
      }
      if (at + hon[i] <= H) {
        at += hon[i];
      } else {
        at = 0;
        dan++;
        at += hon[i];
      }
    }

    if (dan < n) {
      L = H;
    } else {
      R = H + 0.5;
    }
  }
}
int main() {
  int cont = 0;
  while (1) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
      scanf("%d", &hon[i]);
    }
    printf("%d\n", nibun());
  }
}

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

using namespace std;

int n, m;
int hon[101];
int nibun() {
  int R, L;
  R = L = 0;
  for (int i = 0; i < m; i++) {
    L += hon[i];
  }
  while (1) {
    if (R == L) {
      return R;
    }
    double H = (double)(R + L) / 2;

    int dan = 0;
    int at = 0;
    for (int i = 0; i < m; i++) {
      if (hon[i] > H) {
        dan = 10000;
        break;
      }
      if (at + hon[i] <= H) {
        at += hon[i];
      } else {
        at = 0;
        dan++;
        at += hon[i];
      }
    }

    if (dan < n) {
      L = H;
    } else {
      R = H + 0.5;
    }
  }
}
int main() {
  int cont = 0;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; i++) {
      scanf("%d", &hon[i]);
    }
    printf("%d\n", nibun());
  }
}
Problem p00181 (C++)

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

using namespace std;
int m, n, w[100];

int calc(int t) {
  int sum = 0, ret = 1;
  for (int i = 0; i < n; i++) {
    if (w[i] > t)
      return 1500001;
    if (sum + w[i] > t) {
      sum = 0;
      ret++;
    }
    sum += w[i];
  }
  return ret;
}

int main() {

  while (1) {
    cin >> m >> n;
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    int l = 1, h = 1500000;
    while (h - l > 1) {
      int md = (l + h) / 2;
      if (calc(md) > m)
        l = md;
      else
        h = md;
    }

    cout << h << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;
int m, n, w[100];

int calc(int t) {
  int sum = 0, ret = 1;
  for (int i = 0; i < n; i++) {
    if (w[i] > t)
      return 1500001;
    if (sum + w[i] > t) {
      sum = 0;
      ret++;
    }
    sum += w[i];
  }
  return ret;
}

int main() {

  while (1) {
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    int l = 1, h = 1500000;
    while (h - l > 1) {
      int md = (l + h) / 2;
      if (calc(md) > m)
        l = md;
      else
        h = md;
    }

    cout << h << endl;
  }

  return 0;
}
Problem p00181 (C++)

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int M, N;
int W[100];

bool C(int x) {
  int cnt = 0;
  int cur = 0;
  while (cur < N) {
    int next = cur;
    int sum = 0;
    while (next < N && sum + W[next] <= x)
      sum += W[next++];
    cur = next;
    cnt++;
  }
  return cnt <= M;
}

signed main() {
  while (cin >> M >> N, M || N) {
    rep(i, N) cin >> W[i];
    int lb = *min_element(W, W + N) - 1, ub = 1500000;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        ub = mid;
      else
        lb = mid;
    }
    cout << ub << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int M, N;
int W[100];

bool C(int x) {
  int cnt = 0;
  int cur = 0;
  while (cur < N) {
    int next = cur;
    int sum = 0;
    while (next < N && sum + W[next] <= x)
      sum += W[next++];
    cur = next;
    cnt++;
  }
  return cnt <= M;
}

signed main() {
  while (cin >> M >> N, M || N) {
    rep(i, N) cin >> W[i];
    int lb = *max_element(W, W + N) - 1, ub = 1500000;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        ub = mid;
      else
        lb = mid;
    }
    cout << ub << endl;
  }
  return 0;
}
Problem p00182 (C++)

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

bool dfs(int i);
int n, a[50];
bool fil[50];

bool dfs2(int i, int j, int rem) {
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

bool dfs(int i) {
  if (i == n)
    return true;
  if (!fil[i])
    return false;
  return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a[i]);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    puts(dfs(0) ? "YES" : "NO");
  }

  return 0;
}

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

bool dfs(int i);
int n, a[50];
bool fil[50];

bool dfs2(int i, int j, int rem) {
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

bool dfs(int i) {
  if (i == n)
    return true;
  if (!fil[i])
    return false;
  return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a + i);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    puts(dfs(0) ? "YES" : "NO");
  }

  return 0;
}
Problem p00182 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
  if (b == 0) {
    bool ok = true;
    for (int i = 0; i < n; i++)
      if (!u[i]) {
        ok = false;
        break;
      }
    if (ok)
      return 1;
    int at = 0;
    for (int i = 0; i < n; i++) {
      if (v[i]) {
        v[i] = 0;
        int res = solve(0, c[i]);
        if (res)
          return 1;
        v[i] = 1;
      }
    }
    return 0;
  }
  if (a == n)
    return 0;
  if (!u[a] && b >= c[a]) {
    u[a] = v[a] = 1;
    if (solve(a + 1, b - c[a]))
      return 1;
    u[a] = v[a] = 0;
  }
  int rem = 0;
  for (int i = a + 1; i < n; i++)
    if (b >= c[i] && !u[i])
      rem += c[i];
  if (rem < b)
    return 0;
  return solve(a + 1, b);
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    std::sort(c, c + a);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    for (int i = 0; i < a; i++)
      v[i] = u[i] = 0;
    v[0] = u[0] = 1;
    if (solve(0, 0))
      printf("YES\n");
    else
      printf("NO\n");
  }
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
  if (b == 0) {
    bool ok = true;
    for (int i = 0; i < n; i++)
      if (!u[i]) {
        ok = false;
        break;
      }
    if (ok)
      return 1;
    int at = 0;
    for (int i = 0; i < n; i++) {
      if (v[i]) {
        v[i] = 0;
        int res = solve(0, c[i]);
        if (res)
          return 1;
        v[i] = 1;
      }
      if (!u[i])
        break;
    }
    return 0;
  }
  if (a == n)
    return 0;
  if (!u[a] && b >= c[a]) {
    u[a] = v[a] = 1;
    if (solve(a + 1, b - c[a]))
      return 1;
    u[a] = v[a] = 0;
  }
  int rem = 0;
  for (int i = a + 1; i < n; i++)
    if (b >= c[i] && !u[i])
      rem += c[i];
  if (rem < b)
    return 0;
  return solve(a + 1, b);
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    std::sort(c, c + a);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    for (int i = 0; i < a; i++)
      v[i] = u[i] = 0;
    v[0] = u[0] = 1;
    if (solve(0, 0))
      printf("YES\n");
    else
      printf("NO\n");
  }
}
Problem p00182 (C++)

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

typedef long long ll;

int n;
int lim[52], vol[52];
set<ll> no[52];

bool solve(int idx);

bool distribute(int idx, int next) {
  if (idx == n) {
    return false;
  }

  ll bit = 0;

  for (int i = idx; i < n; i++) {
    bit <<= 1;
    if (vol[i] == lim[i]) {
      bit++;
    }
  }

  if (no[idx].find(bit) != no[idx].end()) {
    return false;
  }
  no[idx].insert(bit);

  for (int i = next; i < n; i++) {
    if (vol[i] != 0)
      continue;
    if (vol[idx] < lim[i])
      continue;
    vol[idx] -= lim[i];
    vol[i] = lim[i];

    if (vol[idx] != 0 && distribute(idx, i + 1))
      return true;
    if (vol[idx] == 0 && solve(idx + 1))
      return true;

    vol[idx] += lim[i];
    vol[i] = 0;
  }

  return false;
}

bool solve(int idx) {
  if (idx == n)
    return true;
  if (vol[idx] == 0)
    return false;

  if (solve(idx + 1))
    return true;
  if (distribute(idx, idx + 1))
    return true;

  return false;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> lim[i];
      no[i].clear();
      vol[i] = 0;
    }
    sort(lim, lim + n, greater<int>());

    vol[0] = lim[0];
    cout << (solve(0) ? "YES" : "NO") << endl;
  }
}

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

typedef long long ll;

int n;
int lim[52], vol[52];
set<ll> no[52];

bool solve(int idx);

bool distribute(int idx, int next) {
  if (idx == n) {
    return false;
  }

  ll bit = 0;

  for (int i = idx; i < n; i++) {
    bit <<= 1;
    if (vol[i] == lim[i]) {
      bit++;
    }
  }

  if (no[idx].find(bit) != no[idx].end()) {
    return false;
  }
  no[idx].insert(bit);

  for (int i = next; i < n; i++) {
    if (vol[i] != 0)
      continue;
    if (vol[idx] < lim[i])
      continue;
    if (i - 1 != idx && lim[i - 1] == lim[i] && vol[i - 1] == 0)
      continue;
    vol[idx] -= lim[i];
    vol[i] = lim[i];

    if (vol[idx] != 0 && distribute(idx, i + 1))
      return true;
    if (vol[idx] == 0 && solve(idx + 1))
      return true;

    vol[idx] += lim[i];
    vol[i] = 0;
  }

  return false;
}

bool solve(int idx) {
  if (idx == n)
    return true;
  if (vol[idx] == 0)
    return false;

  if (solve(idx + 1))
    return true;
  if (distribute(idx, idx + 1))
    return true;

  return false;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> lim[i];
      no[i].clear();
      vol[i] = 0;
    }
    sort(lim, lim + n, greater<int>());

    vol[0] = lim[0];
    cout << (solve(0) ? "YES" : "NO") << endl;
  }
}
Problem p00184 (C++)

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

using namespace std;

int judge(int);

int main() {
  int n, age;

  for (;;) {
    int cnt[7] = {0};

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> age;
      cnt[judge(age)]++;
    }
    for (int i = 0; i < 7; i++)
      cout << cnt[i] << endl;
  }
  return 0;
}

int judge(int age) { return age / 10; }

Fixed submission:
#include <iostream>

using namespace std;

int judge(int);

int main() {
  int n, age;

  for (;;) {
    int cnt[7] = {0};

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> age;
      cnt[judge(age)]++;
    }
    for (int i = 0; i < 7; i++)
      cout << cnt[i] << endl;
  }
  return 0;
}

int judge(int age) { return min(age / 10, 6); }
Problem p00185 (C++)

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

int main() {
  int a, d, n;
  bool prime[SIZE];

  for (int i = 0; i < SIZE; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i < SIZE; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < SIZE; j++) {
        prime[j * i] = 0;
      }
    }
  }

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i <= N / 2; i++) {
      if (prime[i] && prime[N - i]) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }
}

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

int main() {
  int a, d, n;
  bool prime[SIZE];

  for (int i = 0; i < SIZE; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i < SIZE; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < SIZE; j++) {
        prime[j * i] = 0;
      }
    }
  }

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i <= N / 2; i++) {
      if (prime[i] && prime[N - i]) {
        cnt++;
      }
    }

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

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

int main() {
  bool prime[1000001] = {true, true};
  vector<int> ps;
  for (int i = 4; i <= 1000000; i += 2)
    prime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2) {
    for (int j = i * 2; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    if (!prime[i])
      ps.push_back(i);
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; i < ps.size(); i++) {
      if (ps[i] > n / 2)
        break;
      if (find(ps.begin(), ps.end(), n - ps[i]) != ps.end())
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int main() {
  bool prime[1000001] = {true, true};
  vector<int> ps;
  for (int i = 4; i <= 1000000; i += 2)
    prime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2) {
    for (int j = i * 2; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    if (!prime[i])
      ps.push_back(i);
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; i < ps.size(); i++) {
      if (ps[i] > n / 2)
        break;
      if (upper_bound(ps.begin(), ps.end(), n - ps[i]) -
          lower_bound(ps.begin(), ps.end(), n - ps[i]))
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00185 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
bool prime[1000010];
void sieve() {
  for (int i = 1; i <= 1000000; i++) {
    prime[i] = true;
  }
  prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= 1000000; j += i) {
        prime[j] = false;
      }
    }
  }
}
int main() {
  int n;
  sieve();
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 2; i <= n / 2; i++) {
      if (!prime[i])
        continue;
      for (int j = i; i + j <= n; j++) {
        if (!prime[j])
          continue;
        if (i + j == n) {
          ans++;
          // cout <<i<<" "<<j<<endl;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
bool prime[1000010];
void sieve() {
  for (int i = 1; i <= 1000000; i++) {
    prime[i] = true;
  }
  prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= 1000000; j += i) {
        prime[j] = false;
      }
    }
  }
}
int main() {
  int n;
  sieve();
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 2; i <= n / 2; i++) {
      if (prime[i] && prime[n - i]) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00185 (C++)

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

int prime[MAX - 4];

void is_prime() {
  for (int i = 0; i < MAX; i++)
    prime[i] = 0;
  prime[0] = prime[1] = 1;
  for (int i = 4; i < MAX; i += 2)
    prime[i] = 1;

  for (int i = 3; i * i < MAX; i++) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX; j += i)
      prime[j] = 1;
  }
}

int main() {
  int n;

  is_prime();

  while (true) {
    cin >> n;
    if (!n)
      break;

    int cnt = 0;
    for (int i = 2; i <= n / 2; i++) {
      if (!prime[i] && !prime[n - i])
        cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

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

int prime[MAX - 4];

void is_prime() {
  for (int i = 0; i < MAX; i++)
    prime[i] = 0;
  prime[0] = prime[1] = 1;
  for (int i = 4; i < MAX; i += 2)
    prime[i] = 1;

  for (int i = 3; i * i < MAX; i++) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX; j += i)
      prime[j] = 1;
  }
}

int main() {
  int n;

  is_prime();

  while (true) {
    cin >> n;
    if (!n)
      break;

    int cnt = 0;
    for (int i = 2; i <= n / 2; i++) {
      if (!prime[i] && !prime[n - i])
        cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p00185 (C++)

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

int p[5000000];

bool prime(int x) {
  if (x == 2)
    return false;

  if (x < 1 || x % 2 == 0)
    return false;

  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

int main() {
  int n, ans;

  int j = 0;
  for (int i = 3; i <= 1000000; i++)
    if (prime(i))
      p[j++] = i;
  while (1) {
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; p[i] < n; i++) {
      for (int j = i; p[j] < n; j++) {
        if (n == (p[i] + p[j])) {

          ans++;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int p[5000000];

bool prime(int x) {
  if (x == 2)
    return false;

  if (x < 1 || x % 2 == 0)
    return false;

  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

int main() {
  int n, ans;

  int j = 0;
  for (int i = 3; i <= 1000000; i++)
    if (prime(i))
      p[j++] = i;
  while (1) {
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; p[i] <= (n + 1) / 2; i++) {
      if (prime(n - p[i]))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00185 (C++)

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

#define MN 500001
bool pr[MN] = {0};

int main() {
  for (int i = 2; i < MN; i++) {
    pr[i] = 1;
  }
  for (int i = 2; i * i < MN; i++) {
    if (pr[i])
      for (int j = i * 2; j < MN; j += i) {
        pr[j] = 0;
      }
  }

  int n, c;
  while (cin >> n, n) {
    c = 0;
    for (int i = 3; i < n / 2 + 1; i++) {
      if (pr[i] && pr[n - i])
        c++;
    }
    cout << c << endl;
  }
}

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

#define MN 1000001
bool pr[MN] = {0};

int main() {
  for (int i = 2; i < MN; i++) {
    pr[i] = 1;
  }
  for (int i = 2; i * i < MN; i++) {
    if (pr[i])
      for (int j = i * 2; j < MN; j += i) {
        pr[j] = 0;
      }
  }

  int n, c;
  while (cin >> n, n) {
    c = 0;
    for (int i = 3; i < n / 2 + 1; i++) {
      if (pr[i] && pr[n - i])
        c++;
    }
    cout << c << endl;
  }
}
Problem p00185 (C++)

Buggy submission (status: Runtime Error):
/*1000000ÈºÌfÅüÍµ½nÈºÌÅåÌfðoÍ·évO*/
#include <iostream>
const int MAX = 1000001;
const int P_NUM = 78498;
using namespace std;

bool p[MAX];
int a[79000] = {};

void f() {
  for (int i = 0; i < MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }
  for (int i = 2; i * i <= MAX + 1; i++) {
    if (p[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }
  for (int i = 2, j = 0; i < MAX; ++i) {
    if (p[i])
      a[j++] = i;
  }
}

int main() {
  int n;

  f();

  while (cin >> n, n) {
    int ans = 0;
    int pos;
    for (pos = 0; n > a[pos]; ++pos)
      ;

    for (int i = 0; i <= pos; ++i) {
      int x = n - a[i];
      if (p[x] && x >= a[i])
        ans++; /*
 for(int j=pos ; j>=i ; --j ){
         if( a[i]+a[j] == n ){
                 ans++;
                 continue;
         }
         if( a[i]+a[j] < n ){
                 continue;
         }
 }*/
    }
    cout << ans << endl;
  }
}

Fixed submission:
/*1000000ÈºÌfÅüÍµ½nÈºÌÅåÌfðoÍ·évO*/
#include <iostream>
const int MAX = 1000001;
const int P_NUM = 78498;
using namespace std;

bool p[MAX];
int a[79000] = {};

void f() {
  for (int i = 0; i < MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }
  for (int i = 2; i * i <= MAX + 1; i++) {
    if (p[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }
  for (int i = 2, j = 0; i < MAX; ++i) {
    if (p[i])
      a[j++] = i;
  }
}

int main() {
  int n;

  f();

  while (cin >> n, n) {
    int ans = 0;
    for (int i = 0; i < n; ++i)
      if (p[i] && p[n - i] && (n - i) >= i)
        ans++;
    cout << ans << endl;
  }
}
Problem p00185 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int prime[200];
int main() {
  int count = 0;
  for (int i = 3; i < 1000; i += 2) {
    bool t = true;
    for (int j = 0; j < count; j++)
      if (i % prime[j] == 0)
        t = false;
    if (t == true) {
      prime[count] = i;
      count++;
    }
  }
  int n;
  while (true) {
    cin >> n;
    n /= 2;
    int ans = 0, k = n * 2 - 3;
    bool t;
    for (int i = 3; i <= n; i += 2) {
      t = true;
      for (int j = 0; j < count; j++) {
        if (i % prime[j] == 0) {
          if (i != prime[j]) {
            t = false;
            break;
          }
        }
        if (k % prime[j] == 0) {
          if (k != prime[j]) {
            t = false;
            break;
          }
        }
      }
      if (t)
        ans++;
      k -= 2;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int prime[200];
int main() {
  int count = 0;
  for (int i = 3; i < 1000; i += 2) {
    bool t = true;
    for (int j = 0; j < count; j++)
      if (i % prime[j] == 0)
        t = false;
    if (t == true) {
      prime[count] = i;
      count++;
    }
  }
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    n /= 2;
    int ans = 0, k = n * 2 - 3;
    bool t;
    for (int i = 3; i <= n; i += 2) {
      t = true;
      for (int j = 0; j < count; j++) {
        if (i % prime[j] == 0) {
          if (i != prime[j]) {
            t = false;
            break;
          }
        }
        if (k % prime[j] == 0) {
          if (k != prime[j]) {
            t = false;
            break;
          }
        }
      }
      if (t)
        ans++;
      k -= 2;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00185 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  vector<int> primes;
  primes.PB(2);
  for (int i = 3; i <= 1e7; i++) {
    bool flg = true;
    for (int j = 0; primes[j] * primes[j] <= i; j++) {
      if (i % primes[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      primes.PB(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; primes[i] <= n / 2; i++) {
      int j = lower_bound(ALL(primes), n - primes[i]) - primes.begin();
      if (primes[j] + primes[i] == n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  vector<int> primes;
  primes.PB(2);
  for (int i = 3; i <= 999991; i++) {
    bool flg = true;
    for (int j = 0; primes[j] * primes[j] <= i; j++) {
      if (i % primes[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      primes.PB(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; primes[i] <= n / 2; i++) {
      int j = lower_bound(ALL(primes), n - primes[i]) - primes.begin();
      if (primes[j] + primes[i] == n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00186 (C++)

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

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 1);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}

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

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 0);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}
Problem p00186 (C++)

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

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    stringstream ss;
    ss << "Q: " << Q1 << " B: " << B << " C1: " << C1 << " C2: " << C2
       << " Q2: " << Q2;

    assert(Q2 >= 1 && ss.str().c_str());

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 0);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}
Problem p00186 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int q1, b, c1, c2, q2;
int main() {
  while (scanf("%d", &q1)) {
    if (q1 == 0)
      break;
    scanf("%d%d%d%d", &b, &c1, &c2, &q2);
    int jidori = b / c1;
    jidori = (jidori < q2 ? jidori : q2);
    b -= jidori * c1;
    int normal = b / c2;
    b -= normal * c2;
    while (jidori + normal < q1) {
      jidori--;
      b += c1;
      int temp = b / c2;
      normal += temp;
      b -= c2 * temp;
    }
    if (jidori <= 0)
      puts("NA");
    else
      printf("%d %d\n", jidori, normal);
  }
}

Fixed submission:
#include <cstdio>
int q1, b, c1, c2, q2;
int main() {
  while (scanf("%d", &q1)) {
    if (q1 == 0)
      break;
    scanf("%d%d%d%d", &b, &c1, &c2, &q2);
    int jidori = b / c1;
    jidori = (jidori < q2 ? jidori : q2);
    b -= jidori * c1;
    int normal = b / c2;
    b -= normal * c2;
    while (jidori + normal < q1) {
      if (jidori <= 0)
        break;
      jidori--;
      b += c1;
      int temp = b / c2;
      normal += temp;
      b -= c2 * temp;
    }
    if (jidori <= 0)
      puts("NA");
    else
      printf("%d %d\n", jidori, normal);
  }
}
Problem p00188 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int main() {

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> vec;
    rep(i, n) {
      int in;
      cin >> in;
      vec.pb(in);
    }
    int key, cnt = 0;
    cin >> key;
    int l, r, m;
    l = 0, r = n - 1;
    while (l <= r) {
      m = (l + r) / 2;
      cnt++;
      if (vec[m] == key)
        break;
      if (vec[m] > key)
        r = m;
      else
        l = m + 1;
    }
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int main() {

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> vec;
    rep(i, n) {
      int in;
      cin >> in;
      vec.pb(in);
    }
    int key, cnt = 0;
    cin >> key;
    int l, r, m;
    l = 0, r = n - 1;
    while (l <= r) {
      m = (l + r) / 2;
      cnt++;
      if (vec[m] == key)
        break;
      if (vec[m] > key)
        r = m - 1;
      else
        l = m + 1;
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p00188 (C++)

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

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    vector<long long int> data(n);
    int left = 0;
    int right = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int counter = 0;
    int tansa;
    cin >> tansa;
    while (true) {
      int half = (left + right) / 2;
      counter++;
      if (left == right) {
        break;
      }
      if (data[half] == tansa) {
        break;
      } else if (data[half] < tansa) {
        left = half + 1;
      } else {
        right = half - 1;
      }
    }
    cout << counter << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    vector<long long int> data(n);
    int left = 0;
    int right = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int counter = 0;
    int tansa;
    cin >> tansa;
    while (true) {
      int half = (left + right) / 2;
      counter++;
      if (left == right) {
        break;
      }
      if (data[half] == tansa) {
        break;
      } else if (data[half] < tansa) {
        left = half + 1;
      } else {
        right = half - 1;
      }
      if (left > right) {
        break;
      }
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00188 (C++)

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

int main() {
  int hi, lo, m, n, a, i, k, d[100];
  while (cin >> n && n != 0) {
    for (i = 0; i < n; i++)
      cin >> d[i];
    cin >> a;
    hi = n - 1;
    lo = k = 0;
    while (true) {
      k++;
      m = (hi + lo) / 2;
      if (d[m] < a)
        lo = m + 1;
      else if (d[m] > a)
        hi = m - 1;
      else
        break;
      if (hi == lo) {
        k++;
        break;
      }
    }
    cout << k << endl;
  }
  return 0;
}

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

int main() {
  int hi, lo, m, n, a, i, k, d[100];
  while (cin >> n && n != 0) {
    for (i = 0; i < n; i++)
      cin >> d[i];
    cin >> a;
    hi = n - 1;
    lo = k = 0;
    while (hi >= lo) {
      k++;
      m = (hi + lo) / 2;
      if (d[m] < a)
        lo = m + 1;
      else if (d[m] > a)
        hi = m - 1;
      else
        break;
      if (hi == lo) {
        k++;
        break;
      }
    }
    cout << k << endl;
  }
  return 0;
}
Problem p00188 (C++)

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

std::vector<int> v;

int main() {
  int n;
  while (std::cin >> n, n) {
    v.clear();

    for (int i = 0; i != n; ++i) {
      int a;
      std::cin >> a;
      v.push_back(a);
    }

    int b;
    std::cin >> b;

    int ans = 0;
    int l = 0, r = v.size() - 1;
    while (l <= r) {
      int mid = (l + r) / 2;

      ++ans;

      if (r <= l)
        break;

      if (v[mid] < b)
        l = mid + 1;
      else if (v[mid] > b)
        r = mid - 1;
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

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

std::vector<int> v;

int main() {
  int n;
  while (std::cin >> n, n) {
    v.clear();

    for (int i = 0; i != n; ++i) {
      int a;
      std::cin >> a;
      v.push_back(a);
    }

    int b;
    std::cin >> b;

    int ans = 0;
    int l = 0, r = v.size() - 1;
    while (l <= r) {
      int mid = (l + r) / 2;

      ++ans;

      if (r <= l)
        break;

      if (v[mid] < b)
        l = mid + 1;
      else if (v[mid] > b)
        r = mid - 1;
      else
        break;
    }

    std::cout << ans << std::endl;
  }

  return 0;
}
Problem p00188 (C++)

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

using namespace std;

int a[100000];
int lef;
int rig;

int ans;

bool binary_search_2(int num) {
  if (lef == rig) {
    ans++;
    return false;
  }
  int middle = (lef + rig) / 2;
  ans++;
  if (a[middle] > num) {
    rig = middle - 1;
    binary_search_2(num);
  } else if (a[middle] < num) {
    lef = middle + 1;
    binary_search_2(num);
  } else if (a[middle] == num) {
    return true;
  }
}

bool binary_search(int num, int n) {
  lef = 0;
  rig = n - 1;
  binary_search_2(num);
}

int main() {

  int n, k;

  while (true) {

    cin >> n;

    if (n == 0) {
      break;
    }

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

    cin >> k;

    ans = 0;

    binary_search(k, n);

    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

int a[100000];
int lef;
int rig;

int ans;

bool binary_search_2(int num) {
  if (lef > rig) {
    return false;
  }
  int middle = (lef + rig) / 2;
  ans++;
  if (a[middle] > num) {
    rig = middle - 1;
    binary_search_2(num);
  } else if (a[middle] < num) {
    lef = middle + 1;
    binary_search_2(num);
  } else if (a[middle] == num) {
    return true;
  }
}

bool binary_search(int num, int n) {
  lef = 0;
  rig = n - 1;
  binary_search_2(num);
}

int main() {

  int n, k;

  while (true) {

    cin >> n;

    if (n == 0) {
      break;
    }

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

    cin >> k;

    ans = 0;

    binary_search(k, n);

    cout << ans << endl;
  }

  return 0;
}
Problem p00188 (C++)

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

int main() {
  int n;
  while (cin >> n, n) {
    int a[100], k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> k;
    int low = 0, up = n - 1, cnt = 0;
    while (1) {
      cnt++;
      if (up == low)
        break;
      int mid = (up + low) / 2;
      if (a[mid] == k)
        break;
      if (a[mid] < k)
        low = mid + 1;
      else
        up = mid - 1;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int main() {
  int n;
  while (cin >> n, n) {
    int a[100], k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> k;
    int low = 0, up = n - 1, cnt = 0;
    while (1) {
      cnt++;
      if (up == low)
        break;
      int mid = (up + low) / 2;
      if (a[mid] == k)
        break;
      if (a[mid] < k)
        low = mid + 1;
      else
        up = mid - 1;
      if (up < low)
        break;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00189 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;

vector<P> edge[10];

int dijkstre(int pos) {
  int min_cost[10];
  fill_n(min_cost, 10, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, pos));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int cost = p.first;
    int now = p.second;
    if (min_cost[now] < cost)
      continue;
    min_cost[now] = cost;
    for (int i = 0; i < edge[now].size(); i++) {
      int nc = edge[now][i].second;
      int next = edge[now][i].first;
      que.push(P(cost + nc, next));
    }
  }
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if (min_cost[i] != INF) {
      sum += min_cost[i];
    }
  }
  return sum;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int l = 100, r = 0;
    for (int i = 0; i < 10; i++) {
      edge[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(P(b, c));
      edge[b].push_back(P(a, c));
      l = min(min(l, a), b);
      r = max(max(r, a), b);
    }
    int ans = INF;
    int pos = l;
    for (int i = l; i <= r; i++) {
      int tmp = dijkstre(i);
      if (ans > tmp) {
        pos = i;
        ans = tmp;
      }
    }
    cout << pos << " " << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;

vector<P> edge[10];

int dijkstre(int pos) {
  int min_cost[10];
  fill_n(min_cost, 10, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, pos));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int cost = p.first;
    int now = p.second;
    if (min_cost[now] < cost)
      continue;
    min_cost[now] = cost;
    for (int i = 0; i < edge[now].size(); i++) {
      int nc = edge[now][i].second;
      int next = edge[now][i].first;
      if (cost + nc < min_cost[next]) {
        que.push(P(cost + nc, next));
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    if (min_cost[i] != INF) {
      sum += min_cost[i];
    }
  }
  return sum;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int l = 100, r = 0;
    for (int i = 0; i < 10; i++) {
      edge[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      edge[a].push_back(P(b, c));
      edge[b].push_back(P(a, c));
      l = min(min(l, a), b);
      r = max(max(r, a), b);
    }
    int ans = INF;
    int pos = l;
    for (int i = l; i <= r; i++) {
      int tmp = dijkstre(i);
      if (ans > tmp) {
        pos = i;
        ans = tmp;
      }
    }
    cout << pos << " " << ans << endl;
  }
}
Problem p00189 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int g[10][10];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i, 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j)
          g[i][j] = 0;
        else
          g[i][j] = 999999999;
      }
    }
    int t = 0;
    for (int i = 0; i < a; i++) {
      int c, d, e;
      scanf("%d%d%d", &c, &d, &e);
      if (t < c)
        t = c;
      if (t < d)
        t = d;
      g[c][d] = e;
      g[d][c] = e;
    }
    t++;
    for (int k = 0; k < t; k++) {
      for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
          if (g[i][k] + g[k][j] < g[i][j])
            g[i][j] = g[i][k] + g[k][j];
        }
      }
    }
    int n = 0;
    ll ans = 9999999999;
    for (int i = 0; i < t; i++) {
      ll now = 0;
      for (int j = 0; j < t; j++)
        now += g[i][j];
      if (ans > now) {
        ans = now;
        n = i;
      }
    }
    printf("%d %lld\n", n, ans);
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int g[10][10];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (i == j)
          g[i][j] = 0;
        else
          g[i][j] = 999999999;
      }
    }
    int t = 0;
    for (int i = 0; i < a; i++) {
      int c, d, e;
      scanf("%d%d%d", &c, &d, &e);
      if (t < c)
        t = c;
      if (t < d)
        t = d;
      g[c][d] = e;
      g[d][c] = e;
    }
    t++;
    for (int k = 0; k < t; k++) {
      for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
          if (g[i][k] + g[k][j] < g[i][j])
            g[i][j] = g[i][k] + g[k][j];
        }
      }
    }
    int n = 0;
    ll ans = 9999999999;
    for (int i = 0; i < t; i++) {
      ll now = 0;
      for (int j = 0; j < t; j++)
        now += g[i][j];
      if (ans > now) {
        ans = now;
        n = i;
      }
    }
    printf("%d %lld\n", n, ans);
  }
}