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

#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

typedef pair<double, double> P;
typedef vector<P> PV;

double room, w[10], wts[1 << 10];
int s;

bool onlyOne(int bit) {
  int cnt = 0;
  for (int i = 0; i < s; i++) {
    if (bit & (1 << i)) {
      cnt++;
    }
  }
  return cnt == 1;
}

PV solve(double x, int rem) {
  PV res;

  if (onlyOne(rem)) {
    res.push_back(P(x, x));
    return res;
  }

  for (int l = 1; l <= rem; l++) {
    if ((rem | l) != rem)
      continue;

    double wl = wts[l];

    for (int r = 1; r <= rem; r++) {
      if (l & r)
        continue;
      if ((l | r) != rem)
        continue;

      double wr = wts[r];
      double a = wr / (wl + wr);

      PV pvl = solve(x - a, l);
      PV pvr = solve(x + (1.0 - a), r);

      for (int i = 0; i < pvl.size(); i++) {
        for (int j = 0; j < pvr.size(); j++) {
          double L = min(pvl[i].first, pvr[j].first);
          double R = max(pvl[i].second, pvr[j].second);
          if (R - L > room)
            continue;
          res.push_back(P(L, R));
        }
      }
    }
  }

  return res;
}

int main() {
  int T;
  cin >> T;

  while (T--) {
    cin >> room >> s;
    for (int i = 0; i < s; i++) {
      cin >> w[i];
    }

    for (int i = 0; i < (1 << s); i++) {
      wts[i] = 0;

      for (int j = 0; j < s; j++) {
        if (i & (1 << j)) {
          wts[i] += w[j];
        }
      }
    }

    PV pv = solve(0, (1 << s) - 1);

    double ans = -1;

    for (int i = 0; i < pv.size(); i++) {
      ans = max(ans, pv[i].second - pv[i].first);
    }

    if (ans == -1) {
      cout << -1 << endl;
    } else {
      cout << fixed << setprecision(10) << ans << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<double, double> P;

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-10)

double r;
int n;
double w[10];

vector<P> solve(vector<int> rem, double x) {
  vector<P> res;
  int n = rem.size();

  if (n == 1) {
    res.push_back(P(x, x));
    return res;
  }

  sort(rem.begin(), rem.end());

  do {
    for (int i = 1; i < n; i++) {
      vector<int> left_rem(rem.begin(), rem.begin() + i);
      vector<int> right_rem(rem.begin() + i, rem.end());
      double lw = 0;
      double rw = 0;
      rep(j, i) lw += w[left_rem[j]];
      rep(j, n - i) rw += w[right_rem[j]];

      double lx = x - rw / (lw + rw);
      double rx = x + 1 - rw / (lw + rw);

      vector<P> lv = solve(left_rem, lx);
      vector<P> rv = solve(right_rem, rx);

      rep(i, lv.size()) {
        rep(j, rv.size()) {
          res.push_back(P(min(lv[i].first, rv[j].first),
                          max(lv[i].second, rv[j].second)));
        }
      }
    }
  } while (next_permutation(rem.begin(), rem.end()));

  return res;
}

int main() {
  int T;
  cin >> T;

  while (T--) {
    cin >> r >> n;
    rep(i, n) { cin >> w[i]; }

    vector<int> rem;
    rep(i, n) rem.push_back(i);

    double ans = -1;
    vector<P> v = solve(rem, 0);
    rep(i, v.size()) {
      if (v[i].second - v[i].first < r) {
        ans = max(ans, v[i].second - v[i].first);
      }
    }
    if (ans == -1)
      printf("-1\\n");
    else
      printf("%.16f\\n", ans);
  }
}