alexjercan/bugnet huggingface dataset:
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double INF = 10000000;
double l;
bool flag;
string str;
vector<double> r;
double dp[1 << 12][12];

void rec(int x, int bit, double nl, double nh) {
  if (dp[bit][x] < nl)
    return;
  dp[bit][x] = nl;
  if (bit == (1 << r.size()) - 1) {
    if (nl + nh <= l)
      flag = true;
    return;
  } else {
    for (int i = 0; i < r.size(); i++) {
      if (!(bit >> i & 1)) {
        double nnh = r[i];
        double nnl = nl + sqrt(pow(nh + nnh, 2) - pow(fabs(nh - nnh), 2));
        if (nnl < dp[bit | 1 << i][i]) {
          dp[bit | 1 << i][i] = nnl;
          rec(i, bit | 1 << i, nnl, nnh);
        }
      }
    }
  }
}

int main(void) {
  while (getline(cin, str)) {
    flag = false;
    r.clear();
    for (int i = 0; i < (1 << 12); i++) {
      for (int j = 0; j < 12; j++) {
        dp[i][j] = INF;
      }
    }
    stringstream input(str);
    input >> l;
    double a;
    while (input >> a)
      r.push_back(a);
    for (int i = 0; i < r.size(); i++) {
      rec(i, 1 << i, r[i], r[i]);
    }
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double INF = 10000000;
double ans;
double l;
bool flag;
string str;
vector<double> r;
double dp[1 << 12][12];

void rec(int x, int bit, double nl, double nh) {
  if (dp[bit][x] < nl)
    return;
  dp[bit][x] = nl;
  if (bit == (1 << r.size()) - 1) {
    if (nl + nh <= l)
      flag = true;
    return;
  } else {
    for (int i = 0; i < r.size(); i++) {
      if (!(bit >> i & 1)) {
        double nnh = r[i];
        double nnl = nl + sqrt(pow(nh + nnh, 2) - pow(fabs(nh - nnh), 2));
        rec(i, bit | 1 << i, nnl, nnh);
      }
    }
  }
}

int main(void) {
  while (getline(cin, str)) {
    flag = false;
    r.clear();
    for (int i = 0; i < (1 << 12) - 1; i++) {
      for (int j = 0; j < 12; j++) {
        dp[i][j] = INF;
      }
    }
    stringstream input(str);
    input >> l;
    double a;
    while (input >> a)
      r.push_back(a);
    for (int i = 0; i < r.size(); i++) {
      rec(i, 1 << i, r[i], r[i]);
    }
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int r[12], n;
double tbl_sqrt[101];

// memo[stat][last] :
//  Ì¶[©çÉcakeðu¢Ä¢­B
// u¢½cakeªstat(bitÅ\\»)AÅãÉu¢½cakeªlastÌÆ«ÉA
// cèÌcakeðu­ÌÉKvÈÅ¬ªmemo[stat][last]B
double memo[1 << 12][12];

double solve(int stat, int last) {
  // printf("stat : %d\\n",stat);
  if (memo[stat][last] > 1e-6)
    return memo[stat][last];

  if (stat == 0)
    return memo[stat][last] = r[last];

  double wmin = 10000;
  for (int i = 0; i < n; i++) {
    if ((stat & (1 << i)) == 0)
      continue;
    stat &= ~(1 << i);
    wmin = min(wmin, 2 * tbl_sqrt[r[last] * r[i]] + solve(stat, i));
    stat |= (1 << i);
  }
  // printf("stat : %d, wmin : %.3f\\n",stat,wmin);
  return memo[stat][last] = wmin;
}

int main() {
  for (int i = 1; i <= 100; i++)
    tbl_sqrt[i] = sqrt(i);

  int box;
  while (~scanf("%d ", &box)) {
    for (n = 1;; n++) {
      scanf("%d", r + n - 1);
      if (getchar() == '\\n')
        break;
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < (1 << n); j++)
        memo[j][i] = 0;

    // printf("n=%d\\n",n);
    double ans = 10000;
    for (int i = 0; i < n; i++) {
      int stat = ((1 << n) - 1) & ~(1 << i);
      // printf("start stat : %d\\n",stat);
      ans = min(ans, r[i] + solve(stat, i));
      // printf("stat : %d, ans : %.3f\\n",stat,ans);
    }
    // printf("ans = %.5f\\n",ans);
    puts(ans <= box ? "OK" : "NA");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  double tbl_sqrt[101];
  for (int i = 1; i <= 100; i++)
    tbl_sqrt[i] = sqrt(i);

  int box;
  while (~scanf("%d ", &box)) {
    int r[12], n;
    for (n = 1;; n++) {
      scanf("%d", r + n - 1);
      if (getchar() == '\\n')
        break;
    }

    sort(r, r + n);

    double w, wmin = 2 * box;
    do {
      w = r[0] + r[n - 1];
      for (int i = 0; i < n - 1; i++)
        w += 2 * tbl_sqrt[r[i] * r[i + 1]];
      wmin = min(wmin, w);
    } while (next_permutation(r, r + n));

    puts(wmin <= box ? "OK" : "NA");
  }

  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SUPrl 13
#define UNDEFCHAN 1145

int rl;
int rs[SUPrl];
int L;
double memf[1 << SUPrl][SUPrl];

void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; }

double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 +
                          // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2
  if (memf[s][ri] != UNDEFCHAN)
    return memf[s][ri];
  double re = 0;

  int a = 0;
  FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++;

  if (a == 1)
    re = 0;
  else {
    FOR(bri, 0, rl) {
      if ((s >> bri & 1) == 0)
        continue;
      if (bri == ri)
        continue;
      re =
          max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2));
    }
  }

  memf[s][ri] = re;
  return re;
}

void showf() {
  FOR(s, 0, 1 << rl)
      FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl;
}

double solve() {
  double re = 0;
  FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri));
  re = -re;
  FOR(ri, 0, rl) re += 2 * rs[ri];
  return re;
}

