prithivMLmods/Coder-Stat huggingface dataset:
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
double dp[301][301][301];
int n, a[4];

int main() {
  cin >> n;
  for (int i = 0; i < n; i += 1) {
    int b;
    cin >> b;
    a[b] += 1;
  }
  for (int sum = 1; sum <= a[1] + a[2] * 2 + a[3] * 3; sum += 1) {
    for (int k = 0; 3 * k <= sum && k <= n; k += 1) {
      for (int j = 0; 3 * k + 2 * j <= sum && j <= n; j += 1) {
        int i = sum - 2 * j - 3 * k;
        if (i > n) {
          continue;
        }
        dp[i][j][k] = ((i > 0 ? i * (1.0 + dp[i - 1][j][k]) : 0.0) +
                       (j > 0 ? j * (1.0 + dp[i + 1][j - 1][k]) : 0.0) +
                       (k > 0 ? k * (1.0 + dp[i][j + 1][k - 1]) : 0.0) +
                       double(n - i - j - k)) /
                      double(i + j + k);
      }
    }
  }
  cout << setprecision(9) << fixed;
  cout << dp[a[1]][a[2]][a[3]] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
double dp[303][303][303];
int n, a[4];

int main() {
  cin >> n;
  for (int i = 0; i < n; i += 1) {
    int b;
    cin >> b;
    a[b] += 1;
  }
  for (int sum = 1; sum <= a[1] + a[2] * 2 + a[3] * 3; sum += 1) {
    for (int k = 0; 3 * k <= sum && k <= n; k += 1) {
      for (int j = 0; 3 * k + 2 * j <= sum && j <= n; j += 1) {
        int i = sum - 2 * j - 3 * k;
        if (i > n) {
          continue;
        }
        dp[i][j][k] = ((i > 0 ? i * (1.0 + dp[i - 1][j][k]) : 0.0) +
                       (j > 0 ? j * (1.0 + dp[i + 1][j - 1][k]) : 0.0) +
                       (k > 0 ? k * (1.0 + dp[i][j + 1][k - 1]) : 0.0) +
                       double(n - i - j - k)) /
                      double(i + j + k);
      }
    }
  }
  cout << setprecision(9) << fixed;
  cout << dp[a[1]][a[2]][a[3]] << endl;
}
Problem p03169 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> s(3);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s.at(a - 1)++;
  }
  vector<vector<vector<double>>> dp(
      n + 1, vector<vector<double>>(n + 1, vector<double>(n + 1)));
  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i <= n; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        if (i)
          dp[i][j][k] += i * dp[i - 1][j][k] / (i + j + k);
        if (j)
          dp[i][j][k] += j * dp[i + 1][j - 1][k] / (i + j + k);
        if (k)
          dp[i][j][k] += k * dp[i][j + 1][k - 1] / (i + j + k);
        dp[i][j][k] += 1.0 * n / (i + j + k);
      }
    }
  }
  cout << setprecision(17) << dp[s.at(0)][s.at(1)][s.at(2)] << endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> s(3);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s.at(a - 1)++;
  }
  vector<vector<vector<double>>> dp(
      n + 1, vector<vector<double>>(n + 1, vector<double>(n + 1)));
  for (int k = 0; k <= n; k++) {
    for (int j = 0; j <= n - k; j++) {
      for (int i = 0; i <= n - k - j; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        if (i)
          dp[i][j][k] += i * dp[i - 1][j][k] / (i + j + k);
        if (j)
          dp[i][j][k] += j * dp[i + 1][j - 1][k] / (i + j + k);
        if (k)
          dp[i][j][k] += k * dp[i][j + 1][k - 1] / (i + j + k);
        dp[i][j][k] += 1.0 * n / (i + j + k);
      }
    }
  }
  cout << setprecision(17) << dp[s.at(0)][s.at(1)][s.at(2)] << endl;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define pb push_back
#define ALL(s) s.begin(), s.end()
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SZ(n) ((int)(n).size())
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

double dp[301][301][301];
ll n;
bool done[301][301][301];

double solve(int on, int tw, int th) {
  if (on + tw + th == 0)
    return 0;
  double &r = dp[on][tw][th];
  if (done[on][tw][th])
    return r;
  double tot = on + tw + th;
  r = (double)n / tot;
  if (on)
    r += (double)on / tot * solve(on - 1, tw, th);
  if (tw)
    r += (double)tw / tot * solve(on + 1, tw - 1, th);
  if (th)
    r += (double)th / tot * solve(on, tw + 1, th - 1);
  return r;
}

