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

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

int main() {

  int n, m; // 問題文のn,m

  while (std::cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    std::vector<int> p(n); // 野菜価格一覧
    for (int i = 0; i < n; ++i) {
      std::cin >> p[i];
    }
    sort(p.begin(), p.end());

    reverse(p.begin(), p.end()); // 降順ソート

    for (int i = m - 1; i < n; i += m) {
      p.erase(p.begin() + i);
    }
    std::cout << accumulate(p.begin(), p.end(), 0) << std::endl;
  }

  return 0;
}


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

int main() {

  int n, m; // 問題文のn,m

  while (std::cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    std::vector<int> p(n); // 野菜価格一覧
    for (int i = 0; i < n; ++i) {
      std::cin >> p[i];
    }
    sort(p.begin(), p.end());

    reverse(p.begin(), p.end()); // 降順ソート

    for (int i = m - 1; i <= n - 1; i += m) {
      p[i] = 0;
    }
    std::cout << accumulate(p.begin(), p.end(), 0) << std::endl;
  }

  return 0;
}
Problem p00227 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
int n, m;
vector<int> vege;

const int INF = 1 << 29;

int main(void) {
  while (cin >> n >> m) {
    vege.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> vege[i];
    }

    sort(vege.begin(), vege.end(), greater<int>());
    int sum = accumulate(vege.begin(), vege.end(), 0);
    for (int i = 1; i - 1 < n / m; i++) {
      sum -= vege[i * m - 1];
    }
    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
int n, m;
vector<int> vege;

const int INF = 1 << 29;

int main(void) {
  while (cin >> n >> m && n && m) {
    vege.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> vege[i];
    }

    sort(vege.begin(), vege.end(), greater<int>());
    int sum = accumulate(vege.begin(), vege.end(), 0);
    for (int i = 1; i - 1 < n / m; i++) {
      sum -= vege[i * m - 1];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00228 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  string table[] = {"0111111", "0000110", "1011011", "1001111", "1100110",
                    "1101101", "1111101", "1111111", "1101111"};
  int n;
  while (cin >> n, ~n) {
    string cur = "0000000";
    rep(i, n) {
      int number;
      cin >> number;
      rep(i, 7) cout << (cur[i] != table[number][i]);
      cout << endl;
      cur = table[number];
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  string table[] = {"0111111", "0000110", "1011011", "1001111", "1100110",
                    "1101101", "1111101", "0100111", "1111111", "1101111"};
  int n;
  while (cin >> n, ~n) {
    string cur = "0000000";
    rep(i, n) {
      int number;
      cin >> number;
      rep(i, 7) cout << (cur[i] != table[number][i]);
      cout << endl;
      cur = table[number];
    }
  }
}
Problem p00231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
using namespace std;
#define START 0
#define END 1

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    multimap<int, pair<int, int>> M;
    for (int i = 0; i < n; ++i) {
      int m, a, b;
      scanf("%d%d%d", &m, &a, &b);
      M.insert(make_pair(a, make_pair(START, m)));
      M.insert(make_pair(b, make_pair(END, m)));
    }

    int total = 0;
    bool ng = false;
    for (map<int, pair<int, int>>::iterator itm = M.begin(); itm != M.end();
         ++itm) {
      int t = itm->first;
      while (t == itm->first) {
        if (itm->second.first == START) {
          total += itm->second.second;
        } else {
          total -= itm->second.second;
        }
        ++itm;
      }
      if (total > 150) {
        ng = true;
      }
      --itm;
    }

    if (ng) {
      puts("NG");
    } else {
      puts("OK");
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
using namespace std;
#define START 0
#define END 1

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    multimap<int, pair<int, int>> M;
    for (int i = 0; i < n; ++i) {
      int m, a, b;
      scanf("%d%d%d", &m, &a, &b);
      M.insert(make_pair(a, make_pair(START, m)));
      M.insert(make_pair(b, make_pair(END, m)));
    }

    int total = 0;
    bool ng = false;
    for (map<int, pair<int, int>>::iterator itm = M.begin(); itm != M.end();
         ++itm) {
      int t = itm->first;
      while (t == itm->first) {
        if (itm->second.first == START) {
          total += itm->second.second;
        } else {
          total -= itm->second.second;
        }
        ++itm;
        if (itm == M.end())
          break;
      }
      if (total > 150) {
        ng = true;
      }
      --itm;
    }

    if (ng) {
      puts("NG");
    } else {
      puts("OK");
    }
  }
  return 0;
}
Problem p00231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P; // time,which,weight
int main() {
  P t[101];
  int n, a, b, c;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      t[i].second.second = a;
      t[i].first = b;
      t[i].second.first = 1;
      t[i + n].second.second = a;
      t[i + n].first = c;
      t[i + n].second.first = 0;
    }
    int now = 0;
    sort(t, t + 2 * n);
    for (int i = 0; i < 2 * n; i++) {

      if (t[i].second.first)
        now += t[i].second.second;
      else
        now -= t[i].second.second;

      if (now > 150) {
        cout << "NG" << endl;
        goto end;
      }
    }

    cout << "OK" << endl;

  end:;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P; // time,which,weight
int main() {
  P t[300];
  int n, a, b, c;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      t[i].second.second = a;
      t[i].first = b;
      t[i].second.first = 1;
      t[i + n].second.second = a;
      t[i + n].first = c;
      t[i + n].second.first = 0;
    }
    int now = 0;
    sort(t, t + 2 * n);
    for (int i = 0; i < 2 * n; i++) {

      if (t[i].second.first)
        now += t[i].second.second;
      else
        now -= t[i].second.second;

      if (now > 150) {
        cout << "NG" << endl;
        goto end;
      }
    }

    cout << "OK" << endl;

  end:;
  }
  return 0;
}
Problem p00231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  while (1) {
    int N;
    cin >> N;
    vector<pair<int, int>> qs;
    for (int i = 0; i < N; ++i) {
      int m, a, b;
      cin >> m >> a >> b;
      qs.push_back(make_pair(a, m));
      qs.push_back(make_pair(b, -m));
    }
    sort(qs.begin(), qs.end());
    int nw = 0;
    bool ok = true;
    for (int i = 0; i < qs.size(); ++i) {
      nw += qs[i].second;
      if (nw > 150)
        ok = false;
    }
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}

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

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<pair<int, int>> qs;
    for (int i = 0; i < N; ++i) {
      int m, a, b;
      cin >> m >> a >> b;
      qs.push_back(make_pair(a, m));
      qs.push_back(make_pair(b, -m));
    }
    sort(qs.begin(), qs.end());
    int nw = 0;
    bool ok = true;
    for (int i = 0; i < qs.size(); ++i) {
      nw += qs[i].second;
      if (nw > 150)
        ok = false;
    }
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}
Problem p00231 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define W_LIM 150