int main() {
  while (cin >> L) { // if(!L) return 0;
    rl = 0;
    while (cin.peek() != '\\n') {
      cin >> rs[rl++];
    }
    resetf();
    double re = solve();
    // showf();
    if (re <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SUPrl 13
#define UNDEFCHAN 1145

int rl;
int rs[SUPrl];
int L;
double memf[1 << SUPrl][SUPrl];

void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; }

double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 +
                          // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2
  if (memf[s][ri] != UNDEFCHAN)
    return memf[s][ri];
  double re = 0;

  int a = 0;
  FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++;

  if (a == 1)
    re = 0;
  else {
    FOR(bri, 0, rl) {
      if ((s >> bri & 1) == 0)
        continue;
      if (bri == ri)
        continue;
      re =
          max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2));
    }
  }

  memf[s][ri] = re;
  return re;
}

void showf() {
  FOR(s, 0, 1 << rl)
      FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl;
}

double solve() {
  double re = 0;
  FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri));
  re = -re;
  FOR(ri, 0, rl) re += 2 * rs[ri];
  return re;
}

int main() {
  while (!cin.eof()) {
    cin >> L; // if(!L) return 0;
    rl = 0;
    while (cin.peek() != '\\n') {
      cin >> rs[rl++];
    }
    resetf();
    double re = solve();
    // showf();
    if (re <= L)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

double dp[1 << 15][15];
int r[10];
int main() {
  string s;
  while (getline(cin, s)) {
    stringstream buf(s);
    double w;
    buf >> w;
    int p = 0;
    while (buf >> r[p++])
      ;
    p--;
    fill(dp[0], dp[1 << 15], INF);
    rep(i, p) dp[1 << i][r[i]] = r[i] * 2;
    rep(i, 1 << p) {
      rep(k, 11) {
        rep(j, p) {
          if (i >> j & 1)
            continue;
          double d = sqrt((k + r[j]) * (k + r[j]) - (k - r[j]) * (k - r[j])) -
                     k + r[j];
          dp[i | 1 << j][r[j]] = min(dp[i | 1 << j][r[j]], dp[i][k] + d);
        }
      }
    }
    rep(i, 11) {
      if (dp[(1 << p) - 1][i] <= w) {
        puts("OK");
        goto g;
      }
    }
    puts("NA");
  g:;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

double dp[1 << 10][11];
int r[10];
int main() {
  string s;
  while (getline(cin, s)) {
    stringstream buf(s);
    double w;
    buf >> w;
    int p = 0;
    while (buf >> r[p++])
      ;
    p--;
    fill(dp[0], dp[1 << 10], INF);
    rep(i, p) dp[1 << i][r[i]] = r[i] * 2;
    rep(i, 1 << p) {
      rep(k, 11) {
        rep(j, p) {
          if (i >> j & 1)
            continue;
          double d = sqrt((k + r[j]) * (k + r[j]) - (k - r[j]) * (k - r[j])) -
                     k + r[j];
          dp[i | 1 << j][r[j]] = min(dp[i | 1 << j][r[j]], dp[i][k] + d);
        }
      }
    }
    rep(i, 11) {
      if (dp[(1 << p) - 1][i] <= w) {
        puts("OK");
        goto g;
      }
    }
    puts("NA");
  g:;
  }
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;

#define Sq(c) ((c) * (c))

const int MAX_N = 13;
const int INF = 1 << 27;

int n;
double r[MAX_N];
double dp[(1 << MAX_N)][MAX_N];
double dist[MAX_N][MAX_N];

double rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];

  if (S == (1 << n) - 1 && v == 0)
    return dp[S][v] = 0;

  double res = INF;
  for (int u = 0; u < n; u++) {
    if (!(S >> u & 1))
      res = min(res, rec(S | 1 << u, u) + dist[v][u]);
  }
  return dp[S][v] = res;
}

int main() {

  double width;
  string str;

  while (getline(cin, str)) {
    stringstream ss(str);

    ss >> width;
    n = 0;
    while (ss >> r[n])
      n++;

    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = sqrt(Sq(r[i] + r[j]) - Sq(r[i] - r[j]));
      }
    }
    // テ・ツキツヲテ・ツ渉ウテ、ツクツ。テァツォツッテ」ツ?ォテヲツウツィテヲツ??
    for (int i = 0; i < n; i++) {
      dist[i][n] = dist[n][i] = r[i];
    }
    n++;

    if (rec(0, 0) <= width)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;

    // printf("%lf\\n", dp[0][0]);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;

#define Sq(c) ((c) * (c))

const int MAX_N = 12;
const int INF = 1 << 27;

int n;
double r[MAX_N + 2];
double dp[(1 << MAX_N) + 2][MAX_N + 2];
double dist[MAX_N + 2][MAX_N + 2];

double rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];

  if (S == (1 << n) - 1 && v == 0)
    return dp[S][v] = 0;

  double res = INF;
  for (int u = 0; u < n; u++) {
    if (!(S >> u & 1))
      res = min(res, rec(S | 1 << u, u) + dist[v][u]);
  }
  return dp[S][v] = res;
}

int main() {

  double width;
  string str;

  while (getline(cin, str)) {
    stringstream ss(str);

    ss >> width;
    n = 0;
    while (ss >> r[n])
      n++;

    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = sqrt(Sq(r[i] + r[j]) - Sq(r[i] - r[j]));
      }
    }
    // テ・ツキツヲテ・ツ渉ウテ、ツクツ。テァツォツッテ」ツ?ォテヲツウツィテヲツ??
    for (int i = 0; i < n; i++) {
      dist[i][n] = dist[n][i] = r[i];
    }
    n++;

    if (rec(0, 0) <= width)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;

    // printf("%lf\\n", dp[0][0]);
  }

  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
