alexjercan/bugnet huggingface dataset:
Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

constexpr int MAX_X = 4, MAX_YN = 50;

int main() {
  while (true) {
    int X, Y, Z;
    int roulette[MAX_X], jump[MAX_YN]{}, get_money[MAX_YN]{};
    float dp[MAX_YN + 1][100 * MAX_YN + 1]{1};
    scanf("%d%d%d", &X, &Y, &Z);
    if (!X) {
      break;
    }
    for (int i = 0; i < X; ++i) {
      scanf("%d", &roulette[i]);
    }
    for (int i = 0; i < Z; ++i) {
      int n, e, a;
      scanf("%d%d%d", &n, &e, &a);
      switch (e) {
      case 1:
        jump[n] = a;
        break;
      case 2:
        get_money[n] = a;
        break;
      case 3:
        get_money[n] = -a;
        break;
      }
    }
    for (int i = 0; i < Y; ++i) {
      for (int j = 0; j <= 5000; ++j) {
        if (dp[i][j] == 0) {
          continue;
        }
        for (int k = 0; k < X; ++k) {
          int ni, nj;
          ni = std::min(i + roulette[k], Y);
          nj = std::max(j + get_money[ni], 0);
          ni = std::min(ni + jump[ni], Y);
          dp[ni][nj] += dp[i][j] / X;
        }
      }
    }
    float ans = 0;
    for (int i = 0; i <= 5000; ++i) {
      ans += dp[Y][i] * i;
    }
    printf("%d\\n", (int)ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

constexpr int MAX_X = 4, MAX_YN = 50, MAX_Z = 49;

int main() {
  while (true) {
    int X, Y, Z;
    int roulette[MAX_X], jump[MAX_YN + 1]{}, get_money[MAX_YN + 1]{};
    float dp[MAX_YN + 1][MAX_Z * 100 + 1]{{1}};
    scanf("%d%d%d", &X, &Y, &Z);
    for (int i = 0; i < X; ++i) {
      scanf("%d", &roulette[i]);
    }
    for (int i = 0; i < Z; ++i) {
      int n, e, a;
      scanf("%d%d%d", &n, &e, &a);
      switch (e) {
      case 1:
        jump[n] = a;
        break;
      case 2:
        get_money[n] = a;
        break;
      case 3:
        get_money[n] = -a;
      }
    }
    for (int i = 0; i < Y; ++i) {
      for (int j = 0; j <= i * 100; ++j) {
        if (!dp[i][j]) {
          continue;
        }
        if (jump[i] > 0) {
          dp[std::min(i + jump[i], Y)][j] += dp[i][j];
          continue;
        }
        for (int k = 0; k < X; ++k) {
          dp[std::min(i + roulette[k], Y)]
            [std::max(0, j + get_money[std::min(i + roulette[k], Y)])] +=
              dp[i][j] / X;
        }
      }
    }
    float ans = 0;
    for (int i = 0; i <= Y * 100; ++i) {
      ans += dp[Y][i] * i;
    }
    printf("%d\\n", (int)ans);
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

// 13
#include <algorithm>
#include <cstdlib>
#include <iostream>

using namespace std;

int x, y, z;
int v[4];
int e[51], a[51];
bool p[51][50001];
double mem[51][50001];

double dfs(int s, int m) {
  if (s >= y) {
    return m;
  } else if (p[s][m]) {
    return mem[s][m];
  } else {
    double sm = 0;
    for (int i = 0; i < x; i++) {
      int c = s + v[i];
      int cm = m;
      if (c < y) {
        if (e[c] == 1) {
          c += a[c];
        } else if (e[c] == 2) {
          cm += a[c];
        } else if (e[c] == 3) {
          if (cm > a[c]) {
            cm -= a[c];
          } else {
            cm = 0;
          }
        }
      }
      sm += dfs(c, cm);
    }
    p[s][m] = true;
    return mem[s][m] = sm * 1. / x;
  }
}

int main() {
  while (cin >> x >> y >> z, x | y | z) {
    for (int i = 0; i < x; i++) {
      cin >> v[i];
    }
    fill(e, e + 51, 0);
    for (int i = 0; i < z; i++) {
      int n;
      cin >> n;
      cin >> e[n] >> a[n];
    }
    fill(p[0], p[51], false);
    cout << (int)dfs(0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 13
#include <algorithm>
#include <cstdlib>
#include <iostream>

using namespace std;

int x, y, z;
int v[4];
int e[51], a[51];
bool p[51][50001];
double mem[51][50001];

double dfs(int s, int m) {
  if (s >= y) {
    return m;
  } else if (p[s][m]) {
    return mem[s][m];
  } else {
    int sm = 0;
    for (int i = 0; i < x; i++) {
      int c = s + v[i];
      int cm = m;
      if (c < y) {
        if (e[c] == 1) {
          c += a[c];
        } else if (e[c] == 2) {
          cm += a[c];
        } else if (e[c] == 3) {
          if (cm > a[c]) {
            cm -= a[c];
          } else {
            cm = 0;
          }
        }
      }
      sm += dfs(c, cm);
    }
    return mem[s][m] = sm * 1. / x;
  }
}

int main() {
  while (cin >> x >> y >> z, x | y | z) {
    for (int i = 0; i < x; i++) {
      cin >> v[i];
    }
    fill(e, e + 51, 0);
    for (int i = 0; i < z; i++) {
      int n;
      cin >> n;
      cin >> e[n] >> a[n];
    }
    fill(p[0], p[51], false);
    cout << (int)dfs(0, 0) << endl;
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: 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 = 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;
}

Error: 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 += 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;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

struct cell {
  int money;
  int jump;
};

double solve(int money, int cur, int &x, int v[], int &y, cell c[]) {
  static double memo[4901][51];

  if (cur == 0)
    for (int i = 0; i <= (y - 1) * 100; ++i)
      for (int j = 0; j <= y; ++j)
        memo[i][j] = -1;

  double ret = 0;

  if (cur == y)
    return money;

  if (memo[money][cur] != -1)
    return memo[money][cur];

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

    int nextCur = min(cur + v[i], y);
    int nextMoney = max(money + c[nextCur].money, 0);

    nextCur = min(nextCur + c[nextCur].jump, y);

    ret += solve(nextMoney, nextCur, x, v, y, c);
  }

  return memo[money][cur] = (ret / (double)x);
}

int main() {
  while (true) {

    int x, y, z;
    int v[4];
    cell c[51];

    scanf("%d%d%d", &x, &y, &z);

    if (x == 0 && y == 0 && z == 0)
      break;

    for (int i = 0; i < x; ++i)
      scanf("%d", v + i);

    for (int i = 0; i <= y; ++i)
      c[i].jump = 0, c[i].money = 0;
    for (int i = 0; i < z; ++i) {
      int t1, t2, t3;
      scanf("%d%d%d", &t1, &t2, &t3);
      if (t2 == 1)
        c[t1].jump = t3;
      else
        c[t1].money = t2 == 2 ? t3 : -t3;
    }

    printf("%d\\n", (int)solve(0, 0, x, v, y, c));
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

struct cell {
  int money;
  int jump;
};

int main() {
  while (true) {

    int x, y, z;
    int v[4];
    cell c[51];
    double e[51], m[51];

    scanf("%d%d%d", &x, &y, &z);

    if (x == 0 && y == 0 && z == 0)
      break;

    for (int i = 0; i <= y; ++i)
      c[i].jump = 0, c[i].money = 0, e[i] = 0, m[i] = 0;

    for (int i = 0; i < x; ++i)
      scanf("%d", v + i);
    for (int i = 0; i < z; ++i) {
      int t1, t2, t3;
      scanf("%d%d%d", &t1, &t2, &t3);
      if (t2 == 1)
        c[t1].jump = t3;
      else if (t2 == 2 || t2 == 3)
        c[t1].money = t2 == 2 ? t3 : -t3;
    }

    e[0] = 1;

    for (int i = 0; i < y; ++i) {
      for (int j = 0; j < x; ++j) {

        int tgt = i + v[j];

        if (c[tgt].jump != 0)
          tgt += c[tgt].jump;
        if (tgt > y)
          tgt = y;

        double ex = e[i] / (double)x;
        e[tgt] += ex;
        m[tgt] += m[i] / (double)x + ex * c[tgt].money;
      }
    }

    printf("%d\\n", (int)m[y]);
  }

  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define FORE(i, a, b) for (int i = (a); i <= (int)(b); i++)
using namespace std;
typedef complex<int> cl;
typedef pair<long double, int> pr;
typedef pair<pr, int> ppr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

double DP[51][5001] = {};
int main() {
  int X, Y, Z;
  int tmp;
  while (cin >> X >> Y >> Z && X) {
    REP(i, 51)
    REP(j, 5001)
    DP[i][j] = 0;
    int event[51][2] = {};
    int eye[10] = {};
    REP(i, X)
    cin >> eye[i];
    REP(i, Z) {
      cin >> tmp;
      cin >> event[tmp][0] >> event[tmp][1];
    }
    DP[0][0] = 1;

    REP(i, Y)
    FORE(k, 0, 5000) {
      double par = (DP[i][k] / (long double)X);
      if (par == 0)
        continue;
      REP(j, X) {
        int money = k;
        int next = min(Y, i + eye[j]);
        switch (event[next][0]) {
        case 1:
          next += event[next][1];
          break;
        case 2:
          money += event[next][1];
          break;
        case 3:
          money -= event[next][1];
          money = max(0, money);
          break;
        }
        next = min(Y, next);
        DP[next][money] += par;
      }
    }
    long double mean = 0;
    FORE(i, 0, 5000) {
      mean += (long double)i * DP[Y][i];
      //	if(DP[Y][i])
      //	cout << DP[Y][i] << " " <<i << endl;
    }
    cout << (int)mean << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define FORE(i, a, b) for (int i = (a); i <= (int)(b); i++)
using namespace std;
typedef complex<int> cl;
typedef pair<double, int> pr;
typedef pair<pr, int> ppr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

double DP[51][5001] = {};
int main() {
  int X, Y, Z;
  int tmp;
  while (cin >> X >> Y >> Z) {
    REP(i, 51)
    REP(j, 5001)
    DP[i][j] = 0;
    int event[51][2] = {};
    int eye[4] = {};
    REP(i, X)
    cin >> eye[i];
    REP(i, Z) {
      cin >> tmp;
      cin >> event[tmp][0] >> event[tmp][1];
    }
    DP[0][0] = 1;
    REP(i, Y)
    REP(j, X)
    FORE(k, 0, 5000) {
      double par = DP[i][k] / (double)X;
      if (par == 0)
        continue;
      int money = k;
      int next = min(Y, i + eye[j]);
      switch (event[next][0]) {
      case 1:
        next += event[next][1];
        break;
      case 2:
        money += event[next][1];
        break;
      case 3:
        money -= event[next][1];
        money = max(0, money);
        break;
      }
      DP[next][money] += par;
    } /*
double mean=0;
FORE(i,1,5000){
mean+=i*DP[Y][i];
}
cout << (int)mean<< endl;
*/
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M, K;
int E[100], A[100];
int v[4];

double dp[51][50000];
void solve() {

  dp[0][0] = 1.0;
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i; j--) {
      for (int k = 0; k <= i * 100; k++) {
        for (int l = 0; l < M; l++) {
          int toval = k;
          int topos = min(N, j + v[l]);
          if (E[topos] == 1)
            topos = min(N, topos + A[topos]);
          else if (E[topos] == 2)
            toval += A[topos];
          else if (E[topos] == 3)
            toval = max(0, toval - A[topos]);
          dp[topos][toval] += dp[j][k] / M;
        }
        dp[j][k] = 0;
      }
    }
  }

  double sum = 0;
  for (int i = 0; i < 50000; i++)
    sum += dp[N][i] * i;
  cout << (int)sum << endl;
}

bool input() {
  memset(dp, 0, sizeof(dp));
  memset(E, 0, sizeof(E));
  memset(A, 0, sizeof(A));

  cin >> M >> N >> K;
  if (M == 0 && N == 0 && K == 0)
    return false;
  for (int i = 0; i < M; i++)
    cin >> v[i];
  for (int i = 0; i < K; i++) {
    int n, e, a;
    cin >> n >> e >> a;
    E[n] = e;
    A[n] = a;
  }
  return true;
}

int main() {
  while (input())
    solve();
  return 0;
}

Error: TLE

Fixed submission:

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

int N, M, K;
int E[100], A[100];
int v[4];

double dp[51][50000];
void solve() {

  dp[0][0] = 1.0;
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= 0; j--) {
      for (int k = 0; k < 50000; k++) {
        if (dp[j][k] < (1e-8))
          continue;
        for (int l = 0; l < M; l++) {
          int toval = k;
          int topos = min(N, j + v[l]);
          if (E[topos] == 1)
            topos = min(N, topos + A[topos]);
          else if (E[topos] == 2)
            toval += A[topos];
          else if (E[topos] == 3)
            toval = max(0, toval - A[topos]);
          dp[topos][toval] += dp[j][k] / M;
        }
        dp[j][k] = 0;
      }
    }
  }
  double sum = 0;
  for (int i = 0; i < 50000; i++)
    sum += dp[N][i] * i;
  cout << (int)sum << endl;
}

bool input() {
  memset(dp, 0, sizeof(dp));
  memset(E, 0, sizeof(E));
  memset(A, 0, sizeof(A));

  cin >> M >> N >> K;
  if (M == 0 && N == 0 && K == 0)
    return false;
  for (int i = 0; i < M; i++)
    cin >> v[i];
  for (int i = 0; i < K; i++) {
    int n, e, a;
    cin >> n >> e >> a;
    E[n] = e;
    A[n] = a;
  }
  return true;
}

int main() {
  while (input())
    solve();
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int X, Y, Z;
int V[4];
P mass[51];
double dp[51][5001];

int main() {
  while (cin >> X >> Y >> Z, X) {
    for (int i = 0; i < X; i++)
      cin >> V[i];
    for (int i = 0; i <= Y; i++) {
      mass[i] = P(0, 0);
    }
    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      mass[N] = P(E, A);
    }
    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 5001; j++) {
        dp[i][j] = 0.0;
      }
    }
    dp[0][0] = 1.0;
    int limit = 0;
    for (int i = 0; i < Y; i++) {
      limit += 100;
      for (int j = 0; j <= limit; j++) {
        if (dp[i][j]) {
          for (int k = 0; k < X; k++) {
            int next_p = min(i + V[k], Y), next_m = j;
            if (mass[next_p].first == 1) {
              next_p = min(next_p + mass[next_p].second, Y);
            } else if (mass[next_p].first == 2) {
              next_m += mass[next_p].second;
            } else if (mass[next_p].first == 3) {
              next_m = max(0, next_m - mass[next_p].second);
            }
            dp[next_p][next_m] += dp[i][j] / (double)X;
          }
        }
      }
    }
    double ans = 0.0;
    for (int i = 0; i < 5001; i++) {
      ans += dp[Y][i] * i;
    }
    cout << (int)ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int X, Y, Z;
int V[4];
P mass[55];

double solve(int now, int money, int cnt) {
  if (now >= Y)
    return (double)((double)money / (double)cnt);
  double ret = 0;
  for (int i = 0; i < X; i++) {
    int next = now + V[i];
    int nm = money;
    if (mass[next].first == 1) {
      next += mass[next].second;
    } else if (mass[next].first == 2) {
      nm += mass[next].second;
    } else if (mass[next].first == 3) {
      nm = max(0, nm - mass[next].second);
    }
    ret += solve(next, nm, cnt * X);
  }
  return ret;
}

int main() {
  while (cin >> X >> Y >> Z, X) {
    for (int i = 0; i < X; i++)
      cin >> V[i];
    for (int i = 0; i < Y; i++) {
      mass[i] = P(0, 0);
    }
    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      mass[N] = P(E, A);
    }
    cout << (int)solve(0, 0, 1) << endl;
  }
}
Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

class Masu {
public:
  int e;
  int a;
};

int x, y, z;
int v[4];
Masu masu[52];

void solve() {
  double dp[52][5001];
  for (int i = 0; i < 52; i++) {
    for (int j = 0; j < 5001; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = 1;

  for (int i = 0; i < y; i++) {
    for (int j = 0; j <= 5000; j++) {
      if (dp[i][j] == 0)
        continue;
      for (int k = 0; k < x; k++) {
        int np = min(y, i + v[k]);
        int nm = j;
        switch (masu[np].e) {
        case 1:
          np = min(y, np + masu[np].a);
          break;
        case 2:
          nm += masu[np].a;
          break;
        case 3:
          nm = max(0, nm - masu[np].a);
          break;
        }

        dp[np][nm] += dp[i][j] * (1.0 / x);
      }
    }
  }

  double ans = 0;
  for (int i = 0; i <= 5000; i++)
    ans += dp[y][i] * i;
  cout << (int)ans << endl;
}

int main() {
  while (1) {
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    for (int i = 0; i < x; i++) {
      cin >> v[i];
    }
    for (int i = 0; i <= y; i++)
      masu[i].e = 0;
    for (int i = 0; i < z; i++) {
      int n;
      cin >> n;
      cin >> masu[n].e >> masu[n].a;
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Mass {
public:
  int E;
  int A;
  Mass() {
    E = 0;
    A = 0;
  }
};

Mass mass[51];
int X, Y, Z;
int V[4];

double P;

void rec(int nmass, int mon, int turn) {
  int tmp, nmon;
  double tmp2;
  if (nmass == Y) {
    tmp2 = mon;
    for (int i = 0; i < turn; i++) {
      tmp2 /= (double)X;
    }
    P += tmp2;
    return;
  }

  for (int i = 0; i < X; i++) {
    tmp = nmass + V[i];
    nmon = mon;
  label:
    if (tmp > Y)
      tmp = Y;
    switch (mass[tmp].E) {
    case 1:
      tmp += mass[tmp].A;
      goto label;
    case 2:
      nmon += mass[tmp].A;
      break;
    case 3:
      nmon -= mass[tmp].A;
      if (nmon < 0)
        nmon = 0;
      break;
    }
    rec(tmp, nmon, turn + 1);
  }
}
void solve() { rec(0, 0, 0); }

int main() {
  int N;
  while (1) {
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0)
      break;
    for (int i = 0; i < X; i++)
      cin >> V[i];
    for (int i = 0; i < Z; i++) {
      cin >> N;
      cin >> mass[N].E >> mass[N].A;
    }
    P = 0;
    solve();
    cout << (int)P << endl;
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int X, Y, Z;
int V[5];
int Me[65];
int Mm[65];
double dp[65][5002];
int used[65][5002];
inline int calc(int a) { return a > 0 ? a : 0; }
double solve(int id, int mon) {
  if (id >= Y)
    return (double)mon;
  if (used[id][mon])
    return dp[id][mon];
  used[id][mon] = 1;
  double ret = 0.0f;
  for (int i = 0; i < X; i++) {
    int next = id + V[i];
    if (Me[next] != 0)
      ret += solve(next + Me[next], calc(mon)) / (double)X;
    else
      ret += solve(next, calc(mon + Mm[next])) / (double)X;
  }
  return dp[id][mon] = ret;
}
int main() {
  while (1) {
    memset(Mm, 0, sizeof(Mm));
    memset(Me, 0, sizeof(Me));
    for (int i = 0; i < 65; i++)
      for (int j = 0; j <= 5001; j++)
        dp[i][j] = 0.0f;
    memset(used, 0, sizeof(used));
    scanf("%d %d %d", &X, &Y, &Z);
    if (!X && !Y && !Z)
      break;
    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)
        Me[N] = A;
      else if (E == 2)
        Mm[N] = A;
      else
        Mm[N] = -A;
    }
    printf("%d\\n", (int)solve(0, 0));
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int X, Y, Z, M;
int V[5];
int Me[65];
int Mm[65];
double dp[65][5002];
int used[65][5002];
inline int calc(int a) { return a > 0 ? a : 0; }
double solve(int id, int mon) {
  if (id >= Y)
    return (double)mon;
  if (used[id][mon])
    return dp[id][mon];
  used[id][mon] = 1;
  double ret = 0.0f;
  for (int i = 0; i < X; i++) {
    int next = id + V[i];
    if (Me[next] != 0)
      ret += solve(next + Me[next], calc(mon)) / (double)X;
    else
      ret += solve(next, calc(mon + Mm[next])) / (double)X;
  }
  return dp[id][mon] = ret;
}
int main() {
  while (1) {
    memset(Mm, 0, sizeof(Mm));
    memset(Me, 0, sizeof(Me));
    for (int i = 0; i <= 65; i++)
      for (int j = 0; j <= 5001; j++)
        dp[i][j] = 0.0f;
    memset(used, 0, sizeof(used));
    M = 0;
    scanf("%d %d %d", &X, &Y, &Z);
    if (!X && !Y && !Z)
      break;
    for (int i = 0; i < X; i++) {
      scanf("%d", &V[i]);
    }
    //    puts("ok");
    for (int i = 0; i < Z; i++) {
      int N, E, A;
      scanf("%d %d %d", &N, &E, &A);
      if (E == 1)
        Me[N] = A;
      else if (E == 2) {
        Mm[N] = A;
        M += A;
      } else
        Mm[N] = -A;
    }
    printf("%d\\n", (int)solve(0, 0));
  }
}