Problem p00232 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-5;

double dp[51][10001]; // i, sum+5000 = ?¢????
int X, Y, Z;
int V[4];
vector<pii> g[51];
pii event[51];

int main() {
  while (cin >> X >> Y >> Z, X || Y || Z) {
    REP(i, X) scanf("%d", V + i);

    fill(event, event + Y + 1, pii(0, 0));
    REP(i, Y + 1) g[i].clear();

    fill(dp[0], dp[Y + 1], 0);

    REP(i, Z) {
      int n, e, a;
      scanf("%d %d %d", &n, &e, &a);
      if (e == 1) {
        event[n] = pii(1, a);
      } else {
        if (e == 3)
          a = -a;
        event[n] = pii(2, a);
      }
    }

    REP(i, Y) {
      REP(j, X) {
        int p = min(i + V[j], Y);
        int c = 0;

        if (event[p].fi == 1)
          p = min(p + event[p].se, Y);
        else if (event[p].fi == 2)
          c = event[p].se;

        g[i].push_back(pii(p, c));
      }
    }

    dp[0][5000] = 1;
    REP(i, Y) {
      REP(j, 10001) {
        if (dp[i][j] == 0)
          continue;

        REP(k, g[i].size()) {
          int np = g[i][k].fi;
          int nc = max(j + g[i][k].se, 5000);

          dp[np][nc] += dp[i][j] / X;
        }
      }
    }

    double ans = 0;
    FOR(i, 5000, 10001) if (dp[Y][i] != 0) ans += (i - 5000) * dp[Y][i];
    ans += EPS;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-5;

int dp[11][51][10001]; // i, mvcnt, sum+5000 = pat
int X, Y, Z;
int V[4];
vector<pii> g[51];
pii event[51];
int rsum[51];

int main() {
  while (cin >> X >> Y >> Z, X || Y || Z) {
    REP(i, X) scanf("%d", V + i);

    fill(event, event + Y + 1, pii(0, 0));
    REP(i, Y + 1) g[i].clear();
    memset(dp, 0, sizeof(dp));

    REP(i, Z) {
      int n, e, a;
      scanf("%d %d %d", &n, &e, &a);
      if (e == 1) {
        event[n] = pii(1, a);
      } else {
        if (e == 3)
          a = -a;
        event[n] = pii(2, a);
        if (a > 0)
          rsum[n] += a;
      }
    }

    for (int i = Y - 1; i >= 0; i--)
      rsum[i] += rsum[i + 1];

    REP(i, Y) {
      REP(j, X) {
        int p = min(i + V[j], Y);
        int c = 0;

        if (event[p].fi == 1)
          p = min(p + event[p].se, Y);
        else if (event[p].fi == 2)
          c = event[p].se;

        g[i].push_back(pii(p, c));
      }
    }

    dp[0][0][5000] = 1;
    REP(i, Y) {
      memset(dp[(i + 10) % 11], 0, sizeof(dp[0]));

      REP(j, Y) {
        REP(k, 10001) if (dp[i % 11][j][k]) {
          if (k - 5000 < 0 && -(k - 5000) > rsum[i])
            continue;

          REP(l, g[i].size()) {
            dp[g[i][l].fi % 11][j + 1][k + g[i][l].se] += dp[i % 11][j][k];
          }
        }
      }
    }

    double ans = 0;

    for (int i = Y; i >= 0; i--) {
      ans /= X;
      FOR(j, 5000, 10001) ans += (ll)(j - 5000) * dp[Y % 11][i][j];
    }
    ans += EPS;

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