int main(void) {
  int n;
  int curW;
  int ev[127];
  int evTime[127];
  int evCnt;
  int broken;

  while (scanf("%d", &n), n) {
    evCnt = 0;
    // read events
    for (int i = 0; i < n; i++) {
      int m, a, b;

      scanf("%d %d %d", &m, &a, &b);
      ev[evCnt] = m;
      evTime[evCnt] = a;
      evCnt++;
      ev[evCnt] = -m;
      evTime[evCnt] = b;
      evCnt++;
    }
    // sort eventTable
    for (int i = 0; i < evCnt; i++) {
      int min = i, minT = evTime[i], tmp;
      for (int j = i + 1; j < evCnt; j++) {
        if (evTime[j] < minT)
          minT = evTime[min = j];
      }
      evTime[min] = evTime[i];
      evTime[i] = minT;
      tmp = ev[min];
      ev[min] = ev[i];
      ev[i] = tmp;
    }

    // simulate
    curW = 0;
    broken = 0;
    for (int i = 0; i < evCnt; i++) {
      curW += ev[i];
      //		printf("t:%d  ev:%d  curW:%d\n", evTime[i], ev[i],
      //curW);
      // ????????????????????????????????????????????????????????§???????????????
      if (evTime[i + 1] == evTime[i])
        continue;
      if (curW > W_LIM)
        broken = 1;
    }
    puts(broken ? "NG" : "OK");
    //		for (int i=0; i<evCnt; i++){
    //			printf("t:%d  ev:%d\n", evTime[i], ev[i]);
    //		}
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

#define W_LIM 150

int main(void) {
  int n;
  int curW;
  int ev[256];
  int evTime[256];
  int evCnt;
  int broken;

  while (scanf("%d", &n), n) {
    evCnt = 0;
    // read events
    for (int i = 0; i < n; i++) {
      int m, a, b;

      scanf("%d %d %d", &m, &a, &b);
      ev[evCnt] = m;
      evTime[evCnt] = a;
      evCnt++;
      ev[evCnt] = -m;
      evTime[evCnt] = b;
      evCnt++;
    }
    // sort eventTable
    for (int i = 0; i < evCnt; i++) {
      int min = i, minT = evTime[i], tmp;
      for (int j = i + 1; j < evCnt; j++) {
        if (evTime[j] < minT)
          minT = evTime[min = j];
      }
      evTime[min] = evTime[i];
      evTime[i] = minT;
      tmp = ev[min];
      ev[min] = ev[i];
      ev[i] = tmp;
    }

    // simulate
    curW = 0;
    broken = 0;
    for (int i = 0; i < evCnt; i++) {
      curW += ev[i];
      //		printf("t:%d  ev:%d  curW:%d\n", evTime[i], ev[i],
      //curW);
      // ????????????????????????????????????????????????????????§???????????????
      if (evTime[i + 1] == evTime[i])
        continue;
      if (curW > W_LIM)
        broken = 1;
    }
    puts(broken ? "NG" : "OK");
    //		for (int i=0; i<evCnt; i++){
    //			printf("t:%d  ev:%d\n", evTime[i], ev[i]);
    //		}
  }
  return 0;
}
Problem p00232 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int solve(int x, int y, int z) {
  vector<int> v(x);
  rep(i, x) cin >> v[i];
  vector<int> e(y + 1, 0), a(y + 1);
  rep(i, z) {
    int p, q, r;
    cin >> p >> q >> r;
    e[p] = q;
    a[p] = r;
  }

  vector<vector<double>> dp(y + 1, vector<double>(5005, 0));
  dp[0][0] = 1;
  rep(i, y) rep(j, 5005) if (dp[i][j] > 0) {
    rep(k, x) {
      int ni = min(i + v[k], y);
      int nj = j;
      if (e[ni] == 1)
        ni += a[ni];
      else if (e[ni] == 2)
        nj += a[ni];
      else if (e[ni] == 3)
        nj = max(nj - a[ni], 0);
      dp[ni][nj] += dp[i][j] / x;
    }
  }

  double ret = 0;
  rep(i, 5005) ret += dp[y][i] * i;
  return (int)ret;
}

int main() {
  int x, y, z;
  while (cin >> x >> y >> z, x | y | z) {
    cout << solve(x, y, z) << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int solve(int x, int y, int z) {
  vector<int> v(x);
  rep(i, x) cin >> v[i];
  vector<int> e(y + 1, 0), a(y + 1);
  rep(i, z) {
    int p, q, r;
    cin >> p >> q >> r;
    e[p] = q;
    a[p] = r;
  }

  vector<vector<double>> dp(y + 1, vector<double>(5005, 0));
  dp[0][0] = 1;
  rep(i, y) rep(j, 5005) if (dp[i][j] > 0) {
    rep(k, x) {
      int ni = min(i + v[k], y);
      int nj = j;
      if (e[ni] == 1)
        ni = min(ni + a[ni], y);
      else if (e[ni] == 2)
        nj += a[ni];
      else if (e[ni] == 3)
        nj = max(nj - a[ni], 0);
      dp[ni][nj] += dp[i][j] / x;
    }
  }

  double ret = 0;
  rep(i, 5005) ret += dp[y][i] * i;
  return (int)ret;
}

int main() {
  int x, y, z;
  while (cin >> x >> y >> z, x | y | z) {
    cout << solve(x, y, z) << endl;
  }

  return 0;
}
Problem p00232 (C++)

Buggy submission (status: Runtime Error):
// AOJ 0232
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility>
#define rep(i, a) for (int i = 0; i < (a); ++i)

typedef std::pair<int, int> P;

int X, Y, Z;
int V[4];
std::map<int, P> evnts;
double dp[50][5001][2];

double rec(int pos, int sum, bool eof) {
  double &ret = dp[pos][sum][eof];

  if (ret >= 0)
    return ret;

  ret = 0;

  if (pos == Y)
    return ret = sum;

  if (evnts.count(pos) && !eof) {
    int a = evnts[pos].second;

    switch (evnts[pos].first) {
    case 1:
      return ret = rec(std::min(Y, pos + a), sum, true);
    case 2:
      return ret = rec(pos, sum + a, true);
    case 3:
      return ret = rec(pos, std::max(0, sum - a), true);
    }
  }

  rep(i, X) ret += rec(std::min(Y, pos + V[i]), sum, false) / X;

  return ret;
}

int main() {
  while (scanf("%d%d%d", &X, &Y, &Z), X | Y | Z) {
    evnts.clear();

    rep(i, X) scanf("%d", V + i);
    int sum = 0;
    rep(i, Z) {
      int N, E, A;
      scanf("%d%d%d", &N, &E, &A);
      evnts[N] = P(E, A);
      if (E == 2)
        sum += A;
    }

    rep(i, Y + 1) rep(j, sum + 1) rep(l, 2) dp[i][j][l] = -1.0;
    printf("%d\n", (int)rec(0, 0, false));
  }

  return 0;
}

Fixed submission:
// AOJ 0232
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility>
#define rep(i, a) for (int i = 0; i < (a); ++i)

typedef std::pair<int, int> P;

int X, Y, Z;
int V[4];
std::map<int, P> evnts;
double dp[51][5001][2];

double rec(int pos, int sum, bool eof) {
  double &ret = dp[pos][sum][eof];

  if (ret >= 0)
    return ret;

  ret = 0;

  if (pos == Y)
    return ret = sum;

  if (evnts.count(pos) && !eof) {
    int a = evnts[pos].second;

    switch (evnts[pos].first) {
    case 1:
      return ret = rec(std::min(Y, pos + a), sum, true);
    case 2:
      return ret = rec(pos, sum + a, true);
    case 3:
      return ret = rec(pos, std::max(0, sum - a), true);
    }
  }

  rep(i, X) ret += rec(std::min(Y, pos + V[i]), sum, false) / X;

  return ret;
}

int main() {
  while (scanf("%d%d%d", &X, &Y, &Z), X | Y | Z) {
    evnts.clear();

    rep(i, X) scanf("%d", V + i);
    int sum = 0;
    rep(i, Z) {
      int N, E, A;
      scanf("%d%d%d", &N, &E, &A);
      evnts[N] = P(E, A);
      if (E == 2)
        sum += A;
    }

    rep(i, Y + 1) rep(j, sum + 1) rep(l, 2) dp[i][j][l] = -1.0;
    printf("%d\n", (int)rec(0, 0, false));
  }

  return 0;
}
Problem p00232 (C++)

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

using namespace std;

struct Cell {
  int e;
  int a;
};

double solve(vector<Cell> &cel, int pos, vector<int> &vs, int money,
             vector<map<int, double>> &mem) {
  double retval = 0;
  int vl = vs.size();
  int cl = cel.size();
  if (!mem[pos].empty() && (mem[pos].find(money) != mem[pos].end())) {
    return mem[pos][money];
  }
  if (pos >= cl)
    return money;
  for (int i = 0; i < vl; ++i) {
    int v = vs[i];
    if (pos + v >= cl) {
      retval += money;
      continue;
    }
    Cell &cur = cel[pos + v];
    if (cur.e == 0)
      retval += solve(cel, pos + v, vs, money, mem);
    else if (cur.e == 1)
      retval += solve(cel, pos + v + cur.a, vs, money, mem);
    else if (cur.e == 2)
      retval += solve(cel, pos + v, vs, money + cur.a, mem);
    else if (cur.e == 3)
      retval +=
          solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, mem);
  }
  mem[pos][money] = retval / vl;
  return mem[pos][money];
}

int main() {
  while (1) {
    int x, y, z;
    int i;
    int vl;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    vector<int> vs(x);
    vector<Cell> cells(y);
    vector<map<int, double>> mem(y);
    for (i = 0; i < x; ++i) {
      cin >> vs[i];
    }
    for (i = 0; i < z; ++i) {
      int n;
      cin >> n;
      cin >> cells[n].e;
      cin >> cells[n].a;
    }
    cout << (int)solve(cells, 0, vs, 0, mem) << endl;
  }
}

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

using namespace std;

struct Cell {
  int e;
  int a;
};

double solve(vector<Cell> &cel, int pos, vector<int> &vs, int money,
             vector<map<int, double>> &mem) {
  double retval = 0;
  int vl = vs.size();
  int cl = cel.size();
  if (!mem[pos].empty() && (mem[pos].find(money) != mem[pos].end())) {
    return mem[pos][money];
  }
  if (pos >= cl)
    return money;
  for (int i = 0; i < vl; ++i) {
    int v = vs[i];
    if (pos + v >= cl) {
      retval += money;
      continue;
    }
    Cell &cur = cel[pos + v];
    if (cur.e == 0)
      retval += solve(cel, pos + v, vs, money, mem);
    else if (cur.e == 1)
      retval += solve(cel, pos + v + cur.a, vs, money, mem);
    else if (cur.e == 2)
      retval += solve(cel, pos + v, vs, money + cur.a, mem);
    else if (cur.e == 3)
      retval +=
          solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, mem);
  }
  mem[pos][money] = retval / vl;
  return mem[pos][money];
}

int main() {
  while (1) {
    int x, y, z;
    int i;
    int vl;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    vector<int> vs(x);
    vector<Cell> cells(y);
    vector<map<int, double>> mem(y + 100);
    for (i = 0; i < x; ++i) {
      cin >> vs[i];
    }
    for (i = 0; i < z; ++i) {
      int n;
      cin >> n;
      cin >> cells[n].e;
      cin >> cells[n].a;
    }
    cout << (int)solve(cells, 0, vs, 0, mem) << endl;
  }
}
Problem p00232 (C++)

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

int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;

double calc(int v, int m) {
  if (v == Y)
    return (double)m;
  if (dp[v][m] > -1.)
    return dp[v][m];

  double sum = 0;
  for (int i = 0; i < V.size(); i++) {
    int tv = min(Y, v + V[i]);
    int tm = max(0, m + gt[v]);
    tv += pl[tv];

    sum += calc(tv, tm);
  }

  return dp[v][m] = sum / V.size();
}

int main() {
  while (scanf("%d %d %d", &X, &Y, &Z), X) {
    V.resize(X);
    pl = vector<int>(Y + 1, 0);
    gt = vector<int>(Y + 1, 0);
    dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));

    for (int i = 0; i < X; i++) {
      scanf("%d", &V[i]);
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      scanf("%d %d %d", &N, &E, &A);
      if (E == 1)
        pl[N] = A;
      if (E == 2)
        gt[N] = A;
      if (E == 3)
        gt[N] = -A;
    }

    printf("%d\n", (int)calc(0, 0));
  }
}

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

