Problem p00120 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000007;
const int INF = 100000000; // 1e8

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> void splitstr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}

vector<int> a;
double memo[12][1 << 12];

double solve(int start, int flag, int rest) {
  if (rest == 0)
    return memo[start][flag] = a[start];
  double ret = INF;
  FOR(i, sz(a)) {
    if ((1 << i) & flag)
      continue;
    double add =
        solve(i, flag | (1 << i), rest - 1) + sqrt((double)a[start] * a[i]) * 2;
    ret = min(ret, add);
  }
  return memo[start][flag] = ret;
}

int main() {
  string s;
  while (getline(cin, s)) {
    splitstr(s, a);
    int n = a[0];
    a.erase(a.begin());
    int m = sz(a);

    double mn = INF;
    FOR(j, m) fill(memo[j], memo[j] + (1 << m), -1.0);
    FOR(i, m) { mn = min(mn, solve(i, 1 << i, m - 1) + a[i]); }

    cout << (n >= mn ? "OK" : "NA") << endl;
  }
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000007;
const int INF = 100000000; // 1e8

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> void splitstr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}

vector<int> a;
double memo[12][1 << 12];

double solve(int start, int flag, int rest) {
  if (memo[start][flag] > 0)
    return memo[start][flag];
  if (rest == 0)
    return memo[start][flag] = a[start];
  double ret = INF;
  FOR(i, sz(a)) {
    if ((1 << i) & flag)
      continue;
    double add =
        solve(i, flag | (1 << i), rest - 1) + sqrt((double)a[start] * a[i]) * 2;
    ret = min(ret, add);
  }
  return memo[start][flag] = ret;
}

int main() {
  string s;
  while (getline(cin, s)) {
    splitstr(s, a);
    int n = a[0];
    a.erase(a.begin());
    int m = sz(a);

    double mn = INF;
    FOR(j, m) fill(memo[j], memo[j] + (1 << m), -1.0);
    FOR(i, m) { mn = min(mn, solve(i, 1 << i, m - 1) + a[i]); }

    cout << (n >= mn ? "OK" : "NA") << endl;
  }
  return 0;
}