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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double real;
typedef pair<int, string> duo;

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T In(){ T x; cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static inline ll pow_mod(ll a, ll k, ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
static inline ll mod_inv(ll a, ll p){ return pow_mod(a,p-2,p); }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
static const int Mod = 1000000000;

static inline int popcnt(int x){ return __builtin_popcount(x); }

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

struct Solver {
  vector<Food> fs;
  vector<Food> best;
  vector<Food> cur;
  bool used[16];
  int best_cg;
  int total_w;
  const int N;

  Solver(int n) : N(n), fs(n) {}
  void init(){
    for (int i = 0; i < N; i++) fs[i].read();
    sort(fs);
    memset(used, 0, sizeof(used));
    best_cg = 1 << 25;
    total_w = 0;
    for (int i = 0; i < N; i++){
      total_w += fs[i].w;
    }
  }
  void solve(int cg=0, int tw=0, int t=1){
    if (t == N + 1){
      if (best_cg > cg){
        best = cur;
        best_cg = cg;
      }
      return;
    }
    if (cg > best_cg) return;
    for (int i = 0; i < N; i++){
      if (used[i]) continue;
      if (fs[i].s < total_w - (tw + fs[i].w)) continue;
      used[i] = true;
      cur.push_back(fs[i]);
      solve(cg + t * fs[i].w, tw + fs[i].w, t + 1);
      cur.pop_back();
      used[i] = false;
    }
  }
  void show(){
    for (int i = 0; i < N; i++) cout << best[i].f << "\n";
  }
};

int main()
{
  int n;
  while (n = in()){
    Solver solver(n);
    solver.init();
    solver.solve();
    solver.show();
  }
  return (0);
}

// priority_queue queue deque first second front top push pop map set stringstream max_element min_element insert count find

C++: #include<stdio.h>
#include<string.h>
#include<float.h>
#include<algorithm>
struct S{char f[32];int w,s;}F[10];
int main()
{
	int n,w,s,i,a[10],r[10];
	double g,b;
	while(scanf("%d",&n),n)
	{
		b=DBL_MAX;
		for(i=0;i<n;++i)scanf("%s%d%d",F[i].f,&F[i].w,&F[i].s),a[i]=i;
		do
		{
			g=w=0;
			for(i=n-1;i>=0;--i)
			{
				if(F[a[i]].s<w)goto N;
				w+=F[a[i]].w;
				g+=(i+1)*F[a[i]].w;
			}
			if(g<b)b=g,memcpy(r,a,sizeof(a));
N:;
		}while(std::next_permutation(a,a+n));
		for(i=0;i<n;++i)puts(F[r[i]].f);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int n;
    while(cin >> n, n) {
        vector<tuple<string, int, int>> v(n);
        for(int i=0; i<n; ++i) {
            string name;
            int w, s;
            cin >> name >> w >> s;
            v[i] = make_tuple(name, w, s);
        }
        vector<int> id(n);
        for(int i=0; i<n; ++i) {
            id[i] = i;
        }
        vector<string> res(n);
        double g = 1e9;
        do {
            bool ok = true;
            int sum = 0;
            for(int i=n-1; i>=0; --i) {
                ok &= sum <= get<2>(v[id[i]]);
                sum += get<1>(v[id[i]]);
            }
            if(ok) {
                double g2 = 0;
                for(int i=0; i<n; ++i) {
                    g2 += get<1>(v[id[i]]) * (i+1);
                }
                if(g2 < g) {
                    g = g2;
                    for(int i=0; i<n; ++i) {
                        res[i] = get<0>(v[id[i]]);
                    }
                }
            }
        } while(next_permutation(id.begin(), id.end()));
        
        for(auto& x : res) {
            cout << x << endl;
        }
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iterator>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

struct Food
{
	vector<int> num;
	double g;

	bool operator < (const Food& f) const
	{
		return g < f.g;
	}
};

int main()
{
	vector<int> num;
	vector<pair<int,int> > a;
	vector<string> name;
	int n;
	while(cin >> n && n)
	{
		num.clear();
		a.clear();
		name.clear();
		rep(i,n)
		{
			string f;
			int w,s;
			cin >> f >> w >> s;

			a.push_back(make_pair(w,s));
			name.push_back(f);
			num.push_back(i);
		}

		vector<Food> food;

		do
		{
			bool flag = true;

			rep(i,num.size()-1)
			{
				int count = 0;
				REP(j,i+1,num.size())
				{
					count += a[num[j]].first;
				}

				if(a[num[i]].second < count)
				{
					flag = false;
				}
			}

			if(flag)
			{
				Food f;
				copy(num.begin(),num.end(),back_inserter(f.num));
				
				double t_a = 0,t_b = 0;
				int j = 1;
				rep(i,num.size())
				{
					t_a += j*a[num[i]].first;
					t_b += a[num[i]].first;
					j++;
				}
				
				f.g = t_a / t_b;
				food.push_back(f);
			}
		}while(next_permutation(num.begin(),num.end()));
		
		sort(food.begin(),food.end());

		rep(i,food[0].num.size())
		{
			cout << name[food[0].num[i]] << endl;
		}

	}
	return 0;
}