int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;

double calc(int v, int m) {
  if (v == Y)
    return (double)m;
  if (dp[v][m] > -1.)
    return dp[v][m];

  double sum = 0;
  for (int i = 0; i < V.size(); i++) {
    int tv = min(Y, v + V[i]);
    int tm = max(0, m + gt[tv]);
    tv = min(Y, tv + pl[tv]);

    sum += calc(tv, tm);
  }

  return dp[v][m] = sum / V.size();
}

int main() {
  while (scanf("%d %d %d", &X, &Y, &Z), X) {
    V.resize(X);
    pl = vector<int>(Y + 1, 0);
    gt = vector<int>(Y + 1, 0);
    dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));

    for (int i = 0; i < X; i++) {
      scanf("%d", &V[i]);
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      scanf("%d %d %d", &N, &E, &A);
      if (E == 1)
        pl[N] = A;
      if (E == 2)
        gt[N] = A;
      if (E == 3)
        gt[N] = -A;
    }

    printf("%d\n", (int)calc(0, 0));
  }
}
Problem p00232 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
double ans, dp[51][3000];
int X, Y, Z;

int dice[4], t[51], u[51], N;
void init();
double dfs(int, int);

int main() {
  while (cin >> X >> Y >> Z && X) {
    init();
    for (int i = 0; i < X; i++)
      cin >> dice[i];
    for (int i = 0; i < Z; i++) {
      cin >> N;
      cin >> t[N] >> u[N];
    }
    cout << (int)dfs(0, 0) << endl;
  }
  return 0;
}

