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 <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef pair <string, II> NODE;
typedef vector <NODE> NODES;

void solve( int n, NODES nodes ) {
    int ming = INT_MAX;
    VS L;
    sort( nodes.rbegin(), nodes.rend() );
    do {
        int wsum = 0, g = 0;
        for ( int i = 0; i < n; ++ i ) {
            if ( wsum > nodes[i].second.second ) goto out_1;
            wsum += nodes[i].second.first;
        }
        for ( int i = 0; i < n; ++ i ) {
            g += ( n - i ) * nodes[i].second.first;
            if ( g >= ming ) goto out_1;
        }
        if ( g < ming ) {
            ming = g;
            VS T;
            for ( int i = 0; i < n; ++ i ) {
                T.push_back( nodes[i].first );
            }
            L = T;
        }
    out_1:;
    } while ( next_permutation( nodes.rbegin(), nodes.rend() ) );

    reverse( L.begin(), L.end() );
    for ( int i = 0; i < n; ++ i ) {
        cout << L[i] << endl;
    }
}

int main() {
    int n;
    while ( cin >> n && n ) {
        NODES nodes( n );
        for ( int i = 0; i < n; ++ i ) {
            cin >> nodes[i].first >> nodes[i].second.first >> nodes[i].second.second;
        }
        solve( n, nodes );
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

struct food {
	string f;
	int w, s;

	food(string f_, int w_, int s_) {
		f = f_; w = w_, s = s_;
	}
};

int N;
vector<food> v;
const int INF = 1000000000;

int G[1 << 10][10];

bool withstand(int status, int next) {
	int sw = 0;
	for(int i = 0; i < N; i++) {
		if(status & (1 << i)) {
			sw += v[i].w;
		}
	}

	return v[next].s >= sw;
}

int DP(int status, int bottom) {
	if(!withstand(status & ~(1 << bottom), bottom))
		return INF;

	if(G[status][bottom] != -1) {
		return G[status][bottom];
	}

	G[status][bottom] = INF;

	int depth = 0;
	for(int i = 0; i < N; i++)
		if(status & (1 << i))
			depth++;

	for(int i = 0; i < N; i++) {
		if(status & (1 << i)) {
			G[status][bottom] = min(G[status][bottom], DP(status & ~(1 << bottom), i) + (N - depth + 1) * v[bottom].w);
		}
	}

	return G[status][bottom];
}

int main() {
	while(cin >> N, N) {
		v.clear();
		for(int i = 0; i < N; i++) {
			string f;
			int w, s;
			cin >> f >> w >> s;
			v.push_back(food(f, w, s));
		}
		for(int i = 0; i < 1 << N; i++) {
			for(int j = 0; j < N; j++) {
				G[i][j] = -1;
			}
		}
		for(int i = 0; i < N; i++) {
			G[0][i] = 0;
		}
		int ret = INF;
		for(int i = 0; i < N; i++) {
			int tmp = DP((1 << N) - 1, i);
			ret = min(ret, tmp < 0 ? INF : tmp);
		}
//		cout << ret << endl;
		queue<int> q;
		int status = (1 << N) - 1;
		while(status) {
			for(int i = 0; i < N; i++) {
				if(ret == G[status][i] && (status & (1 << i))) {
					q.push(i);
					ret -= v[i].w * q.size();
					status &= ~(1 << i);
					break;
				}
			}
		}

		while(!q.empty()) {
			cout << v[q.front()].f << endl;
			q.pop();
		}
	}
}

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=999999999;
		do{
			for(s=w=i=0;i<n;i++){ //from upper
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(n-i)*items[i].first.first;
			}
			if(i==n&&s<S)cout<<s<<endl,S=s,result=items;
		}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<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    while(cin >> n, n){

        vector<int> p(n);
        rep(i, n){
            p[i] = i;
        }

        vector<string> f(n);
        vector<lint> w(n), s(n);
        rep(i, n){
            cin >> f[i] >> w[i] >> s[i];
        }

        vector<int> ans;
        lint mini = 1000000000000LL;

        do{

            bool flag = true;
            lint g = 0, cnt = 0;

            rep(i, n){
                flag &= cnt <= s[p[i]];
                cnt += w[p[i]];
                g += (lint)(n - i) * w[p[i]];
            }

            if(flag && mini > g){
                ans = p;
                mini = g;
            }

        }while(next_permutation(p.begin(), p.end()));

        repr(i, n){
            cout << f[ans[i]] << endl;
        }

    }

    return 0;
}

C++: #include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string f[11]; int n, s[11], w[11], p[11], q[11];
bool ok() {
	for (int i = 0; i < n - 1; i++) {
		int r = 0;
		for (int j = i + 1; j < n; j++) r += w[p[j]];
		if (r > s[p[i]]) return false;
	}
	return true;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) cin >> f[i] >> w[i] >> s[i];
		for (int i = 0; i < n; i++) p[i] = i, q[i] = -1;
		do {
			if (ok()) {
				if (q[0] == -1) for (int i = 0; i < n; i++) q[i] = p[i];
				else {
					int sum1 = 0, sum2 = 0;
					for (int i = 0; i < n; i++) sum1 += w[p[i]] * (i + 1), sum2 += w[q[i]] * (i + 1);
					if (sum1 < sum2) for (int i = 0; i < n; i++) q[i] = p[i];
				}
			}
		} while (next_permutation(p, p + n));
		for (int i = 0; i < n; i++) cout << s[q[i]] << endl;
	}
}