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>
using namespace std;
typedef pair<pair<int,int>,string> item;
main(){
	int n,i,w,s,S;
	string f;
	for(;cin>>n,n;){
		vector<item> items,result;
		for(i=0;i<n;i++)cin>>f>>w>>s,items.push_back(make_pair(make_pair(w,s),f));
		sort(items.begin(),items.end());
		S=999999999;
		do{
			for(s=w=i=0;i<n;i++){ //from upper
				if(items[i].first.second<w)break;
				w+=items[i].first.first;
				s+=(n-i)*items[i].first.first;
			}
			if(i==n&&s<S)cout<<s<<endl,S=s,result=items;
		}while(next_permutation(items.begin(),items.end()));
		reverse(result.begin(),result.end());
		for(i=0;i<n;i++)cout<<result[i].second<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

struct data{
    string name;
    int w, s;
};

bool cmp(data d1, data d2){
    if(d1.w == d2.w){
        return d1.s > d2.s;
    }
    return d1.w > d2.w;
};

signed main(){
    int n;
    while(cin >> n, n){
        vector<data> in(n);
        int sum = 0;
        rep(i, 0, n){
            cin >> in[i].name >> in[i].w >> in[i].s;
            sum += in[i].w;
        }
        vector<string> ans;
        rep(i, 0, n){
            sort(all(in), cmp);
            rep(j, 0, in.size()){
                if(in[j].s >= sum - in[j].w){
                    ans. push_back(in[j].name);
                    sum -= in[j].w;
                    in[j].w = -1;
                    in[j].s = -1;
                }
            }
        }
        rep(i, 0, n){
            cout << ans[i] << endl;
        }
    }
}

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=n-1;i>=0;i--){
      for(int j=0;j<i;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;
}

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

using namespace std;
typedef pair<int, int> P;
typedef pair<string, P> PP;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<PP> Vec;
		int sum_weight = 0;
		for(int i = 0; i < n; ++i)
		{
			string name;
			cin >> name;
			int w, s;
			cin >> w >> s;
			Vec.push_back(PP(name, P(w, s)));
			sum_weight += w;
		}	
		for(int i = 0; i < n; ++i)
		{
			vector<int> ok;
			for(int j = 0; j < n; ++j)
			{
				if(sum_weight - Vec[j].second.first <= Vec[j].second.second)
				{
					ok.push_back(j);
				}
			}
			if(ok.size() == 1)
			{
				cout << Vec[ok[0]].first << endl;
				sum_weight -= Vec[ok[0]].second.first;
				Vec[ok[0]].second.second = -9999999;
			}
			else
			{
				int heaviest = 0;
				int pos = 0;
				for(int k = 0; k < ok.size(); ++k)
				{
					if(heaviest < Vec[ok[k]].second.first)
					{
						heaviest = Vec[ok[k]].second.first;
						pos = k;
					}
				}
				cout << Vec[ok[pos]].first << endl;
				sum_weight -= Vec[ok[pos]].second.first;
				Vec[ok[pos]].second.second = -9999999;
			}
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

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

struct DATA{
	double G;
	vector<FOOD> mFoods;
};

struct DATA dp[1<<10][10];

bool checkWeight(vector<FOOD> vf){
	for(int i=0;i<vf.size();i++){
		int sum = 0;
		for(int j=i+1;j<vf.size();j++){
			sum += vf[j].w;
		}
		if(vf[i].s < sum) return false;
	}

	return true;
}

double getG(vector<FOOD> vf){

	double G=0;
	double sum=0;
	for(int i=0;i<vf.size();i++){
		G += vf[i].w * (i+1);
		sum += vf[i].w;
	}
	return G / sum;
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		struct FOOD* food = new struct FOOD[n];

		for(int i=0;i<n;i++){
			char buf[64];
			int w,s;
			scanf("%s %d %d",buf,&w,&s);
			food[i].name = buf;
			food[i].s = s;
			food[i].w = w;
		}

		int* table = new int[n];
		for(int i=0;i<n;i++) table[i] = i;

		vector<FOOD> Foods;

		struct DATA res;
		res.G = 1000000;
		do{
			for(int i=0;i<n;i++){
				Foods.push_back(food[table[i]]);
			}

			double tmpG;
			if(checkWeight(Foods) &&
				res.G > (tmpG = getG(Foods))){
					res.mFoods = Foods;
					res.G = tmpG;
			}
			Foods.clear();
		}while(next_permutation(table,table+n));

		for(int i=0;i<res.mFoods.size();i++){
			printf("%s\n",res.mFoods[i].name.c_str());
		}

		delete[] food;
		delete[] table;
	}
}