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 <climits>
using namespace std;

struct Food{
  string name;
  int weight, durability;
};

int n, minG;
Food bag[11];
int order[11], bestOrder[11];
bool used[11];

void dfs(int selectedNum, int totalWeight, int numerG){
  if(selectedNum != 0 && numerG > minG) return;

  if(selectedNum == n){
    if(numerG < minG){
      minG = numerG;
      for(int i = 1; i <= n; i++) bestOrder[i] = order[i];
    }
    return;
  }

  for(int i = 1; i <= n; i++){
    if(!used[i] && bag[i].durability >= totalWeight){
      used[i] = true;
      order[selectedNum + 1] = i;
      dfs(selectedNum + 1, totalWeight + bag[i].weight, numerG + (n - selectedNum) * bag[i].weight);
      used[i] = false;
    }
  }
}

int main(void){
  while(cin >> n && n != 0){
    minG = INT_MAX;
    for(int i = 1; i <= n; i++){
      cin >> bag[i].name >> bag[i].weight >> bag[i].durability;
      used[i] = false;
    }
    dfs(0, 0, 0);
    for(int i = n; i >= 1; i--) cout << bag[bestOrder[i]].name << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 入力
string f[10];
int n, w[10], s[10]; 
// used[i] := i番目を選んだかどうか
bool used[10];
// 今まで見つかった中で一番重心が低いもの
double ans_g;
// 解
vector<int> ans;

// 重さに耐えられるかチェック
bool check(vector<int>& v){
	for(int i=0 ; i < v.size() ; i++ ){
		int weight = 0;
		for(int j=i+1 ; j < v.size() ; j++ ){
			weight += w[v[j]];
		}
		if( s[v[i]] < weight )
			return false;
	}
	return true;
}

// 重心を計算
double gravity(vector<int>& v){
	double a = 0, b = 0;
	for(int i=0 ; i < v.size() ; i++ ){
		a += (i+1) * w[v[i]];
		b += w[v[i]];
	}
	return a / b;
}

// 順列生成して解を探す (最大で 10! 通り)
void solve(){
	vector<int> v(n);
	for(int i=0 ; i < n ; i++ ) v[i] = i;
	// 順列生成してチェック
	do{
		double g = gravity(v);
		// 重心が低い かつ 重さに耐えられるとき
		if( check(v) && g < ans_g ){
			ans_g = g;
			ans = v;
		}
	}while( next_permutation(v.begin(),v.end()) );
}

// 探索
void dfs(vector<int>& v, int num){
	// 重さに耐えられるかチェック, 耐えられないときは探索打ち切り
	if( !check(v) ) return;
	
	if( num == n ){
		double g = gravity(v);
		if( g < ans_g ){ // 重心が低いとき
			ans_g = g;
			ans = v;
		}
		return;
	}
	
	for(int i=0 ; i < n ; i++ ){
		if( used[i] ) continue;
		
		used[i] = true;
		v.push_back( i );
		dfs( v , num+1 );
		v.pop_back();
		used[i] = false;
	}
}

int main(){
	while( cin >> n , n ){
		// 初期化
		for(int i=0 ; i < 10 ; i++ ) used[i] = false;
		
		for(int i=0 ; i < n ; i++ ){
			cin >> f[i] >> w[i] >> s[i];
		}
		
		ans_g = 1e+9;
		vector<int> v;
		dfs( v , 0 );
		//solve();
		
		for(int i=0 ; i < ans.size() ; i++ ){
			cout << f[ans[i]] << endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int me[10],rigidity[10],n;
char str[10][21];
void serch(int num,int now,int sum,int point,int co)
{
    if(num==0)return;
    else if(n==now){
        rigidity[point]=-10000;
        cout<<str[point]<<endl;;
        serch(num-1,0,sum-me[point],0,0);
    }
    else if(sum-me[now]<=rigidity[now]&&co==0)serch(num,now+1,sum,now,1);
    else if(sum-me[now]<=rigidity[now]&&me[point]<me[now])serch(num,now+1,sum,now,1);
    else serch(num,now+1,sum,point,co);
}
int main()
{
    while(cin>>n,n){
        int sum=0;
        for(int i=0;i<n;i++){
            cin>>str[i]>>me[i]>>rigidity[i];
            sum+=me[i];
        }
        serch(n,0,sum,0,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;
	}
      }
    }
    
    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>
using namespace std;

string f[11];
int ans[11];
int n;
int w[11],s[11];

void solve(){
	for(int i=1; i<=n; i++)
		ans[i] = i;
	for(int k=1; k<=n; k++){
		for(int i=n; i>k; i--){
			if(s[i] > s[i-1]){
				int tmp = s[i];
				s[i] = s[i-1];
				s[i-1] = tmp;
				tmp = ans[i];
				ans[i] = ans[i-1];
				ans[i-1] = tmp;
			}
		}
	}
}

int main(){
	while(true){
		cin >> n;
		if(!n)
			return 0;
		for(int i=1; i<=n; i++)
			cin >> f[i] >> w[i] >> s[i];
		solve();
		for(int i=1; i<=n; i++)
			cout << f[ans[i]] << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <map>
#define P pair<int,string>
#define PV(w,s,n) multimap< int,P >::value_type((w), P((s),(n)))
using namespace std;
int main()
{
  int n;
  for(;;){
    cin >> n;
    if(n==0) break;
    multimap< int,P,greater<int> > m;
    string l;
    int w,s,t=0;
    while(n--){
      cin >> l >> w >> s;
      m.insert( PV(w,s,l) );
      t += w;
    }
    multimap< int,P >::iterator it;
    while(!m.empty()){
      it = m.begin();
      while(it != m.end()){
	if((*it).second.first >= t - (*it).first){
	  cout << (*it).second.second << endl;
	  t -= (*it).first;
	  m.erase(it);
	  break;
	}
	it++;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)

int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

struct food{
  string fn;
  int g;
  int s;
};
int f[10],an[10],m;
food fd[10],sf;
int ans(int n,int s){
  if(n==-1){
    for(int i = m - 1 ; i >= 0 ; i --)
      cout << fd[an[i]].fn << endl; 
      return 1;
  }
 for (int i=m-1;i>=0;i--) {
        if (f[i]==0 && fd[i].s>=s-fd[i].g) {
            f[i]=1; an[n]=i;
            if (ans(n-1,s-fd[i].g)==1) return 1; 
            f[i]=0;
        }
  }
  return 0;
}

int main(void){
  int i,j,s;
  while(cin >> m){
    if (m==0)break;
    for(i = 0 , s = 0 ; i < m ; i ++){
      f[i] = 0 ; 
      cin >> fd[i].fn >> fd[i].g >> fd[i].s;
      s+=fd[i].g;
    }
    for(i = 1 ; i < m ; i ++){
      for(j = 0  ; j < m - i ; j ++){
	if(fd[j].g>fd[j+1].g){
	  sf=fd[j];
	  fd[j] = fd[j+1];
	  fd[j+1] = sf;
	}
      }
    }
    ans(m-1,s);
  }
  return 0;
}