Problem p00170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct food {
  string f;
  int w;
  int s;
};

int n;
vector<food> foods;
vector<string> res;
pair<int, vector<string>> result;

void f(int b, int w, int k) {
  int i;
  // cout<<b<<" "<<w<<endl<<" ";
  // for(i=0;i<res.size();i++)cout<<res[i]<<endl;
  // cout<<endl;
  if (b == (1 << n) - 1) {
    if (result.first > k) {
      result.first = k;
      result.second.clear();
      for (i = 0; i < n; i++)
        result.second.push_back(res[i]);
    }
  }
  for (i = 0; i < n; i++) {
    if (foods[i].s < w)
      continue;
    res.push_back(foods[i].f);
    f(b | 1 << i, w + foods[i].w, k + w + foods[i].w);
    res.pop_back();
  }
}

int main() {
  while (n = in()) {
    int i;
    foods.clear();
    food buf;
    result.first = 1000000000;
    for (i = 0; i < n; i++) {
      cin >> buf.f >> buf.w >> buf.s;
      foods.push_back(buf);
    }
    res.clear();
    f(0, 0, 0);
    for (i = 0; i < n; i++) {
      cout << result.second[n - i - 1] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct food {
  string f;
  int w;
  int s;
};

int n;
vector<food> foods;
vector<string> res;
pair<int, vector<string>> result;

void f(int b, int w, int k) {
  int i;
  // cout<<b<<" "<<w<<endl<<" ";
  // for(i=0;i<res.size();i++)cout<<res[i]<<endl;
  // cout<<endl;
  if (b == (1 << n) - 1) {
    if (result.first > k) {
      result.first = k;
      result.second.clear();
      for (i = 0; i < n; i++)
        result.second.push_back(res[i]);
    }
  }
  for (i = 0; i < n; i++) {
    if (b & 1 << i)
      continue;
    if (foods[i].s < w)
      continue;
    res.push_back(foods[i].f);
    f(b | 1 << i, w + foods[i].w, k + w + foods[i].w);
    res.pop_back();
  }
}

int main() {
  while (n = in()) {
    int i;
    foods.clear();
    food buf;
    result.first = 1000000000;
    for (i = 0; i < n; i++) {
      cin >> buf.f >> buf.w >> buf.s;
      foods.push_back(buf);
    }
    res.clear();
    f(0, 0, 0);
    for (i = 0; i < n; i++) {
      cout << result.second[n - i - 1] << endl;
    }
  }
  return 0;
}