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 <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;
}

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;
};

bool checkWeight(const struct FOOD* food,const int* table,int n){
	
	int sum=0;
	for(int i=n-1,j=n-2;i>=1,j>=0;i--,j--){
		sum += food[table[i]].w;
		if(food[table[j]].s < sum) return false;
	}

	return true;
}

double getG(const struct FOOD* food,const int* table,int n){

	double G=0;
	double sum=0;
	for(int i=0;i<n;i++){
		G += (double)food[table[i]].w * (i+1);
		sum += (double)food[table[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;

		int* res = new int[n]();
		double minG = 1000000.0;

		do{
			double tmpG;
			if(checkWeight(food,table,n) &&
				minG > (tmpG = getG(food,table,n))){
					copy(table,table+n,res);
					minG = tmpG;
			}
		}while(next_permutation(table,table+n));

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

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

C++: #include <cstdio>
#include <cfloat>
using namespace std;

char f[10][21];
int n, w[10], s[10], a[10], b[10];
bool u[10];
double maxg;

void dfs(int x, int tw, int m, double g) {
  if (m == n) {
    if (maxg < g) {
      maxg = g;
      for (int i=0; i<n; i++) {
        b[i] = a[i];
      }
    }
    return;
  }
  if (x>=n) {
    return;
  }
  if (!u[x] && tw <= s[x]) {
    u[x] = true;
    tw += w[x];
    a[m] = x;
    dfs(0, tw, m+1, g + w[x]*(m+1));
    tw -= w[x];
    u[x] = false;
  }
  dfs(x+1, tw, m, g);
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (!n) break;
    for (int i=0; i<n; i++) {
      scanf("%s %d %d", f[i], &w[i], &s[i]);
    }
    for (int i=0; i<n; i++) {
      u[i] = false;
    }
    maxg = -DBL_MAX;
    dfs(0, 0, 0, 0.);
    for (int i=n-1; i>=0; i--) {
      printf("%s\n", f[b[i]]);
    }
  }
  return 0;
}

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[ans[i]] > s[ans[i-1]]){
				int tmp = ans[i];
				ans[i] = ans[i-1];
				ans[i-1] = tmp;
			}
		}
	}
	bool flag = true;
	for(int i=1; i<=n; i++){
		if(i==n&&flag){
			i=1;
			flag = true;
		}else if(i==n)
			break;
		if(w[ans[i]] < w[ans[i+1]]){
			int tmp = w[ans[i]];
			for(int k=i+2; k<=n; k++)
				tmp += w[ans[k]];
			if(s[ans[i+1]] >= tmp){
				tmp = ans[i];
				ans[i] = ans[i+1];
				ans[i+1] = tmp;
				flag = false;
			}
		}
	}
}

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 <bits/stdc++.h>

using namespace std;

class FoodInfor {
public:
	string name;
	int weight;
	int limit;
};

FoodInfor FoodData[10];
int TableIndex[10], Result[10];


bool checkWeight(int n) {
	int sum = 0;

#if 0
	for (int i = n-1; i > 0; i--) {
		sum += FoodData[TableIndex[i]].weight;
		if (sum > FoodData[TableIndex[i - 1]].limit) {
			return false;
		}
	}
#else
	for (int i = 0; i < n - 1; i++) {
		sum += FoodData[TableIndex[i]].weight;
		if (sum > FoodData[TableIndex[i + 1]].limit) {
			return false;
		}
	}
#endif
	
	return true;
}

int getG(int n) {
	int G = 0;
	for (int i = 0; i < n; i++) {
		G += FoodData[TableIndex[i]].weight * (n-i);
	}
	return G;
}

int main() {
	int n, tmpG, minG;

	while (cin>>n, n) {
		for (int i = 0; i < n; i++) {
			cin >> FoodData[i].name >> FoodData[i].weight >> FoodData[i].limit;
		}

		for (int i = 0; i < n; i++) {
			TableIndex[i] = i;
		}

		minG = INT_MAX;
		do {
			if (checkWeight(n) && minG > (tmpG = getG(n))) {
				//copy(TableIndex, TableIndex + n, Result);	//OK
				//memmove(Result, TableIndex, n);			//NG
				for (int i = 0; i < n; i++) {
					Result[i] = TableIndex[i];
				}
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			cout << FoodData[Result[n-1-i]].name << endl;
		}
	}
}