Problem p03169 (C++)

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

typedef long long ll;
typedef vector<long long> vi;
typedef vector<vi> vvi;
typedef pair<long, long> ii;
#define sz(a) long((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i)            for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define input(v, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
  cin >> v[i]
#define output(v, n)                                                           \
  for (ll i = 0; i < n; i++)                                                   \
  cout << v[i] << " "

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

double pro[302][302][302];
double ex[302][302][302];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll one = 0, two = 0, three = 0;

  ll n, i, j, k;
  cin >> n;
  for (i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x == 1)
      one++;
    if (x == 2)
      two++;
    if (x == 3)
      three++;
  }

  pro[three][two][one] = 1;

  for (i = n; i >= 0; i--) {
    for (j = n; j >= 0; j--) {
      for (k = n; k >= 0; k--) {
        if (i == 0 && k == 0 && j == 0)
          continue;
        if (pro[i][j][k] == 0)
          continue;
        // debug(i,j,k,ex[i][j][k],pro[i][j][k]);

        double pw = 1.0 * (n - i - j - k) / n;
        pw = pw / (1 - pw);

        ex[i][j][k] /= pro[i][j][k];
        ex[i][j][k] += pw;
        if (i) {
          pro[i - 1][j + 1][k] += pro[i][j][k] * (i) / (i + j + k);
          ex[i - 1][j + 1][k] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (i) / (i + j + k));
        }

        if (j) {
          pro[i][j - 1][k + 1] += pro[i][j][k] * (j) / (i + j + k);
          ex[i][j - 1][k + 1] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (j) / (i + j + k));
        }

        if (k) {
          pro[i][j][k - 1] += pro[i][j][k] * (k) / (i + j + k);
          ex[i][j][k - 1] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (k) / (i + j + k));
        }
        debug(i, j, k, ex[i][j][k], pro[i][j][k]);
      }
    }
  }

  cout << fixed << setprecision(10) << ex[0][0][0];

  return 0;
}


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

typedef long long ll;
typedef vector<long long> vi;
typedef vector<vi> vvi;
typedef pair<long, long> ii;
#define sz(a) long((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i)            for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define input(v, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
  cin >> v[i]
#define output(v, n)                                                           \
  for (ll i = 0; i < n; i++)                                                   \
  cout << v[i] << " "

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

double pro[302][302][302];
double ex[302][302][302];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll one = 0, two = 0, three = 0;

  ll n, i, j, k;
  cin >> n;
  for (i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x == 1)
      one++;
    if (x == 2)
      two++;
    if (x == 3)
      three++;
  }

  pro[three][two][one] = 1;

  for (i = n; i >= 0; i--) {
    for (j = n; j >= 0; j--) {
      for (k = n; k >= 0; k--) {
        if (i == 0 && k == 0 && j == 0)
          continue;
        if (pro[i][j][k] == 0)
          continue;
        // debug(i,j,k,ex[i][j][k],pro[i][j][k]);

        double pw = 1.0 * (n - i - j - k) / n;
        pw = pw / (1 - pw);

        ex[i][j][k] /= pro[i][j][k];
        ex[i][j][k] += pw;
        if (i) {
          pro[i - 1][j + 1][k] += pro[i][j][k] * (i) / (i + j + k);
          ex[i - 1][j + 1][k] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (i) / (i + j + k));
        }

        if (j) {
          pro[i][j - 1][k + 1] += pro[i][j][k] * (j) / (i + j + k);
          ex[i][j - 1][k + 1] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (j) / (i + j + k));
        }

        if (k) {
          pro[i][j][k - 1] += pro[i][j][k] * (k) / (i + j + k);
          ex[i][j][k - 1] +=
              (ex[i][j][k] + 1) * (pro[i][j][k] * (k) / (i + j + k));
        }
        //  debug(i,j,k,ex[i][j][k],pro[i][j][k]);
      }
    }
  }

  cout << fixed << setprecision(10) << ex[0][0][0];

  return 0;
}