int main() {
  FIN;
  int x, on = 0, tw = 0, th = 0;
  cin >> n;
  fore(i, 0, n) {
    cin >> x;
    on += x == 1;
    tw += x == 2;
    th += x == 3;
  }
  cout << fixed << setprecision(10) << solve(on, tw, th) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define pb push_back
#define ALL(s) s.begin(), s.end()
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SZ(n) ((int)(n).size())
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

double dp[301][301][301];
ll n;
bool done[301][301][301];

double solve(int on, int tw, int th) {
  if (on + tw + th == 0)
    return 0;
  double &r = dp[on][tw][th];
  if (done[on][tw][th])
    return r;
  done[on][tw][th] = 1;
  double tot = on + tw + th;
  r = (double)n / tot;
  if (on)
    r += (double)on / tot * solve(on - 1, tw, th);
  if (tw)
    r += (double)tw / tot * solve(on + 1, tw - 1, th);
  if (th)
    r += (double)th / tot * solve(on, tw + 1, th - 1);
  return r;
}

int main() {
  FIN;
  int x, on = 0, tw = 0, th = 0;
  cin >> n;
  fore(i, 0, n) {
    cin >> x;
    on += x == 1;
    tw += x == 2;
    th += x == 3;
  }
  cout << fixed << setprecision(10) << solve(on, tw, th) << "\n";
}
Problem p03169 (C++)

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

double dp[301][301][301];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int n;
  cin >> n;
  vector<int> a(n);
  int o = 0, tw = 0, tr = 0;

  for (auto &i : a) {
    cin >> i;
    if (i == 1)
      o++;
    else if (i == 2)
      tw++;
    else
      tr++;
  }

  for (int k = 0; k < 301; ++k)
    for (int j = 0; k + j < 301; ++j)
      for (int i = 0; i + j + k < 301; ++i) {
        if (i + j + k == 0)
          continue;

        double w = 1.0 * n / (i + j + k);
        double pi = 1.0 * i / (i + j + k);
        double pj = 1.0 * j / (i + j + k);
        double pk = 1.0 * k / (i + j + k);

        if (i)
          dp[i][j][k] += dp[i - 1][j][k] * pi;
        if (j)
          dp[i][j][k] += dp[i + 1][j - 1][k] * pj;
        if (k)
          dp[i][j][k] += dp[i][j + 1][k - 1] * pk;
        dp[i][j][k] += w;
      }

  cout << setprecision(10) << fixed << dp[o][tw][tr] << "\n";

  return 0;
}

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

double dp[301][301][301];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  int o = 0, tw = 0, tr = 0;

  for (auto &i : a) {
    cin >> i;
    if (i == 1)
      o++;
    else if (i == 2)
      tw++;
    else
      tr++;
  }

  for (int k = 0; k < 301; ++k)
    for (int j = 0; k + j < 301; ++j)
      for (int i = 0; i + j + k < 301; ++i) {
        if (i + j + k == 0)
          continue;

        double w = 1.0 * n / (i + j + k);
        double pi = 1.0 * i / (i + j + k);
        double pj = 1.0 * j / (i + j + k);
        double pk = 1.0 * k / (i + j + k);

        if (i)
          dp[i][j][k] += dp[i - 1][j][k] * pi;
        if (j)
          dp[i][j][k] += dp[i + 1][j - 1][k] * pj;
        if (k)
          dp[i][j][k] += dp[i][j + 1][k - 1] * pk;
        dp[i][j][k] += w;
      }

  cout << setprecision(10) << fixed << dp[o][tw][tr] << "\n";

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
using namespace std;

const int maxn = 300;
double dp[maxn + 1][maxn + 1][maxn + 1] = {0};
int occur[3] = {0};

void test() {
  double sum = 0;
  double n = 50, e = 30;
  for (double i = 0; i <= 1000; i++) {
    sum += (n - e) / n * pow(e / n, i) * (i + 1);
  }
  cout << sum << endl;
  cout << e / (n - e) << endl;
  cout << n / (n - e) << endl;
}

int main() {
  // test();
  // return 0;
  int n;
  cin >> n;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    occur[x - 1]++;
  }
  for (int k = 0; k <= occur[2]; k++) {
    for (int j = 0; j <= occur[1] + occur[2]; j++) {
      for (int i = 0; i <= occur[0] + occur[1] + occur[2]; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        double s = i + j + k;
        dp[i][j][k] = (k ? dp[i][j + 1][k - 1] * k / s : 0) +
                      (j ? dp[i + 1][j - 1][k] * j / s : 0) +
                      (i ? dp[i - 1][j][k] * i / s : 0) + +n / s;
      }
    }
  }
  cout.precision(10);
  cout << dp[occur[0]][occur[1]][occur[2]] << '\n';
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
using namespace std;

const int maxn = 300;
double dp[3 * maxn + 1][2 * maxn][maxn + 1] = {0};
int occur[3] = {0};

void test() {
  double sum = 0;
  double n = 50, e = 30;
  for (double i = 0; i <= 1000; i++) {
    sum += (n - e) / n * pow(e / n, i) * (i + 1);
  }
  cout << sum << endl;
  cout << e / (n - e) << endl;
  cout << n / (n - e) << endl;
}

