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>
using namespace std;

int n;
char name[10][25];
int w[10], s[10];

int per[10], used[10];
double bestscore;
int bestper[10];

void dfs(int dep) {
	int i;
	if ( dep == n ) {
		for(i = 0; i < n; i++ ) {
			int noww = 0;
			for(int j = i+1; j < n; j++ )
				noww += w[per[j]];
			if ( s[per[i]] < noww ) {
				return;
			}
		}
		double s1 = 0, s2 = 0;
		double score = 0;
		for(i = 0; i < n; i++ ) {
			s1 += (i+1)*w[per[i]];
			s2 += w[per[i]];
		}
		score = s1/s2;
		if ( score < bestscore ) {
			bestscore = score;
			for(i = 0; i < n; i++ )
				bestper[i] = per[i];
			return;
		}
	}
	
	for( i = 0; i < n; i++ ) {
		if ( used[i] == 0 ) {
			per[dep] = i;
			used[i] = 1;
			dfs(dep+1);
			used[i] = 0;
		}
	}
}
int main() {
	while(cin >> n) {
		if ( !n ) break;
		int i;
		bestscore = 252521833;
		for(i = 0; i < n; i++ )
			cin >> name[i] >> w[i] >> s[i];
		dfs(0);
		for(i = 0; i < n; i++ )
			cout << name[bestper[i]] << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
	char name[30][21];
	int wei[30],safe[30],perm[30];
	for(int n;scanf("%d",&n),n;){
		for(int i=0;i<n;i++){
			scanf("%s%d%d",name[i],&wei[i],&safe[i]);
			perm[i]=i;
		}

		int opt[30];
		double gopt=-1;
		do{
			bool b=true;

			int wsum=0,g=0;
			for(int i=0;i<n;i++){
				int now=perm[i];
				if(wsum>safe[now]){
					b=false;
					break;
				}
				wsum+=wei[now];
				g+=(i+1)*wei[now];
			}

			if(!b)	continue;

			if(gopt<(double)g/wsum){
				gopt=(double)g/wsum;
				for(int i=0;i<n;i++)	opt[i]=perm[i];
			}
		}while(next_permutation(perm,perm+n));

		for(int i=n-1;i>=0;i--)	puts(name[opt[i]]);
	}

	return 0;
}

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;
    }

    std::cin >> N;
    
    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 <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<pair<int,int>,string> item;
main(){
	int n,i,w,s,S;
	string f;
	for(;cin>>n,n;){
		vector<item> items,result;
		for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f));
		sort(items.begin(),items.end());
		S=99999;
		do{
			for(s=w=i=0;i<n;i++){
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(n-i)*items[i].first.first;
			}
			if(i==n){
				if(s<S)S=s,result=items;
			}else{
				reverse(items.begin()+i,items.end());
			}
		}while(next_permutation(items.begin(),items.end()));
		reverse(result.begin(),result.end());
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;
struct food {
	char fn[21];
	int g;
	int s;
};
int f[10],an[10],m;
food fd[10],sf;

int ans(int n,int s) {
	int i;
	if (n==-1) { for (i=m-1;i>=0;i--) cout << fd[an[i]].fn << endl; return 1;}
	for (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() {
	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 <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	int n;
	while (cin >> n, n) {
		vector<string> f(n);
		vector<int> w(n), s(n);
		rep(i, n) cin >> f[i] >> w[i] >> s[i];
		vector<int> a(n), res;
		int maxg = 0;
		rep(i, n) a[i] = i;
		do {
			int sw = 0, g = 0;
			bool ok = true;
			rep(i, n) {
				if (sw > s[a[i]]) { ok = false; break; }
				sw += w[a[i]];
				g += (i+1) * w[a[i]];
			}
			if (maxg < g) { maxg = g; res = a; }
		} while (next_permutation(all(a)));
		reverse(all(res));
		rep(i, n) cout << f[res[i]] << endl;
	}
	return 0;
}