Problem p00232 (C++)

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

#define MONEY_MAX 4901

typedef pair<int, int> pii;

int main() {
  while (1) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0) {
      break;
    } else {
      int V[X];
      for (int i = 0; i < X; i++)
        cin >> V[i];
      pii event[Y + 1];
      fill(&event[0].first, &event[Y + 1].first, -1);
      for (int i = 0; i < Z; i++) {
        int N;
        cin >> N;
        cin >> event[N].first >> event[N].second;
      }
      double dp[Y + 1][MONEY_MAX];
      fill(&dp[0][0], &dp[Y + 1][0], 0.0);
      dp[0][0] = 1.0;
      double ans = 0;
      for (int i = 0; i < Y; i++) {
        for (int j = 0; j < MONEY_MAX; j++) {
          if (dp[i][j] == 0)
            continue;
          for (int k = 0; k < X; k++) {
            cout << ans << endl;
            if (i + V[k] >= Y) {
              ans += dp[i][j] * j / (double)X;
            } else if (event[i + V[k]].first == 1) {
              if (i + V[k] + event[i + V[k]].second >= Y)
                ans += dp[i][j] * j / (double)X;
              else
                dp[i + V[k] + event[i + V[k]].second][j] +=
                    dp[i][j] / (double)X;
            } else if (event[i + V[k]].first == 2) {
              dp[i + V[k]][j + event[i + V[k]].second] += dp[i][j] / (double)X;
            } else if (event[i + V[k]].first == 3) {
              if (j <= event[i + V[k]].second)
                dp[i + V[k]][0] += dp[i][j] / (double)X;
              else
                dp[i + V[k]][j - event[i + V[k]].second] +=
                    dp[i][j] / (double)X;
            } else {
              dp[i + V[k]][j] += dp[i][j] / (double)X;
            }
          }
        }
      }
      cout << (int)ans << endl;
    }
  }
}

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

#define MONEY_MAX 4901

typedef pair<int, int> pii;

int main() {
  while (1) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0) {
      break;
    } else {
      int V[X];
      for (int i = 0; i < X; i++)
        cin >> V[i];
      pii event[Y + 1];
      fill(&event[0].first, &event[Y + 1].first, -1);
      for (int i = 0; i < Z; i++) {
        int N;
        cin >> N;
        cin >> event[N].first >> event[N].second;
      }
      double dp[Y + 1][MONEY_MAX];
      fill(&dp[0][0], &dp[Y + 1][0], 0.0);
      dp[0][0] = 1.0;
      double ans = 0;
      for (int i = 0; i < Y; i++) {
        for (int j = 0; j < MONEY_MAX; j++) {
          if (dp[i][j] == 0)
            continue;
          for (int k = 0; k < X; k++) {
            if (i + V[k] >= Y) {
              ans += dp[i][j] * j / (double)X;
            } else if (event[i + V[k]].first == 1) {
              if (i + V[k] + event[i + V[k]].second >= Y)
                ans += dp[i][j] * j / (double)X;
              else
                dp[i + V[k] + event[i + V[k]].second][j] +=
                    dp[i][j] / (double)X;
            } else if (event[i + V[k]].first == 2) {
              dp[i + V[k]][j + event[i + V[k]].second] += dp[i][j] / (double)X;
            } else if (event[i + V[k]].first == 3) {
              if (j <= event[i + V[k]].second)
                dp[i + V[k]][0] += dp[i][j] / (double)X;
              else
                dp[i + V[k]][j - event[i + V[k]].second] +=
                    dp[i][j] / (double)X;
            } else {
              dp[i + V[k]][j] += dp[i][j] / (double)X;
            }
          }
        }
      }
      cout << (int)ans << endl;
    }
  }
}