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 <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require sqrt ceil floor
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <stdint.h>	// require uint64_t
#include <fstream>	// require freopen

using namespace std;

typedef pair <pair<int, int>, string > ITEM;	// ACeÌd³AACeÌãÉu¯éd³ÌãÀAACe¼

vector <ITEM> list;
int N;
vector <vector <int> > order;

int calcG (map <int, int> item )
{
	int res = 0;

	for (int i = 0; i < N; ++i ){
		res += (i+1)*(list[item[i]].first.first) ;
	} // end for

	return res;
}

void dfs (vector <int> & used, int depth )
{
	if (depth >= N ){
		order.push_back(used );
		return;
	} // end if

	for (int i = 0; i < N; ++i ){
		if (used[i] == -1 ){
			used[i] = depth;
			int sum = 0;
			for (int j = 0; j < N; ++j ){
				if (j != i && used[j] == -1 ){	 
					sum += list[j].first.first;
				} // end if
			} // end for
			if (sum <= list[i].first.second ){
				dfs (used, depth + 1 );
			} // end if
			used[i] = -1;
		} // end if
	} // end for
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.lunch", "r", stdin );
	int n;

	while (cin >> n && n ){
		N = n;
		order.clear();
		list.clear();
		list.resize(n);
		for (int i = 0; i < n; ++i){
			cin >> list[i].second;
			cin >> list[i].first.first;	// w
			cin >> list[i].first.second;	// s 
		} // end for

		vector <int> used (n, -1 );

		dfs (used, 0 );

		multimap <int, map <int, int> > rank;
		for (int i = 0; i < order.size(); ++i ){
			map <int, int> packed;
			for (int j = 0; j < order[i].size(); ++j ){
				packed[order[i][j]] = j;
			} // end for
			double G = calcG (packed );
			rank.insert (make_pair (G, packed ) );
		} // end for

		multimap <int, map<int, int> >::iterator it = rank.begin();
		map <int, int> res = (*it).second;
		map <int, int>::iterator jt = res.begin();

		for (; jt != res.end(); ++jt ){
			cout << list[(*jt).second].second << endl;
		} // end for

	} // end while

		
	return 0;
}

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;

			if (g - EPS > ans) {
				continue;
			}

			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<string>
#include<vector>
using namespace std;
int n, w[1000], s[1000];
string S[1000]; vector<int>b; int maxn = 100000000;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S[i] >> w[i] >> s[i];
	}
	vector<int>a(n, 0);
	for (int i = 0; i < n; i++)a[i] = i;
	do {
		for (int i = 0; i < n; i++) {
			int sum = 0;
			for (int j = i + 1; j < n; j++) {
				sum += w[a[j]];
			}
			if (sum > s[i]) goto E;
		}
		int sum2 = 0;
		for (int i = 0; i < n; i++)sum2 += i*w[i];
		if (maxn > sum2)b = a;
	E:;
	} while (next_permutation(a.begin(), a.end()));
	for (int i = 0; i < n; i++)cout << S[a[i]] << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	string str;
	int n, s, w, sum=0;
	while(cin >> n, n) {
		vector<pair<int, pair<int, string> > > v;
		rep(i,n) {
			cin >> str >> w >> s;
			v.push_back(make_pair(w, make_pair(s, str)));
			sum += w;
		}
		sort(v.begin(), v.end());
		vector<string> ret;
		while(!v.empty()) {
			for(int i = v.size()-1; i>=0; --i) {
				if(sum - v[i].first <= v[i].second.first) {
					ret.push_back(v[i].second.second);
					cout << v[i].second.second << endl;
					sum -= v[i].first;
					v.erase(v.begin() + i);
					break;
				}
			}
		}
	}
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

struct food
{
	int w;
	int s;
	string name;

	bool operator < (const food& right) const {
		return w >= right.w;
	}
};

int n;
food fd[10];
string f;
int tw, ts;
bool used[10];

signed main()
{
	while (true)
	{
		cin >> n;
		if (n == 0) break;
 		rep(i,n)
		{
			cin >> f >> tw >> ts;
			fd[i].name = f;
			fd[i].w = tw;
			fd[i].s = ts;
		}

		sort(fd, fd + n);
		int wsum = 0;
		rep(i,n) wsum += fd[i].w;

		rep(i,n) used[i] = false;
		rep(i,n)
		{
			rep(j,n)
			{
				if (!used[j] && fd[j].s >= wsum - fd[j].w)
				{
					//printf("%s\n", fd[j].name);
					cout << fd[j].name << endl;
					wsum -= fd[j].w;
					used[j] = true;
					break;
				}
			}
		}
	}
}

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

int n;
bool use[10];
int w[10],s[10];
vector<int> tmp,ans;
int maximam,total;

void rec(int pos){
  if(pos==n){
    int g;
    g = 0;
    for(int i=0;i<n;i++)g+=(i+1)*w[tmp[i]];
    if(maximam<g){
      maximam = g;
      ans = tmp;
    }
    return;
  }

  for(int i=0;i<n;i++){
    if(!use[i]){
      if(s[i]>=total){
	use[i] = true;
	total += w[i];
	tmp.push_back(i);
	rec(pos+1);
	tmp.pop_back();
	use[i] = false;
	total -= w[i];
      }
    }
  }
}

int main(){
  string f[10];

  while(1){
    cin >> n;
    if(!n)break;

    for(int i=0;i<n;i++){
      cin >> f[i] >> w[i] >> s[i];
      use[i] = false;
    }

    total = 0;
    ans.clear();
    tmp.clear();
    maximam = 0;

    rec(0);

    for(int i=ans.size()-1;i>=0;i--)cout << f[ans[i]] << endl;
  }
}