alexjercan/bugnet huggingface dataset:
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 16;
const int MAX_M = 16;
const double EPS = 1e-10;

double dp[1 << MAX_M][MAX_N];

double prob[MAX_M][MAX_N];
int M, N;

double dfs(int b, int x) {
  if (x == N)
    return 1.0;
  if (b == (1 << M) - 1) {
    return 0.0;
  }
  if (dp[b][x] - -1 > EPS)
    return dp[b][x];
  // cout << b << " " << x << " " << M << " " << N << endl;
  double ret = 0;
  for (int i = 0; i < M; i++) {
    if (b & (1 << i))
      continue;
    double p = 0.0;
    double tmpp = 1.0;
    for (int j = x; j < N; j++) {
      p += tmpp * (1 - prob[i][j]) * dfs(b | (1 << i), j);
      // cout << p << endl;
      // cout << "tmpp: " << tmpp << " prob[i][j]: " << prob[i][j] << endl;
      tmpp *= prob[i][j];
    }
    p += tmpp * dfs(b | (1 << i), N);
    ret = max(ret, p);
  }
  dp[b][x] = ret;
  return ret;
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%lf", &prob[i][j]);
    }
  }

  printf("%0.30lf\\n", dfs(0, 0));
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 16;
const int MAX_M = 16;
const double EPS = 1e-10;

double dp[1 << MAX_M][MAX_N];

double prob[MAX_M][MAX_N];
int M, N;

double dfs(int b, int x) {
  if (x == N)
    return 1.0;
  if (b == (1 << M) - 1) {
    return 0.0;
  }
  if (dp[b][x] - -1 < EPS)
    return dp[b][x];
  // cout << b << " " << x << " " << M << " " << N << endl;
  double ret = 0;
  for (int i = 0; i < M; i++) {
    if (b & (1 << i))
      continue;
    double p = 0.0;
    double tmpp = 1.0;
    for (int j = x; j < N; j++) {
      p += tmpp * (1 - prob[i][j]) * dfs(b | (1 << i), j);
      // cout << p << endl;
      // cout << "tmpp: " << tmpp << " prob[i][j]: " << prob[i][j] << endl;
      tmpp *= prob[i][j];
    }
    p += tmpp * dfs(b | (1 << i), N);
    ret = max(ret, p);
  }
  dp[b][x] = ret;
  return ret;
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%lf", &prob[i][j]);
    }
  }

  printf("%0.12lf\\n", dfs(0, 0));
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n, m;
double dp[(1 << 16)][17];
double a[20][20];

double memo(int state, int enemy) {
  if (enemy == m)
    return 1;
  if (state == (1 << n) - 1)
    return 0;
  if (dp[state][enemy] >= 0)
    return dp[state][enemy];
  double &res = dp[state][enemy];
  res = 0;
  for (int i = 0; i < n; i++) {
    if (state & (1 << i))
      continue;
    double now = 1, sum = 0;
    for (int j = enemy; j < m; j++) {
      sum += now * (1 - a[i][j]) * memo((state | (1 << i)), j);
      now *= a[i][j];
    }
    sum += now;
    res = max(res, sum);
  }
  return dp[state][enemy];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < (1 << 16); i++)
    for (int j = 0; j < 17; j++)
      dp[i][j] = -1;
  cout << memo(0, 0) << endl;
  return 0;
}


Error: MLE

Fixed submission:

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

int n, m;
double dp[(1 << 16)][17][17];
double a[20][20];

double memo(int state, int now, int enemy) {
  if (dp[state][now][enemy] != 0)
    return dp[state][now][enemy];
  double res = 0;
  res += memo(state, now, enemy + 1) * a[now][enemy];
  double nowmax = 0;
  for (int i = 0; i < n; i++) {
    if (state & (1 << i))
      continue;
    if (i == now)
      continue;
    nowmax = max(nowmax, memo(state | (1 << now), i, enemy));
  }
  res += nowmax * (1 - a[now][enemy]);
  dp[state][now][enemy] = res;
  return dp[state][now][enemy];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < (1 << n); i++)
    for (int j = 0; j < n; j++) {
      dp[i][j][m] = 1;
    }
  double ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, memo(0, i, 0));
  }
  cout << ans << endl;
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef double real;

