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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	int n;
	vector<pair<string,pii> > data;
	vector<pair<string,pii> > ans;
	while(cin>>n,n){
		data.clear();
		ans.clear();
		string str;
		int w,s;
		int tmp2 =0;
		for(int i =0;i <n;i++){
			cin>>str;
			scanf("%d %d",&w,&s);
			data.push_back(mp(str,pii(w,s)));
			tmp2 +=data[i].second.first;
		}
		sort(data.begin(),data.end());
		double G =INF;
		do{
			//cout <<"iei"<<endl;
			int sumw =0;
			int tmp1 =0;
			for(int i =n-1;i >=0;i--){
				tmp1 +=(i+1)*data[i].second.first;
			}
			double Gtmp =(double)((double)tmp1/(double)tmp2);
			if(Gtmp>G)continue;
			bool ok =true;
			for(int i =n-1;i >=0;i--){
				if(sumw>data[i].second.second){
					ok =false;
					break;
				}else{
					sumw +=data[i].second.first;
				}
			}
			//cout <<Gtmp<<endl;
			if(ok&&Gtmp <G){
				G =Gtmp;
				ans=data;
				//cout <<"ge"<<endl;
			}
		}while(next_permutation(data.begin(), data.end()));

		for(int i =0;i <n;i++){
			cout <<ans[i].first<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{
	int N;
	while(cin>>N,N)
	{
		vector<pair<string,pair<int,int> > >A;
		for(int i=0;i<N;i++)
		{
			string f;int w,s;cin>>f>>w>>s;
			A.push_back(make_pair(f,make_pair(w,s)));
		}
		sort(A.begin(),A.end());
		vector<string>ans;
		double mG=1e150;
		do{
			bool flag=true;
			int T=0;
			for(int i=0;i<N;i++)
			{
				if(T>A[i].second.second)
				{
					flag=false;
					break;
				}
				T+=A[i].second.first;
			}
			if(flag)
			{
				double G=0;
				for(int i=0;i<N;i++)G+=A[i].second.first*(N-i);
				G/=T;
				if(mG>G)
				{
					mG=G;
					ans.clear();
					for(int i=N;i--;)ans.push_back(A[i].first);
				}
			}
		}while(next_permutation(A.begin(),A.end()));
		for(int i=0;i<N;i++)cout<<ans[i]<<endl;
	}
}

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=0;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 <algorithm>
#include <vector>
#include <numeric>
using namespace std;

string F[10];
int W[10], S[10];

int main() {
  ios::sync_with_stdio(false);
  while(true) {
    int N; cin >> N;
    if(N == 0) break;
    for(int i = 0; i < N; ++i) {
      cin >> F[i] >> W[i] >> S[i];
    }
    vector<int> ord(N);
    for(int i = 0; i < N; ++i) ord[i] = i;
    double ansG = 1e9;
    vector<int> ansOrd;
    do {
      bool ok = true;
      for(int i = 0; i < N; ++i) {
        int w = 0;
        for(int j = i + 1; j < N; ++j) w += W[ord[j]];
        if(w > S[ord[i]]) ok = false;
      }
      if(ok) {
        int g = 0;
        for(int i = 0; i < N; ++i) {
          g += (i + 1) * W[ord[i]];
        }
        double G = 1.0 * g / accumulate(W, W + N, 0);
        if(ansG > G) {
          ansG = G;
          ansOrd = ord;
        }
      }
    } while(next_permutation(ord.begin(), ord.end()));
    for(int item : ansOrd) {
      cout << F[item] << 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;
		vector<int> num,result;
		for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f)),num.push_back(i);
		S=99999;
		do{
			for(s=w=0,i=n-1;~i;i--){
				pair<int,int> weight=items[num[i]].first;
				if(weight.second<w)break;
				w+=weight.first;
				s+=(i+1)*weight.first;
			}
			if(i<0&&s<S)S=s,result=num;
		}while(next_permutation(num.begin(),num.end()));
		for(i=0;i<n;i++)cout<<items[result[i]].second<<endl;
	}
}