Problem p03169 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>            // [PRIMES]               1777 ~2^10.80
using namespace std;                //                       10333 ~2^13.33
using ll = long long;               // seq 1 128 | factor   100333 ~2^16.61
using vl = vector<ll>;              //   | grep -v ' .* '  1300111 ~2^20.31
using vvl = vector<vl>;             //                    10300777 ~2^23.30
using vb = vector<bool>;            //                   100400999 ~2^26.58
using vs = vector<string>;          //                  1300400999 ~2^30.28
using pll = pair<ll, ll>;           //                 10200500333 ~2^33.25
const ll oo = 0x3f3f3f3f3f3f3f3fLL; //                100200400777 ~2^36.54
const double eps = 1e-9;            //               1200300700111 ~2^40.13
#define sz(c) ll((c).size())        //              10200300500777 ~2^43.21
#define all(c) begin(c), end(c)     //             100200300400777 ~2^46.51
#define mp make_pair                //            1200300400600999 ~2^50.09
#define mt make_tuple               //           10200300400600111 ~2^53.18
#define pb push_back                //          100200300400600333 ~2^56.48
#define eb emplace_back             //         1200300400500800999 ~2^60.06
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

ll N;
vector<vector<vector<double>>> dp;

double r(double p, ll d) {
  if (d == 100)
    return p;
  return p + (1 - p) * r(p, d + 1);
}

double f(vl in) {
  if (in[0] == N)
    return 0;
  double *dpp = &dp[in[1]][in[2]][in[3]];
  if (*dpp > -0.5)
    return *dpp;
  double p_null = (double)in[0] / (double)N;
  double acc = (1.0 / (1.0 - p_null));
  FOR(i, 1, sz(in)) {
    if (in[i] == 0)
      continue;
    double p = ((double)in[i] / (double)(N - in[0]));
    vl newin = in;
    newin[i]--;
    newin[i - 1]++;
    acc += f(newin) * p;
  }
  *dpp = acc;
  return acc;
}

int main() {
  ll n;
  cin >> n;
  N = 0;
  vl a;
  dp.resize(900 + 1,
            vector<vector<double>>(600 + 1, vector<double>(300 + 1, -1)));
  a.resize(4, 0);
  FOR(i, 0, n) {
    ll in;
    cin >> in;
    a[in]++;
    N = n;
  }
  cout << fixed << setprecision(10) << f(a) << endl;
} // cin.tie(0) bei schnellem Wechseln


Fixed submission:
#include <bits/stdc++.h>            // [PRIMES]               1777 ~2^10.80
using namespace std;                //                       10333 ~2^13.33
using ll = long long;               // seq 1 128 | factor   100333 ~2^16.61
using vl = vector<ll>;              //   | grep -v ' .* '  1300111 ~2^20.31
using vvl = vector<vl>;             //                    10300777 ~2^23.30
using vb = vector<bool>;            //                   100400999 ~2^26.58
using vs = vector<string>;          //                  1300400999 ~2^30.28
using pll = pair<ll, ll>;           //                 10200500333 ~2^33.25
const ll oo = 0x3f3f3f3f3f3f3f3fLL; //                100200400777 ~2^36.54
const double eps = 1e-9;            //               1200300700111 ~2^40.13
#define sz(c) ll((c).size())        //              10200300500777 ~2^43.21
#define all(c) begin(c), end(c)     //             100200300400777 ~2^46.51
#define mp make_pair                //            1200300400600999 ~2^50.09
#define mt make_tuple               //           10200300400600111 ~2^53.18
#define pb push_back                //          100200300400600333 ~2^56.48
#define eb emplace_back             //         1200300400500800999 ~2^60.06
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

ll N;
vector<vector<vector<double>>> dp;

double r(double p, ll d) {
  if (d == 100)
    return p;
  return p + (1 - p) * r(p, d + 1);
}

double f(vl in) {
  if (in[0] == N)
    return 0;
  double *dpp = &dp[in[1]][in[2]][in[3]];
  if (*dpp > -0.5)
    return *dpp;
  double p_null = (double)in[0] / (double)N;
  double acc = (1.0 / (1.0 - p_null));
  FOR(i, 1, sz(in)) {
    if (in[i] == 0)
      continue;
    double p = ((double)in[i] / (double)(N - in[0]));
    vl newin = in;
    newin[i]--;
    newin[i - 1]++;
    acc += f(newin) * p;
  }
  *dpp = acc;
  return acc;
}

int main() {
  ll n;
  cin >> n;
  N = 0;
  vl a;
  dp.resize(300 + 1,
            vector<vector<double>>(300 + 1, vector<double>(300 + 1, -1)));
  a.resize(4, 0);
  FOR(i, 0, n) {
    ll in;
    cin >> in;
    a[in]++;
    N = n;
  }
  cout << fixed << setprecision(10) << f(a) << endl;
} // cin.tie(0) bei schnellem Wechseln