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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<double> VD;
typedef vector<VD> VVD;

double dist(double r1, double r2) {
  return sqrt(pow(r1 + r2, 2) - pow(r1 - r2, 2));
}

string solve(int x, int n, VI rs) {
  VVD dp(1 << n, VD(n, DBL_MAX));

  REP(i, 0, n) { dp[1 << i][i] = rs[i]; }

  REP(i, 1, 1 << n) {
    REP(j, 0, n) {
      if (dp[i][j] == DBL_MAX) {
        continue;
      }

      REP(k, 0, n) {
        if ((i >> k & 1)) {
          continue;
        }

        dp[i | 1 << k][k] =
            min(dp[i | 1 << k][k], dp[i][j] + dist(rs[j], rs[k]));
      }
    }
  }

  REP(i, 0, n) {
    if (dp.back()[i] + rs[i] <= x) {
      return "OK";
    }
  }

  return "NA";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string str;

  while (getline(cin, str)) {
    ISS iss(str);

    int n;
    iss >> n;

    VI rs;
    int tmp;

    while (iss >> tmp) {
      rs.PB(tmp);
    }

    cout << solve(n, rs.size(), rs) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define UREP(i, m, n)                                                          \\
  for (unsigned int i = (unsigned int)(m); i < (unsigned int)(n); i++)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int main() {
  string line;

  while (getline(cin, line)) {
    istringstream iss(line);

    int l;
    iss >> l;

    VI r;
    int in;

    while (iss >> in) {
      r.PB(in);
    }

    bool able = false;
    sort(ALL(r));

    do {
      double curr = r.front() + r.back();

      REP(i, 0, r.size() - 1) {
        curr += sqrt(pow(r[i] + r[i + 1], 2) - pow(abs(r[i] - r[i + 1]), 2));
      }

      if (curr <= l) {
        able = true;
        break;
      }
    } while (next_permutation(ALL(r)));

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

  return 0;
}