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

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = min(a, b);
    return true;
  } else
    return false;
}
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

struct Elem {
  string name;
  int weight, limit;
};

int dp[1 << 11];
vector<string> vec[1 << 11];

signed main() {
  int N;
  while (cin >> N, N) {
    vector<Elem> elems;
    for (int i = 0; i < N; i++) {
      string name;
      int weight, limit;
      cin >> name >> weight >> limit;
      elems.push_back(Elem{name, weight, limit});
    }

    fill(dp, dp + (1 << N), INF);
    dp[0] = 0;

    for (int bit = 0; bit < (1 << N); bit++) {
      int coeff = N - __builtin_popcount(bit);
      int sum = 0;
      for (int i = 0; i < N; i++) {
        if (bit >> i & 1)
          sum += elems[i].weight;
      }
      for (int i = 0; i < N; i++) {
        if ((bit >> i & 1) or (sum > elems[i].limit))
          continue;
        int nbit = bit | (1 << i);
        if (chmin(dp[nbit], dp[bit] + coeff * elems[i].weight)) {
          vec[nbit] = vec[bit];
          vec[nbit].push_back(elems[i].name);
        }
      }
    }

    vector<string> ans = vec[(1 << N) - 1];
    reverse(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

// 基本テンプレート

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

struct Elem {
  string name;
  int weight, limit;
};

signed main() {
  int N;
  while (cin >> N, N) {
    vector<Elem> elems;
    for (int i = 0; i < N; i++) {
      string name;
      int weight, limit;
      cin >> name >> weight >> limit;
      elems.push_back(Elem{name, weight, limit});
    }

    vector<int> perm(N);
    iota(perm.begin(), perm.end(), 0);

    double g = INF;
    vector<Elem> ans;
    do {
      vector<Elem> cur;
      for (int i = 0; i < N; i++)
        cur.push_back(elems[perm[i]]);

      int sum = 0, ok = true, weighted_sum = 0;
      for (int i = N - 1; i >= 0; i--) {
        if (cur[i].limit < sum)
          ok = false;
        sum += cur[i].weight;
        weighted_sum += (i + 1) * cur[i].weight;
      }

      double tmp_g = 1.0 * weighted_sum / sum;
      if (ok and g > tmp_g) {
        g = tmp_g;
        ans = cur;
      }
    } while (next_permutation(perm.begin(), perm.end()));

    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i].name << endl;
    }
  }
  return 0;
}