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 <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

double getcog(vi& ps,vi& ws)
{
	int size=ps.size();
	int temp=0;
	rep(i,size)
		temp+=(i+1)*ws[ps[i]];
	return (double)temp/accumulate(allof(ws),0);
}

int main()
{
	for(int n;cin>>n,n;){
		vs ns(n);
		vi ws(n),cs(n);
		rep(i,n)
			cin>>ns[i]>>ws[i]>>cs[i];
		
		double cog=1e10;
		vi ps(n),res(n);
		rep(i,n) ps[i]=i;
		do{
			vi ss(n);
			for(int i=n-2;i>=0;i--)
				ss[i]=ss[i+1]+ws[ps[i+1]];
			bool ok=true;
			rep(i,n)
				ok&=ss[i]<=cs[ps[i]];
			if(ok){
				double temp=getcog(ps,ws);
				if(temp<cog){
					cog=temp;
					res=ps;
				}
			}
		}while(next_permutation(allof(ps)));
		
		rep(i,n)
			cout<<ns[res[i]]<<endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>

#define N 11

using namespace std;

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

void dfs(int n, const vector<food> &f, bool flag[N], vector<food> &now, double &bestG, vector<food> &ans){
	int sumwG_denom = 0;
	int sumwG_numer = 0;
	double nowG = 0;

	for(unsigned int i = 0; i < now.size(); ++i){
		int sumw = 0;
		sumwG_numer += (i+1)*now[i].w;
		sumwG_denom += now[i].w;
		for(unsigned int j = i + 1; j < now.size(); ++j){
			sumw += now[j].w;
		}
		if( sumw > now[i].s )
			return ;
	}
	if( sumwG_denom > 0 ){
		nowG = sumwG_numer / (double)sumwG_denom;
		
		
		/*if( now.size() == n ){
			cout << " ------------------ \n";
			for(unsigned int i = 0; i < now.size(); ++i){
				cout << now[i].name << endl;
			}
			cout << "BEST : " << bestG << " G: " << nowG << endl;
		}*/
		

		if( now.size() == n && nowG < bestG ){
			bestG = nowG;
			ans = now;
		}
	}
	
	for(unsigned int i = 0; i < f.size(); ++i){
		if( !flag[i] ){
			flag[i] = true;
			now.push_back( f[i] );
			dfs( n, f, flag, now, bestG, ans );
			now.erase( now.end() - 1 );
			flag[i] = false;
		}
	}
}

int main(){
	while(true){
		int n;
		double bestG = 1000000;
		bool flag[N] = {false,};
		vector< food > vfoods, init, ans;
		
		cin>>n;
		if(n==0)break;
		for(int i = 0; i < n; ++i){
			food f;
			cin>>f.name>>f.w>>f.s;
			vfoods.push_back( f );
		}
		dfs( n, vfoods, flag, init, bestG, ans );
		for(unsigned int i = 0; i < ans.size(); ++i){
			cout << ans[i].name << endl;
		}
	}
	return 0;
}

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

constexpr int MAX_N = 10;
int n;
int w[MAX_N], s[MAX_N];
string name[MAX_N];

int ans_g;
vector<int> ans;

void dfs(int rest, int used, int g, vector<int> &order) {
	if(g >= ans_g) return;
	if(rest == 0) {
		ans = order;
		ans_g = g;
		return;
	}

	int sum_wait = 0;
	for(int i = 0; i < n; ++i) {
		if((1 << i) & used) continue;
		sum_wait += w[i];
	}

	for(int i = 0; i < n; ++i) {
		if((1 << i) & used) continue;
		if(sum_wait - w[i] > s[i]) continue;

		order.emplace_back(i);
		dfs(rest - 1, used | (1 << i), g + (n - rest + 1) * w[i], order);
		order.pop_back();
	}
}

int main() {
	while(cin >> n && n) {
		for(int i = 0; i < n; ++i) {
			cin >> name[i] >> w[i] >> s[i];
		}

		vector<int> tmp;
		ans_g = INT_MAX;
		ans.clear();
		dfs(n, 0, 0, tmp);

		for(const auto &idx : ans) {
			cout << name[idx] << endl;
		}
	}

	return EXIT_SUCCESS;
}

C++: #include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string f[11]; int n, s[11], w[11], p[11], q[11];
bool ok() {
	for (int i = 0; i < n - 1; i++) {
		int r = 0;
		for (int j = i + 1; j < n; j++) r += w[j];
		if (r > s[i]) return false;
	}
	return true;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) cin >> f[i] >> w[i] >> s[i];
		for (int i = 0; i < n; i++) p[i] = i, q[i] = -1;
		do {
			if (ok()) {
				if (q[0] == -1) for (int i = 0; i < n; i++) q[i] = p[i];
				else {
					int sum1 = 0, sum2 = 0;
					for (int i = 0; i < n; i++) sum1 += w[p[i]] * (i + 1), sum2 += w[q[i]] * (i + 1);
					if (sum1 < sum2) for (int i = 0; i < n; i++) q[i] = p[i];
				}
			}
		} while (p, p + n);
		for (int i = 0; i < n; i++) cout << s[q[i]] << endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct P
{
	int w, s; 
	string name;
};

int main()
{
    for(int n; cin >> n && n;)
	{
		vector<P> v(n);
		
		rep(i, n) cin >> v[i].name >> v[i].w >> v[i].s;
		
		vint p(n), ans;
		rep(i, n) p[i] = i;
		
		double suit = 100000;
		
		do 
		{
			double sum = 0;
			double num = 0;
			
			for(int i = n - 1; 0 <= i; i--)
			{
				P &e = v[p[i]];
				
				if(e.s < sum) break;
				
				sum += e.w;
				num += (i + 1) * e.w;
				
				if(i == 0)
				{
					double sub = num / sum;
					
					if(sub < suit)
					{
						ans = p;
						suit = sub;
					}
				}
			}
		}
		while(next_permutation(ALL(p)));
		
		rep(i, n) cout << v[ans[i]].name << endl;
	}
}