Problem:
ランチ
お昼に食べるお弁当を作るために、お店で食べ物を買いました。お店では、食べ物を入れるのに細長い袋しかもらえなかったので、すべての食べ物を縦に積んで袋に入れる必要があります。袋が倒れにくいように、できるだけ重い物を下にして詰めたいのですが、食べ物の中にはやわらかい物もあって、上に重い物を乗せるとつぶれてしまいます。
そこで、食べ物の情報を入力とし、全ての食べ物がつぶれず、かつ全体の重心が最も低くなるような積み方を出力するプログラムを作成してください。それぞれの食べ物ごとに、名前
f
、重さ
w
、上に載せて耐えられる重さ
s
が指定されます。
「全ての食べ物がつぶれない」というのは、下から順に、（
f
1
、
f
2
、... 、
f
n
）と
n
個の食べ物を積んだ時、すべての
f
について、
s
f
i
≥ w
f
i+1
+ w
f
i+2
+
...
+ w
f
n
であることを意味します。また、全体の重心
G
は、
G = (1 × w
f
1
+ 2 × w
f
2
+
...
+ n × w
f
n
)
/
(w
f
1
+ w
f
2
+
...
+w
f
n
)
とします。ただし、解はちょうど1つだけ存在するものとします。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
n
f
1
w
1
s
1
f
2
w
2
s
2
:
f
n
w
n
s
n
１行目に食べ物の個数
n
(1 ≤
n
≤ 10)、続く
n
行に
i
番目の食べ物の名前
f
i
(20 文字以内の半角英文字列)、重さ
w
i
(1 ≤
w
i
≤ 1000)、耐えられる重さ
s
i
(1 ≤
s
i
≤ 1000) が空白区切りで与えられます。
データセットの数は 20 を超えません。
Output
データセットごとに、次の形式で食べ物の名前を下から積み上げる順に出力します。
1 行目: 1 番下の食べ物の名前（半角英文字列）
2 行目: 下から2 番目の食べ物の名前
：
n
行目: 1 番上の食べ物の名前
Sample Input
4
sandwich 80 120
apple 50 200
cheese 20 40
cake 100 100
9
onigiri 80 300
onigiri 80 300
anpan 70 280
mikan 50 80
kanzume 100 500
chocolate 50 350
cookie 30 80
purin 40 400
cracker 40 160
0
Output for the Sample Input
apple
cake
sandwich
cheese
kanzume
purin
chocolate
onigiri
onigiri
anpan
mikan
cracker
cookie

Submitted solutions:

code_snippet: #include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#define INF 1000000000ll
#define MOD 1000000007ll
#define EPS 1e-10
#define REP(i,m) for(long long i=0; i<m; i++)
#define FOR(i,n,m) for(long long i=n; i<m; i++)
#define DUMP(a) for(long long dump=0; dump<(ll)a.size(); dump++) { cout<<a[dump]; if(dump!=n-1) cout<<" "; else cout<<endl; }
#define ALL(v) v.begin(),v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

/*
 貪欲
 ①決定問題 必ず解に含まれる(含むように変換できる)ものから決めていく みんプロ本選B
 ②最適化問題 任意の解から評価を落とさず解に含められるようなものから決めていく 区間スケジューリングとか
 ③
 */

