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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}


string name[10];
int w[10];
int s[10];

vector<int> answer,current;
int used[10];
int n;
int sum = 0;
double jushin = 0 , curr = 0;
int dfs(int x){
	int chksum = 0;
	for(int i = current.size() - 1 ; i >= 0 ; i--){
		if( chksum > s[current[i]] ) return 0;
		chksum += w[current[i]];
	}
	
	if( current.size() == n ) {
		if( jushin < curr ){
			answer = current;
			curr = jushin;
		}
		return 0;
	}
	for(int i = 0 ; i < n ; i++){
		if( !used[i] ){
			used[i] = true;
			current.push_back(i);
			jushin += x * w[i];
			dfs(x+1);
			jushin -= x * w[i];
			current.pop_back();
			used[i] = false;
		}
	}
}
int main(){
	while(cin >> n && n){
		for(int i = 0 ; i < n ; i++){
			cin >> name[i] >> w[i] >> s[i];
		}
		curr = 1e9;
		answer.clear();
		dfs(1);
		for(int i = 0 ; i < n ; i++){
			cout << name[answer[i]] << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> bool chmin(T &a, const T &b){ return a > b && (a = b, true);}
template<class T> bool chmax(T &a, const T &b){ return a < b && (a = b, true);}

int main() {
    int n;
    while(cin >> n, n){
        vector<string> S(n);
        vector<int> W(n), A(n);
        for (int i = 0; i < n; ++i) {
            cin >> S[i] >> W[i] >> A[i];
        }
        vector<int> T(1 << n), cnt(1 << n);
        for (int i = 0; i < (1 << n); ++i) {
            for (int j = 0; j < n; ++j) {
                if(i&(1 << j)) {
                    T[i] = T[i^(1 << j)]+W[j];
                    cnt[i] = cnt[i^(1 << j)]+1;
                }
            }
        }
        vector<int> dp(1 << n, INF<int>), top(1 << n, INF<int>);
        dp[0] = 0;
        for (int i = 0; i < (1 << n); ++i) {
            for (int j = 0; j < n; ++j) {
                if(!(i & (1 << j)) && A[j] >= T[i]){
                    if(chmin(dp[i^(1 << j)], dp[i]+(n+1-cnt[i])*W[j])){
                        top[i^(1 << j)] = j;
                    }
                }
            }
        }
        int cur = (1 << n)-1;
        queue<string> ans;
        while(cur){
            ans.emplace(S[top[cur]]);
            cur -= 1 << top[cur];
        }
        while(!ans.empty()){
            cout << ans.front() << "\n"; ans.pop();
        }
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) rep2(i,0,n)
#define ll long long
#define P pair<int,int>
#define pb push_back

int n,w[10],s[10],used[10],g;
string F[10];
vector<int> ans;

void f(vector<int> order){
	if(order.size()==n){
		int x=0;
		rep(i,n)x+=w[order[i]]*(n-i);
		if(x<g){
			g=x;
			ans=order;
		}
		return;
	}
	int x=0;
	rep(i,order.size())x+=w[order[i]];
	rep(i,n)if(!used[i]&&s[i]>=x){
		order.pb(i);
		used[i]=1;
		f(order);
		used[i]=0;
		order.pop_back();
	}
}

int main(){
	while(cin>>n&&n){
		fill(used,used+n,0);
		g=INF;
		rep(i,n)cin>>F[i]>>w[i]>>s[i];
		vector<int> v;
		f(v);
		rep(i,n)cout<<F[ans[n-1-i]]<<endl;
	}
}