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 <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef unsigned long long ull;
typedef std::pair<int,int> P;

const ull INF = 1001001001001001001;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Food{
    std::string name;
    ull w, s;
};

const int MAX_N = 10;
int N;
std::vector<Food> F;
int order[MAX_N], fact[11];

int main(){
    fact[0] = 1;
    FOR(i, 1, 11){
        fact[i] = fact[i-1] * i;
    }

    while(std::cin >> N, N){
        F.clear();

        REP(i, N){
            std::string name;
            ull w, s;
            std::cin >> name >> w >> s;
        
            F.push_back({name, w, s});
        }

        REP(i, N){
            order[i] = i;
        }
    
        ull min_cost = INF;
        int min_order[MAX_N];
        REP(_, fact[N]){
            // for(int i=0;i<N;i++){
            //     printf("%d, ", order[i]);
            // }
            // puts("");

            bool f = true;
            ull cost = 0, sum = 0;
            REP(i, N){
                int index = order[i];
                if(sum <= F[index].s);
                else{f = false;}

                cost += (N-i) * F[index].w;
                sum += F[index].w;
            }
            //std::cout << cost << std::endl;

            if(f && min_cost > cost){
                min_cost = cost;
                std::copy(order, order+N, min_order);
            }

            std::next_permutation(order, order+N);
        }

        //std::cout << min_cost << std::endl;
        for(int i=N-1;i>=0;i--){
            int index = min_order[i];
            std::cout << F[index].name << std::endl;
        }
    }
}

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const double EPS = 1e-6;

int main(void) {
	int N;
	while (cin >> N, N) {;
		vector<string> F(N);
		VI W(N), S(N);
		for (int i = 0; i < N; i++) {
			cin >> F[i] >> W[i] >> S[i];
		}

		VI p(N);
		iota(p.begin(), p.end(), 0);

		double ans = 1e+8;
		VI best_p(p.begin(), p.end());

		do {
			double g = 0, gt = 0;
			for (int i = 0; i < N; i++) {
				g += (i + 1) * W[p[i]];
				gt += W[p[i]];
			}
			g /= gt;

			bool flg = true;
			for (int i = N - 1; i >= 0; i--) {
				int sum = 0;
				for (int j = i + 1; j < N; j++) {
					sum += W[p[j]];
				}
				flg &= sum <= S[p[i]];
			}

			if (flg && g < ans + EPS) {
				ans = g;
				swap(best_p, p);
			}
		} while (next_permutation(p.begin(), p.end()));

		for (auto i : best_p) {
			cout << F[i] << 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=0;i<n;i++){
      for(int j=0;j<n-1;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;
	}
      }
    }
    
    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<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>
#include<iterator>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define rep(i,n) for(int i=0; i<n; i++)
#define INF (1<<20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;


int main()
{
	int n;
	while (cin >> n, n) 
	{
		vector<pair<pair<int, int>,string> > v;
		bool used[20] = {0};
		int sum = 0;
		rep(i,n){
			string f;
			int w, s;
			cin >> f >> w >> s;
			v.push_back({{w,s},f });
			sum += w;
		}

		sort(v.begin(), v.end());
		reverse(v.begin(),v.end());

		rep(j,n){
			rep(i, n) {
				if (!used[i]) {
					if (v[i].first.second >= sum - v[i].first.first) {
						cout << v[i].second << endl;
						sum -= v[i].first.first;
						used[i] = 1;
						break;
					}
				}
			}
			sort(v.begin(), v.end());
			reverse(v.begin(), v.end());
		}
	}
	return 0;
}