real dp[2][17][1 << 16];
int n, m;
real p[17][17];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int j = 0; j < 17; j++)
    for (int k = 0; k < (1 << 16); k++)
      dp[n % 2][j][k] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int id = i % 2;
    for (int k = (1 << m) - 1; k >= 0; k--) {
      for (int j = 0; j < m; j++) {
        dp[id][j][k] = 0;
        if ((1 << j) & k)
          continue;
        dp[id][j][k] =
            dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
      }
      dp[id][16][k] = 0;
      for (int j = 0; j < m; j++) {
        dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
      }
    }
  }

  printf("%.10lf\\n", dp[0][16][0]);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef double real;

real dp[2][17][1 << 16];
int n, m;
real p[17][17];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int j = 0; j < 17; j++)
    for (int k = 0; k < (1 << 16); k++)
      dp[n % 2][j][k] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int id = i % 2;
    for (int k = (1 << m); k >= 0; k--) {
      for (int j = 0; j < m; j++) {
        dp[id][j][k] = 0;
        if ((1 << j) & k)
          continue;
        dp[id][j][k] =
            dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
      }
      dp[id][16][k] = 0;
      for (int j = 0; j < m; j++) {
        dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
      }
    }
  }

  printf("%.10lf\\n", dp[0][16][0]);
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

double memo[16][1 << 16];
bool dp[16][1 << 16];
double prob[16][16];

int m, n;

double solve(int, int);

inline double solve2(int pos, int flag) {
  if (dp[pos][flag])
    return memo[pos][flag];
  return solve(pos, flag);
}

double solve(int pos, int flag) {
  double ret = 0.0;
  dp[pos][flag] = true;
  REP(i, m) if (flag & (1 << i)) {
    double p = 1.0;
    double tmp = 0.0;
    for (int j = pos; j < n; j++) {
      double pp = p * prob[i][j];
      tmp += (p - pp) * solve2(j, flag ^ (1 << i));
      p = pp;
    }
    tmp += p;
    ret = max(ret, tmp);
  }

  return memo[pos][flag] = ret;
}

