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

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

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

int X;
int V[4];
int Y, Z;
int T[55], E[55];

double dp[55][5010];

signed main() {
  while (cin >> X >> Y >> Z, X || Y || Z) {
    memset(T, 0, sizeof(T));
    memset(dp, 0, sizeof(dp));
    rep(i, X) cin >> V[i];
    rep(i, Z) {
      int a;
      cin >> a;
      cin >> T[a] >> E[a];
    }

    double ans = 0;

    dp[0][0] = 1;
    rep(i, Y) {
      rep(j, i * 100 + 1) dp[Y][j] = 0;
      for (int j = Y - 1; j >= i; j--) {
        rep(k, i * 100 + 1) {
          rep(l, X) {
            int t = j + V[l];
            int c = k;
            chmin(t, Y);
            if (T[t] == 1) {
              t += E[t];
              chmin(t, Y);
            } else if (T[t] == 2) {
              c += E[t];
            } else if (T[t] == 3) {
              c -= E[t];
              if (c < 0)
                c = 0;
            }
            dp[t][c] += dp[j][k] / X;
          }
          dp[j][k] = 0;
        }
      }
      rep(j, i * 100 + 101) ans += dp[Y][j] * j;
    }
    printf("%lld\\n", (int)ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int X;
int V[4];
int Y, Z;
int T[55], E[55];

double dp[55][5010];

signed main() {
  while (cin >> X >> Y >> Z, X || Y || Z) {
    memset(T, 0, sizeof(T));
    memset(dp, 0, sizeof(dp));
    rep(i, X) cin >> V[i];
    rep(i, Z) {
      int a;
      cin >> a;
      cin >> T[a] >> E[a];
    }

    double ans = 0;

    dp[0][0] = 1;
    rep(i, Y) {
      rep(j, i * 100 + 1) dp[Y][j] = 0;
      for (int j = Y - 1; j >= 0; j--) {
        rep(k, i * 100 + 1) {
          rep(l, X) {
            int t = j + V[l];
            int c = k;
            chmin(t, Y);
            if (T[t] == 1) {
              t += E[t];
              chmin(t, Y);
            } else if (T[t] == 2) {
              c += E[t];
            } else if (T[t] == 3) {
              c -= E[t];
              if (c < 0)
                c = 0;
            }
            dp[t][c] += dp[j][k] / X;
          }
          dp[j][k] = 0;
        }
      }
      rep(j, i * 100 + 101) ans += dp[Y][j] * j;
    }
    printf("%lld\\n", (int)ans);
  }
  return 0;
}