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

			int total = 0;
			bool flg = true;
			for (int i = N - 1; i >= 0; i--) {
				flg &= total <= S[p[i]];
				total += W[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 <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<pair<int,int>,string> item;
main(){
	int n,i,j,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=i=0;i<n;i++){
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(n-i)*items[i].first.first;
			}
			if(i==n){
				if(s<S)S=s,result=items;
			}else{
				for(s=w=1;i<n;w++,i++)s*=w;
				for(i=0;i<s-1;i++)next_permutation(items.begin(),items.end());
			}
		}while(next_permutation(items.begin(),items.end()));
		reverse(result.begin(),result.end());
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>

using namespace std;

int main(){
	
	while(true){
		
		int n;
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		string name[10];
		int index[10], index_ans[10];
		double w[10], s[10];
		
		for(int i = 0; i < n; i++){
			cin >> name[i] >> w[i] >> s[i];
			index[i] = i;
		}
		
		double G_min = 100000;
		
		do{
			double weight_sum = 0;
			for(int i = 0; i < n; i++){
				if(weight_sum > s[index[i]]){
					break;
				}
				weight_sum += w[index[i]];
				if(i == n - 1){
					int G_cur = 0;
					for(int j = 0; j < n; j++){
						G_cur += w[index[j]] * (n - j);
					}
					if(G_cur < G_min){
						G_min = G_cur;
						for(int j = 0; j < n; j++){
							index_ans[j] = index[j];
						}
					}
				}
			}
		}while(next_permutation(index, index + n));
		
		for(int i = 0; i < n; i++){
			cout << name[index_ans[n - i - 1]] << endl;
		}
		
	}
	
	return 0;
}

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

struct lunch{
    string name;
    int omosa,genkai;
    };
lunch a[10];
signed main(){
    int b;
    while(cin>>b,b){
	for(int c=0;c<b;c++){
	    string d;int e,f;cin>>d>>e>>f;
	    a[c]={d,e,f};
	    }
	    vector<int>c;
	    for(int i=0;i<b;i++){c.push_back(i);}
	    int MIN=1<<29;
	    do{
		int sum=0;
		bool S=true;
		for(int i=b-1;i>=0;i--){
		    if(sum>a[c[i]].genkai)S=false;
		    sum+=a[c[i]].omosa;
		    }
		 if(S){
		     int zyuusin=0;
		     for(int i=0;i<b;i++){
			 zyuusin+=(i+1)*a[c[i]].omosa;
			 }
			 MIN=min(MIN,zyuusin);
		     }   
		}while(next_permutation(c.begin(),c.end()));
		do{
		int sum=0;
		bool S=true;
		for(int i=b-1;i>=0;i--){
		    if(sum>a[c[i]].genkai)S=false;
		    sum+=a[c[i]].omosa;
		    }
		 if(S){
		     int zyuusin=0;
		     for(int i=0;i<b;i++){
			 zyuusin+=(i+1)*a[c[i]].omosa;
			 }
			 if(zyuusin==MIN){
			     for(int i=0;i<b;i++){
				 cout<<a[c[i]].name<<endl;
				 }
			     }
		     }   
		}while(next_permutation(c.begin(),c.end()));
	}
    }