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 <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;
	char c;
	char str[1024];
	string s;
	ll l;
	int nextInt(){
		scanf("%d", &n);
		return n;
	}
	ll nextLong(){
		scanf("%lld", &l);
		return l;
	}
	char nextChar(){
		scanf("%c", &c);
		return c;
	}
	string next(){
		scanf("%s", str);
		return string(str);
	}
	string nextLine(){
		getline(cin, s);
		return s;
	}
};
Input in;

ll calc(const vector<pair<int, pll> > &v){
	ll sum = 0LL, ret = 0LL;
	rep(i, v.size()){
		if(v[i].se.se < sum) return LINF;
		sum += v[i].se.fi;
		ret += (v.size() - i) * v[i].se.fi;
	}
	return ret;
}

int main(){
	while(1){
		int n = in.nextInt();
		if(n == 0) break;

		vector<string> s(n);
		vector<int> ans(n);
		ll w = LINF;
		vector<pair<int, pll> > v(n);

		rep(i, n){
			s[i] = in.next();
			v[i].fi = i;
			v[i].se.fi = in.nextLong();
			v[i].se.se = in.nextLong();
		}
		sort(all(v));

		do{
			ll tmp = calc(v);
			if(tmp < w){
				w = tmp;
				rep(i, v.size()) ans[i] = v[i].fi;
			}
		}while(next_permutation(all(v)));

		reverse(all(ans));

		rep(i, n) printf("%s\n", s[ans[i]].c_str());
	}
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int n, w[1000], s[1000];
string S[1000]; vector<int>b; int maxn = 100000000;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S[i] >> w[i] >> s[i];
	}
	vector<int>a(n, 0);
	for (int i = 0; i < n; i++)a[i] = i;
	do {
		int sum2 = 0;
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = i + 1; j < n; j++) {
				sum += w[a[j]];
			}
			if (sum > s[i]) goto E;
		}
		for (int i = 0; i < n; i++)sum2 += i*w[i];
		if (maxn > sum2)b = a;
	E:;
	} while (next_permutation(a.begin(), a.end()));
	for (int i = 0; i < n; i++)cout << S[a[i]] << endl;
	return 0;
}

C++: #include<vector>
#include<map>
#include<string>
#include<algorithm>
#define repb(i,n) for(int i=n;i--;)
using namespace std;
int main(){
	int n,s,w,W; string f;
	while(cin>>n,n){
		vector<pair<int,pair<int, string> > > t; W=0;
		repb(i,n){
			cin>>f>>w>>s;
			t.push_back(make_pair(w,make_pair(s,f)));
			W+=w;
		}
		sort(t.begin(),t.end());
		vector<string> fukuro;
		while(!t.empty()){
			repb(i,t.size())if(W-t[i].first<=t[i].second.first){
				fukuro.push_back(t[i].second.second);cout<<t[i].second.second<<endl;
				W-=t[i].first;
				t.erase(t.begin()+i);
				break;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<pair<int,int>,string> item;
main(){
	int n,i,w,s,S;
	string f;
	for(;cin>>n,n;){
		vector<item> items;
		vector<int> num,result;
		for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f)),num.push_back(i);
		S=99999;
		do{
			for(s=w=0,i=n-1;~i;i--){
				if(items[num[i]].first.second<w)break;
				w+=items[num[i]].first.first;
				s+=(i+1)*items[num[i]].first.first;
			}
			if(i<0&&s<S)S=s,result=num;
		}while(next_permutation(num.begin(),num.end()));
		for(i=0;i<n;i++)cout<<items[result[i]].second<<endl;
	}
}

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

int n,a,tmp;
int s[10],w[10];
string f[10],temp;
int t[10];
vector<int>ans;

int get_weight(int);
void swap_data(int);
void sort_data();

int main(){
  while(cin>>n&&n!=0){
    ans.clear();
    for(int i=0;i<n;i++){
      cin>>f[i];
      cin>>w[i]>>s[i];
    }    
    sort_data();
    for(int i=0;i<n;i++){
      for(int j=0;j<n-1;j++){
	if(w[j]<w[j+1]){
	  swap_data(j);
	  a=get_weight(j+1);
	  if(a>s[j])swap_data(j);
	}
      }
    }
    for(int i=0;i<n;i++)cout<<f[i]<<endl;
  }
  return 0;
}

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

void swap_data(int x){
  tmp=w[x];
  w[x]=w[x+1];
  w[x+1]=tmp;

  tmp=s[x];
  s[x]=s[x+1];
  s[x+1]=tmp;

  temp=f[x];
  f[x]=f[x+1];
  f[x+1]=temp;
}

void sort_data(){
  for(int i=n-1;i>0;i--){
    for(int j=0;j<i;j++){
      if(s[j]<s[j+1])swap_data(j);
      else if(s[j]==s[j+1]&&w[j]<w[j+1])swap_data(j);
    }
  }
}

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

struct C {
  int id;
  int w;
  int s;
  static bool compare(const C &l, const C &r) {
    if (l.w > r.w) {
      return true;
    } else if (l.w < r.w) {
      return false;
    } else {
      return (l.s > r.s);
    }
  }
};

int main() {
  C c[10];
  char f[10][21];
  int n, tw, a[10];
  bool u[10];
  while (1) {
    scanf("%d", &n);
    if (!n) break;
    for (int i=0; i<n; i++) {
      c[i].id = i;
      scanf("%s %d %d", f[i], &c[i].w, &c[i].s);
    }
    sort(c, c+n, C::compare);
    tw = 0;
    for (int i=0; i<n; i++) {
      tw += c[i].w;
    }
    for (int i=0; i<n; i++) {
      u[i] = false;
    }
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
        if (!u[j] && tw - c[j].w <= c[j].s) {
          tw -= c[j].w;
          a[i] = c[j].id;
          u[j] = true;
          break;
        }
      }
    }
    for (int i=0; i<n; i++) {
      printf("%s\n", f[a[i]]);
    }
  }
  return 0;
}

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

using namespace std;

int main()
{
	for(int n;scanf("%d",&n),n;){
		vector<string> name(n);
		vector<int> wei(n),safe(n),perm(n);
		for(int i=0;i<n;i++){
			char nm[21];
			scanf("%s%d%d",nm,&wei[i],&safe[i]);
			name[i]=nm;
			perm[i]=i;
		}

		vector<int> opt(n);
		double gopt=-1;
		do{
			bool b=true;

			int wsum=0,g=0;
			for(int i=0;i<n;i++){
				int now=perm[i];
				if(wsum>safe[now]){
					b=false;
					break;
				}
				wsum+=wei[now];
				g+=(i+1)*wei[now];
			}

			if(!b)	continue;

			if(gopt<(double)g/wsum){
				gopt=(double)g/wsum;
				opt=perm;
			}
		}while(next_permutation(perm.begin(),perm.end()));

		for(int i=n-1;i>=0;i--)	puts(name[opt[i]].c_str());
	}

	return 0;
}