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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
int ror, mas, eve;
int N[50];
int E[50];
int A[50];
int V[4];
double memo[50][5000];
double solve(int go, int money) {
  REP(i, eve) {
    if (N[i] == go) {
      if (E[i] == 1) {
        go += A[i];
      }
      if (E[i] == 2) {
        money += A[i];
      }
      if (E[i] == 3) {
        money -= A[i];
        if (money < 0)
          money = 0;
      }
      break;
    }
  }
  if (go >= mas)
    return money;
  if (money < 5000) {
    if (memo[go][money] != -1) {
      return memo[go][money];
    }
  } else if (money > 5000) {
    abort();
  }
  double ret = 0;
  REP(i, ror) { ret += solve(go + V[i], money) / ror; }
  return memo[go][money] = ret;
}

int main(void) {
  while (cin >> ror >> mas >> eve, ror || mas || eve) {
    // memset(memo,0,sizeof(memo));
    REP(i, 50) REP(j, 5000) memo[i][j] = -1;
    REP(i, ror) cin >> V[i];
    REP(i, eve) { cin >> N[i] >> E[i] >> A[i]; }
    cout << (int)(solve(0, 0) + EPS) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
int ror, mas, eve;
int N[50];
int E[50];
int A[50];
int V[4];
double memo[50][5000][50];
double solve(int go, int money, int turn) {
  REP(i, eve) {
    if (N[i] == go) {
      if (E[i] == 1) {
        go += A[i];
      }
      if (E[i] == 2) {
        money += A[i];
      }
      if (E[i] == 3) {
        money -= A[i];
        if (money < 0)
          money = 0;
      }
      break;
    }
  }
  if (go >= mas)
    return money / pow((double)ror, turn);
  if (money < 5000) {
    if (memo[go][money][turn] != -1) {
      return memo[go][money][turn];
    }
  } else if (money > 5000) {
    abort();
  }
  double ret = 0;
  REP(i, ror) { ret += solve(go + V[i], money, turn + 1); }
  return memo[go][money][turn] = ret;
}

int main(void) {
  while (cin >> ror >> mas >> eve, ror || mas || eve) {
    // memset(memo,0,sizeof(memo));
    REP(i, 50) REP(j, 5000) REP(k, 50) memo[i][j][k] = -1;
    REP(i, ror) cin >> V[i];
    REP(i, eve) { cin >> N[i] >> E[i] >> A[i]; }
    cout << (int)(solve(0, 0, 0) + EPS) << endl;
  }
  return 0;
}