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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll m, n;
  cin >> m >> n;
  vector<vector<double>> p(m, vector<double>(n));
  REP(i, m) REP(j, n) cin >> p[i][j];

  // dp[?????????????????° % 2][????´???£???????????????][????????????] = ??????
  vector<vvd> dp(2, vvd(m, vd(1ll << m, 0)));
  // win_max_p[??????????????????????????????????????°][????????§?????? |
  // ????????????] = ????????????????´???£??????????????????
  vvd win_max_p(n, vd(1ll << m, 0));
  REP(i, 1ll << m) REP(j, m) dp[n % 2][j][i & ~(1ll << j)] = 1;

  for (ll i = n - 1; i >= 0; --i)
    for (ll j = (1ll << m) - 1; j >= 0; --j) {
      REP(k, m) if ((j & (1ll << k)) == 0) {
        dp[i % 2][k][j] = win_max_p[i][j | (1ll << k)] * (1 - p[k][i]) +
                          dp[(i + 1) % 2][k][j] * p[k][i];
        win_max_p[i][j] = max(win_max_p[i][j], dp[i % 2][k][j]);
      }
    }
  cout << win_max_p[0][0] << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll m, n;
  cin >> m >> n;
  vector<vector<double>> p(m, vector<double>(n));
  REP(i, m) REP(j, n) cin >> p[i][j];

  // dp[?????????????????°][????´???£???????????????][????????????] = ??????
  vector<vvd> dp(n + 1, vvd(m, vd(1ll << m, 0)));
  // win_max_p[??????????????????????????????????????°][????????§?????? |
  // ????????????] = ????????????????´???£??????????????????
  vvd win_max_p(n, vd(1ll << m, 0));
  REP(i, 1ll << m) REP(j, m) dp[n][j][i & ~(1ll << j)] = 1;

  for (ll i = n - 1; i >= 0; --i)
    for (ll j = (1ll << m) - 1; j >= 0; --j) {
      REP(k, m) if ((j & (1ll << k)) == 0) {
        dp[i][k][j] = win_max_p[i][j | (1ll << k)] * (1 - p[k][i]) +
                      dp[i + 1][k][j] * p[k][i];
        win_max_p[i][j] = max(win_max_p[i][j], dp[i][k][j]);
      }
    }
  cout << win_max_p[0][0] << endl;

  return 0;
}