int main() {
  // test();
  // return 0;
  int n;
  cin >> n;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    occur[x - 1]++;
  }
  for (int k = 0; k <= occur[2]; k++) {
    for (int j = 0; j <= occur[1] + occur[2]; j++) {
      for (int i = 0; i <= occur[0] + occur[1] + occur[2]; i++) {
        if (i == 0 && j == 0 && k == 0)
          continue;
        double s = i + j + k;
        dp[i][j][k] = (k ? dp[i][j + 1][k - 1] * k / s : 0) +
                      (j ? dp[i + 1][j - 1][k] * j / s : 0) +
                      (i ? dp[i - 1][j][k] * i / s : 0) + +n / s;
      }
    }
  }
  cout.precision(10);
  cout << dp[occur[0]][occur[1]][occur[2]] << '\n';
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 310
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, f[4];
double dp[MAXN][MAXN][MAXN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    f[t]++;
  }
  for (int k = 0; k < MAXN; k++) {
    for (int j = 0; j < MAXN; j++) {
      for (int i = 0; i < MAXN; i++) {
        if (i == 0 and j == 0 and k == 0) {
          dp[i][j][k] = 0;
          continue;
        }
        double t = 0;
        if (i > 0)
          t += (double)i * dp[i - 1][j][k] / n;
        if (j > 0)
          t += (double)j * dp[i + 1][j - 1][k] / n;
        if (k > 0)
          t += (double)k * dp[i][j + 1][k - 1] / n;
        t++;
        t /= (double)(i + j + k) / n;
        // cout << t;
        dp[i][j][k] = t;
      }
    }
  }
  cout << fixed << setprecision(11) << dp[f[1]][f[2]][f[3]];
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 310
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, f[4];
double dp[MAXN][MAXN][MAXN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    f[t]++;
  }
  for (int k = 0; k < MAXN - 1; k++) {
    for (int j = 0; j < MAXN - 1; j++) {
      for (int i = 0; i < MAXN - 1; i++) {
        if (i == 0 and j == 0 and k == 0) {
          dp[i][j][k] = 0;
          continue;
        }
        double t = 0;
        if (i > 0)
          t += (double)i * dp[i - 1][j][k] / n;
        if (j > 0)
          t += (double)j * dp[i + 1][j - 1][k] / n;
        if (k > 0)
          t += (double)k * dp[i][j + 1][k - 1] / n;
        t++;
        t /= (double)(i + j + k) / n;
        // cout << t;
        dp[i][j][k] = t;
      }
    }
  }
  cout << fixed << setprecision(11) << dp[f[1]][f[2]][f[3]];
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define For(i, n) rep(i, 0, n)
// #define int long long
// const int INF=1e18;
const int MOD = 1000000007;
typedef pair<int, int> P;

int c[3] = {0, 0, 0}, n;
double mem[305][305][305];
bool f[305][305][305];

double dp(int a, int b, int c) {
  if (!a && !b && !c) {
    return 0;
  }
  f[a][b][c] = 1;
  double ret = n;
  if (a) {
    ret += dp(a - 1, b, c) * a;
  }
  if (b) {
    ret += dp(a + 1, b - 1, c) * b;
  }
  if (c) {
    ret += dp(a, b + 1, c - 1) * c;
  }
  return mem[a][b][c] = ret / (a + b + c);
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int a;
  cin >> n;
  rep(i, 0, n) {
    int a;
    cin >> a;
    a--;
    c[a]++;
  }
  cout << setprecision(20) << dp(c[0], c[1], c[2]) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define For(i, n) rep(i, 0, n)
// #define int long long
// const int INF=1e18;
const int MOD = 1000000007;
typedef pair<int, int> P;

int c[3] = {0, 0, 0}, n;
double mem[305][305][305];
bool f[305][305][305];

double dp(int a, int b, int c) {
  if (!a && !b && !c) {
    return 0;
  }
  if (f[a][b][c]) {
    return mem[a][b][c];
  }
  f[a][b][c] = 1;
  double ret = n;
  if (a) {
    ret += dp(a - 1, b, c) * a;
  }
  if (b) {
    ret += dp(a + 1, b - 1, c) * b;
  }
  if (c) {
    ret += dp(a, b + 1, c - 1) * c;
  }
  return mem[a][b][c] = ret / (a + b + c);
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int a;
  cin >> n;
  rep(i, 0, n) {
    int a;
    cin >> a;
    a--;
    c[a]++;
  }
  cout << setprecision(20) << dp(c[0], c[1], c[2]) << endl;

  return 0;
}
Problem p03169 (C++)

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

using namespace std;

double dp[101][101][101];
bool vis[101][101][101] = {0};
int n;

double solve(int x, int y, int z) {
  if (x <= 0 && y <= 0 && z <= 0)
    return 0;
  // cout << x << y << z << endl;
  if (vis[x][y][z])
    return dp[x][y][z];
  vis[x][y][z] = 1;
  double ans = 1.0;
  // cout << x << " ";
  if (x > 0) {
    // cout << (double)x / n << " ";
    ans += ((double)x / n) * solve(x - 1, y, z);
    // cout << ans << endl;
  }
  if (y > 0)
    ans += ((double)y / n) * solve(x + 1, y - 1, z);
  if (z > 0)
    ans += ((double)z / n) * solve(x, y + 1, z - 1);
  ans /= ((1.0 - ((double)n - x - y - z) / n));
  // cout << ans << endl;
  return dp[x][y][z] = ans;
}

int main() {
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x = 0, y = 0, z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1)
      x++;
    else if (a[i] == 2)
      y++;
    else if (a[i] == 3)
      z++;
  }
  printf("%.10lf", solve(x, y, z));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

double dp[301][301][301];
bool vis[301][301][301] = {0};
int n;

double solve(int x, int y, int z) {
  if (x <= 0 && y <= 0 && z <= 0)
    return 0;
  // cout << x << y << z << endl;
  if (vis[x][y][z])
    return dp[x][y][z];
  vis[x][y][z] = 1;
  double ans = 1.0;
  // cout << x << " ";
  if (x > 0) {
    // cout << (double)x / n << " ";
    ans += ((double)x / n) * solve(x - 1, y, z);
    // cout << ans << endl;
  }
  if (y > 0)
    ans += ((double)y / n) * solve(x + 1, y - 1, z);
  if (z > 0)
    ans += ((double)z / n) * solve(x, y + 1, z - 1);
  ans /= ((1.0 - ((double)n - x - y - z) / n));
  // cout << ans << endl;
  return dp[x][y][z] = ans;
}

