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<algorithm>
#include<string>
#include<vector>
using namespace std;
int n, w[1000], s[1000];
string S[1000]; vector<int>b; int maxn;
int main() {
	while (true) {
		cin >> n; b.clear(); maxn = 1 << 30;
		if (n == 0) { break; }
		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 {
			int sum2 = 0;
			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[a[i]]) goto E;
			}
			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[b[i]] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<climits>
#include<cmath>
#include<stdio.h>
using namespace std;

struct food{
    string name;
    int weight;
    int ue;
    int id;
};
struct com
{
    inline bool operator() (const food& f1, const food& f2){
        return (f1.id > f2.id);
    }
};
int main(){
    int n;
    while(cin>>n,n){
        vector<food>vec,v;
        int totWeight=0;
        for(int i=0;i<n;i++){
            food f;
            cin>>f.name>>f.weight>>f.ue;
            f.id=i;
            totWeight += f.weight;
            vec.push_back(f);
            v.push_back(f);
        }
        sort(vec.begin(),vec.end(),com());
        vector<pair<double,vector<int> > >ve;
        do{
            bool ok=true;
            int tot=0;
            for(int i=n-1;i>=0;i--){
                if(tot>vec[i].ue){
                    ok=false;
                    break;
                }
                tot+=vec[i].weight;
            }
            if(ok){
                double x=0;
                for(int i=1;i<=n;i++){
                    x+=(n-i)*vec[i].weight;
                }
                x/=totWeight;
                vector<int>ids;
                for(int i=0;i<n;i++){
                    ids.push_back(vec[i].id);
                }
                ve.push_back(make_pair(x, ids));
            }
        } while(next_permutation(vec.begin(), vec.end(), com()));
        sort(ve.begin(),ve.end());
        for(int i=0;i<n;i++){
            cout<<v[ve[ve.size()-1].second[i]].name<<endl;
        }
    }
    return 0;
}

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

using namespace std;

class Data{
public:
	int w,s;
	string name;
};

int main(){

	int n;
	while(cin>>n&&n!=0){
		Data d[10];
		for(int i = 0; i < n; i++){
			cin>>d[i].name>>d[i].w>>d[i].s;
		}
		vector<int> v;
		for(int i = 0; i < n; i++){
			v.push_back(i);
		}
		double minW=100000000;
		vector<int> p;
		// ツ全ツ津環づィツづ個渉氾板づ計ツ算ツつキツづゥ
		do{
			int sumW=0;
			bool f=true;
			int Bunshi=0;
			for(int i = n-1; i >= 0; i--){
				Bunshi+=(i+1)*d[v[i]].w;
				if(sumW>d[v[i]].s){
					f=false;
					break;
				}
				sumW+=d[v[i]].w;
			}
			// ツづつづ板づェツづ按つ「ツ湘ェツ債、ツ全ツ妥個づ個重ツ心ツづーツ更ツ新ツつキツづゥ
			if(f){
				double G=(double)Bunshi/sumW;
				if(G<minW){
					minW=min(minW,G);
					p=v;
				}
			}
		}while(next_permutation(v.begin(),v.end()));

		for(int i = 0; i < p.size(); i++){
			cout<<d[p[i]].name<<endl;
		}
	//	cout<<endl;
	}

	return 0;
}

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

int main(){
    int n;
    while(cin >> n, n){
        vector<string> f(n);
        vector<int> w(n), s(n), a(n);
        double wsum = 0;
        for(int i = 0; i < n; i++){
            cin >> f[i] >> w[i] >> s[i];
            a[i] = i;
            wsum += w[i];
        }
        vector<int> ans(n);
        double g = DBL_MAX;
        do{
            double accw = 0, tmpg = 0;
            bool valid = true;
            for(int i = 0; i < n; i++){
                if(s[a[i]] < accw){
                    valid = false;
                    break;
                }
                accw += w[a[i]];
                tmpg += (n-i)*w[a[i]];
            }
            if(valid && tmpg/wsum < g){
                g = tmpg/wsum;
                ans = a;
            }
        }while(next_permutation(a.begin(), a.end()));
        for(int i = n-1; i >= 0; i--){
            cout << f[ans[i]] << endl;
        }
    }
    return 0;
}

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

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

void rec(int pos){
  if(pos==n){
    int weight,g;
    bool f;
    weight = 0;
    f = true;
    for(int i=0;i<tmp.size();i++){
      if(s[tmp[i]] < weight){
	f = false;
	break;
      }
      weight += w[tmp[i]];
    }
    if(!f)return;
    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]){
      use[i] = true;
      tmp.push_back(i);
      rec(pos+1);
      tmp.pop_back();
      use[i] = false;
    }
  }
}

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

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

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