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

// 入力
string f[10];
int n, w[10], s[10]; 
// used[i] := i番目を選んだかどうか
bool used[10];
// 今まで見つかった中で一番重心が低いもの
double ans_g;
// 解
vector<int> ans;

// 重さに耐えられるかチェック
bool check(vector<int>& v){
	for(int i=0 ; i < v.size() ; i++ ){
		int weight = 0;
		for(int j=i+1 ; j < v.size() ; j++ ){
			weight += w[v[j]];
		}
		if( s[v[i]] < weight )
			return false;
	}
	return true;
}

// 重心を計算
double gravity(vector<int>& v){
	double a = 0, b = 0;
	for(int i=0 ; i < v.size() ; i++ ){
		a += (i+1) * w[v[i]];
		b += w[v[i]];
	}
	return a / b;
}

// 順列生成して解を探す (最大で 10! 通り)
void solve(){
	vector<int> v(n);
	for(int i=0 ; i < n ; i++ ) v[i] = i;
	// 順列生成してチェック
	do{
		double g = gravity(v);
		// 重心が低い かつ 重さに耐えられるとき
		if( check(v) && g < ans_g ){
			ans_g = g;
			ans = v;
		}
	}while( next_permutation(v.begin(),v.end()) );
}

// 探索
void dfs(vector<int>& v, int num){
	// 重さに耐えられるかチェック, 耐えられないときは探索打ち切り
	if( !check(v) ) return;
	
	if( num == n ){
		double g = gravity(v);
		if( g < ans_g ){ // 重心が低いとき
			ans_g = g;
			ans = v;
		}
		return;
	}
	
	for(int i=0 ; i < n ; i++ ){
		if( used[i] ) continue;
		
		used[i] = true;
		v.push_back( i );
		dfs( v , num+1 );
		v.pop_back();
		used[i] = false;
	}
}

int main(){
	while( cin >> n , n ){
		// 初期化
		for(int i=0 ; i < 10 ; i++ ) used[i] = false;
		
		for(int i=0 ; i < n ; i++ ){
			cin >> f[i] >> w[i] >> s[i];
		}
		
		ans_g = 1e+9;
		vector<int> v;
		//dfs( v , 0 );
		solve();
		
		for(int i=0 ; i < ans.size() ; i++ ){
			cout << f[ans[i]] << endl;
		}
	}
}

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

const int MAX_N = 10;

struct Data {
	std::string name;
	int weight, max_weight;
	Data() = default;
	Data(int _w, int _m, const std::string& _n) : weight(_w), max_weight(_m), name(_n) {}

	bool operator<(const Data& rhs) const {
		return (weight == rhs.weight) ? 
			(max_weight==rhs.max_weight)? 
			name < rhs.name : max_weight < rhs.max_weight : weight < rhs.weight;
	}
	bool operator>(const Data& rhs) const{
		return (weight == rhs.weight) ?
			(max_weight == rhs.weight) ?
			name > rhs.name : max_weight > rhs.max_weight : weight > rhs.weight;
	}
};


int n, sum;
std::vector<Data> data;
std::string ans[MAX_N];

int main() {
	while (1) {
		std::cin >> n;
		if (n == 0)
			break;
		sum = 0;
		int _w, _m;
		std::string _n;
		for (int i = 0; i < n; ++i) {
			std::cin >> _n >> _w >> _m;
			data.push_back(Data(_w, _m, _n));
			sum += data[i].weight;
		}

		std::sort(data.begin(), data.end(), std::greater<Data>());

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < data.size(); ++j) {
				if (data[j].max_weight >= sum - data[j].weight) {
					sum -= data[j].weight;
					ans[i] = data[j].name;
					data.erase(data.begin() + j);
					break;
				}
			}
		}
		for (int i = 0; i < n; ++i)
			std::cout << ans[i] << std::endl;
	}
	system("pause");
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

struct Food{
  vector<int> num;
  double g;
  bool operator<(const Food& f) const{
    return g < f.g;
  }
};

int main(void){
  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;
	loop(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;
  }

}

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=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&&s<S)S=s,result=items;
		}while(next_permutation(items.begin(),items.end()));
		reverse(result.begin(),result.end());
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}