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

#define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 1e-8;

struct FOOD {
  string name;
  int w;
  int s;
};

bool checkWeight(const struct FOOD *food, const int *table, int n) {
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = i + 1; j < n; j++) {
      sum += food[table[j]].w;
    }
    if (food[table[i]].s < sum)
      return false;
  }

  return true;
}

double getG(const struct FOOD *food, const int *table, int n) {

  double G = 0;
  double sum = 0;
  for (int i = 0; i < n; i++) {
    G += (double)food[table[i]].w * (i + 1);
    sum += (double)food[table[i]].w;
  }
  return G / sum;
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    struct FOOD *food = new struct FOOD[n];

    for (int i = 0; i < n; i++) {
      char buf[64];
      int w, s;
      scanf("%s %d %d", buf, &w, &s);
      food[i].name = buf;
      food[i].s = s;
      food[i].w = w;
    }

    int *table = new int[n];
    for (int i = 0; i < n; i++)
      table[i] = i;

    int *res = new int[n]();
    double minG = 1000000.0;

    do {
      double tmpG;
      if (checkWeight(food, table, n) && minG > (tmpG = getG(food, table, n))) {
        copy(table, table + n, res);
        minG = tmpG;
      }
    } while (next_permutation(table, table + n));

    for (int i = 0; i < n; i++) {
      printf("%s\\n", food[res[i]].name.c_str());
    }

    delete[] food;
    delete[] table;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 1e-8;

struct FOOD {
  string name;
  int w;
  int s;
};

struct DATA {
  double G;
  vector<FOOD> mFoods;
};

struct DATA dp[1 << 10][10];

bool checkWeight(const vector<FOOD> &vf) {
  for (int i = 0; i < vf.size(); i++) {
    int sum = 0;
    for (int j = i + 1; j < vf.size(); j++) {
      sum += vf[j].w;
    }
    if (vf[i].s < sum)
      return false;
  }

  return true;
}

double getG(const vector<FOOD> &vf) {

  double G = 0;
  double sum = 0;
  for (int i = 0; i < vf.size(); i++) {
    G += vf[i].w * (i + 1);
    sum += vf[i].w;
  }
  return G / sum;
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    struct FOOD *food = new struct FOOD[n];

    for (int i = 0; i < n; i++) {
      char buf[64];
      int w, s;
      scanf("%s %d %d", buf, &w, &s);
      food[i].name = buf;
      food[i].s = s;
      food[i].w = w;
    }

    int *table = new int[n];
    for (int i = 0; i < n; i++)
      table[i] = i;

    vector<FOOD> Foods;

    struct DATA res;
    res.G = 1000000;
    do {
      for (int i = 0; i < n; i++) {
        Foods.push_back(food[table[i]]);
      }

      double tmpG;
      if (checkWeight(Foods) && res.G > (tmpG = getG(Foods))) {
        res.mFoods = Foods;
        res.G = tmpG;
      }
      Foods.clear();
    } while (next_permutation(table, table + n));

    for (int i = 0; i < res.mFoods.size(); i++) {
      printf("%s\\n", res.mFoods[i].name.c_str());
    }

    delete[] food;
    delete[] table;
  }
}