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

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

#define req(i, r, k) for (int i = k; i < r; i++)
#define rep(i, r) req(i, r, 0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

  for (int a = 1; 1; a++) {
    int w, n;
    scanf(" %d", &w);
    if (w == 0)
      break;
    scanf(" %d", &n);
    pair<int, int> t[n];
    char c;
    rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); }

    rep(i, w) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }

    dp[0][0] = 0;
    rep(i, n + 1) {
      rep(j, w + 1) {
        if (j >= t[i].second) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
        } else {
          dp[i + 1][j] = dp[i][j];
        }
      }
    }
    long long int maxvalue = 0, thenweight = 0;

    rep(i, w + 1) {
      // cout << dp[i] << " " << i << " " << thenweight << endl;
      if (maxvalue < dp[n][i]) {
        maxvalue = dp[n][i];
        thenweight = i;
      }
    }

    // printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
    cout << "Case " << a << ':' << endl;
    cout << maxvalue << endl;
    cout << thenweight << endl;
  }
  return 0;
}

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

#define req(i, r, k) for (int i = k; i < r; i++)
#define rep(i, r) req(i, r, 0)

const long long int INF = 100000000000000;

long long int dp[1002][1002];

int main() {

  for (int a = 1; 1; a++) {
    int w, n;
    scanf(" %d", &w);
    if (w == 0)
      break;
    scanf(" %d", &n);
    pair<int, int> t[n];
    char c;
    rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); }

    rep(i, w) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }

    dp[0][0] = 0;
    rep(i, n + 1) {
      rep(j, w + 1) {
        if (j >= t[i].second) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
        } else {
          dp[i + 1][j] = dp[i][j];
        }
      }
    }
    long long int maxvalue = 0, thenweight = 0;

    rep(i, w + 1) {
      // cout << dp[i] << " " << i << " " << thenweight << endl;
      if (maxvalue < dp[n][i]) {
        maxvalue = dp[n][i];
        thenweight = i;
      }
    }

    // printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
    cout << "Case " << a << ':' << endl;
    cout << maxvalue << endl;
    cout << thenweight << endl;
  }
  return 0;
}
Problem p00042 (C++)

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

using namespace std;

int main(void) {
  int W, n, v[1000], w[1000], dp[10000][10000], t = 1;

  while (cin >> W, W) {
    cin >> n;

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

    for (int i = 0; i < 1000; i++)
      for (int j = 0; j < 1000; j++)
        dp[i][j] = -1;

    for (int i = 0; i < 1000; i++) {
      dp[0][i] = 0;
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= W; j++)
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);

    cout << "Case " << t << ":" << endl;

    for (int i = W; i >= 0; i--) {
      if (dp[n][i] != dp[n][W]) {
        cout << dp[n][W] << endl << i + 1 << endl;
        break;
      }
    }
    t++;
  }

  return 0;
}

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

using namespace std;

int main(void) {
  int W, n, v[1000], w[1000], dp[1001][1001], t = 1;

  while (cin >> W, W) {
    cin >> n;

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

    for (int i = 0; i < 1000; i++)
      for (int j = 0; j < 1000; j++)
        dp[i][j] = -1;

    for (int i = 0; i < 1000; i++) {
      dp[0][i] = 0;
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= W; j++)
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);

    cout << "Case " << t << ":" << endl;

    for (int i = W; i >= 0; i--) {
      if (dp[n][i] != dp[n][W]) {
        cout << dp[n][W] << endl << i + 1 << endl;
        break;
      }
    }
    t++;
  }

  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

#define loop(lp) for (int lp2 = 0; lp2 < lp; lp2++)
#define ascent(i, a, b) for (int i = a; i < b; i++)

#define pb push_back

using namespace std;

int dp[2000][20000];