double F(double r, double s) {
  return (r + s) * ::cos(asin((r - s) / (r + s)));
}
int main() {
  int l, a[12], n, i, j, k;
  char *p, s[256];
  while (fgets(s, 256, stdin)) {
    p = strtok(s, " ");
    l = atoi(p);
    for (n = 0; p;) {
      p = strtok(0, " ");
      if (p)
        a[n++] = atoi(p);
    }
    std::vector<int> u, v;
    double dp[1 << 12][12];
    std::fill(dp[0], dp[1 << 12], 1e10);
    for (i = 0; i < n; ++i)
      dp[1 << i][i] = a[i];
    for (i = 1; i < (1 << n); ++i) {
      u.clear();
      v.clear();
      for (j = 0; j < n; ++j) {
        if (i & (1 << j))
          u.push_back(j);
        else
          v.push_back(j);
      }
      for (j = 0; j < u.size(); ++j) {
        for (k = 0; k < v.size(); ++k) {
          double &r = dp[i | (1 << v[k])][v[k]];
          r = std::min(r, dp[i][u[j]] + F(a[u[j]], a[v[k]]));
        }
      }
    }
    k = (1 << n) - 1;
    for (i = 0; i < n; ++i)
      dp[k][i] += a[i];
    puts(*std::min_element(dp[k], dp[k] + n) - 1e-7 < l ? "OK" : "NA");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define S(x) ((x) * (x))
int main() {
  int m, n, a[13], i;
  char *p, *q, s[64];
  double c[11][11] = {0};
  for (n = 3; n < 11; ++n)
    for (m = 3; m < 11; ++m)
      c[n][m] = sqrt((double)S(n + m) - S(n - m));
  while (fgets(s, 64, stdin)) {
    m = strtol(s, &p, 10);
    for (n = 0; a[n] = strtol(p, &q, 10), p != q; ++n)
      p = q;
    if (!n)
      continue;
    std::sort(a, a + n);
    do {
      double d = a[0] + a[n - 1];
      for (i = 0; i < n - 1; ++i)
        d += c[a[i]][a[i + 1]];
      if (m >= d) {
        puts("YES");
        goto E;
      }
    } while (std::next_permutation(a, a + n));
    puts("NO");
  E:;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
double dp[13][11][1 << 13];
double calc(vector<int> &v, int s, int n, int p, int b) {
  if (n == 0)
    return p;
  if (dp[n][p][b] != 0)
    return dp[n][p][b];
  double m = 300.0;
  for (int i = 0; i < s; i++) {
    if (!(b & (1 << i))) {
      double d;
      if (p == 0)
        d = v[i];
      else {
        double q = p - v[i];
        double r = p + v[i];
        double cos = sqrt(1.0 - q * q / (r * r));
        d = r * cos;
      }
      d += calc(v, s, n - 1, v[i], (b | (1 << i)));
      m = min(m, d);
    }
  }
  dp[n][p][b] = m;
  return m;
}
int main() {
  string s;
  while (getline(cin, s)) {
    istringstream in(s);
    int n = 0, i;
    double w;
    vector<int> v;
    in >> w;
    while (in >> i) {
      v.push_back(i);
      n++;
    }
    memset(dp, 0, sizeof(dp));
    double r = calc(v, n, n, 0, 0);
    if (w < r)
      cout << "NA" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
double calc(vector<int> &v, int s, int n, int p, int b, double w, double l) {
  if (n == 0)
    return l + p;
  double m = 300.0;
  for (int i = 0; i < s; i++) {
    if (!(b & (1 << i))) {
      double d;
      if (p == 0)
        d = v[i];
      else {
        double q = p - v[i];
        double r = p + v[i];
        double cos = sqrt(1.0 - q * q / (r * r));
        d = r * cos;
      }
      if (l + d > w)
        continue;
      d = calc(v, s, n - 1, v[i], (b | (1 << i)), w, l + d);
      m = min(m, d);
      if (p == 0 && m <= w)
        return m;
    }
  }
  return m;
}
int main() {
  string s;
  while (getline(cin, s)) {
    istringstream in(s);
    int n = 0, i;
    double w;
    vector<int> v;
    in >> w;
    while (in >> i) {
      v.push_back(i);
      n++;
    }
    double r = calc(v, n, n, 0, 0, w, 0);
    if (w < r)
      cout << "NA" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int i, n, k, box;
  double s, a, b, da[15];
  list<double> lis;
  list<double>::iterator it;
  while (cin >> box) {
    n = 0;
    while (cin >> da[n++])
      if (cin.peek() == '\\n')
        break;
    sort(da, da + n);
    lis.push_front(da[0]);
    for (i = 1; i < (n / 2) * 2; i++) {
      k = (i % 2) == 0 ? i / 2 : (n - 1) - i / 2;
      if ((i % 2 == 1 && (lis.front() < lis.back())) ||
          (i % 2 == 0 && (lis.front() > lis.back())))
        lis.push_front(da[k]);
      else
        lis.push_back(da[k]);
    }
    if (n > 1 && n % 2 == 1)
      if (2 * sqrt(da[n / 2] * lis.front()) - lis.front() <
          2 * sqrt(da[n / 2] * lis.back()) - lis.back())
        lis.push_front(da[n / 2]);
      else
        lis.push_back(da[n / 2]);
    s = lis.front();
    for (it = lis.begin(), i = 0; i < n - 1; i++, it++) {
      a = *it++;
      b = *it--;
      s += 2 * sqrt(a * b);
    }
    s += lis.back();
    if (s <= box)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
    lis.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int i, n, k, box;
  double s, a, b, da[15];
  list<double> lis;
  list<double>::iterator it;
  while (true) {
    n = 0;
    cin >> box;
    while (cin >> da[n++])
      if (cin.peek() == '\\n')
        break;
    sort(da, da + n);
    lis.push_front(da[0]);
    for (i = 1; i < (n / 2) * 2; i++) {
      k = (i % 2) == 0 ? i / 2 : (n - 1) - i / 2;
      if ((i % 2 == 1 && (lis.front() < lis.back())) ||
          (i % 2 == 0 && (lis.front() > lis.back())))
        lis.push_front(da[k]);
      else
        lis.push_back(da[k]);
    }
    if (n > 1 && n % 2 == 1)
      if (2 * sqrt(da[n / 2] * lis.front()) - lis.front() <
          2 * sqrt(da[n / 2] * lis.back()) - lis.back())
        lis.push_front(da[n / 2]);
      else
        lis.push_back(da[n / 2]);
    s = lis.front();
    for (it = lis.begin(), i = 0; i < n - 1; i++, it++) {
      a = *it++;
      b = *it--;
      s += 2 * sqrt(a * b);
    }
    s += lis.back();
    if (s <= box)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
    lis.clear();
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int r[13];
double dp[13][13][4097];
int main() {
  int len;
  string s;
  while (cin >> len) {
    getline(cin, s);
    int n = 0, cnt = 0;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] == ' ') {
        r[n] = cnt;
        n++;
        cnt = 0;
      } else {
        cnt *= 10;
        cnt += s[i] - '0';
      }
    }
    r[n] = cnt;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 4097; k++) {
          dp[i][j][k] = 10000.0;
        }
      }
    }
    int w = 1;
    for (int i = 0; i < n; i++) {
      dp[0][i][w] = r[i] + 0.0;
      w *= 2;
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < w; k++) {
          int h = 1;
          for (int e = 0; e < n; e++) {
            if (k % (h * 2) < h) {
              double set = dp[i][j][k];
              set += 2 * (double)sqrt((double)r[j] * r[e]);
              if (dp[i + 1][e][k + h] > set) {
                dp[i + 1][e][k + h] = set;
              }
            }
            h *= 2;
          }
        }
      }
    }
    bool ok = false;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < w; k++) {
        if (dp[n - 1][j][k] + r[j] <= len) {
          ok = true;
        }
      }
    }
    if (ok)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int r[15];
double dp[15][15][100000];
int main() {
  int len;
  string s;
  while (cin >> len) {
    getline(cin, s);
    int n = 0, cnt = 0;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] == ' ') {
        r[n] = cnt;
        n++;
        cnt = 0;
      } else {
        cnt *= 10;
        cnt += s[i] - '0';
      }
    }
    r[n] = cnt;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 100000; k++) {
          dp[i][j][k] = 10000.0;
        }
      }
    }
    int w = 1;
    for (int i = 0; i < n; i++) {
      dp[0][i][w] = r[i] + 0.0;
      w *= 2;
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < w; k++) {
          int h = 1;
          for (int e = 0; e < n; e++) {
            if (k % (h * 2) < h) {
              double set = dp[i][j][k];
              set += 2 * (double)sqrt((double)r[j] * r[e]);
              if (dp[i + 1][e][k + h] > set) {
                dp[i + 1][e][k + h] = set;
              }
            }
            h *= 2;
          }
        }
      }
    }
    bool ok = false;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < w; k++) {
        if (dp[n - 1][j][k] + r[j] <= len) {
          ok = true;
        }
      }
    }
    if (ok)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;

