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<cstdio>
#include<string>
#include<iostream>
#include<cstring>

using namespace std;

int n;
string name[15];
int f[11],w[11],s[11];
int order[11],flag[11],res[11];
int ming,sumw;

void func(int x,int g,int lw){
	if(x==n){
		if(ming>g){
			for(int i=0;i<n;i++)res[i]=order[i];
			ming=g;
		}
	}else{
		for(int i=0;i<n;i++){
			if(!flag[i]){
				flag[i]=1;
				order[x]=i;
				if(lw-w[i]<=s[i])func(x+1,g+(x+1)*w[i],lw-w[i]);
				flag[i]=0;
			}
		}
	}
}

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		memset(order,0,sizeof(int));
		memset(res,0,sizeof(int));
		memset(flag,0,sizeof(int));
		ming=99999999;
		sumw=0;
		for(int i=0;i<n;i++){
			cin >> name[i] >> w[i] >> s[i];
			sumw+=w[i];
		}
		for(int i=0;i<n;i++){
			flag[i]=1;
			order[0]=i;
			if(sumw-w[i]<=s[i])func(1,w[i],sumw-w[i]);
			flag[i]=0;
		}
		for(int i=0;i<n;i++)cout << name[res[i]] << endl;
	}
	return 0;
}

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

using namespace std;

vector<int> w,s,hist;
vector<string> name;
int n,tmpn,tmpm;
int used,ret[10];
string str;
double gmin = 1e+10;

inline double calc_g(){
	double p=0;
	double q=0;
	for(int i=0;i<n;i++){
		p += (n-i) * hist[n-1-i];
		q += hist[i];
	}
	return p/q;
}

void solve(int num, int weight){
	if(num == n){			
		double g = calc_g();
		if(gmin > g){
			gmin = g;
			for(int i=0;i<n;i++) ret[i] = hist[i];
		}
		return;
	}

	for(int i=0;i<n;i++){
		if((used & (1 << i)) == 0){
			if(s[i] >= weight){
				//ツ鳴「ツ使ツ用ツづつゥツづつ重ツづ敖づ可妥渉つヲツづァツづェツつスツづァツ静渉づアツづづ敖づゥ
				used += 1 << i;
				hist.push_back(i);
				solve(num+1, weight+w[i]);
				hist.pop_back();
				used -= 1 << i;
			}
		}
	}
}

int main(void){
	while(cin >> n && n){
		w.clear();
		s.clear();
		hist.clear();
		name.clear();
		used = 0;
		gmin = 1e+10;
		for(int i=0;i<n;i++){
			cin >> str >> tmpn >> tmpm;
			name.push_back(str);
			w.push_back(tmpn);
			s.push_back(tmpm);
		}
		solve(0,0);
		for(int i=0;i<n;i++){
			cout << name[ret[n-i-1]] << endl;
		}
	}
}

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<string> ans;
	while(cin>>n,n){
		data.clear();
		ans.clear();
		string str;
		int w,s;
		double 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;
			double tmp1 =0;
			for(int i =n-1;i >=0;i--){
				tmp1 +=(i+1)*data[i].second.first;
			}
			double Gtmp =(double)(tmp1/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;
				}
			}
			if(ok&&Gtmp <G){
				G =Gtmp;
				ans.clear();
				for(int i =0; i<data.size();i++){
					ans.push_back(data[i].first);
				}
			}
		}while(next_permutation(data.begin(), data.end()));

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

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

struct food {
	string name;
	int w;
	int s;
};
int N;
vector<food>fs;
pair<int,vector<string>> memo[1024];

pair<int,vector<string>> getans(bitset<10>&used,int num,int sum) {
	if (memo[used.to_ulong()].first != -1) {
		return memo[used.to_ulong()];
	}
	else {
		if (used.count() == N)return make_pair(0, vector<string>());
		else {
			pair<int, vector<string>>ans(int(1e8), vector<string>());
			for (int i = 0; i < N; ++i) {
				if (!used[i]&&fs[i].s>=sum) {
					used[i] = true;
					const int nextsum = sum + fs[i].w;
					vector<string>nans;
					auto p = getans(used, num + 1, nextsum);
					p.first += (N-num)*fs[i].w;
					p.second.push_back(fs[i].name);
					if (p.first < ans.first) {
						ans = p;
					}
					used[i] = false;
				}
			}
			return memo[used.to_ulong()] = ans;
		}
	}
}

int main() {
	while (1) {
		for (int i = 0; i < 1024; ++i) {
			memo[i] = make_pair(-1,vector<string>(1," "));
		}
		cin >> N;
		if (!N)break;
		fs.clear();
		for (int i = 0; i < N; ++i) {
			string name; int w, s; cin >> name >> w >> s;
			fs.push_back(food{ name,w,s });
		}
		bitset<10>used;
		auto ans = getans(used, 0, 0);
		for (auto n : ans.second) {
			cout << n << endl;
		}

	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int me[10],soft[10],n;
char str[10][21];
void serch(int num,int now,int sum,int point,int co)
{
    if(num==0)return;
    else if(n==now){
        soft[point]=-10000;
        cout<<str[point]<<endl;;
        serch(num-1,0,sum-me[point],0,0);
    }
    else if(sum-me[now]<=soft[now]&&co==0)serch(num,now+1,sum,now,1);
    else if(sum-me[now]<=soft[now]&&me[point]<me[now])serch(num,now+1,sum,now,1);
    else serch(num,now+1,sum,point,co);
}
int main()
{
    while(cin>>n,n){
        int sum=0;
        for(int i=0;i<n;i++){
            cin>>str[i]>>me[i]>>soft[i];
            sum+=me[i];
        }
        serch(n,0,sum,0,0);
    }

}