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 <algorithm>
#include <climits>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n,sum,ret;
	int info[10][10];
	vector<int> data;
	while(cin >> n && n){
		data.clear();
		rep(i,n)data.push_back(i);
		rep(i,10)rep(j,10)info[i][j] = 0	;
		rep(i,n)rep(j,n){
			cin >> info[i][j];
			info[j][i] = info[i][j] = max(info[i][j],info[j][i]);
		}
		rep(i,n){
			rep(j,n)cout << info[i][j] << " "	;
			cout << endl;
		}
		ret = INT_MAX;
		do{
			sum = 0;
			for(int i=0;i+1<n;i++){
				for(int i=0;i
				sum += info[data[i]][data[i+1]];
			}
			cout << "("<< data[n-1] << ") = " << sum << endl;
			ret = min(ret,sum);
		}while(next_permutation(data.begin(),data.end()));
		cout << ret << 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=0,i=n-1;~i;i--){
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(i+1)*items[i].first.first;
			}
			if(i<0&&s<S)S=s,result=items;
		}while(next_permutation(items.begin(),items.end()));
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>

using namespace std;

const double EPS = 1e-10;
const double d_INF = 100000000.0;
const int I_INF = 1000000000;

int n;
string name[100];
int weight[100];
int cap[100];
vector<int> t_path;
vector<int> path;
double height;

void dfs(int s, int c, int sum, double g){
  if(s == (1<<n)-1){
    if(height > g){
      path = t_path;
      height = g;
    }
    return;
  }
  if(height < g) return;
  int pos = 1;
  int div = 0;
  for(int i=0;i<n;i++){
    if((1<<i) & s){
      ++pos;
      div += weight[i];
    }
  }
  for(int i=0;i<n;i++){
    if((1<<i) & s) continue;
    if(weight[i] > c) continue;
    t_path.push_back(i);
    dfs(s|(1<<i), min(c-weight[i], cap[i]), sum+pos*weight[i], (double)(sum+pos*weight[i])/(div+weight[i]));
    t_path.pop_back();
  }
}

void solve(){
  height = d_INF;
  dfs(0, I_INF, 0, 0.0);
  for(int i=0;i<path.size();i++){
    cout << name[path[i]] << endl;
  }
}

main(){
  while(cin >> n && n){
    for(int i=0;i<n;i++){
      cin >> name[i] >> weight[i] >> cap[i];
    }
    solve();
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

struct food
{
	int f, w, s;
};
struct Sorter
{
	bool operator()(const food& a, const food& b)
	{
		return a.f < b.f;
	}
};
int main()
{
	char name[10][32];
	int n;
	while (scanf("%d", &n), n)
	{
		food foods[10];
		for (int i = 0; i < n; ++i)
		{
			int w, s;
			scanf("%s %d %d", name[i], &w, &s);
			foods[i].f = i;
			foods[i].w = w;
			foods[i].s = s;
		}

		const double Inf = 1e12;
		double minG = Inf;
		int order[10];
		do
		{
			int sumW = 0;
			double g = 0;
			bool ok = true;
			for (int i = 0; i < n; ++i)
			{
				if (sumW > foods[i].s)
				{
					ok = false;
					break;
				}
				sumW += foods[i].w;
				g += (n-i) * foods[i].w;
			}
			g /= sumW;
			if (ok && g < minG)
			{
				minG = g;
				for (int i = 0; i < n; ++i)
					order[i] = foods[i].f;
			}
		} while (next_permutation(foods, foods+n, Sorter()));
		for (int i = n-1; i >= 0; --i)
			printf("%s\n", name[order[i]]);
	}

	return 0;
}