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 <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
#include<iostream>
#include<cmath>

struct Lunch {
	int id, weight, strongth;
	bool operator<(const Lunch that) const { return id < that.id; }
};

bool is_not_crash(const std::vector<Lunch>& lunch) {
	int sum_weight = 0;
	for (auto r = lunch.rbegin(); r != lunch.rend(); ++r) {
		if (sum_weight > r->strongth) return false;
		sum_weight += r->weight;
	}
	return true;
}
double cal_center(const std::vector<Lunch>& lunch) {
	double denominator = 0, numerator = 0;
	for (auto i = 0; i < lunch.size(); ++i) {
		denominator += lunch[i].weight;
		numerator += lunch[i].weight * (i + 1.0);
	}
	return numerator / denominator;
}
int factorial(const int n) {
	int result = 1;
	for (auto i = 2; i <= n; ++i) {
		result *= i;
	}
	return result;
}
int main() {
	while (true) {
		int n; std::cin >> n; if (n == 0) break;
		std::vector<std::string> names(n);
		std::vector<Lunch> lunches(n), result; 
		for (auto i = 0; i < n; ++i) {
			lunches[i].id = i;
			std::cin >> names[i] >> lunches[i].weight >> lunches[i].strongth;
		}
		double min_center = DBL_MAX;
		for (auto i = factorial(n); i > 0; --i) {
			if (is_not_crash(lunches)) {
				auto temp = cal_center(lunches);
				if (temp < min_center) {
					min_center = temp;
					result = lunches;
				}
			}
			std::next_permutation(lunches.begin(), lunches.end());
		}
		for (const auto i : result) {
			std::cout << names[i.id] << '\n';
		}
	}
}

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const double EPS = 1e-6;

int main(void) {
	int N;
	while (cin >> N, N) {;
		vector<string> F(N);
		VI W(N), S(N);
		for (int i = 0; i < N; i++) {
			cin >> F[i] >> W[i] >> S[i];
		}

		VI p(N);
		iota(p.begin(), p.end(), 0);

		double ans = 1e+8;
		VI best_p(p.begin(), p.end());

		do {
			double g = 0, gt = 0;
			for (int i = 0; i < N; i++) {
				g += (i + 1) * W[p[i]];
				gt += W[p[i]];
			}
			g /= gt;

			if (g - EPS > ans) { continue; }

			int total = 0;
			bool flg = true;
			for (int i = N - 1; i >= 0; i--) {
				flg &= total <= S[p[i]];
				total += W[p[i]];
			}

			if (flg && g < ans + EPS) {
				ans = g;
				swap(best_p, p);
			}
		} while (next_permutation(p.begin(), p.end()));

		for (auto i : best_p) {
			cout << F[i] << endl;
		}
	}

	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dat[10];
int use[10];
char str[10][21];
int w[10];
int s[10];
int sum;
int n;
int best[10];
int val;
void solve(int a,int b){
	if(a==n){
		int p=0;
		for(int i=0;i<n;i++)p+=w[dat[i]]*(i+1);
		if(p<val){
			val=p;
			for(int i=0;i<n;i++)best[i]=dat[i];
		}
	}else{
		for(int i=0;i<n;i++){
			if(!use[i]&&s[i]>=b-w[i]){
				use[i]=1;
				dat[a]=i;
				solve(a+1,b-w[i]);
				use[i]=0;
			}
		}
	}
}
int main(){
	while(scanf("%d",&n),n){
		val=999999999;
		for(int i=0;i<n;i++)scanf("%s%d%d",str[i],w+i,s+i);
		sum=0;
		for(int i=0;i<n;i++)sum+=w[i];
		solve(0,sum);
		for(int i=0;i<n;i++)printf("%s\n",str[best[i]]);
	}
}

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

const double inf = 1e10;

int main() {
  for(int n; cin >> n, n;) {
    string f[n];
    int w[n], s[n];
    for(int i = 0; i < n; ++i) {
      cin >> f[i] >> w[i] >> s[i];
    }
    vector<int> t(n), ans;
    for(int i = 0; i < n; ++i) t[i] = i;
    double minG = inf;
    do {
      try {
        for(int i = n-1, sumw = 0; i >= 0; --i) {
          if(s[t[i]] >= sumw); else throw 0;
          sumw += w[t[i]];
        }
        double G = 0;
        int a = 0, b = 0;
        for(int i = 0; i < n; ++i) {
          a += (i+1) * w[t[i]];
          b += w[t[i]];
        }
        G = (double)a/(double)b;
        if(G < minG) {
          ans = t;
          minG = G;
        }
      } catch(...) {}
    } while(next_permutation(t.begin(),t.end()));

    for(int i = 0; i < n; ++i) {
      cout << f[ans[i]] << endl;
    }
  }
  return 0;
}