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++: #include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

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

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int n;
string f[20];
int w[20],s[20];

bool isOK(vi num){
    int cap=99999999;
    
    rep(i,num.size()){
        cap=min(cap-w[num[i]],s[num[i]]);
    }
    if(cap>=0)return true;
    else return false;
}

double solve(vi num){
    
    int up=0;
    rep(i,num.size()){
        up+=(i+1)*w[num[i]];
    }

    int div=0;
    rep(i,num.size()){
        div+=w[num[i]];
    }

    return (double)up/div;
}


int main() {
    
    while(cin>>n){
        if(n==0)break;
        
        vi num;
        rep(i,n){
            num.pb(i);
            cin>>f[i]>>w[i]>>s[i];
        }
        
        double Gmin=99999999;
        vi ans;
        do{
            if( isOK(num) ){
                double res=solve(num);
                
                
                if(Gmin>res){
                    Gmin=res;
                    ans=num;
                    
                }
            }
        }while( next_permutation(all(num)) );
        
        rep(i,n){
            cout<<f[ans[i]]<<endl;
        }
        
    }
    
    return 0;
}

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

#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;
		for(int i =0;i <n;i++){
			cin>>str>>w>>s;
			data.push_back(mp(str,pii(w,s)));
		}
		sort(data.begin(),data.end());
		double G =INF;
		do{
			//cout <<"iei"<<endl;
			int sumw =0;
			double tmp1 =0;
			double tmp2 =0;
			for(int i =n-1;i >=0;i--){
				tmp1 +=(i+1)*data[i].second.first;
				tmp2 +=data[i].second.first;
			}
			double Gtmp =(double)(tmp1/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;
				}
			}
			if(ok&&Gtmp <G){
				G =Gtmp;
				ans =data;
			}
		}while(next_permutation(data.begin(), data.end()));

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

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

const double EPS = 1e-10;

typedef struct
{
  string f;
  int w, s;
} Food;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<Food> foods(n);
    for (int i = 0; i < n; ++i)
      cin >> foods[i].f >> foods[i].w >> foods[i].s;

    vector<int> index(n);
    for (int i = 0; i < n; ++i)
      index[i] = i;

    double bestG = 1e10;
    vector<string> ans(n);
    do {
      int w = 0, kw = 0;
      bool valid = true;
      for (int i = 0; i < n && valid; ++i) {
	if (foods[index[i]].s < w)
	  valid = false;
	w += foods[index[i]].w;
	kw += (n-i) * foods[index[i]].w;
      }

      if (valid) {
	double G = kw * 1.0 / w;
	if (bestG - G > EPS) {
	  bestG = G;
	  for (int i = 0; i < n; ++i)
	    ans[i] = foods[index[i]].f;
	}
      }
    } while (next_permutation(index.begin(), index.end()));

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

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


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

int n,used[10],w[10],s[10];
string name[10];
double ansg;
int ansnum[10],tansnum[10];

void dfs(int weight,int up){
  if(up==n){
    int tw=0;
    rep(i,n)tw+=(n-i)*w[tansnum[i]];
    if(ansg>1.0*tw/weight){
      ansg=1.0*tw/weight;
      rep(i,n)ansnum[i]=tansnum[i];
    }
    return;
  }
  
  rep(i,n){
    if(used[i])continue;
    if(s[i]<weight)return;
  }
  
  rep(i,n){
    if(used[i])continue;
    if(s[i]<weight)return;
    used[i]=1;
    tansnum[up]=i;
    dfs(weight+w[i],up+1);
    used[i]=0;
  }
}

main(){
  while(cin>>n,n){
    ansg=100;
    rep(i,n){
      cin>>name[i]>>w[i]>>s[i];
    }

    dfs(0,0);
    rep(i,n){
      cout<<name[ansnum[n-1-i]]<<endl;
    }
  }
}