int main() {
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x = 0, y = 0, z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1)
      x++;
    else if (a[i] == 2)
      y++;
    else if (a[i] == 3)
      z++;
  }
  printf("%.10lf", solve(x, y, z));
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(a, b) for (int i = a; i < b; i++)
#define mod 10000code200007
#define printDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<int, pair<int, int>>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;

/*E(x,y,z) = (1 + p1*E(x-1,y,z) + p2*E(x+1,y-1,z) + p3*E(x,y+1,z-1))/(1-p0)
(1 - N-(x+y+z))/N = N - N + x + y + z
E(x,y,z) = (n + x*E(x-1,y,z) + y*E(x+1,y-1,z) + z*E(x,y+1,z-1))/(x + y + z);*/

double dp[301][301][301];
double solve(int x, int y, int z, int &n) {
  if (x == 0 && y == 0 && z == 0)
    return 0;
  if (x < 0 || y < 0 || z < 0)
    return 0;

  if (dp[x][y][z] < 0)
    return dp[x][y][z];

  int remaining = n - (x + y + z);
  double exp_val = n + x * solve(x - 1, y, z, n) +
                   y * solve(x + 1, y - 1, z, n) +
                   z * solve(x, y + 1, z - 1, n);

  return dp[x][y][z] = exp_val / (x + y + z);
}

int main() {
  ll i, j, t, n, m, x, p, k;
  fast_io;
  // cin>>t;
  t = 1;
  while (t--) {
    memset(dp, -1, sizeof dp);
    int n, x = 0, y = 0, z = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int sushi;
      cin >> sushi;
      if (sushi == 1)
        x++;
      else if (sushi == 2)
        y++;
      else
        z++;
    }
    printDouble(solve(x, y, z, n));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(a, b) for (int i = a; i < b; i++)
#define mod 10000code200007
#define printDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<int, pair<int, int>>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;

/*E(x,y,z) = (1 + p1*E(x-1,y,z) + p2*E(x+1,y-1,z) + p3*E(x,y+1,z-1))/(1-p0)
(1 - N-(x+y+z))/N = N - N + x + y + z
E(x,y,z) = (n + x*E(x-1,y,z) + y*E(x+1,y-1,z) + z*E(x,y+1,z-1))/(x + y + z);*/

double dp[301][301][301];
double solve(int x, int y, int z, int &n) {
  if (x == 0 && y == 0 && z == 0)
    return 0;
  if (x < 0 || y < 0 || z < 0)
    return 0;

  if (dp[x][y][z] > -0.5)
    return dp[x][y][z];

  int remaining = n - (x + y + z);
  double exp_val = n + x * solve(x - 1, y, z, n) +
                   y * solve(x + 1, y - 1, z, n) +
                   z * solve(x, y + 1, z - 1, n);

  return dp[x][y][z] = exp_val / (x + y + z);
}

int main() {
  ll i, j, t, n, m, x, p, k;
  fast_io;
  // cin>>t;
  t = 1;
  while (t--) {
    memset(dp, -1, sizeof dp);
    int n, x = 0, y = 0, z = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int sushi;
      cin >> sushi;
      if (sushi == 1)
        x++;
      else if (sushi == 2)
        y++;
      else
        z++;
    }
    printDouble(solve(x, y, z, n));
  }
  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(10);
  int n;
  cin >> n;
  vi cnt(4);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
  }

  vector<vector<vector<double>>> dp(
      cnt[1] + 1,
      vector<vector<double>>(cnt[2] + 1, vector<double>(cnt[3] + 1)));
  function<double(int, int, int)> dfs = [&](int i, int j, int k) {
    if (i + j + k == 0)
      return 0.0;
    if (dp[i][j][k])
      return dp[i][j][k];
    double ret = (i ? dfs(i - 1, j, k) * i / (i + j + k) : 0) +
                 (j ? dfs(i + 1, j - 1, k) * j / (i + j + k) : 0) +
                 (k ? dfs(i, j + 1, k - 1) * k / (i + j + k) : 0) +
                 n / double(i + j + k);
    dp[i][j][k] = ret;
    return ret;
  };

  cout << dfs(cnt[1], cnt[2], cnt[3]) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(10);
  int n;
  cin >> n;
  vi cnt(4);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
  }

  vector<vector<vector<double>>> dp(
      301, vector<vector<double>>(301, vector<double>(301)));
  function<double(int, int, int)> dfs = [&](int i, int j, int k) {
    if (i + j + k == 0)
      return 0.0;
    if (dp[i][j][k])
      return dp[i][j][k];
    double ret = (i ? dfs(i - 1, j, k) * i / (i + j + k) : 0) +
                 (j ? dfs(i + 1, j - 1, k) * j / (i + j + k) : 0) +
                 (k ? dfs(i, j + 1, k - 1) * k / (i + j + k) : 0) +
                 n / double(i + j + k);
    dp[i][j][k] = ret;
    return ret;
  };

  cout << dfs(cnt[1], cnt[2], cnt[3]) << endl;
}
Problem p03169 (C++)

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

