Problem p00120 (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;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

vector<ld> rs;
ld memo[13][1 << 12];
ld getans(const int from, bitset<12> &bs) {
  if (memo[from][bs.to_ulong()] < -1) {
    if (bs.count() == 0)
      return 0;
    ld ans = 1e18;
    for (int i = 0; i < rs.size(); ++i) {
      if (bs[i]) {
        ld plus;
        ld a = rs[i];
        if (from == 12) {
          plus = 2 * a;
        } else {
          ld b = rs[from];
          plus = sqrt((a + b) * (a + b) - (a - b) * (a - b)) - b + a;
        }
        bs[i] = false;
        ans = min(ans, plus + getans(i, bs));
        bs[i] = true;
      }
    }
    memo[from][bs.to_ulong()] = ans;
  }
  return memo[from][bs.to_ulong()];
}
vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}
int main() {
  string st;
  while (getline(cin, st)) {
    vector<string> sts = split(st, ' ');
    int W = stoi(sts[0]);
    rs.clear();
    for (int i = 1; i < sts.size(); ++i) {
      rs.push_back(stold(sts[i]));
    }
    for (int i = 0; i < 13; ++i) {
      for (int j = 0; j < (1 << 12); ++j) {
        memo[i][j] = -2;
      }
    }

    bitset<12> bs((1 << rs.size()) - 1);
    ld sum = getans(12, bs);
    if (sum > W)
      cout << "NA" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int dp[40][1 << 20];
double sho[20][20];
int main() {
  string st;
  while (getline(cin, st)) {
    memset(sho, 0, sizeof(sho));
    vector<string> input(split(st, ' '));
    int L = stoi(input[0]);
    vector<int> rads;
    int sum = 0;
    for (int i = 1; i < input.size(); ++i) {
      rads.push_back(stoi(input[i]));
      sum += rads.back() * 2;
    }
    for (int i = 0; i < rads.size(); ++i) {
      for (int j = i + 1; j < rads.size(); ++j) {
        sho[i][j] = (rads[i] + rads[j]) -
                    sqrt(double((rads[i] + rads[j]) * (rads[i] + rads[j]) -
                                ((rads[i] - rads[j]) * (rads[i] - rads[j]))));
        sho[j][i] = sho[i][j];
      }
    }
    vector<int> perms(rads.size());
    iota(perms.begin(), perms.end(), 0);
    double maxsho = 0;
    do {
      double nowsho = 0;
      for (int i = 0; i < perms.size() - 1; ++i) {
        nowsho += sho[perms[i]][perms[i + 1]];
      }
      maxsho = max(maxsho, nowsho);
    } while (next_permutation(perms.begin(), perms.end()));
    if (L > sum - maxsho) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}