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 <algorithm>
#include <vector>

using namespace std;

bool debug = true;

struct food {
    string name;
    int w, s;
    bool operator<(const food& o) const {
        return w == o.w ? (s == o.s ? name < o.name : s < o.s) : w < o.w;
    }
};

int sum(vector<food> ns) {
    int ret = 0;
    for (int i = 0; i < ns.size(); i++) 
        ret += ns[i].w;
    return ret;
}

bool compare_result(const vector<food>& a, const vector<food>& b) {
    //for (int i = 0; i < a.size(); i++) cout << a[i].w << ' '; cout << endl;
    //for (int i = 0; i < b.size(); i++) cout << b[i].w << ' '; cout << endl;
    for (int i = 0; i < a.size(); i++) {
        if (a[i].w > b[i].w) return true;
        if (a[i].w == b[i].w) continue;
        else return false;
    }
    return true;
}

int n;
int main() {
    while (cin >> n, n != 0) {
        vector<food> foods(n);
        for (int i = 0; i < n; i++) {
            food f;
            cin >> f.name >> f.w >> f.s;
            foods[i] = f;
        }
        sort(foods.begin(), foods.end());
        int total_weight = sum(foods);
        vector<food> res(foods);
        do {
            //for (int i = 0; i < n; i++) cout << foods[i].name << ' '; cout << endl;
            int weight_above = total_weight;
            bool found = true;
            for (int i = 0; i < n; i++) {
                weight_above -= foods[i].w;
//                cout << foods[i].name << ' ' << foods[i].s << ' ' << weight_above << endl;
                if (foods[i].s < weight_above) {
                    found = false;
                    break;
                }
            }
            if (!found) {
                //cout << weight_above << endl;
                continue;
            } else {
                if (compare_result(foods, res)) {
                    res = foods;
                    //for (int i = 0; i < n; i++) cout << res[i].name << ' '; cout << endl;
                }
                //for (int i = 0; i < n; i++) 
                //    cout << foods[i].name << ' '
                //         << foods[i].w    << ' '
                //         << foods[i].s    << ' ' << endl;
                //if (debug) cout << endl;
            }
        } while (next_permutation(foods.begin(), foods.end()));
        for (int i = 0; i < n; i++) 
            //cout << res[i].name << ' '
            //     << res[i].w    << ' '
            //     << res[i].s    << ' ' << endl;
            cout << res[i].name << endl;
        
    }
    return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

int n;
double minimum;

using namespace std;

struct Data{
	char name[21];
	int self_weight,bearable_weight,index;
	bool FLG;
};

Data final_data[10];

void recursive(Data data[10],int sum,int index){

	if(index == n){
		double bunshi = 0,bunbo = 0,result;

		for(int i = 1; i <= n-1; i++){
			for(int k = n-1; k >= i; k--){
				if(data[k].index > data[k-1].index)swap(data[k],data[k-1]);
			}
		}


		for(int i = 0; i < n; i++)bunshi += (i+1)*data[i].self_weight;
		for(int i = 0; i < n; i++)bunbo += data[i].self_weight;
		result = bunshi/bunbo;

		if(result < minimum){
			minimum = result;
			for(int k = 0; k < n; k++)final_data[k] = data[k];
		}
		return;
	}

	bool tmpFLG;

	for(int i = 0; i < n; i++){
		if(data[i].FLG == false && data[i].bearable_weight >= sum){
			tmpFLG = true;
			for(int k = 0; k < n; k++){
				if(i != k && data[k].FLG == false && data[k].bearable_weight < sum + data[i].self_weight){
					tmpFLG = false;
					break;
				}
			}

			if(tmpFLG){
				Data new_data[10];
				for(int k = 0; k < n;k++)new_data[k] = data[k];
				new_data[i].FLG = true;
				new_data[i].index = index;
				recursive(new_data,sum+data[i].self_weight,index+1);
			}
		}
	}
}

int main(){

	Data data[10];

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		minimum = 2000000000;

		for(int i = 0; i < n; i++){
			data[i].FLG = false;
			scanf("%s %d %d",data[i].name,&data[i].self_weight,&data[i].bearable_weight);
		}

		recursive(data,0,0);

		for(int i = 0; i < n; i++){
			printf("%s\n",final_data[i].name);
		}

	}

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

struct Food{
	string f;
	int w,s;
	bool operator <(const Food &a)const{
		if(w==a.w) return s>a.s;
		return w>a.w;
	}
};

int main(){
	int N;
	while(cin>>N,N){
		int sumW = 0;
		Food food[10];
		for(int i=0;i<N;i++){
			cin>>food[i].f>>food[i].w>>food[i].s;
			sumW += food[i].w;
		}
		sort(food,food+N);
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(food[j].w == -1) continue;
				if(food[j].s >= sumW-food[j].w){
					cout<<food[j].f<<endl;
					sumW -= food[j].w; food[j].w = -1; break;
				}
			}
		}
		
	}
	return 0;
}