signed main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  static int c[3];
  for (int i = 0, t; i < N; ++i) {
    cin >> t;
    ++c[t - 1];
  }

  static double dp[301][601][901];
  /*function<double(int, int, int)> dfs = [&](int a, int b, int c) {
    if (a + b + c == 0) return 0.0;
    if (vis[a][b][c]) return dp[a][b][c];
    vis[a][b][c] = 1;
    if (a) dp[a][b][c] += (dfs(a - 1, b + 1, c) + 1) * a / N;
    if (b) dp[a][b][c] += (dfs(a, b - 1, c + 1) + 1) * b / N;
    if (c) dp[a][b][c] += (dfs(a, b, c - 1) + 1) * c / N;
    int d = N - a - b - c;
    // if (d) dp[a][b][c] += (dfs(a, b, c) + 1) * d / N;
    return dp[a][b][c] = (dp[a][b][c] + 1.0 * d / N) * N / (N - d);
  };*/
  for (int i = 0; i < c[2] + 1; ++i) {
    for (int j = 0; j < c[1] + c[2] + 1; ++j) {
      for (int k = 0; k < c[0] + c[1] + c[2] + 1; ++k) {
        if (i + j + k == 0)
          continue;
        if (i)
          dp[i][j][k] += (dp[i - 1][j + 1][k] + 1) * i / N;
        if (j)
          dp[i][j][k] += (dp[i][j - 1][k + 1] + 1) * j / N;
        if (k)
          dp[i][j][k] += (dp[i][j][k - 1] + 1) * k / N;
        int l = N - i - j - k;
        dp[i][j][k] = (dp[i][j][k] + 1.0 * l / N) * N / (N - l);
      }
    }
  }

  cout << fixed << setprecision(9) << dp[c[2]][c[1]][c[0]] << endl;

  return 0;
}


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

signed main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  static int c[3];
  for (int i = 0, t; i < N; ++i) {
    cin >> t;
    ++c[t - 1];
  }

  static double dp[301][301][301];
  /*function<double(int, int, int)> dfs = [&](int a, int b, int c) {
    if (a + b + c == 0) return 0.0;
    if (vis[a][b][c]) return dp[a][b][c];
    vis[a][b][c] = 1;
    if (a) dp[a][b][c] += (dfs(a - 1, b + 1, c) + 1) * a / N;
    if (b) dp[a][b][c] += (dfs(a, b - 1, c + 1) + 1) * b / N;
    if (c) dp[a][b][c] += (dfs(a, b, c - 1) + 1) * c / N;
    int d = N - a - b - c;
    // if (d) dp[a][b][c] += (dfs(a, b, c) + 1) * d / N;
    return dp[a][b][c] = (dp[a][b][c] + 1.0 * d / N) * N / (N - d);
  };*/
  for (int i = 0; i < c[2] + 1; ++i) {
    for (int j = 0; j < c[1] + c[2] + 1; ++j) {
      for (int k = 0; k < c[0] + c[1] + c[2] + 1; ++k) {
        if (i + j + k == 0)
          continue;
        if (i)
          dp[i][j][k] += (dp[i - 1][j + 1][k] + 1) * i / N;
        if (j)
          dp[i][j][k] += (dp[i][j - 1][k + 1] + 1) * j / N;
        if (k)
          dp[i][j][k] += (dp[i][j][k - 1] + 1) * k / N;
        int l = N - i - j - k;
        dp[i][j][k] = (dp[i][j][k] + 1.0 * l / N) * N / (N - l);
      }
    }
  }

  cout << fixed << setprecision(9) << dp[c[2]][c[1]][c[0]] << endl;

  return 0;
}
Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
double dp[101][101][101];
int n;
double solve(int i, int j, int k) {
  if (dp[i][j][k] >= 0)
    return dp[i][j][k];
  if (!(i | j | k))
    return 0.0;
  double res = 0.0;
  if (i)
    res += solve(i - 1, j, k) * i;
  if (j)
    res += solve(i + 1, j - 1, k) * j;
  if (k)
    res += solve(i, j + 1, k - 1) * k;
  res += n;
  res /= (i + j + k);
  return dp[i][j][k] = res;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  int a, c[4] = {0};
  for (int i = 0; i < n; i++) {
    cin >> a;
    c[a]++;
    // if(a<3)c[3-a]++;
  }
  memset(dp, -1, sizeof(dp));
  doublecout(solve(c[1], c[2], c[3]));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
double dp[301][301][301];
int n;
double solve(int i, int j, int k) {
  if (dp[i][j][k] >= 0)
    return dp[i][j][k];
  if (!(i | j | k))
    return 0.0;
  double res = 0.0;
  if (i)
    res += solve(i - 1, j, k) * i;
  if (j)
    res += solve(i + 1, j - 1, k) * j;
  if (k)
    res += solve(i, j + 1, k - 1) * k;
  res += n;
  res /= (i + j + k);
  return dp[i][j][k] = res;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  int a, c[4] = {0};
  for (int i = 0; i < n; i++) {
    cin >> a;
    c[a]++;
    // if(a<3)c[3-a]++;
  }
  memset(dp, -1, sizeof(dp));
  doublecout(solve(c[1], c[2], c[3]));
  return 0;
}
Problem p03169 (C++)

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

double memo[305][305][305];

double dp(ll s1, ll s2, ll s3, ll empty, ll full) {
  if (s1 == 0 && s2 == 0 && s3 == 0)
    return 0;
  if (memo[s1][s2][s3] != -1)
    return memo[s1][s2][s3];

  double ret = 0;

  ret = (double)full *
        (1 + ((double)s1 * dp(s1 - 1, s2, s3, empty + 1, full) +
              (double)s2 * dp(s1 + 1, s2 - 1, s3, empty, full) +
              (double)s3 * dp(s1, s2 + 1, s3 - 1, empty, full)) /
                 full) /
        (double)(full - empty);

  return memo[s1][s2][s3] = ret;
}

ll sushi[5];
int main() {

  for (int i = 0; i <= 300; i++) {
    for (int j = 0; j <= 300; j++) {
      for (int k = 0; k <= 300; k++) {
        memo[i][j][k] = -1;
      }
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    ll num;
    scanf("%lld", &num);
    sushi[num]++;
  }
  double ans = dp(sushi[1], sushi[2], sushi[3], 0, n);
  printf("%.12lf\n", ans);
}

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

double memo[305][305][305];

double dp(ll s1, ll s2, ll s3, ll empty, ll full) {
  if (s1 == 0 && s2 == 0 && s3 == 0)
    return 0;
  if (memo[s1][s2][s3] != -1)
    return memo[s1][s2][s3];

  double ret = 0;
  double temp1 = 0, temp2 = 0, temp3 = 0;
  if (s1 != 0) {
    temp1 = (double)s1 * dp(s1 - 1, s2, s3, empty + 1, full);
  }
  if (s2 != 0) {
    temp2 = (double)s2 * dp(s1 + 1, s2 - 1, s3, empty, full);
  }
  if (s3 != 0) {
    temp3 = (double)s3 * dp(s1, s2 + 1, s3 - 1, empty, full);
  }
  ret = (double)full * (1 + (temp1 + temp2 + temp3) / full) /
        (double)(full - empty);

  return memo[s1][s2][s3] = ret;
}

ll sushi[5];
int main() {

  for (int i = 0; i <= 300; i++) {
    for (int j = 0; j <= 300; j++) {
      for (int k = 0; k <= 300; k++) {
        memo[i][j][k] = -1;
      }
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    ll num;
    scanf("%lld", &num);
    sushi[num]++;
  }
  double ans = dp(sushi[1], sushi[2], sushi[3], 0, n);
  printf("%.12lf\n", ans);
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int dp[n + 1];
  memset(dp, 0, sizeof(dp));
  for (i = 0; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= a[j] && dp[i - a[j]] == 0)
        dp[i] = 1;
    }
  }
  cout << (dp[k] == 1 ? "First" : "Second") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int dp[k + 1];
  memset(dp, 0, sizeof(dp));
  for (i = 0; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= a[j] && dp[i - a[j]] == 0)
        dp[i] = 1;
    }
  }
  cout << (dp[k] == 1 ? "First" : "Second") << endl;
  return 0;
}
Problem p03170 (C++)

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

