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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

typedef vector<double> vd;
typedef vector<vd> vvd;

int n;
double dfs(int mask, int pos, int last, vvd &dp, vi &r) {
  if (pos == n) {
    return r[last];
  }
  if (dp[mask][last]) {
    return dp[mask][last];
  }
  double ret = INT_MAX;
  REP(i, n) {
    if (!((1 << i) & mask)) {
      ret = min(ret, dfs(mask | (1 << i), pos + 1, i, dp, r) +
                         sqrt(r[last] * r[i]) * 2);
    }
  }
  return dp[mask][last] = ret;
}

int main() {
  string line;
  while (getline(cin, line)) {
    istringstream iss(line);
    int l;
    iss >> l;
    vi r;
    int rr;
    while (iss >> rr) {
      r.push_back(rr);
    }
    n = r.size();

    double ans = INT_MAX;
    REP(i, n) {
      vvd dp(1 << n, vd(n));
      ans = min(ans, dfs(1 << i, 1, i, dp, r) + r[i]);
    }
    cout << (ans < l + EPS ? "OK" : "NA") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

double getlen(vi &a) {
  double len = a[0] + a[a.size() - 1];
  REP(i, a.size() - 1) { len += sqrt(a[i] * a[i + 1]) * 2; }
  return len;
}

int n;
double dfs(map<int, int> &m, vi &s) {
  if (s.size() == n) {
    return getlen(s);
  }
  double ret = INT_MAX;
  bool flag = false;
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
    if (it->second) {
      flag = true;
      map<int, int> nm(m);
      vi ns(s);
      nm[it->first]--;
      ns.push_back(it->first);
      ret = min(ret, dfs(nm, ns));
    }
  }
  return ret;
}

int main() {
  int l;
  string s;
  while (getline(cin, s)) {
    istringstream iss(s);
    iss >> l;
    vi a;
    int aa;
    while (iss >> aa) {
      a.push_back(aa);
    }
    n = a.size();

    bool ok = false;
    map<int, int> m;
    REP(i, a.size()) { m[a[i]]++; }
    vi s;
    double d = dfs(m, s);

    cout << (d < l + EPS ? "OK" : "NA") << endl;
  }
}