Topic link: http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5761
The meaning of the title: Construct a string so that the happy value is the largest
Code:
#include <iostream> #include <algorithm> #include <string> using namespace std; int T; int n,m; struct node { string str; long long w; }a[1000+100]; bool cmp(node a,node b) { if(a.w==b.w) return a.str<b.str; return a.w>b.w; } intmain () { ios::sync_with_stdio(false); cin>>T; while(T--) { int i; cin>>n>>m; for(i=1;i<=n;i++) { cin>>a[i].str>>a[i].w; } sort(a+1,a+n+1,cmp); long long sum=0; for(int i=1;i<=m;i++) { sum+=(m-i+1)*a[i].w; } cout<<sum<<" "; for(int i=1;i<m;i++) cout<<a[i].str<<" "; cout<<a[m].str<<endl; } return 0; }
English is still too bad, I didn't understand it during the game. . .