int main() {
  scanf("%d%d", &m, &n);
  REP(i, m) REP(j, n) scanf("%lf", &prob[i][j]);
  printf("%.11f\\n", solve(0, (1 << m) - 1));
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

double memo[16][1 << 16];
bool dp[16][1 << 16];
double prob[16][16];

int m, n;

double solve(int pos, int flag) {
  if (dp[pos][flag])
    return memo[pos][flag];
  dp[pos][flag] = true;

  double ret = 0.0;

  REP(i, m) if (flag & (1 << i)) {
    double p = 1.0;
    double tmp = 0.0;
    for (int j = pos; j < n; j++) {
      double pp = p * prob[i][j];
      tmp += (p - pp) * solve(j, flag ^ (1 << i));
      p = pp;
    }
    tmp += p;
    ret = max(ret, tmp);
  }

  return memo[pos][flag] = ret;
}

int main() {
  scanf("%d%d", &m, &n);
  REP(i, m) REP(j, n) scanf("%lf", &prob[i][j]);
  printf("%.11f\\n", solve(0, (1 << m) - 1));
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
double dp[101010][20], a[20][20];
int n, m;
double rec(int mask, int x) {
  if (dp[mask][x] != -1)
    return dp[mask][x];
  if (x == m)
    return dp[mask][x] = 1;
  if (mask == 0)
    return dp[mask][x] = 0;
  double ret = 0;
  rep(i, n) {
    if (mask & 1 << i) {
      double res = 0, pos = 1.0;
      REP(j, x, m) {
        res += rec(mask ^ (1 << i), j) * pos * (1.0 - a[i][j]);
        pos = pos * a[i][j];
      }
      res += pos;
      ret = max(res, ret);
    }
  }
  return dp[mask][x] = ret;
}
int main() {
  cin >> n >> m;
  rep(i, n) rep(j, m) { cin >> a[i][j]; }
  int all = 1 << n;
  rep(i, all) rep(j, m) dp[i][j] = -1;
  cout << fixed << setprecision(12) << rec(all - 1, 0) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
double dp[101010][20], a[20][20];
int n, m;
double rec(int mask, int x) {
  if (dp[mask][x] != 0)
    return dp[mask][x];
  if (x == m)
    return dp[mask][x] = 1;
  if (mask == 0)
    return 0;
  double ret = 0;
  rep(i, n) {
    if (mask & 1 << i) {
      double res = 0, pos = 1.0;
      REP(j, x, m) {
        res += rec(mask ^ (1 << i), j) * pos * (1.0 - a[i][j]);
        pos = pos * a[i][j];
      }
      res += pos;
      ret = max(res, ret);
    }
  }
  return dp[mask][x] = ret;
}
int main() {
  cin >> n >> m;
  rep(i, n) rep(j, m) { cin >> a[i][j]; }
  int all = 1 << n;
  cout << fixed << setprecision(12) << rec(all - 1, 0) << endl;
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> m >> n;
  vector<vector<double>> win(m, vector<double>(n));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> win[i][j];
    }
  }

  vector<vector<double>> dp(1 << m, vector<double>(m + 1, 1.0));
  for (int a = n - 1; a >= 0; --a) { // ツ攻ツ猟ェツつキツづゥツ敵
    vector<vector<double>> next(1 << m, vector<double>(m + 1, 0.0));
    for (int b = 1; b < (1 << m); ++b) { // ツ残ツづィツづ個猫
      for (
          int c = 0; c <= m;
          ++c) { // ツ選ツ妥ーツつオツつスツ猫(c==mツづ按づァツづ篠、ツづ慊つセツ選ツ妥ーツつオツづつ「ツづ按つ「)
        double ret = 0.0;
        if (c == m) {
          for (int i = 0; i < m; ++i) {
            bitset<16> bs(b);
            if (!bs[i])
              continue;

            double p = win[i][a] * dp[b][i];
            bs[i] = false;
            p += (1.0 - win[i][a]) * next[bs.to_ulong()][m];

            ret = max(ret, p);
          }
        } else {
          bitset<16> bs(b);
          ret = win[c][a] * dp[b][c];
          bs[c] = false;
          ret += (1.0 - win[c][a]) * next[bs.to_ulong()][m];
        }
        next[b][c] = ret;
      }
    }
    dp.swap(next);
  }

  printf("%.10f\\n", dp[(1 << m) - 1][m]);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> m >> n;
  vector<vector<double>> win(m, vector<double>(n));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> win[i][j];
    }
  }

  vector<vector<double>> dp(1 << m, vector<double>(m + 1, 1.0));
  for (int a = n - 1; a >= 0; --a) { // ツ攻ツ猟ェツつキツづゥツ敵
    vector<vector<double>> next(1 << m, vector<double>(m + 1, 0.0));
    for (int b = 1; b < (1 << m); ++b) { // ツ残ツづィツづ個猫
      for (
          int c = 0; c <= m;
          ++c) { // ツ選ツ妥ーツつオツつスツ猫(c==mツづ按づァツづ篠、ツづ慊つセツ選ツ妥ーツつオツづつ「ツづ按つ「)
        double ret = 0.0;
        for (int i = 0; i < m; ++i) {
          bitset<16> bs(b);
          if (!bs[i])
            continue;
          if (c != m && c != i)
            continue;

          double p = win[i][a] * dp[b][i];
          bs[i] = false;
          p += (1.0 - win[i][a]) * next[bs.to_ulong()][m];

          ret = max(ret, p);
        }
        next[b][c] = ret;
      }
    }
    dp.swap(next);
  }

  printf("%.10f\\n", dp[(1 << m) - 1][m]);

  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int M, N;
double P[16][16];

int i, j, k, S;
double win, lose, ans;
double dp[17][1 << 16][16];

