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

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef pair<string, II> NODE;
typedef vector<NODE> NODES;

void solve(int n, NODES nodes) {
  int ming = INT_MAX;
  VS L;
  VI ID(n);
  for (int i = 0; i < n; ++i) {
    ID[i] = i;
  }
  do {
    int wsum = 0, g = 0;
    for (int i = 0; i < n; ++i) {
      if (wsum > nodes[ID[i]].second.second)
        goto out_1;
      wsum += nodes[ID[i]].second.first;
    }
    for (int i = 0; i < n; ++i) {
      g += (n - i) * nodes[ID[i]].second.first;
      if (g >= ming)
        goto out_1;
    }
    if (g < ming) {
      ming = g;
      VS T;
      for (int i = 0; i < n; ++i) {
        T.push_back(nodes[ID[i]].first);
      }
      L = T;
    }
  out_1:;
  } while (next_permutation(ID.begin(), ID.end()));

  reverse(L.begin(), L.end());
  for (int i = 0; i < n; ++i) {
    cout << L[i] << endl;
  }
}

int main() {
  int n;
  while (cin >> n && n) {
    NODES nodes(n);
    for (int i = 0; i < n; ++i) {
      cin >> nodes[i].first >> nodes[i].second.first >> nodes[i].second.second;
    }
    solve(n, nodes);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef pair<string, II> NODE;
typedef vector<NODE> NODES;

void solve(int n, NODES nodes) {
  int ming = INT_MAX;
  VS L;
  sort(nodes.rbegin(), nodes.rend());
  do {
    int wsum = 0, g = 0;
    for (int i = 0; i < n; ++i) {
      if (wsum > nodes[i].second.second)
        goto out_1;
      wsum += nodes[i].second.first;
    }
    for (int i = 0; i < n; ++i) {
      g += (n - i) * nodes[i].second.first;
      if (g >= ming)
        goto out_1;
    }
    if (g < ming) {
      ming = g;
      VS T;
      for (int i = 0; i < n; ++i) {
        T.push_back(nodes[i].first);
      }
      L = T;
    }
  out_1:;
  } while (next_permutation(nodes.rbegin(), nodes.rend()));

  reverse(L.begin(), L.end());
  for (int i = 0; i < n; ++i) {
    cout << L[i] << endl;
  }
}

int main() {
  int n;
  while (cin >> n && n) {
    NODES nodes(n);
    for (int i = 0; i < n; ++i) {
      cin >> nodes[i].first >> nodes[i].second.first >> nodes[i].second.second;
    }
    solve(n, nodes);
  }
  return 0;
}