Go: package main

import (
	"bufio"
		"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
var f = make([]string, 10)
var s, w = make([]int, 10), make([]int, 10)
var n int
func main() {
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			tmpf :=buf[0]
			tmpw, _ := strconv.Atoi(buf[1])
			tmps, _ := strconv.Atoi(buf[2])
			f[i] = tmpf
			s[i] = tmps
			w[i] = tmpw
		}
		sort()
		for i := 0; i < n; i++ {
			for j := 0; j < n-1; j++ {
				if w[j]<w[j+1] {
					swap(j)
					a := get_weight(j+1)
					if a>s[j] {
						swap(j)
					}
				}
			}
		}
		for i := 0; i < n; i++ {
			fmt.Println(f[i])
		}
	}
}

func sort() {
	for i := n-1; i > 0; i-- {
		for j := 0; j < i; j++ {
			if s[j]<s[j+1] {
				swap(j)
			} else if s[j]==s[j+1]&&w[j]<w[j+1] {
				swap(j)
			}
		}
	}
}

func swap(x int) {
	w[x], w[x+1] = w[x+1], w[x]
	s[x], s[x+1] = s[x+1], s[x]
	f[x], f[x+1] = f[x+1], f[x]
}

func get_weight(x int) int {
	ret := 0
	for i := x; i < n; i++ {
		ret += w[i]
	}
	return ret
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
ostream &operator<<(ostream &os, const pair<int,int> &p){return(os<<"("<<p.first<<","<<p.second<<")");}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

struct obj {
    string f;
    int w, s;
    obj(string _f, int _w, int _s) : f(_f),w(_w),s(_s){ }
};


void dfs(vector<obj> &v, int used, int n, int tw, vector<int> &lst, vector<int> &mn, int &mng){
    if(used == (1<<n)-1){
        int g = 0, k = 1;
        FOREACH(it,lst) g += v[*it].w * k++;
        if(mng > g){
            mng = g; mn = lst;
        }
        return;
    }
    REP(i,n){
        if(used & (1<<i)) continue;
        if(tw-v[i].w > v[i].s) continue;
        lst.pb(i);
        dfs(v,used | (1<<i), n, tw-v[i].w,lst,mn,mng);
        lst.pop_back();
    }
}

int solve(vector<obj> &v){
    int n = v.size();
    int tw = 0;
    vector<int> lst, mn;
    int mng = INT_MAX;

    REP(i,n) tw += v[i].w;
    dfs(v,0,n,tw,lst,mn,mng);
    FOREACH(it,mn) cout << v[*it].f << endl;
    return 0;
}

int main(){
    int n;
    while(cin >> n && n){
        string f; int w, s;
        vector<obj> v;
        REP(i,n){
            cin >> f >> w >> s;
            v.pb(obj(f,w,s));
        }
        solve(v);
    }
    return 0;
}

C++: #include <cstdio>
#include <algorithm>

using namespace std;

struct lunch{
  int w,s;
  char name[21];

  bool operator < (const lunch &l)const{
    if(w != l.w){
      return w < l.w;
    }else{
      return s < l.s;
    }
  }
};

lunch N[10],ans[10];
int n;

void make(){
  for(int i = 0 ; i < n ; i++){
    ans[i] = N[i];
  }
}

bool check(int &G){
  int Ws1 = 0,Ws2 = 0;
  for(int i = 0 ; i < n ; i++){
    if(N[i].s < Ws2) return false;
    Ws1 += (i+1)*N[i].w;
    Ws2 += N[i].w;
  }
  G = Ws1;

  return true;
}

int main(){
  while(scanf("%d" ,&n) ,n){
    for(int i = 0 ; i < n ; i++){
      scanf("%s %d %d" ,N[i].name ,&N[i].w ,&N[i].s);
    }
    sort(N,N+n);

    int G = 0,t;

    do{
      if(check(t)){
	if(G < t){
	  make();
	  G = t;
	}
      }
    }while(next_permutation(N,N+n));

    for(int i = n-1 ; i >= 0 ; i--){
      printf("%s\n" ,ans[i].name);
    }
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct Food{
  string name;int w,s;
  Food(string a,int b,int c){name=a;w=b;s=c;};
};

int main()
{
  int n;
  string f;
  int w,s;
  while(cin>>n,n){
    vector<Food>food;
    vector<int>v;
    vector<int>ans;
    for(int i=0;i<n;i++){
      v.push_back(i);
      cin>>f>>w>>s;
      food.push_back(Food(f,w,s));
    }

    double res=1e10;
    do{
      bool ok=true;
      double sum=food[v[n-1]].w;
      double sum2=n*food[v[n-1]].w;
      for(int i=n-2;i>=0;i--){
	if(food[v[i]].s<sum){
	  ok=false;
	  break;
	}
	sum+=food[v[i]].w;
	sum2+=(i+1)*food[v[i]].w;
      }
      if(ok && res>sum2/sum){
	res=sum2/sum;
	ans=v;
      }
    }while(next_permutation(v.begin(),v.end()));

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