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

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef pair<double, vi> P;
struct food {
  string name;
  int w;
  int s;
};
int n;
vector<food> a;

P calc(int bit, int s, double g, double d) {
  if (s < 0)
    return P(-1, {});
  int _n = __builtin_popcount(bit);
  if (_n == n)
    return P(g / d, {});

  P ans = P(9999999999.0, {});
  FOR(i, n) {
    if (bit & 1 << i)
      continue;
    int nbit = bit | 1 << i;
    int ns = min(s - a[i].w, a[i].s);
    double ng = g + (_n + 1) * a[i].w;
    double nd = d + a[i].w;
    P x = calc(nbit, ns, ng, nd);
    if (x.first < 0)
      continue;
    if (ans.first > x.first) {
      ans = x;
      ans.second.insert(ans.second.begin(), i);
    }
  }
  return ans;
}

int main() {
  while (cin >> n && n) {
    a.clear();
    FOR(i, n) {
      food f;
      cin >> f.name >> f.w >> f.s;
      a.push_back(f);
    }
    P ans = calc(0, INT_MAX, 0, 0);
    FOR(i, n) { cout << a[ans.second[i]].name << endl; }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
#define all(a) a.begin(), a.end()
typedef vector<int> vi;
struct food {
  string name;
  int w, s;
};
typedef vector<food> vf;
int n;
int main() {
  while (cin >> n && n) {
    vf a;
    vi ans, s;
    for (int i = 0; i < n; i++) {
      food f;
      s.push_back(i);
      cin >> f.name >> f.w >> f.s;
      a.push_back(f);
    }
    double t = 9999999999999;
    do {
      vi sum;
      double r = 0, f = 0;
      for (int i = 0; i < n; i++) {
        sum.push_back(a[s[i]].w);
        r += (i + 1) * a[s[i]].w;
        f += a[s[i]].w;
      }
      for (int i = n - 1; i > 0; i--)
        sum[i - 1] += sum[i];
      sum.push_back(0);
      bool ok = true;
      for (int i = 0; i < n; i++) {
        if (sum[i + 1] > a[s[i]].s)
          ok = false;
      }
      if (!ok)
        continue;
      if (t > r / f) {
        t = r / f;
        ans = s;
      }
    } while (next_permutation(all(s)));
    for (int i = 0; i < n; i++) {
      cout << a[ans[i]].name << endl;
    }
  }
}