void init() {
  for (int i = 0; i < 51; i++) {
    for (int j = 0; j < 3000; j++)
      dp[i][j] = -1.0;
    t[i] = u[i] = 0;
  }
}

double dfs(int pos, int cnt) {
  if (pos >= Y)
    return (double)cnt;
  if (dp[pos][cnt] != -1.0)
    return dp[pos][cnt];
  double res = 0;
  for (int i = 0; i < X; i++) {
    int npos = pos + dice[i];
    if (t[npos] == 1)
      res += dfs(npos + u[npos], cnt);
    else if (t[npos] == 2)
      res += dfs(npos, cnt + u[npos]);
    else
      res += dfs(npos, max(0, cnt - u[npos]));
  }
  return res / (double)X;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
double ans, dp[51][3000];
int X, Y, Z;

int dice[4], t[51], u[51], N;
void init();
double dfs(int, int);

int main() {
  while (cin >> X >> Y >> Z && X) {
    init();
    for (int i = 0; i < X; i++)
      cin >> dice[i];
    for (int i = 0; i < Z; i++) {
      cin >> N;
      cin >> t[N] >> u[N];
    }
    cout << (int)dfs(0, 0) << endl;
  }
  return 0;
}

void init() {
  for (int i = 0; i < 51; i++) {
    for (int j = 0; j < 3000; j++)
      dp[i][j] = -1.0;
    t[i] = u[i] = 0;
  }
}

double dfs(int pos, int cnt) {
  if (pos >= Y)
    return (double)cnt;
  if (dp[pos][cnt] != -1.0)
    return dp[pos][cnt];
  double res = 0;
  for (int i = 0; i < X; i++) {
    int npos = pos + dice[i];
    if (t[npos] == 1)
      res += dfs(npos + u[npos], cnt);
    else if (t[npos] == 2)
      res += dfs(npos, cnt + u[npos]);
    else
      res += dfs(npos, max(0, cnt - u[npos]));
  }
  return dp[pos][cnt] = res / (double)X;
}
Problem p00234 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 11][11][51];

