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 <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int n;
vi w, s;
vi array;

int best;
vi bestarray;

void solve(int g, int st, int k)
{
	if(g >= best)
		return;
	
	if(k == n){
		best = g;
		bestarray = array;
		return;
	}
	
	reep(i, k, n){
		if(st >= w[array[i]]){
			swap(array[k], array[i]);
			solve(g+(k+1)*w[array[k]], min(st-w[array[k]], s[array[k]]), k+1);
			swap(array[k], array[i]);
		}
	}
}

int main()
{
	while(scanf("%d", &n), n){
		vector<string> f(n);
		w.resize(n);
		s.resize(n);
		rep(i, n){
			char name[32];
			scanf("%s%d%d", name, &w[i], &s[i]);
			f[i] = name;
		}
		
		best = 100000000;
		array.resize(n);
		rep(i, n)
			array[i] = i;
		solve(0, 10000000, 0);
		rep(i, n){
			puts(f[bestarray[i]].c_str());
		}
	}

	return 0;
}

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

struct edge {
  string Name;
  int weight, deep;
  bool operator<(const edge& e) const {
    return(weight > e.weight);
  }
};

int main()
{
  int n;
  while(cin >> n, n) {
    list< edge > edges;
    int ALL = 0;
    for(int i = 0; i < n; i++) {
      edge e;
      cin >> e.Name >> e.weight >> e.deep;
      ALL += e.weight;
      edges.push_back(e);
    }
    edges.sort();
    while(!edges.empty()) {
      for(list< edge >::iterator it = edges.begin(); ; ++it) {
        if(ALL - it -> weight <= it -> deep) {
          cout << it -> Name << endl;
          ALL -= it -> weight;
          edges.erase(it);
          break;
        }
      }
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n;
  while(cin>>n, n){
    vector<int> ans(n), per(n), w(n), s(n);
    vector<string> name(n);
    int g = INT_MAX;
    REP(i,n){
      cin>>name[i]>>w[i]>>s[i];
      per[i] = i;
    }
    do{
      int gg = 0;
      int ww = 0;
      bool flag = true;
      for(int i=n-1; i>=0; i--){
	int pp = per[i];
	if(s[pp] < ww){
	  flag = false;
	  break;
	}
	ww += w[pp];
	gg += (i+1) * w[pp];
      }
      if(flag && g > gg){
	g = gg;
	ans = per;
      }
    }while(next_permutation(per.begin(), per.end()));
    
    REP(i,n) cout << name[ans[i]] << endl;
  }
  return 0;
}

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};
struct item
{
	string str;
	int w,s;
};
item it[11];
int n;
vector<int> ans,curr;
double G,tmpg;
bool used[10];
void dfs(int x){
	int tmp =0;
	for(int i =curr.size()-1;i >=0;i--){
		if(tmp >it[curr[i]].s)return;
		tmp +=it[curr[i]].w;
	}
	if(curr.size()==n){
		if(tmpg<G){
			G =tmpg;
			ans =curr;
		}
		return;
	}
	for(int i =0;i <n;i++){
		if(!used[i]){
			used[i]=true;
			curr.push_back(i);
			tmpg +=(x*it[i].w);
			dfs(x+1);
			tmpg -=(x*it[i].w);
			curr.pop_back();
			used[i]=false;
		}
	}
	return;
}


int main(){
	while(cin>>n,n){
		for(int i =0;i <n;i++){
			cin>>it[i].str>>it[i].w>>it[i].s;
		}
		G =INF;
		tmpg = 0;
		dfs(1);
		for(int i =0;i <n;i++){
			cout <<it[ans[i]].str<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>

using namespace std;

typedef pair<int, string> P;
typedef pair<int, P> Q;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		Q q;
		priority_queue<Q, vector<Q>, greater<Q> > pq;
		for (int i = 0; i < n; i++) {
			cin >> (q.second).second >> (q.second).first >> q.first;
			pq.push(q);
		}
		vector<Q> data;
		q = pq.top();
		pq.pop();
		data.push_back(q);
		int w = (q.second).first;
		while (!pq.empty()) {
			data.push_back(pq.top());
			pq.pop();
			int sum = 0;
			for (int i = 0; i < data.size()-1; i++) {
				sum += (data[i].second).first;
			}
			for (int i = data.size()-1; i > 0; i--) {
				if ((data[i].second).first < (data[i-1].second).first) {
					if (sum-(data[i-1].second).first+(data[i].second).first <= data[i-1].first) {
						sum -= (data[i-1].second).first;
						swap(data[i], data[i-1]);
					} else {
						break;
					}
				} else {
					break;
				}
			}
		}
		for (int i = data.size()-1; i >= 0; i--) {
			cout << (data[i].second).second << endl;
		}
	}
	return 0;
}

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

vector<int> choice;
string name[10];
int weight[10];
int s[10];
bool done[10];
int n;
pair<double,vector<int> > ret;
void bt(){
	int test;
	for(int i=0;i<choice.size();i++){
		test = 0;
		for(int j=i+1;j<choice.size();j++){
			test+=weight[choice[j]];
		}
		if(s[choice[i]] < test)return;
	}
	if(choice.size() == n){
		double G=0,DIV=0;	
		for(int i=0;i<choice.size();i++){
			G += (i+1)*weight[choice[i]];
			DIV += weight[choice[i]];
		}
		ret = min(ret,make_pair(G/DIV,choice));
	}
	
	for(int i=0;i<n;i++){
		if(!done[i]){
			done[i] = true;
			choice.push_back(i);
			bt();
			done[i] = false;
			choice.pop_back();
		}
	}
}

int main(){
	while(cin >> n, n){
		ret = make_pair(1<<21,0);
		for(int i=0;i<n;i++){
			cin >> name[i] >> weight[i] >> s[i];
		}
		bt();
		for(int i=0;i<ret.second.size();i++){
			cout << name[ret.second[i]] << endl;
		}
	}
}