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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i, n) repeat_from(i, 0, n)
#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= (m); --(i))
#define repeat_reverse(i, n) repeat_from_reverse(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
using namespace std;
struct mobile_t {
  int w;    // weight
  double l; // left
  double r; // right
};
const double eps = 0.0001;
double evaluate(vector<int> const &p, double r) {
  stack<mobile_t> stk;
  for (int w : p) {
    if (w == 0) {
      if (stk.size() < 2)
        return -1;
      mobile_t x = stk.top();
      stk.pop();
      mobile_t y = stk.top();
      stk.pop();
      mobile_t z;
      z.w = x.w + y.w;
      double dx = y.w / (double)(x.w + y.w);
      double dy = x.w / (double)(x.w + y.w);
      z.l = max(x.l + dx, y.l - dy);
      z.r = max(x.r - dx, y.r + dy);
      if (r + eps < z.r + z.l)
        return -1;
      stk.push(z);
    } else {
      stk.push((mobile_t){w, 0.0, 0.0});
    }
  }
  if (stk.size() != 1)
    return -1;
  return stk.top().l + stk.top().r;
}
int main() {
  int datasets;
  cin >> datasets;
  repeat(dataset, datasets) {
    double r;
    cin >> r;
    int s;
    cin >> s;
    vector<int> w(s);
    repeat(i, s) cin >> w[i];
    double result = -1;
    vector<int> p;
    repeat(i, s) p.push_back(w[i]);
    repeat(i, s - 1) p.push_back(0);
    sort(p.begin(), p.end());
    do {
      result = max(result, evaluate(p, r));
    } while (next_permutation(p.begin(), p.end()));
    if (result == -1) {
      cout << -1 << endl;
    } else {
      printf("%.16lf\\n", result);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i, n) repeat_from(i, 0, n)
#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= (m); --(i))
#define repeat_reverse(i, n) repeat_from_reverse(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
using namespace std;
struct mobile_t {
  int w;    // weight
  double l; // left
  double r; // right
};
const double eps = 0.0001;
double evaluate(string const &pat, double r, vector<int> const &w) {
  stack<mobile_t> stk;
  for (char c : pat) {
    if (c == 'L' or c == 'R') {
      if (stk.size() < 2)
        return -1;
      mobile_t x = stk.top();
      stk.pop();
      mobile_t y = stk.top();
      stk.pop();
      mobile_t z;
      z.w = x.w + y.w;
      double xofs = y.w / (double)(x.w + y.w);
      double yofs = x.w / (double)(x.w + y.w);
      z.l = max(x.l + xofs, y.l - yofs);
      z.r = max(x.r - xofs, y.r + yofs);
      if (c == 'R') {
        z = {z.w, z.r, z.l};
      }
      if (r + eps < z.r + z.l)
        return -1;
      stk.push(z);
    } else {
      int i = c - '0';
      stk.push((mobile_t){w[i], 0.0, 0.0});
    }
  }
  if (stk.size() != 1)
    return -1;
  return stk.top().l + stk.top().r;
}
int main() {
  int datasets;
  cin >> datasets;
  repeat(dataset, datasets) {
    double r;
    cin >> r;
    int s;
    cin >> s;
    vector<int> w(s);
    repeat(i, s) cin >> w[i];
    double result = -1;
    repeat(ln, s - 1 + 1) {
      int rn = s - 1 - ln;
      string pat;
      repeat(i, s) pat += i + '0';
      repeat(i, ln) pat += 'L';
      repeat(i, rn) pat += 'R';
      sort(pat.begin(), pat.end());
      do {
        result = max(result, evaluate(pat, r, w));
      } while (next_permutation(pat.begin(), pat.end()));
    }
    if (result == -1) {
      cout << -1 << endl;
    } else {
      printf("%.16lf\\n", result);
    }
  }
  return 0;
}