void dfs(int x, int bit, int y, int air, int c) {
  if (air <= 0)
    return;
  if (!((bit >> x) & 1)) {
    if (fie[x][y] > 0)
      air = min(air + fie[x][y], m);
    if (fie[x][y] < 0)
      c += abs(fie[x][y]);
    bit |= 1 << x;
  }
  if (c > f)
    return;
  if (cost[x][bit][y][air] <= c)
    return;
  cost[x][bit][y][air] = c;
  if (y == h - 1)
    ans = min(ans, c);
  else {
    dfs(x, 0, y + 1, air - 1, c);
    if (x - 1 >= 0)
      dfs(x - 1, bit, y, air - 1, c);
    if (x + 1 < w)
      dfs(x + 1, bit, y, air - 1, c);
  }
}

int main(void) {
  while (1) {
    ans = INF;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
          for (int k = 0; k <= 50; k++) {
            cost[i][bit][j][k] = INF;
          }
        }
      }
    }
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &fie[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      dfs(i, 0, 0, o - 1, 0);
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 10][11][51];

void dfs(int x, int bit, int y, int air, int c) {
  if (air <= 0)
    return;
  if (!((bit >> x) & 1)) {
    if (fie[x][y] > 0)
      air = min(air + fie[x][y], m);
    if (fie[x][y] < 0)
      c += abs(fie[x][y]);
    bit |= 1 << x;
  }
  if (c > f)
    return;
  if (cost[x][bit][y][air] <= c)
    return;
  cost[x][bit][y][air] = c;
  if (y == h - 1)
    ans = min(ans, c);
  else {
    dfs(x, 0, y + 1, air - 1, c);
    if (x - 1 >= 0)
      dfs(x - 1, bit, y, air - 1, c);
    if (x + 1 < w)
      dfs(x + 1, bit, y, air - 1, c);
  }
}

int main(void) {
  while (1) {
    ans = INF;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
          for (int k = 0; k <= 50; k++) {
            cost[i][bit][j][k] = INF;
          }
        }
      }
    }
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &fie[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      dfs(i, 0, 0, o - 1, 0);
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00235 (C++)

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

using namespace std;

vector<vector<int>> graph(20);
int bridges[20][20];
bool used[20][20];

int calc_sum(int from) {
  int sum = 0;

  for (int i = 0; i < graph[from].size(); ++i) {
    int to = graph[from][i];
    if (graph[to].size() == 1)
      continue;
    if (used[from][to])
      continue;
    used[to][from] = true;
    sum += bridges[from][to] * 2 + calc_sum(to);
  }

  return sum;
}

int calc_max(int from) {
  int ma = 0;

  for (int i = 0; i < graph[from].size(); ++i) {
    int to = graph[from][i];
    if (graph[to].size() == 1)
      continue;
    if (used[from][to])
      continue;
    used[to][from] = true;
    ma = max(ma, bridges[from][to] + calc_max(to));
  }

  return ma;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(bridges, 0, sizeof(bridges));
    graph.clear();
    for (int i = 0; i < n - 1; ++i) {
      int a, b, t;
      cin >> a >> b >> t;
      a--;
      b--;
      bridges[a][b] = bridges[b][a] = t;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }

    memset(used, 0, sizeof(used));
    int sum = calc_sum(0);
    memset(used, 0, sizeof(used));
    int ma = calc_max(0);

    cout << sum - ma << endl;
  }
}

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

