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;


double getG (vector <ITEM> item )
{
	int size = item.size();

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

	ans /= total_w;

	return ans;
}

bool is_gain_up (vector <ITEM> cand )
{
	int size = cand.size();

	for (int i = 0; i < size - 1; ++i ){
		int total_w = 0;
		for (int j = i + 1; j < size; ++j ){
			total_w += cand[j].first.second;
		} // end if
		if (total_w <= cand[i].first.first ) {
			continue;
		}else{
			return false;
		} // end if
	} // end for
	
	return true;
}

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

		vector <int> order(n, 0 );
		for (int i = 0; i < n; ++i ){
			order[i] = i;
		} // end for
		vector <ITEM> res;
		vector <ITEM> cand;
		double minG = DBL_MAX;
		do{
			cand.clear();
			cand.resize(n);
			for (int i = 0; i < n; ++i ){
				cand[i] = list[order[i]];
			} // end for
			if (is_gain_up(cand) ){
				if (minG > getG(cand ) ){
					minG = getG(cand );
					res = cand;
				} // end if
			} // end if
		}while (next_permutation (order.begin(), order.end() ) );

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

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

struct Food{
	string f;
	int w,s;
	bool operator <(const Food &a)const{
		if(w==a.w) return s>a.s;
		return w>a.w;
	}
};

int main(){
	int N;
	while(cin>>N,N){
		int sumW = 0;
		Food food[10];
		for(int i=0;i<N;i++){
			cin>>food[i].f>>food[i].w>>food[i].s;
			sumW += food[i].w;
		}
		sort(food,food+N);
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(food[j].w == -1) continue;
				if(food[j].s >= sumW-food[j].w){
					cout<<food[j].f<<endl;
					sumW -= food[j].w; food[j].w = -1;
				}
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<utility>
#include<cmath>
#include<sstream>
using namespace std;
#define all(a) a.begin(),a.end()
typedef vector<int> vi;
struct food
{
	string name;
	int w,s;
};
typedef vector<food> vf;
int n;
int main()
{
	while(cin>>n&&n)
	{
		vf a;
		vi ans,s;
		for(int i=0;i<n;i++)
		{
			food f;
			s.push_back(i);
			cin>>f.name>>f.w>>f.s;
			a.push_back(f);
		}
		double t=9999999999999;
		do
		{
			vi sum;
			double r=0,f=0;
			for(int i=0;i<n;i++)
			{
				sum.push_back(a[s[i]].w);
				r+=(i+1)*a[s[i]].w;
				f+=a[s[i]].w;
			}
			for(int i=n-1;i>0;i--)sum[i-1]+=sum[i];
			sum.push_back(0);
			bool ok=true;
			for(int i=0;i<n;i++){if(sum[i+1]>a[s[i]].s)ok=false;}
			if(!ok)continue;
			if(t>r/f)
			{
				t=r/f;
				ans=s;
			}
		}while(next_permutation(all(s)));
		for(int i=0;i<n;i++)
		{
			cout<<a[ans[i]].name<<endl;	
		}
		
	}
}

C++: 
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef pair <string, II> NODE;
typedef vector <NODE> NODES;

void solve( int n, NODES nodes ) {
    int ming = INT_MAX;
    VS L;
    sort( nodes.begin(), nodes.end() );
    do {
        int wsum = 0, g = 0;
        for ( int i = 0; i < n; ++ i ) {
            if ( wsum > nodes[i].second.second ) goto out_1;
            wsum += nodes[i].second.first;
        }
        for ( int i = 0; i < n; ++ i ) {
            g += ( n - i ) * nodes[i].second.first;
            if ( g >= ming ) goto out_1;
        }
        if ( g < ming ) {
            ming = g;
            VS T;
            for ( int i = 0; i < n; ++ i ) {
                T.push_back( nodes[i].first );
            }
            L = T;
        }
    out_1:;
    } while ( next_permutation( nodes.begin(), nodes.end() ) );

    reverse( L.begin(), L.end() );
    for ( int i = 0; i < n; ++ i ) {
        cout << L[i] << endl;
    }
}

int main() {
    int n;
    while ( cin >> n && n ) {
        NODES nodes( n );
        for ( int i = 0; i < n; ++ i ) {
            cin >> nodes[i].first >> nodes[i].second.first >> nodes[i].second.second;
        }
        solve( n, nodes );
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAXN = 10;
const double inf = 1e10;

int n;
string f[MAXN];
int w[MAXN], s[MAXN];
double minG;
vector<int> t, ans, used;

void rec(int k, int sumww, int sumw) {
  if(k == -1) {
    double G = (double)sumww/(double)sumw;
    if(G < minG) {
      ans = t;
      minG = G;
    }
    return;
  }
  for(int i = 0; i < n; ++i) {
    t[k] = i;
    if(used[t[k]]) continue;
    if(s[t[k]] >= sumw) {
      used[t[k]] = true;
      rec(k-1, sumww + (k+1)*w[t[k]], sumw + w[t[k]]);
      used[t[k]] = false;
    }
  }
}

int main() {
  for(; cin >> n, n;) {
    for(int i = 0; i < n; ++i) {
      cin >> f[i] >> w[i] >> s[i];
    }
    minG = inf;
    t = used = vector<int>(n);
    rec(n-1, 0, 0);
    for(int i = 0; i < n; ++i) {
      cout << f[ans[i]] << endl;
    }
  }
  return 0;
}