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 <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    vector<string> f(n);
    vector<int> w(n), s(n), ord(n);
    for (int i = 0; i < n; i++) {
      cin >> f[i] >> w[i] >> s[i];
      ord[i] = i;
    }
    double G = 1e9;
    vector<int> ans(n);
    do {
      int sum = 0, no = 0;
      for (int i = n - 1; i >= 0; i--) {
        if (s[ord[i]] < sum) no++;
        sum += w[ord[i]];
      }
      if (no) continue;
      int tmp = 0;
      for (int i = 0; i < n; i++) {
        tmp += (i + 1) * w[ord[i]];
      }
      double g = 1. * tmp / sum;
      if (G - g > 1e-10) {
        G = g;
        ans = ord;
      }
    } while (next_permutation(ord.begin(), ord.end()));
    for (int i = 0; i < n; i++) {
      cout << f[ans[i]] << endl;
    }
  }

  return 0;
}

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 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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int w[],int k[],int b[],int a[],int c,int n,int e,int g){
  int i,j;
  int bb[10];
  if(c==n){
    if(a[10]==0||a[10]>g){
      a[10]=g;
      memcpy(a,b,10*sizeof(int));
    }
  }else{
    for(i=0;i<n;i++){
      if(k[i]>=e){
	for(j=0;j<c;j++){
	  if(i==b[j])
	    break;
	}
	if(j==c){
	  memcpy(bb,b,sizeof(bb));
	  bb[c]=i;
	  f(w,k,bb,a,c+1,n,e+w[i],g+w[i]*(n-c));
	}
      }else
	break;
    }
  }
  return;
}
int main(){
  int h,i;
  int w[10],k[10],b[10],a[11],n;
  char s[10][64];
  for(;;){
    cin>>n;
    if(n==0)
      break;
    for(i=0;i<n;i++)
      cin>>s[i]>>w[i]>>k[i];
    do{
      h=0;
      for(i=1;i<n;i++){
	if(k[i-1]<k[i]){
	  swap(k[i-1],k[i]);
	  swap(w[i-1],w[i]);
	  swap(s[i-1],s[i]);
	  h=1;
	}
      }
    }while(h);
    memset(b,0,sizeof(b));
    memset(a,0,sizeof(a));
    f(w,k,b,a,0,n,0,0);
    for(i=0;i<n;i++)
      cout<<s[a[n-i-1]]<<endl;
  }
  return 0;
}

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

using namespace std;

struct itm{
	string str;
	int w;
	int s;
	bool f;
};

int main()
{
	int n, w, p;
	itm it[11];
	string st[11];
	bool f;
	
	while(cin >> n && n){
		w = p = 0;
		for(int i = 0; i < n; i++){
			cin >> it[i].str >> it[i].w >> it[i].s;
			w += it[i].w;
			it[i].f = true;
		}
		
		for(int i = 0; i < n; i++){
			f = false;
			for(int j = 0; j < n; j++){
				if(it[j].f && it[j].s >= (w-it[j].w)){
					f = true;
					p = j;
					for(int k = j+1; k < n; k++){
						if(it[k].w > it[p].w && it[k].f && it[k].s >= (w-it[k].w)){
							p = k;
						}
					}
				}
				if(f){
					break;
				}
			}
			if(f){
				st[i] = it[p].str;
				w -= it[p].w;
				it[p].f = false;
				cout << it[p].str << endl;
			}
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require sqrt ceil floor
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <stdint.h>	// require uint64_t
#include <fstream>	// require freopen

using namespace std;

typedef pair <pair<int, int>, string > ITEM;	// ACeÌd³AACeÌãÉu¯éd³ÌãÀAACe¼

int memo[10][10];		// s: ACe¼ ñ:½iÚ© -1: Ü¾ãÉÏßé©Ç¤©¢èA0:ÏßÈ¢ 1: Ïßé
vector <ITEM> list;
int N;

int dfs (vector <int> order, int curr )
{
	if (curr == N ){
		return 1;
	} // end if

	if (memo[order[curr]][curr] == 0 ){
		return 0;
	} // end if
		
	int total_wight = 0;

	for (int i = curr + 1; i < N; ++i ){
		total_wight += list[order[i]].first.first;
	} // end if

	if (total_wight <= list[order[curr]].first.second ){
		memo[order[curr]][curr] = 1;
		return dfs (order, curr + 1 );
	}else{
		return (memo[order[curr]][curr] = 0);
	} // end if

	return 0;
}

int getG (vector <int> order )
{
	int res = 0;

	for (int i = 0; i < order.size(); ++i ){
		res += (i+1)*(list[order[i]].first.first);
	} // end for

	return res;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.lunch", "r", stdin );
	int n;

	while (cin >> n && n ){
		N = n;
		list.clear();
		list.resize(n);
		memset (memo, -1, sizeof(memo) );
		for (int i = 0; i < n; ++i){
			cin >> list[i].second;
			cin >> list[i].first.first;	// w
			cin >> list[i].first.second;	// s 
		} // end for
		vector <int> order(n, 0 );
		for (int i = 0; i < n; ++i ){
			order[i] = i;
		} // end for

		vector <int> minorder;
		int minG = INT_MAX;
		do{
			if (dfs (order, 0) > 0){
				int currG = getG (order );
				if (currG < minG ){
					minorder = order;
					minG = currG;
				} // end if
			} // end if
		}while (next_permutation (order.begin(), order.end() ) );

		for (int i = 0; i < n; ++i ){
			cout << list[minorder[i]].second << endl;
		} // end for

	} // end loop
		
	return 0;
}