Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef double ll;
int main() {
  int n;
  cin >> n;
  int a1 = 0, a2 = 0, a3 = 0;
  ll dp[n + 1][n + 1][n + 1];
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    if (x == 1)
      a1++;
    if (x == 2)
      a2++;
    if (x == 3)
      a3++;
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    dp[i][0][0] = (dp[i - 1][0][0] + 1) + (ll)((ll)(n - i) / i);
  }
  for (int j = 1; j <= n; ++j) {
    for (int i = 0; i <= n; ++i) {
      if (i > 0)
        dp[i][j][0] = (n - i - j + (dp[i - 1][j][0] + 1) * (i) +
                       (dp[i + 1][j - 1][0] + 1) * (j)) /
                      ((ll)(i + j));
      else
        dp[i][j][0] = (n - j + (dp[i + 1][j - 1][0] + 1) * (j)) / ((ll)j);
    }
  }
  for (int k = 1; k <= n; ++k) {
    for (int j = 0; j < n; ++j) {
      for (int i = 0; i < n; ++i) {
        if (i > 0 && j > 0)
          dp[i][j][k] = (n - i - j - k + (dp[i - 1][j][k] + 1) * (i) +
                         (dp[i + 1][j - 1][k] + 1) * (j) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(i + k + j));
        else if (i > 0)
          dp[i][j][k] = (n - i - k + (dp[i - 1][j][k] + 1) * (i) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(i + k));
        else if (j > 0)
          dp[i][j][k] = (n - j - k + (dp[i + 1][j - 1][k] + 1) * (j) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(k + j));
        else
          dp[i][j][k] = (n - k + (dp[i][j + 1][k - 1] + 1) * (k)) / ((ll)(k));
      }
    }
  }
  printf("%0.15lf\n", dp[a1][a2][a3]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef double ll;
int main() {
  int n;
  cin >> n;
  int a1 = 0, a2 = 0, a3 = 0;
  ll dp[n + 1][n + 1][n + 1];
  for (int i = 0; i < n; ++i) {
    ll x;
    cin >> x;
    if (x == 1)
      a1++;
    if (x == 2)
      a2++;
    if (x == 3)
      a3++;
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    dp[i][0][0] = (dp[i - 1][0][0] + 1) + (ll)((ll)(n - i) / i);
  }
  for (int j = 1; j <= n; ++j) {
    for (int i = 0; i < n; ++i) {
      if (i > 0)
        dp[i][j][0] = (n - i - j + (dp[i - 1][j][0] + 1) * (i) +
                       (dp[i + 1][j - 1][0] + 1) * (j)) /
                      ((ll)(i + j));
      else
        dp[i][j][0] = (n - j + (dp[i + 1][j - 1][0] + 1) * (j)) / ((ll)j);
    }
  }
  for (int k = 1; k <= n; ++k) {
    for (int j = 0; j < n; ++j) {
      for (int i = 0; i < n; ++i) {
        if (i > 0 && j > 0)
          dp[i][j][k] = (n - i - j - k + (dp[i - 1][j][k] + 1) * (i) +
                         (dp[i + 1][j - 1][k] + 1) * (j) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(i + k + j));
        else if (i > 0)
          dp[i][j][k] = (n - i - k + (dp[i - 1][j][k] + 1) * (i) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(i + k));
        else if (j > 0)
          dp[i][j][k] = (n - j - k + (dp[i + 1][j - 1][k] + 1) * (j) +
                         (dp[i][j + 1][k - 1] + 1) * (k)) /
                        ((ll)(k + j));
        else
          dp[i][j][k] = (n - k + (dp[i][j + 1][k - 1] + 1) * (k)) / ((ll)(k));
      }
    }
  }
  printf("%0.15lf\n", dp[a1][a2][a3]);
}