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 <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>

using namespace std; using namespace placeholders;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector< int > VI;
typedef vector< VI > VVI;
typedef vector< string > VS;
typedef stringstream SS;
typedef pair< int, int > PII;
typedef vector< pair< int, int > > VPII;

template < typename T > inline void input_n( vector< T > &out ) { copy_n( istream_iterator< T >( cin ), out.size(), out.begin() ); };
template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) (c).begin(), (c).end()
#define AALL( a, t ) (t*)a, (t*)a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define EM( n ) emplace( n )
#define EB( n ) emplace_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

struct Food
{
	int id;
	string name;
	double w, s;

	Food( const int id, const string &name, const int w, const int s ) :
		id( id ), name( name ), w( w ), s( s )
	{
		return;
	}

	bool operator<( const Food &rhs ) const
	{
		return id < rhs.id;
	}
};

double center( const vector< Food > &foods )
{
	const int N = foods.size();

	for ( int i = N - 1, sum = 0; 0 <= i; sum += foods[ i-- ].w )
	{
		if ( foods[i].s < sum )
		{
			return numeric_limits< double >::max();
		}
	}

	double g = 0, ss = 0;
	REP( i, 0, N )
	{
		g += ( i + 1 ) * foods[i].w;
		ss += foods[i].w;
	}

	return g / ss;
}


int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( int n; cin >> n, n; )
	{
		vector< Food > foods;

		REP( i, 0, n )
		{
			string f;
			int w, s;
			cin >> f >> w >> s;

			foods.emplace_back( i, f, w, s );
		}

		double ming = numeric_limits<double>::max();
		vector< Food > res;

		do
		{
			const double g = center( foods );
			if ( g < ming )
			{
				res = foods;
				ming = g;
			}
		}
		while ( next_permutation( ALL( foods ) ) );

		FOR( r, res )
		{
			cout << r.name << endl;
		}
	}

	return 0;
}

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

string f[11];
int ans[11];
int n;
int w[11],s[11];

void solve(){
	for(int i=1; i<=n; i++)
		ans[i] = i;
	for(int k=1; k<=n; k++){
		for(int i=n; i>k; i--){
			if(s[ans[i]] > s[ans[i-1]]){
				int tmp = ans[i];
				ans[i] = ans[i-1];
				ans[i-1] = tmp;
			}
		}
	}
	bool flag = true;
	for(int i=1; i<=n; i++){
		if(i==n&&flag)
			i=1;
		else if(i==n)
			break;
		if(w[ans[i]] < w[ans[i+1]]){
			int tmp = w[ans[i]];
			for(int k=i+2; k<=n; k++)
				tmp += w[ans[k]];
			if(s[ans[i+1]] >= tmp){
				tmp = ans[i];
				ans[i] = ans[i+1];
				ans[i+1] = tmp;
				flag = false;
			}
		}
	}
}

int main(){
	while(true){
		cin >> n;
		if(!n)
			return 0;
		for(int i=1; i<=n; i++)
			cin >> f[i] >> w[i] >> s[i];
		solve();
		for(int i=1; i<=n; i++)
			cout << f[ans[i]] << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define INF 1e+9
using namespace std;

int main(){
	int n;
	while(cin >> n,n){
		int w[10],s[10],dp[1 << 10][10],prev[1 << 10][10][2];
		string f[10];
		for(int i = 0;i < (1 << n);i++){
			for(int j = 0;j < n;j++) dp[i][j] = INF;
		}
		for(int i = 0;i < n;i++) dp[0][i] = 0;
		for(int i = 0;i < n;i++) cin >> f[i] >> w[i] >> s[i];
		for(int i = 1;i < (1 << n);i++){
			int sum = 0,cnt = 0;
			for(int j = 0;j < n;j++){
				if((i >> j) & 1) {
					sum += w[j];
					cnt++;
				}
			}
			for(int j = 0;j < n;j++){
				if(((i >> j) & 1) && s[j] >= sum - w[j]){
					for(int k = 0;k < n;k++){
						if(dp[i & ~(1 << j)][k] + (n - cnt + 1) * w[j] < dp[i][j]){
							dp[i][j] = dp[i & ~(1 << j)][k] + (n - cnt + 1) * w[j];
							prev[i][j][0] = i & ~(1 << j);
							prev[i][j][1] = k;
						}
					}
				}
			}
		}
		int mi = INF,mix;
		for(int i = 0;i < n;i++) {
			if(mi > dp[(1 << n) - 1][i]){
				mi = dp[(1 << n) - 1][i];
				mix = i;
			}
		}
		cout << f[mix] << endl;
		int pos = (1 << n) - 1;
		for(int i = 0;i < n - 1;i++){
			cout << f[prev[pos][mix][1]] << endl;
			int p = prev[pos][mix][0];
			mix = prev[pos][mix][1];
			pos = p;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)
#define EPS (1e-8)

string name[10];
int w[10], s[10];
int num[10], an[10];
double mi;
int n;

void dfs(int pos, double sum, double g) {
	if (pos == n) {
		g /= (double)sum;
		if (mi > g + EPS) {
			mi = g;
			REP(i, 10) an[i] = num[i];
		}
	}
	else {
		REP(i, n) if (num[i] == -1 && s[i] >= sum - EPS) {
			num[i] = pos;
			dfs(pos + 1, sum + w[i], g + w[i] * (n - pos));
			num[i] = -1;
		}
	}
}

int main() {
	while (cin >> n, n) {
		REP(i, n) cin >> name[i] >> w[i] >> s[i];

		fill(num, num + n, -1);
		fill(an, an + n, -1);
		mi = INF;

		dfs(0, 0.0, 0.0);
		vector<int> ans(n);
		for (int i = n - 1; i >= 0; i--) REP(j, n) if (an[j] == i) ans[n - i - 1] = j;
		REP(i, n) cout << name[ans[i]] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct Food {
	int id;
	string name;
	int w, s;
	
	Food(){id = 0; w = 0; s = 0;}
	
	bool operator < (const Food &t) const {
		return id < t.id;
	}
};

int main()
{
	int n;
	
	while (scanf("%d", &n), n){
		vector<Food> f(n);
		
		for (int i = 0; i < n; i++){
			f[i].id = i;
			cin >> f[i].name >> f[i].w >> f[i].s;
		}
		
		vector<Food> res(n);
		int minG = 1001001001;
		
		do {
			int sum = 0;
			bool fl = false;
			for (int i = n - 1; i >= 0; i--){
				if (f[i].s < sum){
					fl = true;
					break;
				}
				sum += f[i].w;
			}
			
			if (!fl){
				int G = 0;
				for (int i = 0; i < n; i++){
					G += f[i].w * (i + 1);
				}
				
				if (minG > G){
					res = f;
					minG = G;
				}
			}
			
		} while (next_permutation(f.begin(), f.end()));
		
		for (int i = 0; i < n; i++){
			cout << res[i].name << endl;
		}
	}
	
	return 0;
}