const int MAXN = 1e2 + 2, K = 1e5 + 5;
int a[MAXN];
bool dp[K];
int n, k;

void input();
void solve();

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  input();
  solve();
  return 0;
}

void input() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
}

void solve() {
  for (int i = 1; i <= k; i++)
    for (int j = 0; j < n; j++)
      if (a[j] >= i && dp[i - a[j]] == false)
        dp[i] = true;
  dp[k] ? cout << "First" << endl : cout << "Second" << endl;
}


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

const int MAXN = 1e2 + 2, K = 1e5 + 5;
int a[MAXN];
bool dp[K];
int n, k;

void input();
void solve();

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  input();
  solve();
  return 0;
}

void input() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
}

void solve() {
  for (int i = 1; i <= k; i++)
    for (int j = 0; j < n; j++)
      if (a[j] <= i && dp[i - a[j]] == false)
        dp[i] = true;
  dp[k] ? cout << "First" << endl : cout << "Second" << endl;
}
Problem p03170 (C++)

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // dp[i] : TRUE if first player wins with i stones left
  vector<bool> dp(k + 1);
  for (int i = 0; i <= k; i++) {
    for (int x : a) {
      if (i >= x && !dp[i - k])
        dp[i] = true;
    }
  }
  cout << (dp[k] ? "First" : "Second") << '\n';

  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // dp[i] : TRUE if first player wins with i stones left
  vector<bool> dp(k + 1);
  for (int i = 0; i <= k; i++) {
    for (int x : a) {
      if (i >= x && !dp[i - x])
        dp[i] = true;
    }
  }
  cout << (dp[k] ? "First" : "Second") << '\n';

  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> memo(1e5 + 5, -1);

int dp(vector<int> &v, int i) {
  if (i == 0)
    return 0;
  memo[i] = 0;
  for (int e : v) {

    if (e <= i) {
      if (!dp(v, i - e))
        memo[i] = 1;
    }
  }
  return memo[i];
}

signed main() {
  cin.tie(0);
  cin.sync_with_stdio();
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int &i : v)
    cin >> i;
  cout << (dp(v, k) ? "First" : "Second");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> memo(1e5 + 5, -1);

int dp(vector<int> &v, int i) {
  if (i == 0)
    return 0;
  if (memo[i] != -1)
    return memo[i];
  memo[i] = 0;
  for (int e : v) {

    if (e <= i) {
      if (!dp(v, i - e))
        memo[i] = 1;
    }
  }
  return memo[i];
}

signed main() {
  cin.tie(0);
  cin.sync_with_stdio();
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int &i : v)
    cin >> i;
  cout << (dp(v, k) ? "First" : "Second");
}
Problem p03170 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

const char sp = ' ';
const char cmm = ',';
const int MOD = 1000000007;

using ll = long long;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
  ll r = 1;
  while (e > 0) {
    if (e & 1)
      r = (r % m) * (b % m) % m;
    b = (b % m) * (b % m) % m;
    e >>= 1;
  }
  return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

