Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
const int MAX = 210000;
const LL base = 1e9 + 7;

struct Node {
  int x, y, w;
};

double dp[301][301][301];
int za[301];
int zt[4];
void solve() {

  double result = 0;
  int n;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> za[i];
    zt[za[i]]++;
  }

  dp[0][0][0] = 0;

  for (int k = 0; k <= zt[3]; ++k) {
    for (int j = 0; j <= zt[2] + zt[3]; ++j) {
      for (int i = 0; i <= n; ++i) {
        int cc = i + j + k;
        if (cc == 0)
          continue;
        double tt = 0;
        if (i > 0) {
          tt += double(i) / n * dp[i - 1][j][k];
        }
        if (j > 0) {
          tt += double(j) / n * dp[i + 1][j - 1][k];
        }
        if (k > 0) {
          tt += double(k) / n * dp[i][j + 1][k - 1];
        }
        dp[i][j][k] = (1 + tt) * n / cc;
      }
    }
  }
  result = dp[zt[1]][zt[2]][zt[3]];
  cout << setprecision(12) << result << endl;
}

int main() {
  const int inId = 0;
  const int outId = 0;
  if (inId > 0) {
    cerr << "redirect stdin to input " << inId << endl;
    string s = "input";
    s += to_string(inId);
    s += ".txt";
    auto r = freopen(s.c_str(), "r", stdin);
    if (r == nullptr) {
      cerr << "invalid input file " << s;
    }
  }
  if (outId > 0) {
    cerr << "redirect stdout to stdout " << outId << endl;
    string s = "stdout";
    s += to_string(outId);
    s += ".txt";
    auto r = freopen(s.c_str(), "w", stdout);
    if (r == nullptr) {
      cerr << "invalid output file " << s;
    }
  }
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0); // close sync between cout and printf
  cout.precision(10);
  solve();
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
const int MAX = 210000;
const LL base = 1e9 + 7;

struct Node {
  int x, y, w;
};

double dp[301][301][301];
int za[301];
int zt[4];
void solve() {

  double result = 0;
  int n;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> za[i];
    zt[za[i]]++;
  }

  dp[0][0][0] = 0;

  for (int k = 0; k <= zt[3]; ++k) {
    for (int j = 0; j <= zt[2] + zt[3]; ++j) {
      for (int i = 0; i <= n; ++i) {
        int cc = i + j + k;
        if (cc == 0)
          continue;
        if (cc > n)
          continue;
        double tt = 0;
        if (i > 0) {
          tt += double(i) / n * dp[i - 1][j][k];
        }
        if (j > 0) {
          tt += double(j) / n * dp[i + 1][j - 1][k];
        }
        if (k > 0) {
          tt += double(k) / n * dp[i][j + 1][k - 1];
        }
        dp[i][j][k] = (1 + tt) * n / cc;
      }
    }
  }
  result = dp[zt[1]][zt[2]][zt[3]];
  cout << setprecision(12) << result << endl;
}

int main() {
  const int inId = 0;
  const int outId = 0;
  if (inId > 0) {
    cerr << "redirect stdin to input " << inId << endl;
    string s = "input";
    s += to_string(inId);
    s += ".txt";
    auto r = freopen(s.c_str(), "r", stdin);
    if (r == nullptr) {
      cerr << "invalid input file " << s;
    }
  }
  if (outId > 0) {
    cerr << "redirect stdout to stdout " << outId << endl;
    string s = "stdout";
    s += to_string(outId);
    s += ".txt";
    auto r = freopen(s.c_str(), "w", stdout);
    if (r == nullptr) {
      cerr << "invalid output file " << s;
    }
  }
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0); // close sync between cout and printf
  cout.precision(10);
  solve();
}