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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
double memo[1 << 12][12][12];
double dist(double a, double b) {
  if (a < b)
    swap(a, b);
  return sqrt(((a + b) * (a + b)) - ((a - b) * (a - b)));
}
double search(int bit, int dep, int prev, vi cake) {
  if (dep == cake.size()) {
    return (double)cake[prev];
  }
  if (bit != 0 && memo[bit][dep][prev]) {
    return memo[bit][dep][prev];
  }
  double ret = INF;
  rep(i, cake.size()) {
    if (!(bit >> i & 1)) {
      if (bit == 0) {
        ret = min(ret, search(bit | 1 << i, dep + 1, i, cake) + cake[i]);
      } else {
        ret = min(ret, search(bit | 1 << i, dep + 1, i, cake) +
                           dist(cake[prev], cake[i]));
      }
    }
  }
  if (prev != -1)
    memo[bit][dep][prev] = ret;
  return ret;
}
int main() {
  string str;
  while (getline(cin, str)) {
    memset(memo, 0, sizeof(memo));
    stringstream ss(str);
    vi cake;
    int tmp;
    double len;
    bool f = false;
    while (ss >> tmp) {
      if (!f)
        len = (double)tmp;
      else
        cake.PB(tmp);
      f = true;
    }
    double ret = search(0, 0, -1, cake);
    // printf("%lf \\n",ret);
    if (len - ret > -EPS)
      puts("OK");
    else
      puts("NA");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const int EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
double memo[1 << 12][12];
double dist(double a, double b) {
  if (a < b)
    swap(a, b);
  return sqrt(((a + b) * (a + b)) - ((a - b) * (a - b)));
}
double search(int bit, int dep, int prev, vi cake) {
  if (dep == cake.size()) {
    return (double)cake[prev];
  }
  if (memo[bit][dep]) {
    return memo[bit][dep];
  }
  double ret = INF;
  rep(i, cake.size()) {
    if (!(bit >> i & 1)) {
      if (bit == 0) {
        ret = min(ret, search(bit | 1 << i, dep + 1, i, cake) + cake[i]);
      } else {
        ret = min(ret, search(bit | 1 << i, dep + 1, i, cake) +
                           dist(cake[prev], cake[i]));
      }
    }
  }
  return memo[bit][prev] = ret;
}
int main() {
  int len;
  while (~scanf("%d", &len)) {
    rep(i, 1 << 12) {
      rep(j, 12) { memo[i][j] = 0.0000; }
    }
    vi cake;
    char c;
    while (~(c = getchar())) {
      if (c == '\\n')
        break;
      else if (c == ' ') {
        int tmp;
        scanf("%d", &tmp);
        cake.PB(tmp);
      }
    }
    double ret = search(0, 0, -1, cake);
    if (len - ret >= 0.0)
      puts("OK");
    else
      puts("NA");
  }
  return 0;
}