template <typename T> void ppp(T n) { cout << n << endl; }

bool dp[100001] = {false};

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  rep(i, n) cin >> a[i];

  for (int i = 1; i <= k; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i - a[i] < 0)
        break;
      dp[i] |= !dp[i - a[j]];
    }
  }

  ppp(dp[k] ? "First" : "Second");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

const char sp = ' ';
const char cmm = ',';
const int MOD = 1000000007;

using ll = long long;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
  ll r = 1;
  while (e > 0) {
    if (e & 1)
      r = (r % m) * (b % m) % m;
    b = (b % m) * (b % m) % m;
    e >>= 1;
  }
  return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

template <typename T> void ppp(T n) { cout << n << endl; }

bool dp[100001] = {false};

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  rep(i, n) cin >> a[i];

  for (int i = 1; i <= k; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i - a[j] < 0)
        break;
      dp[i] |= !dp[i - a[j]];
    }
  }

  ppp(dp[k] ? "First" : "Second");

  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <cstdint>
#include <cstdio>
#include <vector>

int main() {
  size_t N, K;
  scanf("%zu %zu", &N, &K);

  std::vector<size_t> a(N);
  for (auto &ai : a)
    scanf("%zu", &ai);

  std::vector<size_t> dp(K + 1, 0);
  for (size_t i = 0; i < K; ++i)
    for (size_t j = 0; j < N; ++j) {
      if (dp[i + a[j]] > K)
        break;
      if (dp[i] % 2 == 0 || dp[i + a[j]] % 2 == 0)
        dp[i + a[j]] = dp[i] + 1;
    }

  // for (size_t i = 0; i <= K; ++i)
  //   fprintf(stderr, "%zu%c", dp[i], i<K? ' ':'\n');

  puts((dp[K] % 2 > 0) ? "First" : "Second");
}


Fixed submission:
#include <cstdint>
#include <cstdio>
#include <vector>

