Problem p03169 (C++)

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

#define ll long long
#define mod 1e9 + 7
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define vll vector<ll>
#define rep(i, j, n) for (int i = j; i < n; i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
priority_queue<pll, vector<pll>, greater<pll>> pq;
priority_queue<ll, vector<ll>, greater<ll>> pq1;

ll n;
double dp[301][301][301];
ll arr[301];

double solve(ll a, ll b, ll c) {
  // cout<<a<<" "<<b<<" "<<c<<"\n";
  if (a < 0 || b < 0 || c < 0)
    return 0;
  if (a == 0 && b == 0 && c == 0)
    return 0;

  if (dp[a][b][c] != -1)
    return dp[a][b][c];

  double p = (1.0) * (a + b + c);

  dp[a][b][c] = 0.0;

  dp[a][b][c] = (a / p) * solve(a - 1, b, c) +
                (b / p) * solve(a + 1, b - 1, c) +
                (c / p) * solve(a, b + 1, c - 1) + (double)n / p;
  return dp[a][b][c];
}

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // ll n;
  cin >> n;
  ll a = 0, b = 0, c = 0;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1)
      a++;
    else if (arr[i] == 2)
      b++;
    else
      c++;
  }
  for (int i = 0; i < 301; i++) {
    for (int j = 0; j < 301; j++) {
      for (int k = 0; k < 301; k++)
        dp[i][j][k] = -1.0;
    }
  }
  // memset(dp,-1.00,sizeof(dp));
  // dp[0][0][0]=(1.0);
  // dp[1][0][0]=(1.0);
  double ans = solve(a, b, c);
  cout << setprecision(27) << ans;
  return 0;
}


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

#define ll long long
#define mod 1e9 + 7
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define vll vector<ll>
#define rep(i, j, n) for (int i = j; i < n; i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
priority_queue<pll, vector<pll>, greater<pll>> pq;
priority_queue<ll, vector<ll>, greater<ll>> pq1;

ll n;
double dp[301][301][301];
ll arr[301];

double solve(ll a, ll b, ll c) {
  // cout<<a<<" "<<b<<" "<<c<<"\n";
  if (a < 0 || b < 0 || c < 0)
    return 0;
  if (a == 0 && b == 0 && c == 0)
    return 0;

  if (dp[a][b][c] != -1)
    return dp[a][b][c];

  double p = (1.0) * (a + b + c);

  dp[a][b][c] = 0.0;

  dp[a][b][c] = (a / p) * solve(a - 1, b, c) +
                (b / p) * solve(a + 1, b - 1, c) +
                (c / p) * solve(a, b + 1, c - 1) + (double)n / p;
  return dp[a][b][c];
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // ll n;
  cin >> n;
  ll a = 0, b = 0, c = 0;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1)
      a++;
    else if (arr[i] == 2)
      b++;
    else
      c++;
  }
  for (int i = 0; i < 301; i++) {
    for (int j = 0; j < 301; j++) {
      for (int k = 0; k < 301; k++)
        dp[i][j][k] = -1.0;
    }
  }
  // memset(dp,-1.00,sizeof(dp));
  // dp[0][0][0]=(1.0);
  // dp[1][0][0]=(1.0);
  double ans = solve(a, b, c);
  cout << setprecision(27) << ans;
  return 0;
}