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 <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 minG;
vector <int> minorder;
int total;

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;
}

void bt (vector <int> & order, int curr )
{
	if (curr == N ){
		int currG = getG (order );
		if (currG < minG ){
			minorder = order;
			minG = currG;
		} // end if
		return;
	} // end if

	if (order[curr] != -1 && memo[order[curr]][curr] == 0 ){
		return;
	} // end if

	if (order[curr] == -1 ){
		vector <bool> used (N, false );
		for (int i = 0; i < curr; ++i ){
			used[order[i]] = true;
		} // end if

		for (int i = 0; i < N; ++i ){
			if (!used[i] ) {
				order[curr] = i;
				int curr_w = total;
				for (int j = 0; j <= curr; ++j ){
					curr_w -= list[order[j]].first.first;
				} // end for
				if (curr_w <= list[order[curr]].first.second ){
					memo[order[curr]][curr] = 1;
					bt (order, curr + 1 );
				}else{
					memo[order[curr]][curr] = 0;
				}// end if
				order[curr] = -1;
			} // end if
		} // end for
	} // end if

}

void init (int n )
{
	N = n;
	list.clear();
	list.resize(n);
	minorder.clear();
	minorder.resize(n);
	total = 0;
	minG = INT_MAX;
	memset (memo, -1, sizeof(memo) );
}	

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

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

		bt (order, 0 );

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

	} // end loop
		
	return 0;
}

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

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 += (i+1) * w[hist[n-1-i]];
		q += w[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();
		memset(ret, 0, sizeof(ret));
		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 <bits/stdc++.h>

using namespace std;

int main() {
  
  int N;
  while(cin >> N && N) {
    string name[N];
    int weight[N], strength[N];
    
    for(int i=0; i<N; i++)
      cin >> name[i] >> weight[i] >> strength[i];
    
    int per[N]; for(int i=0; i<N; i++) per[i] = i;
    int save[N]; for(int i=0; i<N; i++) save[i] = i;
    
    int mx = 0;
    int mxG = 0;
    
    do {
      
      int sum_weight = weight[per[0]];
      int G = weight[per[0]];
      bool ok = 1;
      for(int i=1; i<N; i++) {
	if(sum_weight <= strength[per[i]]) {
	  sum_weight += weight[per[i]];
	  G += (i+1)*weight[per[i]];
	}
	else {
	  ok = 0; break;
	}
      }
      
      if(ok) {
	if(G > mxG) {
	  mxG = G;
	  for(int i=0; i<N; i++) save[i] = per[i];
	}
      }
      
    } while(next_permutation(per, per+N));
    
    for(int i=N-1; i>=0; i--) {
      cout << name[save[i]] << endl;
    }
    
  }  
  
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include <vector>

using namespace std;

struct PP{
	int f;int w;
	string str;
};

int main(){
	
	int a,b;
	string L;
	int n;
	while(1){
	cin>>n;if(n==0)break;

	struct PP  P[12];
	for(int i=0;i<n;i++){
		cin>>P[i].str>>P[i].w>>P[i].f;
	}
	for(int i=0;i<n;i++)
		for(int j=n-1;j>i;j--)
			if(P[j].f>P[j-1].f){
				struct PP T;
				T=P[j-1];P[j-1]=P[j];P[j]=T;
			}
bool f=true;
	while(1){int s=0;
	f=true;

		for(int i=n-1;i>=0;i--){
			
			if(i>0&&P[i].w>P[i-1].w){
				if(P[i].f>=P[i-1].w+s){
				struct PP TT;
				TT=P[i-1];P[i-1]=P[i];P[i]=TT;
				f=false;
			}
			}
				s+=P[i].w;
		}
		if(f==true)break;
	}
	for(int i=0;i<n;i++)
		cout<<P[i].str<<endl;
}
	return 0;
}