/*
 DP
 ①DPはある程度決め打ちをするべき(解を帰納的に構成できないか？)
 */

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(1) {
	ll n;
	cin>>n;
	if(n==0) break;
	vector<string> name(n);
	vector<P> ws(n);
	REP(i,n) {
		cin>>name[i];
		cin>>ws[i].first>>ws[i].second;
	}
	vector<ll> perm(n);
	REP(i,n) perm[i]=i;
	vector<ll> ret_perm(n,-1);
	ll buf=INF;
	do {
		ll sum=0;
		bool d=true;
		REP(i,n) {
			if(ws[perm[n-1-i]].second<sum) {
				d=false;
				break;
			}
			sum+=ws[perm[n-1-i]].first;
		}
		if(d) {
			ll g=0;
			REP(i,n) g+=(i+1)*ws[perm[i]].first;
			if(g<buf) {
				buf=g;
				REP(i,n) ret_perm[i]=perm[i];
			}
		}
	} while(next_permutation(ALL(perm)));
	REP(i,n) {
		cout<<name[ret_perm[i]]<<endl;
	}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3224
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n;
    while(cin >> n, n){
        string s;
        int v1, v2, minv = 1 << 30;
        vector<tuple<string,int,int>> a(n);
        for(int i = 0; i < n; i++){
            cin >> s >> v1 >> v2;
            a[i] = {s, v1, v2};
        }
        sort(a.begin(), a.end());
        vector<string> ans;
        do{
            bool flag = true;
            int G = 0;
            for(int j = n - 1, W = 0; j >= 0; j--){
                if(W > get<2>(a[j]))flag = false;
                G += (j + 1) * get<1>(a[j]);
                W += get<1>(a[j]);
            }
            if(flag && G < minv){
                minv = G;
                ans.clear();
                for(int i = 0; i < n; i++)ans.push_back(get<0>(a[i]));
            }
        }while(next_permutation(a.begin(), a.end()));
        for(int i = 0; i < n; i++){
            cout << ans[i] << '\n';
        }
    }
}
accuracy: 1
time_ms: 300
memory_kb: 3424
final_rank: 20

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

struct stuff{
    string name;
    int w;
    int s;
    int idx;
    bool operator <(const stuff& a) const{
        return idx < a.idx;
    }
};

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;

        vector<stuff> a(n);
        for(int i=0; i<n; i++){
            cin >> a[i].name >> a[i].w >> a[i].s;
            a[i].idx = i;
        }
        vector<stuff> ans;
        double g = 1e9;
        do{
            int sum = 0;
            int weighted_sum = 0;
            bool ok = true;
            for(int i=0; i<n; i++){
                if(sum > a[i].s){
                    ok = false;
                    break;
                }
                sum += a[i].w;
                weighted_sum += a[i].w *(n-i);
            }
            if(!ok) continue;
            double newg = (double)weighted_sum/sum;
            if(newg < g){
                g = newg;
                ans = a;
            }
        }while(next_permutation(a.begin(), a.end()));
        for(int i=n-1; i>=0; i--){
            cout << ans[i].name << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 3332
final_rank: 18

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>

using namespace std;

vector<int> foods;
vector<int> solution;
string f[10];
int w[10];
int s[10];
bool flags[10];
int crG;
int crW;
int minG;

bool IsValid()
{
	int temp = crW;
	for (int i : foods)
	{
		temp -= w[i];
		if (s[i] < temp)
			return false;
	}
	return true;
}

void Backtrace(int n, int floor)
{
	if (floor == n + 1)
	{
		if (crG < minG)
		{
			minG = crG;
			copy(foods.begin(), foods.end(), solution.begin());
		}
		return;
	}
	for (size_t i = 0; i < n; i++)
	{
		if (!flags[i] && crG < minG)
		{
			flags[i] = true;
			crW += w[i];
			crG += floor * w[i];
			foods.push_back(i);
			if(IsValid())
			    Backtrace(n, floor + 1);
			foods.erase(foods.end() - 1);
			crG -= floor * w[i];
			crW -= w[i];
			flags[i] = false;
		}
	}
}

int main()
{
	int n;
	cin >> n;
	while (n)
	{
		for (size_t i = 0; i < n; i++)
		{
			cin >> f[i] >> w[i] >> s[i];
		}
		fill(flags, flags + n, false);
		solution.resize(n);
		minG = 100001;
		crG = 0;
		crW = 0;
		Backtrace(n, 1);
		for (int i : solution)
			cout << f[i] << endl;
		cin >> n;
	}
	//system("pause");
}
accuracy: 1
time_ms: 50
memory_kb: 3160
final_rank: 9

code_snippet: #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;
		}
	}
}
accuracy: 1
time_ms: 70
memory_kb: 3148
final_rank: 10

