Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cin.exceptions(cin.failbit)
#define check(x) cout << #x << " : " << (x) << endl
#define all(v) v.begin(), v.end()
#define endl '\n'
#define show(a)                                                                \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << endl
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> mypair;
typedef priority_queue<int> max_heap;
typedef priority_queue<mypair, vector<mypair>, greater<mypair>> min_heap;
const int mod = 1000000007;
const int inf = 1e9 + 100;
const int N = 300;
ld dp[N + 1][N + 1][N + 1];
int a[4];

int main() {
  boost;
  ld n;
  cin >> n;

  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i or j or k)
          dp[i][j][k] =
              (n / (ld(i + j + k))) +
              ((k > 0) ? (dp[i][j + 1][k - 1] * k) / (ld(i + j + k)) : 0.0) +
              ((j > 0) ? (dp[i + 1][j - 1][k] * j) / (ld(i + j + k)) : 0.0) +
              ((i > 0) ? (dp[i - 1][j][k] * i) / (ld(i + j + k)) : 0.00);
        // cout<<i<<" "<<j<<" "<<k<<" :
        // "<<fixed<<setprecision(15)<<dp[i][j][k]<<endl;
        // check((n/(ld(i+j+k))));
      }
    }
  }
  int cn = int(n);
  while (cn--) {
    int t;
    cin >> t;
    a[t]++;
  }
  cout << fixed << setprecision(15) << dp[a[1]][a[2]][a[3]];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cin.exceptions(cin.failbit)
#define check(x) cout << #x << " : " << (x) << endl
#define all(v) v.begin(), v.end()
#define endl '\n'
#define show(a)                                                                \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << endl
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> mypair;
typedef priority_queue<int> max_heap;
typedef priority_queue<mypair, vector<mypair>, greater<mypair>> min_heap;
const int mod = 1000000007;
const int inf = 1e9 + 100;
const int N = 315;
ld dp[N + 1][N + 1][N + 1];
int a[4];

int main() {
  boost;
  ld n;
  cin >> n;

  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i or j or k)
          dp[i][j][k] =
              (n / (ld(i + j + k))) +
              ((k > 0) ? (dp[i][j + 1][k - 1] * k) / (ld(i + j + k)) : 0.0) +
              ((j > 0) ? (dp[i + 1][j - 1][k] * j) / (ld(i + j + k)) : 0.0) +
              ((i > 0) ? (dp[i - 1][j][k] * i) / (ld(i + j + k)) : 0.00);
        // cout<<i<<" "<<j<<" "<<k<<" :
        // "<<fixed<<setprecision(15)<<dp[i][j][k]<<endl;
        // check((n/(ld(i+j+k))));
      }
    }
  }
  int cn = int(n);
  while (cn--) {
    int t;
    cin >> t;
    a[t]++;
  }
  cout << fixed << setprecision(15) << dp[a[1]][a[2]][a[3]];

  return 0;
}