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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  int n;
  vector<pair<string, pii>> data;
  vector<pair<string, pii>> ans;
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    data.clear();
    ans.clear();
    string str;
    int w, s;
    int tmp2 = 0;
    for (int i = 0; i < n; i++) {
      cin >> str;
      scanf("%d %d", &w, &s);
      data.push_back(mp(str, pii(w, s)));
      tmp2 += data[i].second.first;
    }
    sort(data.begin(), data.end());
    double G = 1145141;
    do {
      // cout <<"iei"<<endl;
      int sumw = 0;
      int tmp1 = 0;
      for (int i = n - 1; i >= 0; i--) {
        tmp1 += (i + 1) * data[i].second.first;
      }
      double Gtmp = (double)((double)tmp1 / (double)tmp2);
      if (Gtmp > G)
        continue;
      bool ok = true;
      for (int i = n - 1; i >= 0; i--) {
        if (sumw > data[i].second.second) {
          ok = false;
          break;
        } else {
          sumw += data[i].second.first;
        }
      }
      // cout <<Gtmp<<endl;
      if (ok && Gtmp < G) {
        G = Gtmp;
        ans = data;
        // cout <<"ge"<<endl;
      }
    } while (next_permutation(data.begin(), data.end()));

    for (int i = 0; i < n; i++) {
      printf("%s\\n", ans[i].first.c_str());
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int main() {
  int n;
  vector<pair<string, pii>> data;
  vector<pair<string, pii>> ans;
  while (cin >> n, n) {
    data.clear();
    ans.clear();
    string str;
    int w, s;
    int tmp2 = 0;
    for (int i = 0; i < n; i++) {
      cin >> str;
      scanf("%d %d", &w, &s);
      data.push_back(mp(str, pii(w, s)));
      tmp2 += data[i].second.first;
    }
    sort(data.begin(), data.end());
    double G = INF;
    do {
      // cout <<"iei"<<endl;
      int sumw = 0;
      int tmp1 = 0;
      for (int i = n - 1; i >= 0; i--) {
        tmp1 += (i + 1) * data[i].second.first;
      }
      double Gtmp = (double)((double)tmp1 / (double)tmp2);
      if (Gtmp > G)
        continue;
      bool ok = true;
      for (int i = n - 1; i >= 0; i--) {
        if (sumw > data[i].second.second) {
          ok = false;
          break;
        } else {
          sumw += data[i].second.first;
        }
      }
      // cout <<Gtmp<<endl;
      if (ok && Gtmp < G) {
        G = Gtmp;
        ans = data;
        // cout <<"ge"<<endl;
      }
    } while (next_permutation(data.begin(), data.end()));

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