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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

vector<int> dices;
struct masu {
  int num;
  int type;
  int a;
};
map<int, masu> mp;
int X, Y, Z;
vector<vector<ld>> memo;

ld getans(const int nowplace, const int nowmoney) {
  if (memo[nowplace][nowmoney] < -0.5) {
    ld ans = 0;

    for (auto di : dices) {
      int nextmoney(nowmoney);
      int next = nowplace + di;
      if (next < Y) {
        if (mp.find(next) != mp.end()) {
          auto it = mp.find(next);
          masu action(it->second);
          if (action.type == 1) {
            next += action.a;
          } else if (action.type == 2) {
            nextmoney += action.a;
          } else {
            nextmoney -= action.a;
            nextmoney = max(nextmoney, 0);
          }
        }
      }
      if (next < Y) {
        ans += getans(next, nextmoney);
      } else {
        ans += nextmoney;
      }
    }
    memo[nowplace][nowmoney] = ans / dices.size();
  }
  return memo[nowplace][nowmoney];
}
int main() {
  while (1) {
    cin >> X >> Y >> Z;
    if (!X)
      break;
    memo = vector<vector<ld>>(Y, vector<ld>(10000, -1));
    dices.clear();
    for (int i = 0; i < X; ++i) {
      int v;
      cin >> v;
      dices.emplace_back(v);
    }
    mp.clear();
    for (int i = 0; i < Z; ++i) {
      int n, e, a;
      cin >> n >> e >> a;
      mp[n] = masu{n, e, a};
    }
    ld ans = getans(0, 0);
    cout << int(ans) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

vector<int> dices;
struct masu {
  int num;
  int type;
  int a;
};
map<int, masu> mp;
int X, Y, Z;
vector<vector<ld>> memo;

ld getans(const int nowplace, const int nowmoney) {
  if (memo[nowplace][nowmoney] < -0.5) {
    ld ans = 0;

    for (auto di : dices) {
      int nextmoney(nowmoney);
      int next = nowplace + di;
      if (next < Y) {
        if (mp.find(next) != mp.end()) {
          auto it = mp.find(next);
          masu action(it->second);
          if (action.type == 1) {
            next += action.a;
          } else if (action.type == 2) {
            nextmoney += action.a;
          } else {
            nextmoney -= action.a;
            nextmoney = max(nextmoney, 0);
          }
        }
      }
      if (next < Y) {
        ans += getans(next, nextmoney);
      } else {
        ans += nextmoney;
      }
    }
    memo[nowplace][nowmoney] = ans / dices.size();
  }
  return memo[nowplace][nowmoney];
}
int main() {
  while (1) {
    cin >> X >> Y >> Z;
    if (!X)
      break;
    memo = vector<vector<ld>>(Y, vector<ld>(100000, -1));
    dices.clear();
    for (int i = 0; i < X; ++i) {
      int v;
      cin >> v;
      dices.emplace_back(v);
    }
    mp.clear();
    for (int i = 0; i < Z; ++i) {
      int n, e, a;
      cin >> n >> e >> a;
      mp[n] = masu{n, e, a};
    }
    ld ans = getans(0, 0);
    cout << int(ans) << endl;
  }
  return 0;
}