int main() {
  int w_max, count = 0;
  while (cin >> w_max, w_max) {
    count++;
    int n;
    cin >> n;

    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n + 1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max + 1) {
        if (w <= j) {
          if (dp[i - 1][j] < dp[i - 1][j - w] + v) {
            dp[i][j] = dp[i - 1][j - w] + v;

            if (w_sum.first < dp[i][j] ||
                w_sum.first == dp[i][j] && j < w_sum.second) {
              w_sum = make_pair(dp[i][j], j);

              // cout << "i = " << i << " " << "wsum = " << w_sum.second <<
              // endl;
            }
          } else {
            dp[i][j] = dp[i - 1][j];
          }
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    /*
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
        printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

#define loop(lp) for (int lp2 = 0; lp2 < lp; lp2++)
#define ascent(i, a, b) for (int i = a; i < b; i++)

#define pb push_back

using namespace std;

int dp[1010][10010];

int main() {
  int w_max, count = 0;
  while (cin >> w_max, w_max) {
    count++;
    int n;
    cin >> n;

    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n + 1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max + 1) {
        if (w <= j) {
          if (dp[i - 1][j] < dp[i - 1][j - w] + v) {
            dp[i][j] = dp[i - 1][j - w] + v;

            if (w_sum.first < dp[i][j] ||
                w_sum.first == dp[i][j] && j < w_sum.second) {
              w_sum = make_pair(dp[i][j], j);

              // cout << "i = " << i << " " << "wsum = " << w_sum.second <<
              // endl;
            }
          } else {
            dp[i][j] = dp[i - 1][j];
          }
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    /*
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
        printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
  }
  return 0;
}
Problem p00042 (C++)

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

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 10000;

int dp[MAX_w + 1][MAX_v + 1];

int n, wet;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if (dp[i][j] != -1) {

    return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  return dp[i][j] = res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;

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

  cout << "Case"
       << " " << ca << ":" << endl;

  int ans = rec(0, wet);
  cout << ans << endl;

  for (int w = wet - 1; w >= 0; w--) {
    if (ans != rec(0, w)) {
      wmin = w + 1;
      break;
    }
  }

  cout << wmin << endl;
}

int main() {

  char x;
  int ca = 0;

  while (1) {

    ca++;
    cin >> wet;
    if (wet == 0)
      break;
    cin >> n;

    for (int i = 0; i < n; i++) {

      cin >> v[i] >> x >> w[i];
    }

    solve(ca);
  }

  return 0;
}

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

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 1000;

int dp[MAX_w + 1][MAX_v + 1];

int n, wet;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if (dp[i][j] != -1) {

    return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  return dp[i][j] = res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;

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

  cout << "Case"
       << " " << ca << ":" << endl;

  int ans = rec(0, wet);
  cout << ans << endl;

  for (int w = wet - 1; w >= 0; w--) {
    if (ans != rec(0, w)) {
      wmin = w + 1;
      break;
    }
  }

  cout << wmin << endl;
}

int main() {

  char x;
  int ca = 0;

  while (1) {

    ca++;
    cin >> wet;
    if (wet == 0)
      break;
    cin >> n;

    for (int i = 0; i < n; i++) {

      cin >> v[i] >> x >> w[i];
    }

    solve(ca);
  }

  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)

using namespace std;
typedef long long ll;

int dp[2000][20000]; // dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[2000], V[2000];

int w, n;

void solve() {
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= w; j++) {
      if (j - W[i] < 0) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - W[i]] + V[i]);
      }
    }
  }

  cout << dp[0][w] << endl;
  for (int i = w; i >= 1; i--) {
    if (dp[0][i] != dp[0][i - 1]) {
      cout << i << endl;
      break;
    }
  }
}

int main() {
  int count = 1;
  while (1) {
    memset(dp, 0, sizeof(dp));

    cin >> w >> n;
    if (w == 0)
      break;
    char a;
    rep(i, n) cin >> V[i] >> a >> W[i];

    cout << "Case " << count << ":" << endl;
    count++;
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)

using namespace std;
typedef long long ll;

int dp[1001][10001]; // dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[1001], V[1001];

int w, n;

void solve() {
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= w; j++) {
      if (j - W[i] < 0) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - W[i]] + V[i]);
      }
    }
  }

  cout << dp[0][w] << endl;
  for (int i = w; i >= 1; i--) {
    if (dp[0][i] != dp[0][i - 1]) {
      cout << i << endl;
      break;
    }
  }
}

int main() {
  int count = 1;
  while (1) {
    memset(dp, 0, sizeof(dp));

    cin >> w >> n;
    if (w == 0)
      break;
    char a;
    rep(i, n) cin >> V[i] >> a >> W[i];

    cout << "Case " << count << ":" << endl;
    count++;
    solve();
  }
  return 0;
}
Problem p00042 (C++)

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

using namespace std;

int dp[10000][10000];

int main() {
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while (scanf("%d", &W) && W) {
    c++;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for (int j = 0; j < W; ++j)
      if (dp[N][j] < dp[N][W])
        res++;
    printf("Case %d:\n%d\n%d\n", c, dp[N][W], res);
  }
  return 0;
}

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

using namespace std;

int dp[1001][1001];

int main() {
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while (scanf("%d", &W) && W) {
    c++;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for (int j = 0; j < W; ++j)
      if (dp[N][j] < dp[N][W])
        res++;
    printf("Case %d:\n%d\n%d\n", c, dp[N][W], res);
  }
  return 0;
}
Problem p00042 (C++)

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

int main() {
  int w, n;
  int count = 0;
  while (cin >> w, w) {
    vector<pair<int, int>> v;
    long long dp[1000][1000];
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++) {
      int s, t;
      scanf("%d,%d", &s, &t);
      v.push_back(make_pair(s, t));
    }
    for (int i = n - 1; 0 <= i; i--) {
      for (int j = 0; j <= w; j++) {
        if (j < v[i].second)
          dp[i][j] = dp[i + 1][j];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - v[i].second] + v[i].first);
      }
    }
    cout << "Case " << ++count << ":" << endl;
    cout << dp[0][w] << endl;
    for (int i = w;; i--) {
      if (dp[0][i] != dp[0][w]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}

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

int main() {
  int w, n;
  int count = 0;
  while (cin >> w, w) {
    vector<pair<int, int>> v;
    long long dp[1010][1010];
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++) {
      int s, t;
      scanf("%d,%d", &s, &t);
      v.push_back(make_pair(s, t));
    }
    for (int i = n - 1; 0 <= i; i--) {
      for (int j = 0; j <= w; j++) {
        if (j < v[i].second)
          dp[i][j] = dp[i + 1][j];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - v[i].second] + v[i].first);
      }
    }
    cout << "Case " << ++count << ":" << endl;
    cout << dp[0][w] << endl;
    for (int i = w;; i--) {
      if (dp[0][i] != dp[0][w]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}
Problem p00042 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
  while (~scanf("%d", &W)) {
    if (!W)
      break;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d", &w[i], &v[i]);
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= w[i - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        }
      }
    }
    printf("Case %d:\n", ++c);
    printf("%d\n", dp[N][W]);
    printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
  while (~scanf("%d", &W)) {
    if (!W)
      break;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d,%d", &v[i], &w[i]);
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= w[i - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        }
      }
    }
    printf("Case %d:\n", ++c);
    printf("%d\n", dp[N][W]);
    printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
  }
  return 0;
}
Problem p00042 (C++)

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

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second > b.second;
}
int main() {
  int n, W;
  int t = 1;
  while (cin >> W >> n && W) {
    vector<pair<int, int>> cur;
    cur.push_back({0, 0});
    for (int i = 0; i < n; i++) {
      int w, c;
      scanf("%d,%d", &c, &w);
      for (int j = 0, t = cur.size(); j < t; j++)
        cur.push_back(pair<int, int>(cur[j].first + w, cur[j].second + c));
      sort(cur.begin(), cur.end(), cmp);
      int maxi = -1;
      vector<pair<int, int>> nex;
      for (int j = 0; j < cur.size(); j++) {
        if (maxi >= cur[j].second)
          continue;
        nex.push_back(cur[j]);
        maxi = cur[j].second;
      }
      cur = nex;
    }
    int ans = 0, ans2;
    for (int i = 0; i < cur.size(); i++) {
      if (cur[i].first <= W) {
        ans = cur[i].second;
        ans2 = cur[i].first;
      }
    }

    cout << "Case " << t++ << ":" << endl;
    cout << ans << endl;
    cout << ans2 << endl;
  }
}

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

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second > b.second;
}
int main() {
  int n, W;
  int t = 1;
  while (cin >> W >> n && W) {
    vector<pair<int, int>> cur;
    cur.push_back({0, 0});
    for (int i = 0; i < n; i++) {
      int w, c;
      scanf("%d,%d", &c, &w);
      for (int j = 0, t = cur.size(); j < t; j++)
        cur.push_back(pair<int, int>(cur[j].first + w, cur[j].second + c));
      sort(cur.begin(), cur.end(), cmp);
      int maxi = -1;
      vector<pair<int, int>> nex;
      for (int j = 0; j < cur.size(); j++) {
        if (maxi >= cur[j].second)
          continue;

        if (cur[j].first <= W)
          nex.push_back(cur[j]);
        maxi = cur[j].second;
      }
      cur = nex;
    }
    int ans = 0, ans2;
    for (int i = 0; i < cur.size(); i++) {
      if (cur[i].first <= W) {
        ans = cur[i].second;
        ans2 = cur[i].first;
      }
    }

    cout << "Case " << t++ << ":" << endl;
    cout << ans << endl;
    cout << ans2 << endl;
  }
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
// バグ有

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int W, N;
  char d;
  int counter = 1;
  int takara[1000][2];
  while (1) {
    cin >> W;
    if (W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> takara[i][0] >> d >> takara[i][1];
    }
    vector<vector<int>> dp(W, vector<int>(N + 1));
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[j][i] = 0;
      }
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (i == 0) {
          continue;
        }
        if (j < takara[i - 1][1]) {
          dp[j][i] = dp[j][i - 1];
        } else {
          dp[j][i] = max(dp[j][i - 1],
                         dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]);
        }
      }
    }
    int MAX[2];
    MAX[0] = 0;
    MAX[1] = 10000;
    for (int i = 0; i <= W; i++) {
      if (MAX[0] == dp[i][N]) {
        MAX[1] = min(MAX[1], i);
      } else if (MAX[0] < dp[i][N]) {
        MAX[0] = dp[i][N];
        MAX[1] = i;
      }
    }
    cout << "Case " << counter << ':' << endl
         << MAX[0] << endl
         << MAX[1] << endl;
    counter++;
  }
  return 0;
}

Fixed submission:
// バグ有

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int W, N;
  char d;
  int counter = 1;
  int takara[1000][2];
  while (1) {
    cin >> W;
    if (W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> takara[i][0] >> d >> takara[i][1];
    }
    vector<vector<int>> dp(W + 1, vector<int>(N + 1));
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[j][i] = 0;
      }
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (i == 0) {
          continue;
        }
        if (j < takara[i - 1][1]) {
          dp[j][i] = dp[j][i - 1];
        } else {
          dp[j][i] = max(dp[j][i - 1],
                         dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]);
        }
      }
    }
    int MAX[2];
    MAX[0] = 0;
    MAX[1] = 10000;
    for (int i = 0; i <= W; i++) {
      if (MAX[0] == dp[i][N]) {
        MAX[1] = min(MAX[1], i);
      } else if (MAX[0] < dp[i][N]) {
        MAX[0] = dp[i][N];
        MAX[1] = i;
      }
    }
    cout << "Case " << counter << ':' << endl
         << MAX[0] << endl
         << MAX[1] << endl;
    counter++;
  }
  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
typedef pair<int, int> P;

int W, N, v[1000], w[1000];
int I;

P dp[1000][1000];
bool done[1000][1000];
P solve(int i, int r) {
  if (done[i][r])
    return dp[i][r];
  P result;
  if (i == N)
    result = P(0, r);
  else {
    if (r - w[i] >= 0) {
      P p = solve(i + 1, r - w[i]);
      p.first += v[i];
      result = max(p, solve(i + 1, r));
    } else {
      result = solve(i + 1, r);
    }
  }
  done[i][r] = true;
  return dp[i][r] = result;
}

int main() {
  while (I++, scanf("%d%d", &W, &N), W) {
    for (int i = 0; i < N; i++)
      scanf("%d,%d", v + i, w + i);
    memset(done, 0, sizeof(done));
    P p = solve(0, W);
    printf("Case %d:\n%d\n%d\n", I, p.first, W - p.second);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
typedef pair<int, int> P;

int W, N, v[1000], w[1000];
int I;

P dp[1001][1001];
bool done[1001][1001];
P solve(int i, int r) {
  if (done[i][r])
    return dp[i][r];
  P result;
  if (i == N)
    result = P(0, r);
  else {
    if (r - w[i] >= 0) {
      P p = solve(i + 1, r - w[i]);
      p.first += v[i];
      result = max(p, solve(i + 1, r));
    } else {
      result = solve(i + 1, r);
    }
  }
  done[i][r] = true;
  return dp[i][r] = result;
}

int main() {
  while (I++, scanf("%d%d", &W, &N), W) {
    for (int i = 0; i < N; i++)
      scanf("%d,%d", v + i, w + i);
    memset(done, 0, sizeof(done));
    P p = solve(0, W);
    printf("Case %d:\n%d\n%d\n", I, p.first, W - p.second);
  }
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
using namespace std;
int dp[1001][1001];
int main() {
  int W, N, v[1000], w[1000];
  int n = 1;
  int lastj = 0;
  char jyama;
  while (cin >> W && W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i]) {
          dp[i + 1][j] = dp[i][j];
        } else {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
          if (j > 0 && dp[i][j] != dp[i][j - 1])
            lastj = j;
        }
      }
    }
    cout << "Case " << n << ":" << endl;
    cout << dp[N][W] << endl;
    cout << lastj << endl;
    ++n;
  }
}

Fixed submission:
#include <iostream>
#include <stdio.h>
using namespace std;
int dp[1005][1005];
int main() {
  int W, N, v[1000], w[1000];
  int n = 1;
  int lastj = 0;
  char jyama;
  while (cin >> W && W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i]) {
          dp[i + 1][j] = dp[i][j];
        } else {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
          if (j > 0 && dp[i][j] != dp[i][j - 1])
            lastj = j;
        }
      }
    }
    cout << "Case " << n << ":" << endl;
    cout << dp[N][W] << endl;
    cout << lastj << endl;
    ++n;
  }
}
Problem p00044 (C++)

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

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define PRIM 50021

bool prim[50022];
int a_prime[50022];

int main() {
  memset(prim, true, sizeof(prim));
  int N;
  prim[0] = false;
  FOR(i, 1, sqrt(PRIM)) {
    if (prim[i]) {
      for (int j = (i + 1); (i + 1) * j <= PRIM; ++j) {
        prim[(i + 1) * j - 1] = false;
      }
    }
  }
  while (scanf("%d", &N)) {
    repr(i, N - 2) {
      if (prim[i]) {
        printf("%d ", i + 1);
        break;
      }
    }
    FOR(i, N, PRIM) {
      if (prim[i]) {
        printf("%d\n", i + 1);
        break;
      }
    }
  }
  return 0;
}

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

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define PRIM 50021

bool prim[50022];
int a_prime[50022];

int main() {
  memset(prim, true, sizeof(prim));
  int N;
  prim[0] = false;
  FOR(i, 1, sqrt(PRIM)) {
    if (prim[i]) {
      for (int j = (i + 1); (i + 1) * j <= PRIM; ++j) {
        prim[(i + 1) * j - 1] = false;
      }
    }
  }
  while (cin >> N) {
    repr(i, N - 2) {
      if (prim[i]) {
        printf("%d ", i + 1);
        break;
      }
    }
    FOR(i, N, PRIM) {
      if (prim[i]) {
        printf("%d\n", i + 1);
        break;
      }
    }
  }
  return 0;
}
Problem p00044 (C++)

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

using namespace std;

int main() {

  vector<int> arr(80000);

  for (int i = 0; i < 80000; ++i)
    arr[i] = 1;

  for (int i = 2; i < sqrt(80000); ++i) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < 80000; j++)
        arr[i * (j + 2)] = 0;
    }
  }

  while (1) {
    int n;
    cin >> n;

    for (int i = n - 1; i >= 0; --i) {
      if (arr[i] == 1) {
        cout << i << " ";
        break;
      }
    }

    for (int i = n + 1; i <= 80000; ++i) {
      if (arr[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
}


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

using namespace std;

int main() {

  vector<int> arr(80000);

  for (int i = 0; i < 80000; ++i)
    arr[i] = 1;

  for (int i = 2; i < sqrt(80000); ++i) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < 80000; j++)
        arr[i * (j + 2)] = 0;
    }
  }

  int n;

  while (cin >> n) {

    for (int i = n - 1; i >= 0; --i) {
      if (arr[i] == 1) {
        cout << i << " ";
        break;
      }
    }

    for (int i = n + 1; i <= 80000; ++i) {
      if (arr[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00044 (C++)

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

int isprime[50101];

int main() {
  memset(isprime, 0, sizeof(isprime));
  for (int i = 4; i < 50101; i += 2)
    isprime[i] = 1;
  isprime[1] = 1;
  for (int i = 3; i * i < 50101; i += 2)
    if (isprime[i] == 0)
      for (int j = 2; i * j < 50101; ++j)
        isprime[i * j] = 1;

  int n;
  while (cin >> n, n) {
    for (int k = n - 1; k > 0; --k)
      if (isprime[k] == 0) {
        cout << k << " ";
        break;
      }

    for (int k = n + 1; k < 50101; ++k)
      if (isprime[k] == 0) {
        cout << k << endl;
        break;
      }
  }
}

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

int isprime[50101];

int main() {
  memset(isprime, 0, sizeof(isprime));
  for (int i = 4; i < 50101; i += 2)
    isprime[i] = 1;
  isprime[1] = 1;
  for (int i = 3; i * i < 50101; i += 2)
    if (isprime[i] == 0)
      for (int j = 2; i * j < 50101; ++j)
        isprime[i * j] = 1;

  int n;
  while (cin >> n) {
    for (int k = n - 1; k > 0; --k)
      if (isprime[k] == 0) {
        cout << k << " ";
        break;
      }

    for (int k = n + 1; k < 50101; ++k)
      if (isprime[k] == 0) {
        cout << k << endl;
        break;
      }
  }
}
Problem p00044 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#define N 50001

void Eratos(void);

int prime[N];

int main(void) {
  int n;
  int lessmax, bigmin;

  Eratos();

  while (scanf("%d", &n) != EOF) {
    for (int i = n - 1;; i--) {
      if (prime[i] == 1) {
        lessmax = i;
        break;
      }
    }
    for (int i = n + 1;; i++) {
      if (prime[i] == 1) {
        bigmin = i;
        break;
      }
    }

    printf("%d %d\n", lessmax, bigmin);
  }

  return 0;
}

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

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

Fixed submission:
#include <math.h>
#include <stdio.h>
#define N 50060

void Eratos(void);

int prime[N];

int main(void) {
  int n;
  int lessmax, bigmin;

  Eratos();

  while (scanf("%d", &n) != EOF) {
    for (int i = n - 1;; i--) {
      if (prime[i] == 1) {
        lessmax = i;
        break;
      }
    }
    for (int i = n + 1;; i++) {
      if (prime[i] == 1) {
        bigmin = i;
        break;
      }
    }

    printf("%d %d\n", lessmax, bigmin);
  }

  return 0;
}

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

  for (int i = 2; i <= sqrt(N); i++) {
    for (int j = 2; i * j < N; j++) {
      prime[i * j] = 0;
    }
  }
}
Problem p00044 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, no, input;
  int prime[500];
  int ptr = 0, c = 0;
  int flag = 0;
  int last;

  while (cin >> input) {
    flag = ptr = c = 0;
    prime[ptr++] = 2;
    prime[ptr++] = 3;
    for (no = 5;; no += 2) {
      if (flag == 2) {
        break;
      }
      if (input + 1 <= no) {
        flag = 1;
        c++;
      }
      if (c == 1) {
        if (input == prime[ptr - 1])
          last = ptr - 2;
        else
          last = ptr - 1;
      }
      for (i = 1; i < ptr; i++) {
        if (no % prime[i] == 0)
          break;
      }
      if (ptr == i) {
        prime[ptr++] = no;
        if (flag == 1) {
          flag = 2;
        }
      }
    }

    printf("%d %d\n", prime[last], prime[ptr - 1]);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, no, input;
  int prime[10000];
  int ptr = 0, c = 0;
  int flag = 0;
  int last;

  while (cin >> input) {
    flag = ptr = c = 0;
    prime[ptr++] = 2;
    prime[ptr++] = 3;
    for (no = 5;; no += 2) {
      if (flag == 2) {
        break;
      }
      if (input + 1 <= no) {
        flag = 1;
        c++;
      }
      if (c == 1) {
        if (input == prime[ptr - 1])
          last = ptr - 2;
        else
          last = ptr - 1;
      }
      for (i = 1; i < ptr; i++) {
        if (no % prime[i] == 0)
          break;
      }
      if (ptr == i) {
        prime[ptr++] = no;
        if (flag == 1) {
          flag = 2;
        }
      }
    }

    printf("%d %d\n", prime[last], prime[ptr - 1]);
  }
  return 0;
}
Problem p00044 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int p[50100];
int main() {
  int i, j;
  for (i = 2; i * i < 50100; i++) {
    if (p[i] == 0) {
      for (j = i * 2; j < 50100; j += i) {
        p[j] = 1;
      }
    }
  }
  while (cin >> i, i) {
    for (j = i - 1; p[j]; j--) {
    }
    cout << j << ' ';
    for (j = i + 1; p[j]; j++) {
    }
    cout << j << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int p[50100];
int main() {
  int i, j;
  for (i = 2; i * i < 50100; i++) {
    if (p[i] == 0) {
      for (j = i * 2; j < 50100; j += i) {
        p[j] = 1;
      }
    }
  }
  while (cin >> i) {
    for (j = i - 1; p[j]; j--) {
    }
    cout << j << ' ';
    for (j = i + 1; p[j]; j++) {
    }
    cout << j << endl;
  }
  return 0;
}
Problem p00044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N (50000)

bool prime[N + 1];

int main() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

  for (int i = 2; i * i <= N; i++) {
    for (int j = i * i; j <= N; j += i) {
      prime[j] = false;
    }
  }

  int n;
  while (~scanf("%d", &n)) {
    int l = n - 1;
    while (!prime[l])
      l--;
    int r = n + 1;
    while (!prime[r])
      r++;
    printf("%d %d\n", l, r);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N (50500)

bool prime[N + 1];

int main() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

  for (int i = 2; i * i <= N; i++) {
    for (int j = i * i; j <= N; j += i) {
      prime[j] = false;
    }
  }

  int n;
  while (~scanf("%d", &n)) {
    int l = n - 1;
    while (!prime[l])
      l--;
    int r = n + 1;
    while (!prime[r])
      r++;
    printf("%d %d\n", l, r);
  }

  return 0;
}
Problem p00045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  map<double, double> m;
  double a, b, ave, sum;
  while (scanf("%lf,%lf", &a, &b)) {
    if (!a)
      break;
    m[a] = b;
  }
  map<double, double>::iterator it = m.begin();
  while (it != m.end()) {
    ave += (*it).second, sum += ((*it).first * (*it).second);
    it++;
  }
  ave /= m.size();
  ave += 0.5;
  cout << sum << endl << (int)ave << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  map<double, double> m;
  double a, b, ave = 0, sum = 0;
  while (scanf("%lf,%lf", &a, &b) != EOF) {
    m[a] = b;
  }
  map<double, double>::iterator it = m.begin();
  while (it != m.end()) {
    ave += (*it).second, sum += ((*it).first * (*it).second);
    it++;
  }
  ave /= m.size();
  ave += 0.5;
  cout << sum << endl << (int)ave << endl;
}
Problem p00045 (C++)

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

int main() {
  int a, b;
  int ca = 0;
  double cb = 0;
  int i = 0;
  while (scanf("%d,%d", &a, &b)) {
    ca += a * b;
    cb += b;
    i++;
  }
  double cc = cb / i;
  double cd = round(cc);
  cout << ca << endl << cd << endl;
  return 0;
}

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

int main() {
  int a, b;
  int ca = 0;
  double cb = 0;
  int i = 0;
  while (scanf("%d,%d", &a, &b) != EOF) {
    ca += a * b;
    cb += b;
    i++;
  }
  double cc = cb / i;
  double cd = round(cc);
  cout << ca << endl << cd << endl;
  return 0;
}
Problem p00045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int main() {
  int a, b, c = 0, d = 0, n = 0;
  while (scanf("%d,%d", &a, &b))
    c += a * b, d += b, n++;
  printf("%d\n%d\n", c, (int)floor((double)d / n + 0.5));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int main() {
  int a, b, c = 0, d = 0, n = 0;
  while (~scanf("%d,%d", &a, &b))
    c += a * b, d += b, n++;
  printf("%d\n%d\n", c, (int)floor((double)d / n + 0.5));
  return 0;
}
Problem p00045 (C++)

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

using namespace std;

int main() {

  int value, num, i = 0, total = 0, ave = 0;

  while (scanf("%d %d", &value, &num) != EOF) {
    total += value * num;
    ave += num;
    i++;
  }

  cout << total << endl << (int)(ave / (double)i + 0.5) << endl;

  return 0;
}

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

using namespace std;

int main() {

  int value, num, i = 0, total = 0, ave = 0;

  while (scanf("%d, %d", &value, &num) != EOF) {
    total += value * num;
    ave += num;
    i++;
  }

  cout << total << endl << (int)(ave / (double)i + 0.5) << endl;

  return 0;
}
Problem p00045 (C++)

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

int main() {
  int price, amount;
  int sum = 0, ave = 0, i = 0;
  while (scanf("%d,%d", &price, &amount)) {
    sum += price * amount;
    ave += amount;
    i++;
  }

  printf("%d\n%d\n", sum, (int)(ave / (double)i + 0.5));
  return 0;
}

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

int main() {
  int price, amount;
  int sum = 0, ave = 0, i = 0;
  while (scanf("%d,%d", &price, &amount) != EOF) {
    sum += price * amount;
    ave += amount;
    i++;
  }

  printf("%d\n%d\n", sum, (int)(ave / (double)i + 0.5));
  return 0;
}
Problem p00045 (C++)

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

int main() {
  int m = 0, n = 0, c = 0, i, j;
  char a;
  while (cin >> i >> a >> j, i) {
    m += (i * j);
    n += j;
    c++;
  }
  double am = 1.0 * n / c;
  if (am * 10 - int(am / 10) > 4)
    am = am + 1;
  cout << m << endl << int(am) << endl;
}

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

int main() {
  int m = 0, n = 0, c = 0, i, j;
  char a;
  while (cin >> i >> a >> j) {
    m += (i * j);
    n += j;
    c++;
  }
  double am = 1.0 * n / c;
  if (am * 10 - int(am / 10) > 4)
    am = am + 1;
  cout << m << endl << int(am) << endl;
}
Problem p00045 (C++)

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

int main() {
  double a = 0.0, b = 0.0, sum = 0.0, sumw = 0.0;
  double cnt = 0.0;
  while (scanf("%lf,%lf", &a, &b) != '\n') {
    sum = sum + a * b;
    sumw = sumw + b;
    cnt = cnt + 1.0;
  }

  cout << sum << endl;
  cout << (int)floor((sumw / cnt) + 0.5) << endl;
}

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

int main() {
  double a = 0.0, b = 0.0, sum = 0.0, sumw = 0.0;
  double cnt = 0.0;
  while (scanf("%lf,%lf", &a, &b) != EOF) {
    sum = sum + a * b;
    sumw = sumw + b;
    cnt = cnt + 1.0;
  }

  cout << sum << endl;
  cout << (int)floor((sumw / cnt) + 0.5) << endl;
}
Problem p00046 (C++)

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

using namespace std;

void Solution() {
  int n = 0;
  double a[n];

  while (cin >> a[n]) {
    n++;
  }

  //    for (int i = 0; i < n - 1; i++)
  //    {
  //        for (int j = i + 1; j < n; j++)
  //        {
  //            if (a[i] < a[j])
  //            {
  //                double temp = a[i];          //swap
  //                a[i] = a[j];
  //                a[j] = temp;
  //            }
  //        }
  //    }

  sort(a, a + n); // sorting as ascedning sequence

  // cout.setf(ios::fixed); //"setf" is used for “half-adjusting”
  // cout.precision(2);

  //    cout<<a[0] - a[n - 1]<<endl;
  cout << a[n - 1] - a[0] << endl;
}

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

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

using namespace std;

void Solution() {
  int n = 0;
  double a[100]; // notice the definition.

  while (cin >> a[n]) {
    n++;
  }

  //    for (int i = 0; i < n - 1; i++)
  //    {
  //        for (int j = i + 1; j < n; j++)
  //        {
  //            if (a[i] < a[j])
  //            {
  //                double temp = a[i];          //swap
  //                a[i] = a[j];
  //                a[j] = temp;
  //            }
  //        }
  //    }

  sort(a, a + n); // sorting as ascedning sequence

  // cout.setf(ios::fixed); //"setf" is used for “half-adjusting”
  // cout.precision(2);

  //    cout<<a[0] - a[n - 1]<<endl;
  cout << a[n - 1] - a[0] << endl;
}

int main() {
  Solution();
  return 0;
}
Problem p00046 (C++)

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

using namespace std;

int main() {
  double n, h = 0.0, l = 999999.0;

  while (scanf("%lf", &n)) {
    h = max(h, n);
    l = min(l, n);
  }

  printf("%.1lf\n", h - l);
}

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

using namespace std;

int main() {
  double n, h = 0.0, l = 999999.0;

  while (cin >> n) {
    h = max(h, n);
    l = min(l, n);
  }

  printf("%.1lf\n", h - l);
}
Problem p00046 (C++)

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

using namespace std;

int main(void) {
  double a, mx, mn;
  cin >> a;
  mx = mn = a;
  while (1) {
    if (cin.eof() == -1) {
      break;
    }
    cin >> a;
    mx = max(mx, a);
    mn = min(mn, a);
  }
  cout << mx - mn << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main(void) {
  double a, mx, mn;
  cin >> a;
  mx = mn = a;
  while (1) {
    if (cin.eof()) {
      break;
    }
    cin >> a;
    mx = max(mx, a);
    mn = min(mn, a);
  }
  cout << mx - mn << endl;
  return 0;
}
Problem p00046 (C++)

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

int main() {
  vector<double> v;
  vector<double>::iterator pv = v.begin();
  double d;
  while (cin >> d) {
    v.push_back(d);
  }
  sort(v.begin(), v.end());
  double a = *pv;
  pv = v.end();
  double b = *pv;
  cout << b - a << endl;
  return 0;
}

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

int main() {
  vector<double> v;
  vector<double>::iterator pv = v.begin();
  double d;
  while (cin >> d) {
    v.push_back(d);
  }
  sort(v.begin(), v.end());
  int z = v.size();
  double a = v[z - 1];
  double b = v[0];
  double c = a - b;
  cout << c << endl;
  return 0;
}
Problem p00047 (C++)

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

int main() {
  int A = 1;
  int B = 0;
  int C = 0;
  while (true) {
    string s;
    cin >> s;
    if (s[0] == 'A') {
      if (s[2] == 'B') {
        int tmp;
        tmp = B;
        B = A;
        A = tmp;
      } else if (s[2] == 'C') {
        int tmp;
        tmp = C;
        C = A;
        A = tmp;
      }
    }
    if (s[0] == 'B') {
      if (s[2] == 'A') {
        int tmp;
        tmp = A;
        A = B;
        B = tmp;
      } else if (s[2] == 'C') {
        int tmp;
        tmp = C;
        C = B;
        B = tmp;
      }
    } else if (s[0] == 'C') {
      if (s[2] == 'A') {
        int tmp;
        tmp = A;
        A = C;
        C = tmp;
      } else if (s[2] == 'B') {
        int tmp;
        tmp = B;
        B = C;
        C = tmp;
      }
    }
  }
  if (A == 1) {
    cout << "A" << endl;
  } else if (B == 1) {
    cout << "B" << endl;
  } else if (C == 1) {
    cout << "C" << endl;
  }
  return 0;
}


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

int main() {
  int A = 1;
  int B = 0;
  int C = 0;
  string s;
  while (cin >> s) {
    if (s[0] == 'A') {
      if (s[2] == 'B') {
        int tmp;
        tmp = B;
        B = A;
        A = tmp;
      } else if (s[2] == 'C') {
        int tmp;
        tmp = C;
        C = A;
        A = tmp;
      }
    }
    if (s[0] == 'B') {
      if (s[2] == 'A') {
        int tmp;
        tmp = A;
        A = B;
        B = tmp;
      } else if (s[2] == 'C') {
        int tmp;
        tmp = C;
        C = B;
        B = tmp;
      }
    } else if (s[0] == 'C') {
      if (s[2] == 'A') {
        int tmp;
        tmp = A;
        A = C;
        C = tmp;
      } else if (s[2] == 'B') {
        int tmp;
        tmp = B;
        B = C;
        C = tmp;
      }
    }
  }
  if (A == 1) {
    cout << "A" << endl;
  } else if (B == 1) {
    cout << "B" << endl;
  } else if (C == 1) {
    cout << "C" << endl;
  }
  return 0;
}
Problem p00047 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int cup[3] = {1};
  int x, y;
  while (scanf(" %d,%d", &x, &y) != EOF) {
    swap(cup[x - 'A'], cup[y - 'A']);
  }
  for (int i = 0; i < 3; i++) {
    if (cup[i] > 0) {
      printf("%c\n", 'A' + i);
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int cup[3] = {1};
  char x, y;
  while (scanf(" %c", &x) != EOF) {
    scanf(",%c", &y);
    swap(cup[x - 'A'], cup[y - 'A']);
  }
  for (int i = 0; i < 3; i++) {
    if (cup[i] > 0) {
      printf("%c\n", 'A' + i);
      break;
    }
  }
  return 0;
}
Problem p00047 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main() {
  char X, Y, P = 'A';

  while (scanf("%c,%c", &X, &Y)) {
    if (X == P) {
      P = Y;
    } else if (Y == P) {
      P = X;
    }
  }

  printf("%c\n", P);

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main() {
  char X, Y, P = 'A';

  while (scanf("%c,%c", &X, &Y) != EOF) {
    if (X == P) {
      P = Y;
    } else if (Y == P) {
      P = X;
    }
  }

  printf("%c\n", P);

  return 0;
}
Problem p00047 (C++)

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

using namespace std;

int main(void) {
  char f, t;
  int data[3] = {1, 0, 0};
  while (1) {
    int tmp;
    if (scanf("%c,%c") == EOF)
      break;
    tmp = data[t - 'A'];
    data[t - 'A'] = data[f - 'A'];
    data[f - 'A'] = tmp;
  }
  if (data[0] == 1)
    cout << 'A' << endl;
  else if (data[1] == 1)
    cout << 'B' << endl;
  else
    cout << 'C' << endl;
  return 0;
}

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

using namespace std;

int main(void) {
  char f, t;
  int data[3] = {1, 0, 0};
  while (1) {
    int tmp;
    if (scanf("%c,%c", &f, &t) == EOF)
      break;
    tmp = data[t - 'A'];
    data[t - 'A'] = data[f - 'A'];
    data[f - 'A'] = tmp;
  }
  if (data[0] == 1)
    cout << 'A' << endl;
  else if (data[1] == 1)
    cout << 'B' << endl;
  else
    cout << 'C' << endl;
  return 0;
}
Problem p00047 (C++)

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

using namespace std;

int main() {
  int cup[3] = {1, 0, 0}, temp = 0, count = 0;
  char cup_str[5] = "ABC,";
  char cup1, cup2, cup3;

  while (1) {
    cin >> cup1 >> cup2 >> cup3;

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        if (cup1 == cup_str[j] && cup3 == cup_str[i]) {
          temp = cup[j];
          cup[j] = cup[i];
          cup[i] = temp;
        }
      }
    }

    for (int i = 0; i < 3; i++) {
      if (cup[i] == 1)
        cout << cup_str[i] << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int cup[3] = {1, 0, 0}, temp = 0, count = 0;
  char cup_str[5] = "ABC,";
  char cup1, cup2, cup3;

  while (1) {
    cin >> cup1 >> cup2 >> cup3;

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        if (cup1 == cup_str[j] && cup3 == cup_str[i]) {
          temp = cup[j];
          cup[j] = cup[i];
          cup[i] = temp;
        }
      }
    }

    count++;
    if (count == 50)
      break;
  }

  for (int i = 0; i < 3; i++) {
    if (cup[i] == 1)
      cout << cup_str[i] << endl;
  }

  return 0;
}