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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

string line;
int S, r, N;
vi cakes;

const int MAX_N = 15;
const int TSIZE = (2 << MAX_N) + 1;
double edges[MAX_N][MAX_N];
double dp[TSIZE][MAX_N];
bitset<15> used;

double minval;

void dfs(int v, int s, double value) {
  if (used.count() == N) {
    minval = min(minval, value + cakes[s] + cakes[v]);
    return;
  }

  int state = used.to_ulong();
  if (dp[state][v] <= value)
    return;

  dp[state][v] = value;
  for (int i = 0; i < N; i++) {
    if (v != i && !used[i]) {
      value += edges[v][i];
      used.set(i);
      dfs(i, s, value);
      used.reset(i);
      value -= edges[v][i];
    }
  }
}

void solve() {
  N = cakes.size();
  memset(&edges[0][0], 0, sizeof(double) * MAX_N * MAX_N);
  for (int i = 0; i < TSIZE; i++)
    for (int j = 0; j < MAX_N; j++)
      dp[i][j] = 10000.0;

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      double d = 2.0 * sqrt((double)(cakes[i] * cakes[j]));
      edges[i][j] = edges[j][i] = d;
    }
  }

  minval = 10000.0;
  for (int i = 0; i < N; i++) {
    used.set(i);
    dfs(i, i, 0.0);
    used.reset(i);
  }
  // cout << minval << endl;
  cout << (minval <= S ? "OK" : "NA") << endl;
}

int main() {
  while (1) {
    getline(cin, line);
    if (line == "")
      break;

    stringstream ss;
    ss << line;
    ss >> S;
    cakes.clear();
    while (ss >> r) {
      cakes.push_back(r);
    }
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

string line;
int S, r, N;
vi cakes;

const int MAX_N = 15;
double edges[MAX_N][MAX_N];
bitset<15> used;

double minval;

void dfs(int v, double value) {
  if (used.count() == N) {
    minval = min(minval, value + cakes[v]);
    return;
  }

  for (int i = 0; i < N; i++) {
    if (v != i && !used[i]) {
      value += edges[v][i];
      used.set(i);
      dfs(i, value);
      used.reset(i);
      value -= edges[v][i];
    }
  }
}

void solve() {
  N = cakes.size();
  memset(&edges[0][0], 0, sizeof(double) * MAX_N * MAX_N);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      double d = 2.0 * sqrt((double)(cakes[i] * cakes[j]));
      edges[i][j] = edges[j][i] = d;
    }
  }

  minval = 10000.0;
  for (int i = 0; i < N; i++) {
    used.set(i);
    dfs(i, cakes[i]);
    used.reset(i);
  }
  cout << (minval <= S ? "OK" : "NA") << endl;
}

int main() {
  while (1) {
    getline(cin, line);
    if (line == "")
      break;

    stringstream ss;
    ss << line;
    ss >> S;
    cakes.clear();
    while (ss >> r) {
      cakes.push_back(r);
    }
    solve();
  }
}