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 <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
 
struct Elem {
    string name;
    int weight, limit;
};

signed main() {
    int N;
    while(cin >> N, N) {
        vector<Elem> elems;
        for(int i=0; i<N; i++) {
            string name; int weight, limit;
            cin >> name >> weight >> limit;
            elems.push_back(Elem{name, weight, limit});
        }

        vector<int> perm(N);
        iota(perm.begin(), perm.end(), 0);

        double g = INF;
        vector<Elem> ans;
        do {
            vector<Elem> cur;
            for(int i=0; i<N; i++) cur.push_back(elems[ perm[i] ]);

            int sum = 0, ok = true, weighted_sum = 0;
            for(int i=N-1; i>=0; i--) {
                if(cur[i].limit < sum) ok = false;
                sum += cur[i].weight;
                weighted_sum += (i+1) * cur[i].weight;
            }

            double tmp_g = 1.0 * weighted_sum / sum;
            if(ok and g > tmp_g) {
                g = tmp_g;
                ans = cur;
            }
        }while(next_permutation(perm.begin(), perm.end()));
    
        for(int i=0; i<ans.size(); i++) {
            cout << ans[i].name << endl;
        }
    }
    return 0;
}

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

const int MAX_N = (int)1e6;
struct Data {
	std::string name;
	int weight, max_weight;
	Data() = default;
	Data(int _w, int _m, const std::string& _n) : weight(_w), max_weight(_m), name(_n) {}

	bool operator<(const Data& rhs) const {
		return (weight == rhs.weight) ? 
			(max_weight==rhs.max_weight)? 
			name < rhs.name : max_weight < rhs.max_weight : weight < rhs.weight;
	}
	bool operator>(const Data& rhs) const{
		return (weight == rhs.weight) ?
			(max_weight == rhs.weight) ?
			name > rhs.name : max_weight > rhs.max_weight : weight > rhs.weight;
	}
};


int n, sum;
std::vector<Data> data;
std::string ans[MAX_N];

int main() {
	while (1) {
		std::cin >> n;
		if (n == 0)
			break;
		sum = 0;
		int _w, _m;
		std::string _n;
		for (int i = 0; i < n; ++i) {
			std::cin >> _n >> _w >> _m;
			data.push_back(Data(_w, _m, _n));
			sum += data[i].weight;
		}

		std::sort(data.begin(), data.end(), std::greater<Data>());

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < data.size(); ++j) {
				if (data[j].max_weight >= sum - data[j].weight) {
					sum -= data[j].weight;
					ans[i] = data[j].name;
					data.erase(data.begin() + j);
					break;
				}
			}
		}
		for (int i = 0; i < n; ++i)
			std::cout << ans[i] << std::endl;
	}
	system("pause");
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Data
{
public:
    int w, s;
    string name;
    bool operator<(const Data& d) const{
        return s < d.s;
    }
};

int n;
vector<Data> d;
vector<bool> used;
vector<int> bestIndex;
int bestG;

void solve(int k, vector<int>& index, int g, int sumW)
{
    if(k == n){
        if(g < bestG){
            bestG = g;
            bestIndex = index;
        }
        return;
    }

    for(int i=0; i<n; ++i){
        if(used[i])
            continue;
        if(d[i].s >= sumW){
            used[i] = true;
            index[k] = i;
            solve(k+1, index, g+(n-k)*d[i].w, sumW+d[i].w);
            used[i] = false;
        }else{
            return;
        }
    }
}

int main()
{
    for(;;){
        cin >> n;
        if(n == 0)
            return 0;

        d.resize(n);
        for(int i=0; i<n; ++i)
            cin >> d[i].name >> d[i].w >> d[i].s;
        sort(d.begin(), d.end());

        bestG = INT_MAX;
        used.assign(n, false);
        vector<int> index(n);
        solve(0, index, 0, 0);
        for(int i=n-1; i>=0; --i)
            cout << d[bestIndex[i]].name << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define eps (1e-9)
#define equals(a,b) ((fabs((a)-(b))) < eps)
using namespace std;

struct P
{
  string name;
  int w,s,index;
  P(string name="$",int w=inf,int s=inf,int index=-inf):name(name),w(w),s(s),index(index){}
  bool operator < (const P& a)const
  {
    if(w != a.w)return w < a.w;
    if(s != a.s)return s < a.s;
    if(index != a.index)return index < a.index;
    return name < a.name;
  }
};


int main()
{
  int n;
  while(cin >> n,n)
    {
      P info[n];
      string names[n];
      double sum = 0;
      rep(i,n)
	{
	  cin >> info[i].name >> info[i].w >> info[i].s;
	  info[i].index = i;
	  names[i] = info[i].name;
	  sum += info[i].w;
	}
      sort(info,info+n);
      int ans[n],total[n];
      double G = inf;
      do
	{
	  double fac = 0;
	  bool skip = false;
	  rep(i,n)
	    {
	      fac += (i+1)*info[i].w;	  
	      total[i] = info[i].w + (i!=0?total[i-1]:0);
	    }
	  double pG = fac/sum;
	  if(equals(G,pG) || G < pG)continue;
	  
	  rep(i,n)
	    {
	      int on = (int)sum - total[i];
	      if(on > info[i].s)
		{
		  skip = true;
		  break;
		}
	    }       
	  if(skip)continue;


	  G = pG;
	  rep(i,n)ans[i] = info[i].index;

	}while(next_permutation(info,info+n));
      rep(i,n)cout << names[ans[i]] << endl;

    }
  return 0;
}