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 <string>
#include <vector>
#include <cstring>

using namespace std;

vector<int> w,s,hist;
vector<string> name;
int n,tmpn,tmpm;
int used,ret[10];
string str;
double gmin = 1e+10;

inline double calc_g(){
	double p=0;
	double q=0;
	for(int i=0;i<n;i++){
		p += (n-i) * hist[n-1-i];
		q += hist[i];
	}
	return p/q;
}

void solve(int num, int weight){
	if(num == n){			
		double g = calc_g();
		if(gmin > g){
			gmin = g;
			for(int i=0;i<n;i++) ret[i] = hist[i];
		}
		return;
	}

	for(int i=0;i<n;i++){
		if((used & (1 << i)) == 0){
			if(s[i] >= weight){
				//ツ鳴「ツ使ツ用ツづつゥツづつ重ツづ敖づ可妥渉つヲツづァツづェツつスツづァツ静渉づアツづづ敖づゥ
				used |= 1 << i;
				hist.push_back(i);
				solve(num+1, weight+w[i]);
				hist.pop_back();
				used ^= 1 << i;
			}
		}
	}
}

int main(void){
	while(cin >> n && n){
		w.clear();
		s.clear();
		hist.clear();
		name.clear();
		memset(ret, 0, sizeof(ret));
		used = 0;
		gmin = 1e+10;
		for(int i=0;i<n;i++){
			cin >> str >> tmpn >> tmpm;
			name.push_back(str);
			w.push_back(tmpn);
			s.push_back(tmpm);
		}
		solve(0,0);
		for(int i=0;i<n;i++){
			cout << name[ret[n-i-1]] << endl;
		}
	}
}

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;
int main() {
	while (true) {
		cin >> n; b.clear(); maxn = 1 << 30;
		if (n == 0) { break; }
		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 <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[p[j]];
		if (r > s[p[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 (next_permutation(p, p + n));
		for (int i = 0; i < n; i++) cout << f[q[i]] << endl;
	}
}

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

// 入力
string f[10];
int n, w[10], s[10]; 
// used[i] := i番目を選んだかどうか
bool used[10];
// 今まで見つかった中で一番重心が低いもの
double ans_g;
// 解
vector<int> ans;

// 重さに耐えられるかチェック
bool check(vector<int>& v){
	int g[10] = {0};
	for(int i=0 ; i < v.size() ; i++ ){
		for(int j=0 ; j < i ; j++ ){
			g[j] += w[v[i]];
		}
	}
	for(int i=0 ; i < v.size() ; i++ ){
		if( g[i] > s[v[i]] )
			return false;
	}
	return true;
}

// 重心を計算
double gravity(vector<int>& v){
	int a = 0, b = 0;
	for(int i=0 ; i < v.size() ; i++ ){
		a += (i+1) * w[v[i]];
		b += w[v[i]];
	}
	return (double) a / b;
}

// 探索
void dfs(vector<int>& v, int num){
	if( num == n ){
		double g = gravity(v);
		if( g < ans_g ){
			ans = v;
		}
		return;
	}
	
	for(int i=0 ; i < n ; i++ ){
		if( used[i] ) continue;
		
		used[i] = true;
		v.push_back( i );
		// 重さに耐えられるかチェック
		if( check(v) ){
			dfs( v , num+1 );
		}
		v.pop_back();
		used[i] = false;
	}
}

int main(){
	while( cin >> n , n ){
		for(int i=0 ; i < 10 ; i++ ) used[i] = false;
		
		for(int i=0 ; i < n ; i++ ){
			cin >> f[i] >> w[i] >> s[i];
		}
		ans_g = 1e+9;
		vector<int> v;
		dfs( v , 0 );
		for(int i=0 ; i < ans.size() ; i++ ){
			cout << f[ans[i]] << endl;
		}
	}
}