using namespace std;

vector<vector<int>> graph(20);
int bridges[20][20];
bool used[20][20];

int calc_sum(int from) {
  int sum = 0;

  for (int i = 0; i < graph[from].size(); ++i) {
    int to = graph[from][i];
    if (graph[to].size() == 1)
      continue;
    if (used[from][to])
      continue;
    used[to][from] = true;
    sum += bridges[from][to] * 2 + calc_sum(to);
  }

  return sum;
}

int calc_max(int from) {
  int ma = 0;

  for (int i = 0; i < graph[from].size(); ++i) {
    int to = graph[from][i];
    if (graph[to].size() == 1)
      continue;
    if (used[from][to])
      continue;
    used[to][from] = true;
    ma = max(ma, bridges[from][to] + calc_max(to));
  }

  return ma;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(bridges, 0, sizeof(bridges));
    for (int i = 0; i < 20; ++i)
      graph[i].clear();
    for (int i = 0; i < n - 1; ++i) {
      int a, b, t;
      cin >> a >> b >> t;
      a--;
      b--;
      bridges[a][b] = bridges[b][a] = t;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }

    memset(used, 0, sizeof(used));
    int sum = calc_sum(0);
    memset(used, 0, sizeof(used));
    int ma = calc_max(0);

    cout << sum - ma << endl;
  }
}
Problem p00235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

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

const double EPS = 1e-10;
const int INF = 1001001001;

struct Edge {
  Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
  int src, dst, cost;
};

vector<Edge> G[20];
int cost[20];
int f(int now, int pre, int dist) {
  cost[now] = dist;
  int sum = 0;
  rep(i, G[now].size()) {
    Edge next = G[now][i];
    if (next.dst != pre && G[next.dst].size() > 1)
      sum += f(next.dst, now, dist + next.cost) + next.cost;
  }
  return sum;
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    fill(cost, cost + n, 0);
    rep(i, n - 1) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      --a;
      --b;
      G[a].push_back(Edge(a, b, c));
      G[b].push_back(Edge(b, a, c));
    }
    int sum = f(0, -1, 0);
    printf("%d\n", sum * 2 - *max_element(cost, cost + n));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

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

const double EPS = 1e-10;
const int INF = 1001001001;

struct Edge {
  Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
  int src, dst, cost;
};

vector<Edge> G[20];
int cost[20];
int f(int now, int pre, int dist) {
  cost[now] = dist;
  int sum = 0;
  rep(i, G[now].size()) {
    Edge next = G[now][i];
    if (next.dst != pre && G[next.dst].size() > 1)
      sum += f(next.dst, now, dist + next.cost) + next.cost;
  }
  return sum;
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    fill(cost, cost + n, 0);
    rep(i, n) G[i].clear();
    rep(i, n - 1) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      --a;
      --b;
      G[a].push_back(Edge(a, b, c));
      G[b].push_back(Edge(b, a, c));
    }
    int sum = f(0, -1, 0);
    printf("%d\n", sum * 2 - *max_element(cost, cost + n));
  }
  return 0;
}
Problem p00235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
  MAX = max(MAX, b);
  S[a] = true;
  for (P i : rinsetu[a]) {
    if (S[i.second] == false && X[i.second]) {
      saiki(i.second, b + i.first);
    }
  }
  S[b] = false;
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(X, true, sizeof(X));
    int sum = 0;
    for (int b = 0; b < a; b++) {
      rinsetu[b].clear();
    }
    for (int b = 1; b < a; b++) {
      int c, d, e;
      cin >> c >> d >> e;
      c--;
      d--;
      rinsetu[c].push_back(P(e, d));
      rinsetu[d].push_back(P(e, c));
      sum += e * 2;
    }
    for (int i = 1; i < a; i++) {
      if (rinsetu[i].size() == 1) {
        sum -= rinsetu[i][0].first * 2;
        X[i] = false;
      }
    }
    memset(S, false, sizeof(S));
    MAX = 0;
    saiki(0, 0);
    cout << sum - MAX << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
  MAX = max(MAX, b);
  S[a] = true;
  for (P i : rinsetu[a]) {
    if (S[i.second] == false && X[i.second]) {
      saiki(i.second, b + i.first);
    }
  }
  S[a] = false;
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(X, true, sizeof(X));
    int sum = 0;
    for (int b = 0; b < a; b++) {
      rinsetu[b].clear();
    }
    for (int b = 1; b < a; b++) {
      int c, d, e;
      cin >> c >> d >> e;
      c--;
      d--;
      rinsetu[c].push_back(P(e, d));
      rinsetu[d].push_back(P(e, c));
      sum += e * 2;
    }
    for (int i = 1; i < a; i++) {
      if (rinsetu[i].size() == 1) {
        sum -= rinsetu[i][0].first * 2;
        X[i] = false;
      }
    }
    memset(S, false, sizeof(S));
    MAX = 0;
    saiki(0, 0);
    cout << sum - MAX << endl;
  }
}
Problem p00238 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, c, a, b;
  while (cin >> n, n) {
    cin >> c;
    int y = 0;
    for (i = 0; i < c; c++) {
      cin >> a >> b;
      y += b - a;
    }
    if (y >= n)
      cout << "OK" << endl;
    else
      cout << n - y << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, c, a, b;
  while (cin >> n, n) {
    cin >> c;
    int y = 0;
    for (i = 0; i < c; i++) {
      cin >> a >> b;
      y += b - a;
    }
    if (y >= n)
      cout << "OK" << endl;
    else
      cout << n - y << endl;
  }
}
Problem p00239 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int n, k, j, P, Q, R, C, flg;
  int i[1001], p1[101], q1[101], r1[101], c[1701], p[101], q[101], r[101];
  flg = 0;
  scanf("%d", &n);
  while (n != 0) {
    for (j = 1; j <= n; j++) {
      scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
      p1[j] = p[j] * 4;
      q1[j] = q[j] * 9;
      r1[j] = r[j] * 4;
      c[j] = p1[j] + q1[j] + r1[j];
    }
    scanf("%d %d %d %d", &P, &Q, &R, &C);
    for (j = 1; j <= n; j++) {
      if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
        printf("%d\n", i[j]);
        flg = 1;
      }
    }
    if (flg == 0) {
      printf("NA\n");
    }
    flg = 0;
    scanf("%d", &n);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int n, k, j, P, Q, R, C, flg;
  int i[1001], p1[101], q1[101], r1[101], c[1701], p[1001], q[1001], r[1001];
  flg = 0;
  scanf("%d", &n);
  while (n != 0) {
    for (j = 1; j <= n; j++) {
      scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
      p1[j] = p[j] * 4;
      q1[j] = q[j] * 9;
      r1[j] = r[j] * 4;
      c[j] = p1[j] + q1[j] + r1[j];
    }
    scanf("%d %d %d %d", &P, &Q, &R, &C);
    for (j = 1; j <= n; j++) {
      if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
        printf("%d\n", i[j]);
        flg = 1;
      }
    }
    if (flg == 0) {
      printf("NA\n");
    }
    flg = 0;
    scanf("%d", &n);
  }
  return 0;
}
Problem p00239 (C++)

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

