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<algorithm>
#include<vector>
using namespace std;
typedef pair<int,string> P;
typedef pair<int,P> PP;

int n,a;
PP in[10],tmp;
int t[10];
vector<int>ans;

int get_weight(int);

int main(){
  while(cin>>n&&n!=0){
    ans.clear();
    for(int i=0;i<n;i++)cin>>in[i].second.second>>in[i].second.first>>in[i].first;
    sort(in,in+n);

    for(int i=n-1;i>=0;i--){
      for(int j=0;j<i;j++){
       
	if(in[j].second.first>in[j+1].second.first){
	  tmp=in[j];in[j]=in[j+1];in[j+1]=tmp;
	
	  a=get_weight(j+1);
	  if(a>in[j+1].first){tmp=in[j];in[j]=in[j+1];in[j+1]=tmp;}
	}
	
	
      }
    }
    
    for(int i=n-1;i>=0;i--)cout<<in[i].second.second<<endl;
    
    
  }
  return 0;
}

int get_weight(int x){
  int res=0;
  for(int i=0;i<x;i++)res+=in[i].second.first;
  return res;
}

C++: #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 <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	while(cin>>n,n){
		vector<pair<string,pii>> v(n);
		rep(i,n) cin>>v[i].F>>v[i].S.F>>v[i].S.S;
		sort(ALL(v));
		double a=0;
		vector<string> ans;
		do{
			int S=0;
			rep(i,n){
				// cout<<i<<" "<<S<<endl;
				if(S>v[i].S.S){
					// cout<<S<<" "<<v[i].S.F<<" "<<i<<endl;
					double t=0;
					rep(j,i) t+=(j+1)*v[j].S.F;
					t/=S;
					if(a<t){
						a=t;
						ans.clear();
						rep(j,i) ans.PB(v[j].F);
						// cout<<a<<endl;
						// rep(j,ans.size()) cout<<ans[j]<<" ";
						// cout<<endl;
					}
					break;
				}
				if(i==n-1){
					double t=0;
					rep(j,n) t+=(j+1)*v[j].S.F;
					S+=v[i].S.F;
					t/=S;
					if(a<t){
						a=t;
						ans.clear();
						rep(j,i+1) ans.PB(v[j].F);
						// cout<<a<<endl;
						// rep(j,ans.size()) cout<<ans[j]<<" ";
						// cout<<endl;
					}
					break;
				}
				S+=v[i].S.F;
			}
		}while(next_permutation(ALL(v)));
		reverse(ALL(ans));
		rep(i,ans.size()){
			cout<<ans[i]<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

struct food {
	string f; int w, s;
};
int main() {
	int n;
	while (scanf("%d", &n), n) {
		vector<food>v(n);
		int sum = 0;
		rep(i, n) {
			string f; int w, s; cin >> f >> w >> s;
			v[i] = { f,w,s }; sum += w;
		}
		sort(v.begin(), v.end(), [](food a, food b) {return a.w > b.w; });
		while (!v.empty()) {
			int i = 0;
			while (v[i].s < sum - v[i].w)i++;
			sum -= v[i].w;
			cout << v[i].f << endl;
			v.erase(v.begin() + i);
		}
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
long n,i,j,k,w[10],s[10],sw;
string f[10];
while(cin>>n){
if(n==0)break;
for(i=0;i<n;i++)cin>>f[i]>>w[i]>>s[i];
for(sw=0,i=0;i<n;i++)sw+=w[i];
for(k=0;k<n;k++){
for(j=-1,i=0;i<n;i++){
if(sw-w[i]<=s[i]){
if(j==-1)j=i;else if(w[j]<w[i])j=i;
}
}
cout<<f[j]<<endl;
sw-=w[j];w[j]=0;
}
}
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,j,w,s,S;
	string f;
	for(;cin>>n,n;){
		vector<item> items,result;
		for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f));
		sort(items.begin(),items.end());
		S=99999;
		do{
			for(s=w=i=0;i<n;i++){
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(n-i)*items[i].first.first;
			}
			if(i==n){
				if(s<S)S=s,result=items;
			}else if(i>0){
				reverse(items.begin()+i,items.end());
			}
		}while(next_permutation(items.begin(),items.end()));
		reverse(result.begin(),result.end());
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}

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

using namespace std;

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

double calc_g(){
	double p=0;
	double q=0;
	for(int i=0;i<n;i++){
		p += (n-i-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){
			cout << g << endl;
			gmin = g;
			ret = hist;
		}
		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();
		ret.clear();
		name.clear();
		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;
		}
	}
}