# HDU-1074 Doing Homework

## Description

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.

Sample Input

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Sample Output

2
Computer
Math
English
3
Computer
English
Math

## Problem solving

$0$$000$ 就代表都未完成

$5$$101$ 就代表第一个和第三个作业完成，第二个未完成

$7$$111$ 就代表都完成

$1101$ 可由 $1001$得到

$1101$ 不可由 $0001$得到

$dp_i$表示到达状态$i$会扣的最少的分，那么显然最后作业全部完成后扣的最少的分为$dp_{2^{n}-1}$，关于作业完成顺序，我们添加一个辅助数组记录每次转移的状态的上一个状态，递归输出即可。

## Code

const int maxn = 20;
int dp[1<<maxn],fa[1<<maxn],cost[1<<maxn],d[maxn],c[maxn];
string s[maxn];
void print(int x){
if(!x)  return ;
print(x-(1<<fa[x]));
cout<<s[fa[x]]<<endl;
}
int main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i=0;i<n;i++)    cin>>s[i]>>d[i]>>c[i];
for(int i=1;i<(1<<n);i++){
dp[i]=0x3f3f3f3f;
for(int j=n-1;j>=0;j--){
int mid=(1<<j);
if(!(i&mid))    continue;
int val=max(0,cost[i-mid]+c[j]-d[j]);
if(dp[i]>dp[i-mid]+val){
dp[i]=dp[i-mid]+val;
cost[i]=cost[i-mid]+c[j];
fa[i]=j;
}
}
}
cout<<dp[(1<<n)-1]<<endl;
print((1<<n)-1);
}
}