int main() {
  size_t N, K;
  scanf("%zu %zu", &N, &K);

  std::vector<size_t> a(N);
  for (auto &ai : a)
    scanf("%zu", &ai);

  std::vector<size_t> dp(K + 1, 0);
  for (size_t i = 0; i < K; ++i)
    for (size_t j = 0; j < N; ++j) {
      if (i + a[j] > K)
        break;
      if (dp[i] % 2 == 0 || dp[i + a[j]] % 2 == 0)
        dp[i + a[j]] = dp[i] + 1;
    }

  // for (size_t i = 0; i <= K; ++i)
  //   fprintf(stderr, "%zu%c", dp[i], i<K? ' ':'\n');

  puts((dp[K] % 2 > 0) ? "First" : "Second");
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
const ll inf = 1e16;
const ll md = 1000000007;
bool stone[105];

int main() {
  int n, k;
  vi aa;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    aa.push_back(a);
  }
  for (int i = 1; i <= k; i++) {
    for (int a : aa) {
      if (i - a < 0)
        continue;
      if (stone[i - a] == false) {
        stone[i] = true;
        break;
      }
    }
  }
  if (stone[k])
    puts("First");
  else
    puts("Second");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
const ll inf = 1e16;
const ll md = 1000000007;
bool stone[100005];

int main() {
  int n, k;
  vi aa;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    aa.push_back(a);
  }
  for (int i = 1; i <= k; i++) {
    for (int a : aa) {
      if (i - a < 0)
        continue;
      if (stone[i - a] == false) {
        stone[i] = true;
        break;
      }
    }
  }
  if (stone[k])
    puts("First");
  else
    puts("Second");
  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void Fast() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  Fast();
  int n, i, k;
  cin >> n >> k;
  int *v = new int[n];
  bool **dp = new bool *[k];
  for (i = 0; i <= k; i++)
    dp[i] = new bool[2];
  for (i = 0; i < n; i++)
    cin >> v[i];
  for (i = 0; i <= k; i++) {
    dp[i][0] = 1, dp[i][1] = 0;
    for (int j = 0; j < n; j++) {
      if (i - v[j] >= 0)
        dp[i][1] |= dp[i - v[j]][0];
      if (i - v[j] >= 0)
        dp[i][0] &= dp[i - v[j]][1];
    }
  }
  cout << (dp[k][1] ? "First" : "Second");
  delete[] v;
  for (i = 0; i <= k; i++)
    delete[] dp[i];
  delete[] dp;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void Fast() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  Fast();
  int n, i, k;
  cin >> n >> k;
  int *v = new int[n];
  bool **dp = new bool *[k + 1];
  for (i = 0; i <= k; i++)
    dp[i] = new bool[2];
  for (i = 0; i < n; i++)
    cin >> v[i];
  for (i = 0; i <= k; i++) {
    dp[i][0] = 1, dp[i][1] = 0;
    for (int j = 0; j < n; j++) {
      if (i - v[j] >= 0)
        dp[i][1] |= dp[i - v[j]][0];
      if (i - v[j] >= 0)
        dp[i][0] &= dp[i - v[j]][1];
    }
  }
  cout << (dp[k][1] ? "First" : "Second");
  delete[] v;
  for (i = 0; i <= k; i++)
    delete[] dp[i];
  delete[] dp;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define rep(i, n) for (long long int(i) = 0; (i) < (n); ++(i))
#define ref(i, a, b) for (long long int(i) = (a); (i) <= (b); ++(i))
#define INF 0xFFFFFF
#define endl '\n'
const unsigned int mod = 1e9 + 7;
const int maxInt = 2147483647;
const int mx = 100005;
int dp[mx], a[mx];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  ref(i, 1, n) cin >> a[i];
  ref(i, 0, k) {
    if (dp[i] == 0) {
      ref(j, 1, n) { dp[i + a[j]] = 1; }
    }
  }
  if (dp[k])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define rep(i, n) for (long long int(i) = 0; (i) < (n); ++(i))
#define ref(i, a, b) for (long long int(i) = (a); (i) <= (b); ++(i))
#define INF 0xFFFFFF
#define endl '\n'
const unsigned int mod = 1e9 + 7;
const int maxInt = 2147483647;
const int mx = 100005;
int dp[2 * mx], a[mx];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  ref(i, 1, n) cin >> a[i];
  ref(i, 0, k) {
    if (dp[i] == 0) {
      ref(j, 1, n) { dp[i + a[j]] = 1; }
    }
  }
  if (dp[k])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
using lint = long long;
typedef pair<int, int> P;
const int inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<bool> dp(n + 1, false);
  for (int j = a[0]; j <= k; ++j) {
    bool flag = false;
    rep(i, n) {
      if (j < a[i])
        break;
      if (!dp[j - a[i]])
        flag = true;
    }
    dp[j] = flag;
  }
  if (dp[k]) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
using lint = long long;
typedef pair<int, int> P;
const int inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<bool> dp(k + 1, false);
  for (int j = a[0]; j <= k; ++j) {
    bool flag = false;
    rep(i, n) {
      if (j < a[i])
        break;
      if (!dp[j - a[i]])
        flag = true;
    }
    dp[j] = flag;
  }
  if (dp[k]) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[100001];
int main() {
  freopen("final_input.txt", "r", stdin);
  int n;
  cin >> n;
  int k;
  cin >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    dp[arr[i]] = 1;
  }
  dp[0] = 0;

  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if ((i - arr[j]) < 0) {
        dp[i] = 0;
      } else {
        dp[i] = !dp[i - arr[j]];
        if (dp[i] == 1) {
          break;
        }
      }
    }
  }

  if (dp[k] == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[100001];
int main() {
  // freopen("final_input.txt", "r", stdin);
  int n;
  cin >> n;
  int k;
  cin >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    dp[arr[i]] = 1;
  }
  dp[0] = 0;

  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if ((i - arr[j]) < 0) {
        dp[i] = 0;
      } else {
        dp[i] = !dp[i - arr[j]];
        if (dp[i] == 1) {
          break;
        }
      }
    }
  }

  if (dp[k] == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  //	freopen("SNOOKER.inp","r",stdin);
  //	freopen("SNOOKER.out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}

using ll = long long;
#define endl "\n"
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define what_is(x) cerr << #x << " is :" << (x) << endl
#define what_are(x, y)                                                         \
  cerr << #x << " is : " << (x) << " ; " << #y << " is: " << y << endl
#define pii pair<int, int>
#define fi first
#define se second

const int MOD = 1e9 + 7;
const int inf = INT_MAX;
const int MAX_N = 1e5 + 7;

int n, k;
bool dp[MAX_N];
int a[107];
void solve() {

  fill(dp, dp + MAX_N, false);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 0; i <= k; i++) {
    if (!dp[i])

      for (int j = 1; j <= n; j++) {
        dp[i + a[j]] = true;
      }
  }

  if (dp[k])
    cout << "First";
  else
    cout << "Second";
}
/*----------------------------------------------------------------------

-----------------------------------------------------------------------*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  //	freopen("SNOOKER.inp","r",stdin);
  //	freopen("SNOOKER.out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}

using ll = long long;
#define endl "\n"
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define what_is(x) cerr << #x << " is :" << (x) << endl
#define what_are(x, y)                                                         \
  cerr << #x << " is : " << (x) << " ; " << #y << " is: " << y << endl
#define pii pair<int, int>
#define fi first
#define se second

const int MOD = 1e9 + 7;
const int inf = INT_MAX;
const int MAX_N = 1e5 + 7;

int n, k;
bool dp[MAX_N];
int a[107];
void solve() {

  fill(dp, dp + MAX_N, false);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 0; i <= k; i++) {
    if (!dp[i])

      for (int j = 1; j <= n; j++) {
        dp[min(i + a[j], k + 7)] = true;
      }
  }

  if (dp[k])
    cout << "First";
  else
    cout << "Second";
}
/*----------------------------------------------------------------------

-----------------------------------------------------------------------*/
Problem p03170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int n, k, a[107];
bool dp[100007], vis[100007];

bool solve(int rem) {
  if (vis[rem])
    return dp[rem];
  bool ret = false;
  for (int i = 1; i <= n; i++) {
    if (a[i] > rem)
      continue;
    ret = max((int)ret, solve(rem - a[i]) ^ 1);
  }
  dp[rem] = ret;
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cout << (solve(k) ? "First" : "Second");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int n, k, a[107];
bool dp[100007], vis[100007];

bool solve(int rem) {
  if (vis[rem])
    return dp[rem];
  vis[rem] = true;
  bool ret = false;
  for (int i = 1; i <= n; i++) {
    if (a[i] > rem)
      continue;
    ret = max((int)ret, solve(rem - a[i]) ^ 1);
  }
  dp[rem] = ret;
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cout << (solve(k) ? "First" : "Second");
}