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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;

vector<double> a;
double memo[1 << 12][13];
double calc(int a1, int a2) {
  double t = a[a1] + a[a2];
  double s = a[a1] - a[a2];
  return sqrt(t * t - s * s);
}

double dp(int bit, int s) {
  if (memo[bit][s] >= 0)
    return memo[bit][s];
  double ans = 9999999999999.0;
  bool ok = true;
  for (int i = 0; i < a.size(); i++) {
    if (!(bit & (1 << i))) {
      ok = false;
      double x = (bit ? 0.0 : a[i]);
      ans = min(ans, dp(bit | (1 << i), i) + calc(i, s) + x);
    }
  }
  if (ok)
    return a[s];
  return memo[bit][s] = ans;
}
int main() {
  string s;
  while (getline(cin, s)) {
    for (auto &x : memo)
      for (auto &y : x)
        y = -1.0;
    a.clear();
    stringstream ss(s);
    double n;
    ss >> n;
    double temp;
    while (ss >> temp) {
      a.push_back(temp);
    }
    bool ok = false;
    double ans = 99999999999999999;
    for (int i = 0; i < a.size(); i++) {
      ans = min(ans, dp(1 << i, i) + a[i]);
    }
    ok = (ans <= n);
    if (ok)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
double calc(vector<double> a) {
  double ans = 0;
  for (int i = 0; i < a.size() - 1; i++) {
    double t = a[i] + a[i + 1];
    double r = abs(a[i] - a[i + 1]);
    ans += sqrt(t * t - r * r);
  }
  ans += a.front() + a.back();
  return ans;
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream ss(s);
    double n;
    vector<double> a, A;
    ss >> n;
    double temp;
    while (ss >> temp) {
      a.push_back(temp);
    }
    bool ok = false;
    sort(all(a));
    double ans = n * n;
    do {
      ans = min(ans, calc(a));
    } while (next_permutation(all(a)));

    ok = (ans <= n);

    if (ok)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}