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 <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)

int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

struct food{
  string fn;
  int g;
  int s;
};
int f[10],an[10],m;
food fd[10],sf;
int ans(int n,int s){
  if(n==-1){
    for(int i = m - 1 ; i >= 0 ; i --){
      cout << fd[an[i]].fn << endl; 
      return 1;
    }
  }
  for(int i = m - 1 ; i >= 0 ; i --){
    if(f[i] == 0 && fd[i].s >= s-fd[i].g){
      f[i] = 1; an[n] = i;
      if(ans(n-1,s-fd[i].g)==1)return 1;
      f[i] = 0;
    }
  }
  return 0;
}

int main(void){
  int i,j,s;
  while(cin>>m){
    if(m==0)break;
    for(i = 0 , s = 0 ; i < m ; i ++){
      f[i] = 0 ; 
      cin>>fd[i].fn>>fd[i].g>>fd[i].s;
      s+=fd[i].g;
    }
    for(i = 1 ; i < m ; i ++){
      for(j = 0  ; j < m - i ; j ++){
	if(fd[j].g>fd[j+1].g){
	  sf=fd[j];
	  fd[j] = fd[j+1];
	  fd[j+1] = sf;
	}
      }
    }
    ans(m-1,s);
  }
  return 0;
}

C++: #include<stdio.h>
#include<string.h>
typedef struct i{char f[21];int s,w;} i;
i g[10];
int n,t[10],f[10],ans[10],max;
void rec(int d)
{
	int i,w=0,G=0;
	if(d==n)
	{
		for(i=0;i<n;i++)
		{
			if(w>g[t[i]].s)
				return;
			w+=g[t[i]].w;
			G+=g[t[i]].w*(i+1);
		}
		if(max<G)
		{
			max=G;
			memcpy(ans,t,40);
		}
	}
	else
	{
		for(i=0;i<n;i++)
		{
			if(!f[i])
			{
				f[i]=1;
				t[d]=i;
				rec(d+1);
				f[i]=0;
			}
		}
	}
}

int main()
{
	int i;
	for(;;)
	{
		memset(ans,0,40);
		memset(f,0,40);
		max=0;
		scanf("%d",&n);
		if(!n)
			return 0;
		for(i=0;i<n;i++)
			scanf("%s %d%d",g[i].f,&g[i].w,&g[i].s);
		rec(0);
		while(-- n>=0)
			puts(g[ans[n]].f);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

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

int n;
vector<food> foods;
vector<string> res;
pair<int,vector<string> > result;

void f(int b,int w,int k){
	int i;
//cout<<b<<" "<<w<<endl<<" ";
//for(i=0;i<res.size();i++)cout<<res[i]<<endl;
//cout<<endl;
	if(b==(1<<n)-1){
		if(result.first>k){
			result.first=k;
			result.second.clear();
			for(i=0;i<n;i++)result.second.push_back(res[i]);
		}
	}
	for(i=0;i<n;i++){
		if(b&1<<i)continue;
		if(foods[i].s<w)continue;
		res.push_back(foods[i].f);
		f(b|1<<i,w+foods[i].w,k+w+foods[i].w);
		res.pop_back();
	}
}

int main() {
	while(n=in()){
		int i;
		foods.clear();
		food buf;
		result.first=1000000000;
		for(i=0;i<n;i++){
			cin>>buf.f>>buf.w>>buf.s;
			foods.push_back(buf);
		}
		res.clear();
		f(0,0,0);
		for(i=0;i<n;i++){
			cout<<result.second[n-i-1]<<endl;
		}
	}
	return 0;
}

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[a[i]]) goto E;
			}
			for (int i = 0; i < n; i++) {
				sum2 += i*w[a[i]];
			}
			if (maxn > sum2) {
				b = a; maxn = sum2;
			}
		E:;
		} while (next_permutation(a.begin(), a.end()));
		for (int i = 0; i < n; i++) { cout << S[b[i]] << endl; }
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

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

int n;
vector<food> foods;
vector<string> res;
pair<int,vector<string> > result;

void f(int b,int w,int k){
	int i;
//cout<<b<<" "<<w<<endl<<" ";
//for(i=0;i<res.size();i++)cout<<res[i]<<endl;
//cout<<endl;
	if(b==(1<<n)-1){
		if(result.first>k){
			result.first=k;
			result.second.clear();
			for(i=0;i<n;i++)result.second.push_back(res[i]);
		}
	}
	for(i=0;i<n;i++){
		if(foods[i].s<w)continue;
		res.push_back(foods[i].f);
		f(b|1<<i,w+foods[i].w,k+w+foods[i].w);
		res.pop_back();
	}
}

int main() {
	while(n=in()){
		int i;
		foods.clear();
		food buf;
		result.first=1000000000;
		for(i=0;i<n;i++){
			cin>>buf.f>>buf.w>>buf.s;
			foods.push_back(buf);
		}
		res.clear();
		f(0,0,0);
		for(i=0;i<n;i++){
			cout<<result.second[n-i-1]<<endl;
		}
	}
	return 0;
}

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++){
	a=get_weight(j)+in[j+1].second.first;
	if(a<=in[j].first&&in[j].second.first>in[j+1].second.first){
	  tmp=in[j];in[j]=in[j+1];in[j+1]=tmp;
	}
	/*
else if(a<=in[j].first&&(in[j].second.first==in[j+1].second.first&&in[j].first>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;
}