double dp[1 << 12][12];

int main() {
  string s;
  int size;
  int r[12];
  int n;
  while (getline(cin, s)) {
    stringstream ss(s);
    ss >> size;
    n = 0;
    while (ss >> r[n])
      n++;
    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        dp[i][j] = 1e10;
    for (int j = 0; j < n; j++)
      dp[1 << j][j] = r[j];
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if (!(i >> j & 1))
          continue;
        for (int k = 0; k < n; k++) {
          if (i >> k & 1)
            continue;
          dp[i | 1 << k][k] =
              min(dp[i | 1 << k][k], dp[i][j] + 2 * sqrt(r[j] * r[k]));
        }
      }
    }
    bool ok = false;
    for (int j = 0; j < n; j++) {
      if (dp[(1 << n) - 1][j] + r[j] <= size)
        ok = true;
    }
    cout << (ok ? "OK" : "NA") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;

int main() {
  string s;
  int size;
  int r[12];
  int n;
  while (getline(cin, s)) {
    int perm[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    stringstream ss;
    ss << s;
    ss >> size;
    n = 0;
    while (ss >> r[n])
      n++;
    bool ok = false;
    do {
      double w;
      w = r[perm[0]];
      for (int i = 1; i < n; i++)
        w += 2 * sqrt(r[perm[i - 1]] * r[perm[i]]);
      if (1 < n)
        w += r[perm[n - 1]];
      if (w <= size) {
        ok = true;
        break;
      }
    } while (next_permutation(perm, perm + n));
    cout << (ok ? "OK" : "NA") << endl;
  }
  return 0;
}