Description:

Doki Doki Literature Club! is a visual novel developed by Team Salvato. The protagonist is invited by his childhood friend, Sayori, to join their high school’s literature club. The protagonist then meets the other members of the club: Natsuki, Yuri, and the club president Monika. The protagonist starts to participate in the club’s activities such as writing and sharing poetry, and grows close to the four girls. What a lovely story!

A very important feature of the game is its poetry writing mechanism. The player is given a list of various words to select from that will make up his poem. Each girl in the Literature Club has different word preferences, and will be very happy if the player’s poem is full of her favorite words.
The poem writing mini-game (from wikipedia)
BaoBao is a big fan of the game and likes Sayori the most, so he decides to write a poem to please Sayori. A poem of m words s 1 , s 2 , , s m s_{1},s_{2},……,s_{m} s1,s2,,sm is nothing more than a sequence of m strings, and the happiness of Sayori after reading the poem is calculated by the formula
H = <munderover> i = 1 m </munderover> ( m i + 1 ) × f ( s i ) H = \sum_{i=1}^m (m-i+1)\times f(s_i) H=i=1m(mi+1)×f(si)
where H is the happiness and f ( s i ) f(s_{i}) f(si) is Sayori’s preference to the word s i s_{i} si .

Given a list of n words and Sayori’s preference to each word, please help BaoBao select words from the list and finish the poem with these m words to maximize the happiness of Sayori.

Please note that each word can be used at most once!

Input:

There are multiple test cases. The first line of input contains an integer T (about 100), indicating the number of test cases. For each test case:

The first line contains two integers n and m ( 1 m n 100 1\le m \le n \le 100 1mn100 ), indicating the number of words and the length of the poem.

For the following n lines, the i-th line contains a string consisting of lowercased English letters w i ( 1 w i 15 ) w_{i} (1 \le |w_{i}| \le 15) wi(1wi15) and an integer f ( w i ) ( 1 0 9 f ( w i ) 1 0 9 ) f(w_{i}) (-10^{9} \le f(w_{i}) \le 10^{9}) f(wi)(109f(wi)109) , indicating the -th word and Sayori’s preference to this word. It’s guaranteed that w i w j w_{i} \neq w_{j} wi̸=wj for all i j i \neq j i̸=j .

Output:

For each test case output one line containing an integer H and m strings s 1 , s 2 , , s m s_{1},s_{2},……,s_{m} s1,s2,,sm separated by one space, indicating the maximum possible happiness and the corresponding poem. If there are multiple poems which can achieve the maximum happiness, print the lexicographically smallest one.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

A sequence of m strings a 1 , a 2 , , a m a_{1},a_{2},……,a_{m} a1,a2,,am is lexicographically smaller than another sequence of m strings b 1 , b 2 , , b m b_{1},b_{2},……,b_{m} b1,b2,,bm , if there exists a k ( 1 k m ) k ( 1 \le k \le m ) k(1km) such that a i = b i a_i=b_i ai=bi for all 1 i k 1 \le i \le k 1ik and a k a_k ak is lexicographically smaller than .

A string s 1 = a 1 a 2 a x s_1=a_1a_2……a_x s1=a1a2ax is lexicographically smaller than another string s 1 = b 1 b 2 b x s_1=b_1b_2……b_x s1=b1b2bx, if there exists a k ( 1 k min ( x , y ) ) k ( 1\le k \le \min(x,y)) k(1kmin(x,y)) such that a i = b i a_i=b_i ai=bi for all 1 i k 1 \le i \le k 1ik and a k &lt; b k a_k&lt;b_k ak<bk, or a i = b i a_i=b_i ai=bi for all 1 i min ( x , y ) 1 \le i \le \min(x,y) 1imin(x,y) and x &lt; y x \lt y x<y .

Sample Input:

4
10 8
hello 0
world 0
behind 0
far 1
be 2
spring 10
can 15
comes 20
winter 25
if 200
5 5
collegiate 0
programming -5
zhejiang 10
provincial 5
contest -45
3 2
bcda 1
bcd 1
bbbbb 1
3 2
a 1
aa 1
aaa 1

Sample Output:

2018 if winter comes can spring be far behind
15 zhejiang provincial collegiate programming contest
3 bbbbb bcd
3 a aa

题目连接

直接结构体排序,按照公式计算H,最后输出(行末不能有多余空格)。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2+5;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;

struct word {
    string _word;
    ll like;
};

int t;
int n, m;
ll sum;
word a[maxn];

bool cmp(word a, word b) {
    if (a.like == b.like) {
        return a._word < b._word;
    }
    return a.like > b.like;
}

int main() {
    //fropen("in.txt", "r", stdin);
    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            cin >> a[i]._word >> a[i].like;
        }
        sort(a, a + n, cmp);
        sum = 0;
        for (int i = 0; i < m; ++i) {
            sum += (m - i) * (a[i].like);
        }
        cout << sum;
        for (int i = 0; i < m; ++i) {
            cout << " " << a[i]._word;
        }
        cout << endl;
    }
    return 0;
}