int main(void) {
  cin >> M >> N;
  for (i = 0; i < M; i++)
    for (j = 0; j < N; j++)
      cin >> P[i][j];

  /* 状態:
   *   dp[i][S][j] =
   *     集合Sのねこをすでに使っていて,
   *     敵iとねこjが戦うときに最善の行動をした場合の確率
   * 基底:
   *   dp[N][S][j] = 1
   *   where S in [0, 1 << N), j in [0, M)
   * 遷移:
   *   dp[i][S][j] = dp[i + 1][S][j] * P[j][i] + max(dp[i][S | (1 << k)][k]) *
   * (1 - P[j][i]) where k not in S
   */

  for (S = 0; S < (1 << M); S++)
    for (j = 0; j < M; j++)
      dp[N % 2][S][j] = 1;

  for (i = N - 1; i >= 0; i--) {
    for (S = 0; S < (1 << M); S++)
      for (j = 0; j < M; j++)
        dp[i % 2][S][j] = 0;
    for (S = (1 << M) - 1; S >= 0; S--) {
      for (j = 0; j < M; j++)
        if (S & (1 << j)) {
          win = dp[(i + 1) % 2][S][j] * P[j][i];
          lose = 0;
          for (k = 0; k < M; k++)
            if (!(S & (1 << k))) {
              lose = max(lose, dp[i % 2][S | (1 << k)][k] * (1 - P[j][i]));
            }
          dp[i % 2][S][j] = max(dp[i % 2][S][j], win + lose);
        }
    }
  }

  ans = 0;
  for (i = 0; i < M; i++)
    ans = max(ans, dp[0][1 << i][i]);
  printf("%.15lf\\n", ans);
}


Error: MLE

Fixed submission:

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

int M, N;
double P[16][16];

int i, j, k, S;
double win, lose, ans;
double dp[17][1 << 16][16];

int main(void) {
  cin >> M >> N;
  for (i = 0; i < M; i++)
    for (j = 0; j < N; j++)
      cin >> P[i][j];

  /* 状態:
   *   dp[i][S][j] =
   *     集合Sのねこをすでに使っていて,
   *     敵iとねこjが戦うときに最善の行動をした場合の確率
   * 基底:
   *   dp[N][S][j] = 1
   *   where S in [0, 1 << N), j in [0, M)
   * 遷移:
   *   dp[i][S][j] = dp[i + 1][S][j] * P[j][i] + max(dp[i][S | (1 << k)][k]) *
   * (1 - P[j][i]) where k not in S
   */

  for (S = 0; S < (1 << M); S++)
    for (j = 0; j < M; j++)
      dp[N][S][j] = 1;

  for (i = N - 1; i >= 0; i--) {
    for (S = (1 << M) - 1; S >= 0; S--) {
      for (j = 0; j < M; j++)
        if (S & (1 << j)) {
          win = dp[i + 1][S][j] * P[j][i];
          lose = 0;
          for (k = 0; k < M; k++)
            if (!(S & (1 << k))) {
              lose = max(lose, dp[i][S | (1 << k)][k] * (1 - P[j][i]));
            }
          dp[i][S][j] = max(dp[i][S][j], win + lose);
        }
    }
  }

  ans = 0;
  for (i = 0; i < M; i++)
    ans = max(ans, dp[0][1 << i][i]);
  printf("%.15lf\\n", ans);
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, m;
double prob[20][20];
double dp[20][1 << 20];

double f(int opp, int rem) {
  if (dp[opp][rem] != -1) {
    return dp[opp][rem];
  }
  if (opp == n) {
    return dp[opp][rem] = 1.0;
  }
  if (rem == 0) {
    return dp[opp][rem] = 0.0;
  }

  double ret = 0;
  for (int i = 0; i < m; i++) {
    if ((rem >> i) & 1) {
      double win = 1.0;
      double res = 0;
      for (int j = opp; j <= n; j++) {
        res += win * (1.0 - prob[i][j]) * f(j, rem ^ (1 << i));
        win *= prob[i][j];
      }
      ret = max(ret, res);
    }
  }

  return dp[opp][rem] = ret;
}

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

  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> prob[i][j];
    }
  }

  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 1 << m; j++) {
      dp[i][j] = -1;
    }
  }

  cout << fixed << setprecision(15) << f(0, (1 << m) - 1) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define double long double

int n, m;
double prob[20][20];
double dp[20][1 << 20];

double f(int opp, int rem) {
  if (dp[opp][rem] != -1) {
    return dp[opp][rem];
  }
  if (opp == n) {
    return 1.0;
  }
  if (rem == 0) {
    return 0.0;
  }

  double ret = 0;
  for (int i = 0; i < m; i++) {
    if ((rem >> i) & 1) {
      double win = 1.0;
      double res = 0;
      for (int j = opp; j <= n; j++) {
        res += win * (1.0 - prob[i][j]) * f(j, rem ^ (1 << i));
        win *= prob[i][j];
      }
      ret = max(ret, res);
    }
  }

  return dp[opp][rem] = ret;
}

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

  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> prob[i][j];
    }
  }

  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 1 << m; j++) {
      dp[i][j] = -1;
    }
  }

  cout << fixed << setprecision(15) << f(0, (1 << m) - 1) << endl;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
  const int a = lives.to_ulong();
  if (!(memo[now][a] < -1)) {
    return memo[now][a];
  } else {
    ld amax = 0;
    for (int i = 0; i < M; ++i) {
      if (lives[i]) {
        lives[i] = false;
        ld per = 0;
        ld living = 1;
        for (int enemy = now; enemy < N; ++enemy) {
          per += living * (1 - pers[i][enemy]) * check(enemy, lives);
          living *= pers[i][enemy];
        }
        per += living;
        amax = max(amax, per);
        lives[i] = true;
      }
    }
    return memo[now][a] = amax;
  }
}