code_snippet: #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 1
	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 * (i + 1);
	}
	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[i]].name << endl;
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3168
final_rank: 13

code_snippet: #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;

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

int getG(int n) {
	int G = 0;
	for (int i = 0; i < n; i++) {
		G += FoodData[TableIndex[i]].weight * (i + 1);
	}
	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[i]].name << endl;
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3192
final_rank: 15

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {
	int G = 0;
	for (int i = 0; i < n; i++) {
		G += FoodData[TableIndex[i]].weight * (i + 1);
	}
	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);
				for (int i = 0; i < n; i++) {
					Result[i] = TableIndex[i];
				}
				//memmove(Result, TableIndex, n);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			cout << FoodData[Result[i]].name << endl;
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3044
final_rank: 1

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {
	int G = 0;
	for (int i = 0; i < n; i++) {
		G += FoodData[TableIndex[i]].weight * (i + 1);
	}
	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);
				//memmove(Result, TableIndex, n);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			cout << FoodData[Result[i]].name << endl;
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3044
final_rank: 3

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {

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

	return G;
}

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

	while (cin>>n, n) {

		for (int i = 0; i < n; i++) {
			/*char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;*/
			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);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			//printf("%s\n", FoodData[Result[i]].name.c_str());
			cout << FoodData[Result[i]].name << endl;
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3052
final_rank: 5

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {

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

	return G;
}

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

	while (cin>>n, n) {

		for (int i = 0; i < n; i++) {
			/*char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;*/
			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);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3056
final_rank: 4

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {

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

	return G;
}

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

	while (~scanf("%d", &n), n) {

		for (int i = 0; i < n; i++) {
			/*char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;*/
			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);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3164
final_rank: 12

code_snippet: #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;

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

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

	return true;
}

int getG(int n) {

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

	return G;
}

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

	while (~scanf("%d", &n), n) {

		for (int i = 0; i < n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

		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);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3108
final_rank: 8

code_snippet: #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;

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

	return true;
}

int getG(int n) {

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

	return G;
}

int main() {
	int n, tmpG;

	while (~scanf("%d", &n), n) {

		for (int i = 0; i < n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

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

		int minG = 1000000;

		do {
			if (checkWeight(n) && minG > (tmpG = getG(n))) {
				copy(TableIndex, TableIndex + n, Result);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i < n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3148
final_rank: 11

code_snippet: #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;
	/*for (int i = n - 1, j = n - 2; i >= 1, j >= 0; i--, j--) {
		sum += FoodData[TableIndex[i]].weight;
		if (FoodData[TableIndex[j]].limit < sum) return false;
	}*/
	for (int i = n-1; i > 0; i--) {
		sum += FoodData[TableIndex[i]].weight;
		if (FoodData[TableIndex[i - 1]].limit < sum) {
			return false;
		}
	}

	return true;
}

int getG(int n) {

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

	return G;
}

int main() {
	int n, tmpG;

	while (~scanf("%d", &n)) {
		if (n == 0) break;

		for (int i = 0; i<n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

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

		int minG = 1000000;

		do {
			if (checkWeight(n) && minG > (tmpG = getG(n)) ) {
				copy(TableIndex, TableIndex + n, Result);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i<n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3160
final_rank: 14

code_snippet: #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;
	for (int i = n - 1, j = n - 2; i >= 1, j >= 0; i--, j--) {
		sum += FoodData[TableIndex[i]].weight;
		if (FoodData[TableIndex[j]].limit < sum) return false;
	}

	return true;
}

int getG(int n) {

	int G = 0;
	//double sum = 0;
	for (int i = 0; i<n; i++) {
		G += (double)FoodData[TableIndex[i]].weight * (i + 1);
		//sum += (double)FoodData[TableIndex[i]].weight;
	}
	return G;
}

int main() {
	int n, tmpG;

	while (~scanf("%d", &n)) {
		if (n == 0) break;

		//struct FoodData* FoodData = new struct FoodData[n];

		for (int i = 0; i<n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

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

		int minG = 1000000;

		do {
			if (checkWeight(n) && minG > (tmpG = getG(n)) ) {
				copy(TableIndex, TableIndex + n, Result);
				minG = tmpG;
			}
		} while (next_permutation(TableIndex, TableIndex + n));

		for (int i = 0; i<n; i++) {
			printf("%s\n", FoodData[Result[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3256
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;


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

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

bool checkWeight(int n) {

	int sum = 0;
	for (int i = n - 1, j = n - 2; i >= 1, j >= 0; i--, j--) {
		sum += FoodData[TableIndex[i]].weight;
		if (FoodData[TableIndex[j]].limit < sum) return false;
	}

	return true;
}

int getG(int n) {

	int G = 0;
	//double sum = 0;
	for (int i = 0; i<n; i++) {
		G += (double)FoodData[TableIndex[i]].weight * (i + 1);
		//sum += (double)FoodData[TableIndex[i]].weight;
	}
	return G;
}

int main() {
	int n;
	while (~scanf("%d", &n)) {
		if (n == 0) break;

		//struct FoodData* FoodData = new struct FoodData[n];

		for (int i = 0; i<n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

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

		int res[10];
		int minG = 1000000;

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

		for (int i = 0; i<n; i++) {
			printf("%s\n", FoodData[res[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3096
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 1e-8;

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

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

bool checkWeight(int n) {

	int sum = 0;
	for (int i = n - 1, j = n - 2; i >= 1, j >= 0; i--, j--) {
		sum += FoodData[TableIndex[i]].weight;
		if (FoodData[TableIndex[j]].limit < sum) return false;
	}

	return true;
}

double getG(int n) {

	double G = 0;
	double sum = 0;
	for (int i = 0; i<n; i++) {
		G += (double)FoodData[TableIndex[i]].weight * (i + 1);
		sum += (double)FoodData[TableIndex[i]].weight;
	}
	return G / sum;
}

int main() {
	int n;
	while (~scanf("%d", &n)) {
		if (n == 0) break;

		//struct FoodData* FoodData = new struct FoodData[n];

		for (int i = 0; i<n; i++) {
			char buf[64];
			int w, s;
			scanf("%s %d %d", buf, &w, &s);
			FoodData[i].name = buf;
			FoodData[i].limit = s;
			FoodData[i].weight = w;
		}

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

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

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

		for (int i = 0; i<n; i++) {
			printf("%s\n", FoodData[res[i]].name.c_str());
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3100
final_rank: 6

code_snippet: #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';
		}
	}
}
accuracy: 1
time_ms: 100
memory_kb: 3032
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{
	int N;
	while(cin>>N,N)
	{
		vector<pair<string,pair<int,int> > >A;
		for(int i=0;i<N;i++)
		{
			string f;int w,s;cin>>f>>w>>s;
			A.push_back(make_pair(f,make_pair(w,s)));
		}
		sort(A.begin(),A.end());
		vector<string>ans;
		double mG=1e150;
		do{
			bool flag=true;
			int T=0;
			for(int i=0;i<N;i++)
			{
				if(T>A[i].second.second)
				{
					flag=false;
					break;
				}
				T+=A[i].second.first;
			}
			if(flag)
			{
				double G=0;
				for(int i=0;i<N;i++)G+=A[i].second.first*(N-i);
				G/=T;
				if(mG>G)
				{
					mG=G;
					ans.clear();
					for(int i=N;i--;)ans.push_back(A[i].first);
				}
			}
		}while(next_permutation(A.begin(),A.end()));
		for(int i=0;i<N;i++)cout<<ans[i]<<endl;
	}
}
accuracy: 1
time_ms: 400
memory_kb: 3180
final_rank: 19