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 <bits/stdc++.h>
 
using namespace std;
 
struct lunch {
    int w, s;
    char name[21];
 
    bool operator < (const lunch &l) const {
	if (w != l.w) {
	    return w < l.w;
	} else {
	    return s < l.s;
	}
    }
};
 
lunch N[10], ans[10];
int n;
 
void make()
{
    for (int i = 0; i < n; i++) {
	ans[i] = N[i];
    }
}
 
bool check(int &G)
{
    int Ws1 = 0, Ws2 = 0;
    for (int i = 0; i < n; i++) {
	if (N[i].s < Ws2) return false;
	Ws1 += (i+1)*N[i].w;
	Ws2 += N[i].w;
    }
    G = Ws1;
    return true;
}
 
int main()
{
    while (scanf("%d", &n), n) {
	for (int i = 0; i < n; i++) {
	    scanf("%s %d %d", N[i].name, &N[i].w, &N[i].s);
	}
	sort(N, N+n);
 
	int G = 0,t;
 
	do {
	    if (check(t)) {
		if (G < t) {
		    make();
		    G = t;
		}
	    }
	} while (next_permutation(N, N+n));
 
	for (int i = n-1; i >= 0; i--) {
	    printf("%s\n", ans[i].name);
	}
    }
    return 0;
}

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

using namespace std;

vector<int> w,s,hist;
vector<string> name;
int n,tmpn,tmpm;
int used,ret[10];
string str;
double gmin = 1e+10;

inline double calc_g(){
	double p=0;
	double q=0;
	for(int i=0;i<n;i++){
		p += (n-i) * hist[n-1-i];
		q += hist[i];
	}
	return p/q;
}

void solve(int num, int weight){
	if(num == n){			
		double g = calc_g();
		if(gmin > g){
			gmin = g;
			for(int i=0;i<n;i++) ret[i] = hist[i];
		}
		return;
	}

	for(int i=0;i<n;i++){
		if((used & (1 << i)) == 0){
			if(s[i] >= weight){
				//ツ鳴「ツ使ツ用ツづつゥツづつ重ツづ敖づ可妥渉つヲツづァツづェツつスツづァツ静渉づアツづづ敖づゥ
				used += 1 << i;
				hist.push_back(i);
				solve(num+1, weight+w[i]);
				hist.pop_back();
				used -= 1 << i;
			}
		}
	}
}

int main(void){
	while(cin >> n && n){
		w.clear();
		s.clear();
		hist.clear();
		name.clear();
		memset(ret, 0, sizeof(ret));
		used = 0;
		gmin = 1e+10;
		for(int i=0;i<n;i++){
			cin >> str >> tmpn >> tmpm;
			name.push_back(str);
			w.push_back(tmpn);
			s.push_back(tmpm);
		}
		solve(0,0);
		for(int i=0;i<n;i++){
			cout << name[ret[n-i-1]] << endl;
		}
	}
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int w[],int k[],int b[],int a[],int c,int n,int e,int g){
  int i,j;
  int bb[10];
  if(c==n){
    if(a[10]==0||a[10]>g){
      a[10]=g;
      memcpy(a,b,10*sizeof(int));
    }
  }else{
    for(i=0;i<n;i++){
      if(k[i]>=e){
	for(j=0;j<c;j++){
	  if(i==b[j])
	    break;
	}
	if(j==c){
	  memcpy(bb,b,sizeof(bb));
	  bb[c]=i;
	  f(w,k,bb,a,c+1,n,e+w[i],g+w[i]*(n-c));
	}
      }else
	break;
    }
  }
  return;
}
int main(){
  int h,i;
  int w[10],k[10],b[10],a[11],n;
  char s[10][64];
  for(;;){
    cin>>n;
    if(n==0)
      break;
    for(i=0;i<n;i++)
      cin>>s[i]>>w[i]>>k[i];
    do{
      h=0;
      for(i=1;i<n;i++){
	if(k[i-1]<k[i]){
	  swap(k[i-1],k[i]);
	  swap(w[i-1],w[i]);
	  swap(s[i-1],s[i]);
	  h=1;
	}
      }
    }while(h);
    memset(b,0,sizeof(b));
    memset(a,0,sizeof(a));
    f(w,k,b,a,0,n,0,0);
    for(i=0;i<n;i++)
      cout<<s[a[n-i-1]]<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std;

struct lunch{
  int w,s;
  char name[21];

  bool operator < (const lunch &l)const{
    if(w != l.w){
      return w < l.w;
    }else{
      return s < l.s;
    }
  }
};

lunch N[10],ans[10];
int n;

void make(){
  for(int i = 0 ; i < n ; i++){
    ans[i] = N[i];
  }
}

bool check(int &G){
  int Ws1 = 0,Ws2 = 0;
  for(int i = n-1 ; i >= 0 ; i--){
    if(N[i].s < Ws2) return false;
    Ws1 += (i+1)*N[i].w;
    Ws2 += N[i].w;
  }
  G = Ws1 / Ws2;

  return true;
}

int main(){
  while(scanf("%d" ,&n) ,n){
    for(int i = 0 ; i < n ; i++){
      scanf("%s %d %d" ,N[i].name ,&N[i].w ,&N[i].s);
    }
    sort(N,N+n);

    int G = 0,t;

    do{
      if(check(t)){
	if(G <= t){
	  make();
	  G = t;
	}
      }
    }while(next_permutation(N,N+n));

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

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int n, w[1000], s[1000];
string S[1000]; vector<int>b; int maxn;
int main() {
	while (true) {
		cin >> n; b.clear(); maxn = 1 << 30;
		for (int i = 0; i < n; i++) {
			cin >> S[i] >> w[i] >> s[i];
		}
		vector<int>a(n, 0);
		for (int i = 0; i < n; i++)a[i] = i;
		do {
			int sum2 = 0;
			for (int i = 0; i < n; i++) {
				int sum = 0;
				for (int j = i + 1; j < n; j++) {
					sum += w[a[j]];
				}
				if (sum > s[i]) goto E;
			}
			for (int i = 0; i < n; i++)sum2 += i*w[i];
			if (maxn > sum2)b = a;
		E:;
		} while (next_permutation(a.begin(), a.end()));
		for (int i = 0; i < n; i++)cout << S[a[i]] << endl;
	}
	return 0;
}