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++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

struct FOOD{
	string name;
	int w;
	int s;
};

bool checkWeight(const struct FOOD* food,const int* table,int n){
	for(int i=0;i<n;i++){
		int sum = 0;
		for(int j=i+1;j<n;j++){
			sum += food[table[j]].w;
		}
		if(food[table[i]].s < sum) return false;
	}

	return true;
}

double getG(const struct FOOD* food,const int* table,int n){

	double G=0;
	double sum=0;
	for(int i=0;i<n;i++){
		G += (double)food[table[i]].w * (i+1);
		sum += (double)food[table[i]].w;
	}
	return G / sum;
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		struct FOOD* food = new struct FOOD[n];

		for(int i=0;i<n;i++){
			char buf[64];
			int w,s;
			scanf("%s %d %d",buf,&w,&s);
			food[i].name = buf;
			food[i].s = s;
			food[i].w = w;
		}

		int* table = new int[n];
		for(int i=0;i<n;i++) table[i] = i;

		int* res = new int[n]();
		double minG = 1000000.0;

		do{
			double tmpG;
			if(checkWeight(food,table,n) &&
				minG > (tmpG = getG(food,table,n))){
					copy(table,table+n,res);
					minG = tmpG;
			}
		}while(next_permutation(table,table+n));

		for(int i=0;i<n;i++){
			printf("%s\n",food[res[i]].name.c_str());
		}

		delete[] food;
		delete[] table;
	}
}

C++: #include<iostream>
#include<string>
#include<list>
#include<map>
using namespace std;

const int MAX_F = 15;

class tabemono{
private:
	int No;
	int W;
	int S;
public:
	int retS();
	int retW();
	int retNo();
	void setNo(int no);
	void setW(int w);
	void setS(int s);
};

int tabemono::retS(){
	return S;
}

int tabemono::retW(){
	return W;
}

int tabemono::retNo(){
	return No;
}

void tabemono::setNo(int no){
	No = no;
}

void tabemono::setW(int w){
	W = w;
}

void tabemono::setS(int s){
	S = s;
}

int main(){
	int n;
	while(cin >>n){
		if(n == 0) break;
		
		list<tabemono> hukuro;
		multimap<int, int, greater<int> > tmpmap;
		string food[MAX_F];
		int weight[MAX_F], strenght[MAX_F], sum = 0;
		for(int i = 0; i < n; i++){
			cin >>food[i] >>weight[i] >>strenght[i];
			sum += weight[i];
			tmpmap.insert( map<int, int, greater<int> >::value_type(strenght[i], i) );
		}
		
		map<int, int, greater<int> >::iterator itm;
		for(itm = tmpmap.begin(); itm != tmpmap.end(); itm++){
			tabemono t;
			t.setNo((*itm).second);
			t.setW(weight[(*itm).second]);
			t.setS(strenght[(*itm).second]);
			hukuro.push_back(t);
		}
		
		while(hukuro.empty() != 1){
			int max = -1;
			list<tabemono>::iterator maxit = hukuro.begin();
			list<tabemono>::iterator itl;
			if(hukuro.size() != 1){
				for(itl = hukuro.begin(); itl != hukuro.end(); itl++){
					int tmp = (*itl).retW();
 					if((*itl).retS() < (sum - tmp)){
						itl--;
						break;
					}
					if(tmp > max){
						max = tmp;
						maxit = itl;
					}
				}
			}
			cout <<food[(*maxit).retNo()] <<endl;
			sum -= (*maxit).retW();
			hukuro.erase(maxit);
		}
		
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;

int n;
string f[10];
int w[10], s[10];
bool used[10];

void dfs(int sum, int G, int &minG, vector<string> &v, vector<string> &ans) {
	if ((int)v.size() == n) {
		if (G < minG) {
			minG = G;
			ans = v;
		}
		return ;
	}
	for (int i=0; i<n; ++i) {
		if ( ! used[i] && sum - w[i] <= s[i]) {
			used[i] = true;
			v.push_back(f[i]);
			dfs(sum-w[i], G+v.size()*w[i], minG, v, ans);
			used[i] = false;
			v.pop_back();
		}
	}
	return ;
}

int main() {
	while (cin >> n, n) {
		int sum = 0;
		for (int i=0; i<n; ++i) {
			cin >> f[i] >> w[i] >> s[i];
			sum += w[i];
		}
		memset(used, false, sizeof used);
		int minG = INT_MAX;
		vector<string> v, ans;
		dfs(sum, 0, minG, v, ans);
		for (int i=0; i<(int)ans.size(); ++i) {
			cout << ans[i] << endl;
		}
	}
	return 0;
}

C++: #include <cstring>
#include <cfloat>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAX_N = 10;
int n;
string f[MAX_N];
int w[MAX_N];
int s[MAX_N];

double ming;
vector<int> minindeies;

vector<int> selected;
bool used[MAX_N];
void permutation(int r, int limit) {
	if (r == 0) {
		double a = 0., b = 0.;
		for (int i = 0; i < n; i++) {
			a += w[selected[i]] * (i + 1);
			b += w[selected[i]];
		}
		double g = a / b;
		if (g < ming) {
			ming = g;
			minindeies = selected;
		}
	}
	else {
		for (int i = 0; i < n; i++) {
			if (used[i]) continue;
			if (w[i] > limit) continue;
			used[i] = true;
			selected.push_back(i);
			permutation(r - 1, min(limit - w[i], s[i]));
			selected.pop_back();
			used[i] = false;
		}
	}
}

int main () {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> f[i] >> w[i] >> s[i];
		}
		memset(used, 0, sizeof(used));
		ming = DBL_MAX;
		permutation(n, 0x7ffffff);
		for (int i = 0; i < n; i++) {
			cout << f[minindeies[i]] << endl;
		}
	}
	return 0;
}