题目

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

Hint

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the
word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

题解

状态压缩动态规划
在二进制情况下,第 i 位为 1 表示已做完 为 0 表示未做
这样可以用最多 2^15 的数来表示所有情况

dp[i] 表示当前处于 i 状态
循环判断其他科目是否已完成,如果未完成,将现在状态加上完成的状态记录为新状态 NewState
由于需要记录所花费的时间,和前驱
因此需要 min time last 三个变量
dp[NewState].min > dp[i].min + ReduceScore

循环所有的状态即可
所有科目均为 1 就是最后答案
然后根据前驱输出科目即可
(由于本身科目是按照字典序的,并且只有严格小于会更新状态,因此最后答案必然是字典序最小的)

由于输出是逆序,需要用栈等结构倒过来输出

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <bitset>
using namespace std;

const int INF = 0x7FFFFFFF / 2;
const int maxn = 16;
const int maxstate = (1 << 15);


int Deadline[maxn],Score[maxn];
struct Node {
    string Name;
    int Deadline;
    int Time;
};
struct Node2 {
    int min;
    int time;
    int last;
};
Node Homework[maxn];
Node2 dp[maxstate];
stack<string> s;


int SetI(int num,int i,bool flag) {
    if(flag)
        return num | (1 << i);
    else
        return num & (0 << i);
}
bool GetI(int num,int i) {
    return num >> i & 1;
}
int GetChange(int num1,int num2) {
    int n = num1 ^ num2;
    int i = 0;
    while(n) {
        if(n & 1)
            return i;
        n >>= 1;
        i++;
    }
    return 0;
}

void Do() {
    int n;
    cin >> n;

    int sum = 0;
    int mt = 0;

    for(int i = 0;i < n;i++)
        cin >> Homework[i].Name >> Homework[i].Deadline >> Homework[i].Time;

    dp[0].min = 0;
    for(int i = 1;i <= (1 << n) - 1;i++)
        dp[i].min = INF;

    for(int i = 0;i < (1 << n) - 1;i++) {
        for(int j = 0;j < n;j++)
            if(!GetI(i,j)) {
                int NewState = SetI(i,j,1);
                int Time = dp[i].time + Homework[j].Time;
                int ReduceScore = (Time > Homework[j].Deadline) ? (Time - Homework[j].Deadline) : 0;
                if(dp[NewState].min > dp[i].min + ReduceScore) {
                    dp[NewState].min = dp[i].min + ReduceScore;
                    dp[NewState].time = Time;
                    dp[NewState].last = i;
                }
            }
    }

    cout<<dp[(1 << n) - 1].min<<endl;

    int k = (1 << n) - 1;
    while(k) {
        int pos = GetChange(k,dp[k].last);
        s.push(Homework[pos].Name);
        k = dp[k].last;
    }

    while(!s.empty()) {
        cout << s.top() << endl;
        s.pop();
    }
    return;
}
int main() {
    cin.tie(0);
    std::cin.sync_with_stdio(false);

    int T;
    cin >> T;
    while(T--)
        Do();

    return 0;
}