Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;

template <class T> inline T sqr(T x) { return x * x; }

double dp[101][101][101] = {};

double solve(int c1, int c2, int c3, int n) {
  if (c1 <= 0 || c2 < 0 || c3 < 0)
    return 0.0;
  if (c1 < c2 || c2 < c3 || c1 < c3)
    return 0.0;
  if (dp[c1][c2][c3] != 0)
    return dp[c1][c2][c3];

  int sum = c1;
  if (sum == 1 && c1 - c2 == 1)
    return dp[c1][c2][c3] = 1.0 * n;
  return dp[c1][c2][c3] = (solve(c1 - 1, c2, c3, n) * (double)(c1 - c2) / n +
                           solve(c1, c2 - 1, c3, n) * (double)(c2 - c3) / n +
                           solve(c1, c2, c3 - 1, n) * (double)c3 / n + 1) *
                          n / sum;
}

void solve() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, 0, n) cin >> a[i];
  int cnt[3] = {};
  REP(i, 0, n) REP(j, 0, a[i]) cnt[j]++;
  // cout<< solve(cnt[0], cnt[1], cnt[2], n) << endl;
  printf("%.12lf\n", solve(cnt[0], cnt[1], cnt[2], n));
}

int main() {
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;

template <class T> inline T sqr(T x) { return x * x; }

double dp[301][301][301] = {};

double solve(int c1, int c2, int c3, int n) {
  if (c1 <= 0 || c2 < 0 || c3 < 0)
    return 0.0;
  if (c1 < c2 || c2 < c3 || c1 < c3)
    return 0.0;
  if (dp[c1][c2][c3] != 0)
    return dp[c1][c2][c3];

  int sum = c1;
  if (sum == 1 && c1 - c2 == 1)
    return dp[c1][c2][c3] = 1.0 * n;
  return dp[c1][c2][c3] = (solve(c1 - 1, c2, c3, n) * (double)(c1 - c2) / n +
                           solve(c1, c2 - 1, c3, n) * (double)(c2 - c3) / n +
                           solve(c1, c2, c3 - 1, n) * (double)c3 / n + 1) *
                          n / sum;
}

void solve() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, 0, n) cin >> a[i];
  int cnt[3] = {};
  REP(i, 0, n) REP(j, 0, a[i]) cnt[j]++;
  // cout<< solve(cnt[0], cnt[1], cnt[2], n) << endl;
  printf("%.12lf\n", solve(cnt[0], cnt[1], cnt[2], n));
}

int main() {
  solve();

  return 0;
}