int main() {
  cin >> M >> N;
  pers.resize(M);
  for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 1 << 16; ++j) {
      memo[i][j] = -2;
    }
  }
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      ld p;
      cin >> p;
      pers[i].push_back(p);
    }
  }
  bitset<16> bs((1 << 16) - 1);
  ld ans = check(0, bs);
  cout << fixed << setprecision(22) << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
  const int a = lives.to_ulong();
  if (!memo[now][a] < -1) {
    return memo[now][a];
  } else {
    ld amax = 0;
    for (int i = 0; i < M; ++i) {
      if (lives[i]) {
        lives[i] = false;
        ld per = 0;
        ld living = 1;
        for (int enemy = now; enemy < N; ++enemy) {
          per += living * (1 - pers[i][enemy]) * check(enemy, lives);
          living *= pers[i][enemy];
        }
        per += living;
        amax = max(amax, per);
        lives[i] = true;
      }
    }
    return memo[now][a] = amax;
  }
}

int main() {
  cin >> M >> N;
  pers.resize(M);
  for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 1 << 16; ++j) {
      memo[i][j] = -2;
    }
  }
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      ld p;
      cin >> p;
      pers[i].push_back(p);
    }
  }
  bitset<16> bs((1 << 16) - 1);
  ld ans = check(0, bs);
  cout << fixed << setprecision(22) << ans << endl;
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n;
int m;
double dp[2][(1 << 16) + 1][17];
double P[17][17];

int main() {
  scanf("%d%d", &m, &n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lf", &P[i][j]);
    }
  }

  int dst = 0;
  int src = 1;
  int lim = 1 << m;
  for (int i = 1; i < lim; i++) {
    for (int j = 0; j < m; j++) {
      if (!(i >> j & 1))
        continue;
      dp[src][i][j] = 1.0;
    }
    dp[src][i][n] = 1.0;
  }

  for (int i = n - 1; i >= 0; --i) {
    for (int st = 0; st < lim; ++st) {
      for (int j = 0; j < m; j++) {
        if (!(st >> j & 1))
          continue;
        double p = 1.0;
        double cand = 0.0;
        int nst = st & ~(1 << j);
        cand += dp[dst][nst][m] * (1 - P[j][i]);
        cand += dp[src][st][j] * P[j][i];
        dp[dst][st][j] = cand;
        dp[dst][st][m] = max(dp[dst][st][m], cand);
      }
    }
    swap(src, dst);
    fill(dp[dst][0], dp[dst][lim], 0);
  }
  printf("%.20f\\n", dp[src][lim - 1][m]);
}

Error: MLE

Fixed submission:

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

int n;
int m;
double dp[17][(1 << 16) + 1][17];
double P[17][17];

int main() {
  scanf("%d%d", &m, &n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lf", &P[i][j]);
    }
  }

  int lim = 1 << m;
  for (int i = 1; i < lim; i++) {
    for (int j = 0; j < m; j++) {
      if (!(i >> j & 1))
        continue;
      dp[n][i][j] = 1.0;
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    for (int st = 0; st < lim; ++st) {
      for (int j = 0; j < m; j++) {
        if (!(st >> j & 1))
          continue;
        double p = 1.0;
        double cand = 0.0;
        int nst = st & ~(1 << j);
        cand += dp[i][nst][n] * (1 - P[j][i]);
        cand += dp[i + 1][st][j] * P[j][i];
        dp[i][st][j] = cand;
        dp[i][st][n] = max(dp[i][st][n], cand);
      }
    }
  }
  printf("%.20f\\n", dp[0][lim - 1][n]);
}