Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

double prob[16][16];
double dp[1 << 16][17];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> M >> N;
  REP(i, M) REP(j, N) cin >> prob[i][j];

  double ans = 0.;
  FOR(b, 1, 1 << M)
  dp[b][N] = 1.;
  for (int i = N - 1; i >= 0; --i) {
    for (int b = 0; b < 1 << M; ++b) {
      for (int j = 0; j < M; ++j) {
        if ((b >> j & 1) == 0)
          continue;
        double p = 1., sum = 0.;
        for (int k = i; k < N; ++k) {
          sum += p * (1. - prob[j][k]) * dp[b & ~(1 << j)][k];
          p *= prob[j][k];
        }
        sum += p * dp[b][N];
        dp[b][i] = max(dp[b][i], sum);
      }
    }
  }

  ans = dp[(1 << M) - 1][0];
  cout << fixed << setprecision(10) << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

double prob[16][16];
double dp[1 << 16][17][16];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> M >> N;
  REP(i, M) REP(j, N) cin >> prob[i][j];

  double ans = 0.;
  FOR(b, 1, 1 << M) REP(i, M) if (b >> i & 1) dp[b][N][i] = 1.;
  for (int b = 1; b < 1 << M; ++b) {
    for (int i = N - 1; i >= 0; --i) {
      for (int j = 0; j < M; ++j) {
        if (b >> j & 1 == 0)
          continue;
        int x = 0;
        REP(k, M)
        if ((b >> k & 1) && dp[b & ~(1 << j)][i][k] > dp[b & ~(1 << j)][i][x])
            x = k;
        dp[b][i][j] =
            max(dp[b][i][j], prob[j][i] * dp[b][i + 1][j] +
                                 (1. - prob[j][i]) * dp[b & ~(1 << j)][i][x]);
      }
    }
  }

  REP(i, M)
  ans = max(ans, dp[(1 << M) - 1][0][i]);
  cout << fixed << setprecision(10) << ans << endl;

  return 0;
}