int main() {
  int N;
  int s[100], h[100], L[100], C[100];
  while (cin >> N) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> h[i] >> L[i] >> C[i];
    }
    int P, Q, R, c;
    int a = 0;
    cin >> P >> Q >> R >> c;
    for (int i = 0; i < N; i++) {
      if ((h[i] <= P) && (L[i] <= Q) && (C[i] <= R) &&
          ((h[i] * 4 + L[i] * 9 + C[i] * 4) <= c)) {
        cout << s[i] << endl;
        a = 1;
      }
    }
    if (a == 0)
      cout << "NA" << endl;
  }
  return 0;
}

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

int main() {
  int N;
  int s[1000], h[1000], L[1000], C[1000];
  while (cin >> N) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> h[i] >> L[i] >> C[i];
    }
    int P, Q, R, c;
    int a = 0;
    cin >> P >> Q >> R >> c;
    for (int i = 0; i < N; i++) {
      if ((h[i] <= P) && (L[i] <= Q) && (C[i] <= R) &&
          ((h[i] * 4 + L[i] * 9 + C[i] * 4) <= c)) {
        cout << s[i] << endl;
        a = 1;
      }
    }
    if (a == 0)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00240 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double y;
    cin >> y;

    pair<int, double> p;
    auto f1 = [](double y, double r) { return 1 + y * r / 100; };
    auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
    rep(i, n) {
      int b, t;
      double r;
      cin >> b >> r >> t;
      show(t == 1 ? f1(y, r) : f2(y, r));
      if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
        p.first = b;
        p.second = (t == 1 ? f1(y, r) : f2(y, r));
      }
    }
    cout << p.first << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double y;
    cin >> y;

    pair<int, double> p;
    auto f1 = [](double y, double r) { return 1 + y * r / 100; };
    auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
    rep(i, n) {
      int b, t;
      double r;
      cin >> b >> r >> t;
      if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
        p.first = b;
        p.second = (t == 1 ? f1(y, r) : f2(y, r));
      }
    }
    cout << p.first << endl;
  }
}
Problem p00240 (C++)

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

using namespace std;

class Bank {
public:
  int id, r, t;
  Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};

int main() {
  while (true) {
    int n;
    cin >> n;
    int y;
    cin >> y;

    vector<Bank> vb;
    for (int i = 0; i < n; ++i) {
      int b, r, t;
      cin >> b >> r >> t;

      vb.push_back(Bank(b, r, t));
    }

    // ganpon * (1 + y * r/100)
    // ganpon * (1 + r/100)^y

    double maxi = 0;
    int ans = -1;

    for (int i = 0; i < vb.size(); ++i) {
      double g = 100000;
      double r1 = g * (1. + y * vb[i].r / 100.);
      double r2 = g * pow(1 + vb[i].r / 100., y);
      if (vb[i].t == 1) {
        if (maxi < r1) {
          maxi = r1;
          ans = vb[i].id;
        }
      } else {
        if (maxi < r2) {
          maxi = r2;
          ans = vb[i].id;
        }
      }
    }

    cout << ans << endl;
  }
}

// 1

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

using namespace std;

class Bank {
public:
  int id, r, t;
  Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int y;
    cin >> y;

    vector<Bank> vb;
    for (int i = 0; i < n; ++i) {
      int b, r, t;
      cin >> b >> r >> t;

      vb.push_back(Bank(b, r, t));
    }

