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 <algorithm>
#include <string>
using namespace std;

int N;
string F[10];
int W[10], S[10];

bool isValid(int id[]) {
  int sumW = 0;
  for(int i = N-1; i >= 0; --i) {
    if(S[id[i]] < sumW) return false;
    sumW += W[id[i]];
  }
  return true;
}

double getG(int id[]) {
  int a, b;
  a = b = 0;
  for(int i = 0; i < N; ++i) {
    a += W[id[i]]*(i+1);
    b += W[id[i]];
  }
  return (double)a/b;
}

void solve() {
  int id[N], ans[N];
  double g = -1;
  for(int i = 0; i < N; ++i) id[i] = i;
  do {
    if(isValid(id)) {
      double newG = getG(id);
      if(g == -1 || newG < g) {
	g = newG;
	for(int i = 0; i < N; ++i) ans[i] = id[i];
      }
    }
  } while(next_permutation(id, id+N));
  for(int i = 0; i < N; ++i) cout << F[ans[i]] << endl;
}

int main() {
  while(cin >> N && N) {
    for(int i = 0; i < N; ++i)
      cin >> F[i] >> W[i] >> S[i];
    solve();
  }
  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 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
}

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<algorithm>
#include<vector>
using namespace std;
typedef pair<int,string> P;
typedef pair<int,P> PP;

int n,a;
PP in[10],tmp;
int t[10];
vector<int>ans;

int get_weight(int);

int main(){
  while(cin>>n&&n!=0){
    ans.clear();
    for(int i=0;i<n;i++)cin>>in[i].second.second>>in[i].second.first>>in[i].first;
    sort(in,in+n);

    for(int i=0;i<n;i++){
      for(int j=0;j<n-1;j++){
	a=get_weight(j)+in[j+1].second.first;
	if(a<=in[j].first&&in[j].second.first>in[j+1].second.first){
	  tmp=in[j];in[j]=in[j+1];in[j+1]=tmp;
	}else if(a<=in[j].first&&(in[j].second.first==in[j+1].second.first&&in[j].first>in[j+1].first)){
	  tmp=in[j];in[j]=in[j+1];in[j+1]=tmp;
	}
      }
    }
    
    for(int i=n-1;i>=0;i--)cout<<in[i].second.second<<endl;
    
    
  }
  return 0;
}

int get_weight(int x){
  int res=0;
  for(int i=0;i<x;i++)res+=in[i].second.first;
  return res;
}