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

struct Item
{
	string name;
	int w;
	int s;
};

class ShieldDesc
{
public:
	bool operator()( const Item& a, const Item& b ) const
	{
		if( a.s > b.s ) return true;
		return false;
	}
};

double calcG( vector< Item >& v, vector< int > p )
{
	double num = 0.0, den = 0.0;
	for( int i = 0; i < (int)p.size(); ++i ){
		num += ( v[ p[ i ] ].w * ( i + 1 ) );
		den += v[ p[ i ] ].w;
	}
	return num / den;
}

vector< int > rec( vector< Item >& v, int used, vector< int > p, vector< int > r, vector< int >& a )
{
	int rest = v.size() - p.size();
	if( rest == 0 ){
		if( a.empty() || calcG( v, p ) < calcG( v, a ) ){
			a = p;
		}
		return a;
	}

	int shift = 0;
	for( int i = 0; i < rest; ++i ){
		int bit = 1;
		while( used & bit << shift ){
			++shift;
		}
		vector< int > pp = p;
		vector< int > rr = r;
		int w = v[ shift ].w;
		bool failed = false;
		for( int j = p.size() - 1; j >= 0; --j ){
			rr[ j ] -= w;
			if( rr[ j ] < 0 ){
				failed = true;
				break;
			}
		}
		if( failed ){
			++shift;
			continue;
		}

		pp.push_back( shift );
		rr.push_back( v[ shift ].s );
		rec( v, used | ( 1 << shift ), pp, rr, a );
		++shift;
	}
	return a;
}

vector< int > stack( vector< Item >& v )
{
	vector< int > p, r, a;
	return rec( v, 0, p, r, a );
}

int main()
{
	int n;

	while( cin >> n && n ){
		vector< Item > v;
		for( int i = 0; i < n; ++i ){
			Item item;
			cin >> item.name >> item.w >> item.s;
			v.push_back( item );
		}
		sort( v.begin(), v.end(), ShieldDesc() );

		vector< int > ans = stack( v );
		for( int i = 0; i < n; ++i ){
			cout << v[ ans[ i ] ].name << endl;
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define R(i,a,n) for(int i=a;i<n;i++)
using namespace std;
string s[12];
int n,a[11],an[11];
double v[12],w[12];
main(){
  while(cin>>n,n){
    double ans=1e9,t,x,y;
    r(i,n)a[i]=i;
    r(i,n)cin>>s[i]>>w[i]>>v[i];
    do{
      int f=0;
      r(i,n){t=0;
        R(j,i+1,n)t+=w[a[j]];
        if(v[a[i]]<t)f++;
      }
      if(f)continue;
      y=x=0;
      r(i,n)x+=w[a[i]]*(i+1),y+=w[a[i]];
      if(x/y<ans){
        ans=x/y;
        r(i,n)an[i]=a[i];
      }
    }while(next_permutation(a,a+n));
    r(i,n)cout<<s[an[i]]<<endl;
  }
}

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;


double getG (ITEM add, vector <ITEM> res )
{
	int size = res.size();

	double total_w = (double)add.first.second;
	for (int i = 0; i < size; ++i ){
		total_w += (double)res[i].first.second;
	} // end for

	double ans = (double)(add.first.second);
	for (int i = 0; i < size; ++i ){
		ans += (double)( (size + 1 - i)*(res[i].first.second) );
	} // end for

	ans /= total_w;

	return ans;
}

bool is_gain_up (int n, vector <ITEM> list, vector <bool> used)
{
	int size = list.size();
	vector <ITEM> remain;

	for (int i = 0; i < size; ++i ){
		if (i != n && !used[i] ){
			remain.push_back(list[i] );
		} // end if
	} // end for

	int total_w = 0;
	for (int i = 0; i < remain.size(); ++i ){
		total_w += remain[i].first.second;
	} // end for

	return  (total_w <= list[n].first.first );
}


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

	while (cin >> n && n ){
		vector <ITEM> list(n);
		for (int i = 0; i < n; ++i){
			cin >> list[i].second;
			cin >> list[i].first.second;
			cin >> list[i].first.first;
		} // end for

		sort (list.begin(), list.end() );
	
		vector <bool> used (n, false );
		used[n-1] = true;
		vector <ITEM> res;
		res.push_back(list[n-1]);

		for (int j = 0; j < n - 1; ++j ){
			double minG = DBL_MAX;
			int minindex = -1;
			for (int i = 0; i < n - 1; ++i ){
				if (i != j && !used[i] && is_gain_up(i, list, used) && minG > getG (list[i], res ) ){
						minG = getG (list[i], res );			
						minindex = i;
				} // end if
			} // end for
			if (minindex != -1){
				used[minindex] = true;
				res.push_back(list[minindex] );
			} // end if
		} // end for

		for (int i = 0; i < res.size(); ++i ){
//			cout << res[i].first.first << ':' << res[i].first.second << ':' << res[i].second << endl;
			cout << res[i].second << endl;
		} // end for
//		cout << endl;
	
	} // end while
	
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while(true){
      cin>>n;
      if(n==0) break;
      std::vector<string> f(n);
      std::vector<int> w(n),s(n),v(n),res(n);
      for(int i=0;i<n;i++) cin>>f[i]>>w[i]>>s[i];
      for(int i=0;i<n;i++) v[i]=i;
      double mi=1e9;
      do{
          double g=0;//,sum=0;
          for(int i=1;i<=n;i++){
              g+=i*w[v[i-1]];
             // sum+=w[v[i-1]];
          }
          bool flag=true;
          for(int i=0;i<n;i++){
              int wa=0;
              for(int j=i+1;j<n;j++) wa+=w[v[j]];
              if(s[v[i]]<wa) flag=false;
          }
          if(flag){
              if(g<mi){
                  for(int i=0;i<n;i++) res[i]=v[i];
                  mi=g;
              }
          }
      }while(next_permutation(v.begin(),v.end()));
      for(int i=0;i<n;i++) cout<<f[res[i]]<<endl;
  }
}