    // ganpon * (1 + y * r/100)
    // ganpon * (1 + r/100)^y

    double maxi = 0;
    int ans = -1;

    for (int i = 0; i < vb.size(); ++i) {
      double g = 100000;
      double r1 = g * (1. + y * vb[i].r / 100.);
      double r2 = g * pow(1 + vb[i].r / 100., y);
      if (vb[i].t == 1) {
        if (maxi < r1) {
          maxi = r1;
          ans = vb[i].id;
        }
      } else {
        if (maxi < r2) {
          maxi = r2;
          ans = vb[i].id;
        }
      }
    }

    cout << ans << endl;
  }
}

// 1
Problem p00243 (C++)

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  int cnt = 1;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    cnt += change(ny, nx, c, f);
  }
  return cnt;
}

bool dfs(int t = 0, int sum = 1) {
  if (lim == t) {
    bool ok = true;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (fld[0][0] != fld[i][j])
          ok = false;
      }
    return ok;
  }

  int _fld[10][10];
  memcpy(_fld, fld, sizeof(_fld));

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      int la = change(0, 0, k, fld[0][0]);
      if (la > sum)
        ret |= dfs(t + 1, la);
      memcpy(fld, _fld, sizeof(fld));
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  int cnt = 1;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    cnt += change(ny, nx, c, f);
  }
  return cnt;
}

bool dfs(int t = 0, int sum = 1) {
  if (lim == t) {
    bool ok = true;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (fld[0][0] != fld[i][j])
          ok = false;
      }
    return ok;
  }

  int _fld[10][10];
  memcpy(_fld, fld, sizeof(_fld));

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      int la = change(0, 0, k, fld[0][0]);
      if (la >= sum)
        ret |= dfs(t + 1, la);
      memcpy(fld, _fld, sizeof(fld));
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}
Problem p00246 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;

int list(int n, int k, int g) {
  int i;
  if (k == 10 && n == h) {
    for (i = 0; i < h; i++)
      da[v][a[i]]++;
    dn[v] = h;
    v++;
  }
  if (n == h || k >= 10)
    return 1;
  for (i = g; i < 10; i++) {
    a[n] = i;
    list(n + 1, k + i, i);
  }
  return 0;
}

int dfs(int g, int u, int t) {
  int i, j, k;
  if (an < g)
    an = g;
  if (g + t / dn[u] <= an)
    return 0;
  for (i = u; i < 41; i++) {
    f = 0;
    for (j = 1; j < 10; j++) {
      a[j] -= da[i][j];
      if (a[j] < 0) {
        f = 1;
        for (k = j; k > 0; k--)
          a[k] += da[i][k];
        break;
      }
    }
    if (f == 0) {
      dfs(g + 1, i, t - dn[i]);
      for (j = 1; j < 10; j++)
        a[j] += da[i][j];
    }
  }
  return 0;
}

int main() {
  int i, j;
  v = 0;
  for (i = 0; i < 40; i++)
    for (j = 0; j < 11; j++)
      da[i][j] = 0;
  for (i = 2; i < 11; i++) {
    h = i;
    list(0, 0, 1);
  }
  while (cin >> n && n > 0) {
    for (i = 0; i < 11; i++)
      a[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> j;
      a[j]++;
    }
    v = 0;
    an = 0;
    dfs(0, 0, n);
    cout << an << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;

int list(int n, int k, int g) {
  int i;
  if (k == 10 && n == h) {
    for (i = 0; i < h; i++)
      da[v][a[i]]++;
    dn[v] = h;
    v++;
  }
  if (n == h || k >= 10)
    return 1;
  for (i = g; i < 10; i++) {
    a[n] = i;
    list(n + 1, k + i, i);
  }
  return 0;
}

int dfs(int g, int u, int t) {
  int i, j, k;
  if (an < g)
    an = g;
  if (g + t / dn[u] <= an)
    return 0;
  for (i = u; i < 41; i++) {
    f = 0;
    for (j = 1; j < 10; j++) {
      a[j] -= da[i][j];
      if (a[j] < 0) {
        f = 1;
        for (k = j; k > 0; k--)
          a[k] += da[i][k];
        break;
      }
    }
    if (f == 0) {
      dfs(g + 1, i, t - dn[i]);
      for (j = 1; j < 10; j++)
        a[j] += da[i][j];
    }
  }
  return 0;
}

int main() {
  int i, j;
  v = 0;
  for (i = 0; i < 40; i++)
    for (j = 0; j < 11; j++)
      da[i][j] = 0;
  for (i = 2; i < 11; i++) {
    h = i;
    list(0, 0, 1);
  }
  while (cin >> n && n > 0) {
    for (i = 0; i < 11; i++)
      a[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> j;
      a[j]++;
    }
    v = 0;
    an = 0;
    while (v < 5) {
      while (true) {
        f = 0;
        for (i = 1; i < 10; i++)
          if (a[i] < da[v][i]) {
            f = 1;
            break;
          }
        if (f == 1)
          break;
        for (i = 1; i < 10; i++)
          a[i] -= da[v][i];
        an++;
        n -= 2;
      }
      v++;
    }
    dfs(an, 5, n);
    cout << an << endl;
  }
  return 0;
}