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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

struct Food{
    std::string name;
    ull w, s;
};

const int MAX_N = 10;
int N;
std::vector<Food> F;
int order[MAX_N], fact[11];

int main(){
    fact[0] = 1;
    FOR(i, 1, 11){
        fact[i] = fact[i-1] * i;
    }

    std::cin >> N;
    
    REP(i, N){
        std::string name;
        ull w, s;
        std::cin >> name >> w >> s;
        
        F.push_back({name, w, s});
    }

    REP(i, N){
        order[i] = i;
    }
    
    ull min_cost = INF;
    int min_order[MAX_N];
    REP(_, fact[N]){
        // for(int i=0;i<N;i++){
        //     printf("%d, ", order[i]);
        // }
        // puts("");

        bool f = true;
        ull cost = 0, sum = 0;
        REP(i, N){
            int index = order[i];
            if(sum <= F[index].s);
            else{f = false;}

            cost += (N-i) * F[index].w;
            sum += F[index].w;
        }
        //std::cout << cost << std::endl;

        if(f && min_cost > cost){
            min_cost = cost;
            std::copy(order, order+N, min_order);
        }

        std::next_permutation(order, order+N);
    }

    //std::cout << min_cost << std::endl;
    for(int i=N-1;i>=0;i--){
        int index = min_order[i];
        std::cout << F[index].name << std::endl;
    }
}

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

using namespace std;

struct Entry {
	Entry(){ }
	Entry(const Entry &obj) : name(obj.name), w(obj.w), s(obj.s) { }
	Entry &operator=(const Entry &obj){ name = obj.name; w = obj.w; s = obj.s; return *this; }
	string name;
	int w, s;
};

struct WeightComparator {
	bool operator()(const Entry &a, const Entry &b){ return a.w < b.w; }
};

int main(){
	while(true){
		int n;
		cin >> n;
		if(n == 0){ break; }
		vector<Entry> entries;
		int wsum = 0;
		while(n--){
			Entry e;
			cin >> e.name >> e.w >> e.s;
			wsum += e.w;
			entries.push_back(e);
		}
		priority_queue<Entry, vector<Entry>, WeightComparator> q;
		while(!entries.empty() || !q.empty()){
			vector<Entry>::iterator it = entries.begin();
			while(it != entries.end()){
				if((*it).s >= wsum - (*it).w){
					q.push(*it);
					it = entries.erase(it);
				}else{
					++it;
				}
			}
			wsum -= q.top().w;
			cout << q.top().name << endl;
			q.pop();
		}
	}
	return 0;
}

C++: //30
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    string f[10];
    int w[10],s[10];
    for(int i=0;i<n;i++){
      cin>>f[i]>>w[i]>>s[i];
    }
    int x[]={0,1,2,3,4,5,6,7,8,9};
    int ans[10];
    double g=1<<30;
    do{
      bool f=true;
      for(int i=0;i<n;i++){
	int ws=0;
	for(int j=i+1;j<n;j++){
	  ws+=w[x[j]];
	}
	f&=s[x[i]]>=ws;
      }
      if(f){
	double ww=0;
	double ws=0;
	for(int i=0;i<n;i++){
	  ww+=(i+1)*w[x[i]];
	  ws+=w[i];
	}
	if(ww/ws<g){
	  g=ww/ws;
	  copy(x,x+n,ans);
	}
      }
    }while(next_permutation(x,x+n));
    for(int i=0;i<n;i++){
      cout<<f[ans[i]]<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int n;
bool co[10]={false};
string in[10]={""};
int in1[10]={0};
int in2[10]={0};
int out[10]={0};
double mini=-1.0;
void solve(int a,vector<int>tmp){
	//cout<<a<<endl;
	//if(a==3)rep(i,n)cout<<" "<<co[i];
	if(a==n){
		double sum=0;
		double sum1=0;
		rep(i,n){
			sum+=(i+1)*in1[tmp[i]];
			sum1+=in1[tmp[i]];
		}
		double g=sum/sum1;
		if(mini<0||mini>g){
			mini=g;
			rep(i,n)out[i]=tmp[i];
		}
	}else{
	rep(i,n){
		if(co[i])continue;
		int sum=in1[i];
		bool h=true;
		for(int j=tmp.size()-1;j>=0;j--){
			if(in2[tmp[j]]<sum){h=false;break;}
			sum+=in1[tmp[j]];
		}
		if(h){
			tmp.pb(i);
			co[i]=true;
			solve(a+1,tmp);
			tmp.erase(tmp.begin()+tmp.size()-1);
			co[i]=false;
		}
	}}
	//cout<<a<<endl;
}
int main(){
	while(cin>>n,n){
		mini=-1.0;
		rep(i,n)co[i]=false;
		rep(i,n)cin>>in[i]>>in1[i]>>in2[i];
		vector<int>tmp;
		solve(0,tmp);
		rep(i,n)cout<<in[out[i]]<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int n,a,tmp;
int s[10],w[10];
string f[10],temp;
int t[10];
vector<int>ans;

int get_weight(int);
void swap_data(int);
void sort_data();

int main(){
  while(cin>>n&&n!=0){
    ans.clear();
    for(int i=0;i<n;i++){
      cin>>f[i];
      cin>>w[i]>>s[i];
    }

    
    sort_data();

    //for(int i=0;i<n;i++)cout<<f[i]<<' '<<w[i]<<' '<<s[i]<<endl;
    //cout<<'='<<endl;
    for(int k=0;k<n;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n-1;j++){
	  if(w[j]<w[j+1]){
	    swap_data(j);
	    
	    a=get_weight(j+1);
	    if(a>s[j])swap_data(j);
	    

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

int get_weight(int x){
  int res=0;
  for(int i=x;i<n;i++)res+=w[i];
  return res;
}

void swap_data(int x){
  
  tmp=w[x];
  w[x]=w[x+1];
  w[x+1]=tmp;

  tmp=s[x];
  s[x]=s[x+1];
  s[x+1]=tmp;

  temp=f[x];
  f[x]=f[x+1];
  f[x+1]=temp;
}

void sort_data(){
  for(int i=n-1;i>0;i--){
    for(int j=0;j<i;j++){
      if(s[j]<s[j+1])swap_data(j);
      else if(s[j]==s[j+1]&&w[j]<w[j+1])swap_data(j);
    }
  }
}

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