Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<pi>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define R cin >>
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout << (p) << endl;
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
void sonic() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-11;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> void co(T e) { cout << e << "\n"; }
template <typename T> void co(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << "\n";
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a * b / g;
}
bool prime(ll n) {
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

Vec fac, finv;

ll PowMod(ll a, ll n) {
  if (n == 1)
    return a;
  if (n % 2 == 0)
    return PowMod(a * a % MOD, n / 2);
  return a * PowMod(a * a % MOD, n / 2) % MOD;
}

ll combi(ll n, ll k) {
  if (k > n)
    return 0;
  return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}

double memo[300][300][300];
double f(int i, int j, int k, int n) {
  if (i + j + k == 0)
    return 0.0;
  if (memo[i][j][k] != 0.0)
    return memo[i][j][k];
  double s = i + j + k;
  double ret = n - s;
  if (i)
    ret += (f(i - 1, j, k, n) + 1) * i;
  if (j)
    ret += (f(i + 1, j - 1, k, n) + 1) * j;
  if (k)
    ret += (f(i, j + 1, k - 1, n) + 1) * k;
  ret /= s;
  memo[i][j][k] = ret;
  return ret;
}

int main() {
  double n, r, a = 0, b = 0, c = 0;
  cin >> n;
  rep(i, n) {
    cin >> r;
    if (r == 1)
      a++;
    else if (r == 2)
      b++;
    else
      c++;
  }
  cout << fixed << setprecision(15) << f(a, b, c, n) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<pi>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define R cin >>
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout << (p) << endl;
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
void sonic() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-11;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> void co(T e) { cout << e << "\n"; }
template <typename T> void co(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << "\n";
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a * b / g;
}
bool prime(ll n) {
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

Vec fac, finv;

ll PowMod(ll a, ll n) {
  if (n == 1)
    return a;
  if (n % 2 == 0)
    return PowMod(a * a % MOD, n / 2);
  return a * PowMod(a * a % MOD, n / 2) % MOD;
}

ll combi(ll n, ll k) {
  if (k > n)
    return 0;
  return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}

double memo[310][310][310];
double f(int i, int j, int k, int n) {
  if (i + j + k == 0)
    return 0.0;
  if (memo[i][j][k] != 0.0)
    return memo[i][j][k];
  double s = i + j + k;
  double ret = n - s;
  if (i)
    ret += (f(i - 1, j, k, n) + 1) * i;
  if (j)
    ret += (f(i + 1, j - 1, k, n) + 1) * j;
  if (k)
    ret += (f(i, j + 1, k - 1, n) + 1) * k;
  ret /= s;
  memo[i][j][k] = ret;
  return ret;
}

int main() {
  double n, r, a = 0, b = 0, c = 0;
  cin >> n;
  rep(i, n) {
    cin >> r;
    if (r == 1)
      a++;
    else if (r == 2)
      b++;
    else
      c++;
  }
  cout << fixed << setprecision(15) << f(a, b, c, n) << endl;
}