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<algorithm>
#include<string>
using namespace std;
int main(){
    int n;
    while(cin>>n,n){
        string Name[10];
        double w[10],s[10];
        for(int i=0;i<n;i++)cin>>Name[i]>>w[i]>>s[i];
        vector<int>p(n),ans;
        for(int i=0;i<n;i++)p[i]=i;
        double mi=1<<25;
        do{
            double gu=0.0,gl=0.0,t=0.0;
            bool flag=true;
            for(int i=0;i<n;i++){
                int pos=p[i];
                gu+=(n-i)*w[pos];gl+=w[pos];
                if(s[pos]<t){
                    flag=false;
                    break;
                }
                t+=w[pos];

            }
            if(flag){
                double g=gu/gl;
                if(mi>g){
                    ans=p;
                    mi=g;
                }
            }
        }while(next_permutation(p.begin(),p.end()));

        reverse(ans.begin(),ans.end());
        for(int i=0;i<n;i++)cout<<Name[ans[i]]<<endl;
    }
    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;
		if (n == 0) { break; }
		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[a[i]]) goto E;
			}
			for (int i = 0; i < n; i++)sum2 += i*w[a[i]];
			if (maxn > sum2)b = a;
		E:;
		} while (next_permutation(a.begin(), a.end()));
		for (int i = 0; i < n; i++)cout << S[b[i]] << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main(){
	int n,k,s[105],b[105],i,j,x;
	while(1){
		scanf("%d%d",&n,&k);
		if(n==0 && k==0)break;
		for(i=0;i<k+1;i++)scanf("%d",&s[i]),b[i]=0;
		for(i=0;i<n;i++){
			for(j=0;j<k;j++){
				scanf("%d",&x);
				b[j]+=x;
			}
		}
		x=0;
		for(i=0;i<n;i++)
			if(b[i]>s[i]){x=1;break;}
		if(x==1)printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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)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<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll,ll> pll;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
#define MOD 1000000007
#define ARRAY_MAX 100005

const int INF = 1e9+7;


int main(){

    int n;
    while(cin >> n,n){

        vector<int> p(n);//配列のインデックス
        vector<string> f(n);
        vector<int> w(n),s(n),ans(n);
        int mini = INF;

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

        for(int i = 0;i < n;i++){
            cin >> f[i] >> w[i] >> s[i];
        }

        do{

            bool flag = true;
            int cnt = 0,g = 0;

            for(int i = 0;i < n;i++){

                flag &= (cnt <= s[p[i]]);//上から見る
                cnt += w[p[i]];
                g += (n-i)*w[p[i]];
            }

            if(flag == true && g < mini){
                ans = p;
                mini = g;
            }

        }while(next_permutation(p.begin(),p.end()));

        for(int i = n-1;i >= 0;i--){
            cout << f[ans[i]] << endl;
        }
    }


    return 0;
}

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

class Data
{
public:
    int w, s;
    string name;
    bool operator<(const Data& d){
        return s < d.s;
    }
};

int n;
vector<Data> d;
vector<bool> used;
vector<int> bestIndex;
int bestG;

void solve(int k, vector<int>& index, int g, int sumW)
{
    if(k == n){
        if(g < bestG){
            bestG = g;
            bestIndex = index;
        }
        return;
    }

    for(int i=0; i<n; ++i){
        if(used[i])
            continue;
        if(d[i].s >= sumW){
            used[i] = true;
            index[k] = i;
            solve(k+1, index, g+(n-k)*d[i].w, sumW+d[i].w);
            used[i] = false;
        }else{
            return;
        }
    }
}

int main()
{
    for(;;){
        cin >> n;
        if(n == 0)
            return 0;

        d.resize(n);
        for(int i=0; i<n; ++i)
            cin >> d[i].name >> d[i].w >> d[i].s;
        sort(d.begin(), d.end());

        bestG = INT_MAX;
        used.assign(n, false);
        vector<int> index(n);
        solve(0, index, 0, 0);
        for(int i=n-1; i>=0; --i)
            cout << d[bestIndex[i]].name << endl;
    }
}