Analysis: Use priority queues.
Take next as priority, small first-out team
Sort after reading the data, initialize the first element of the queue (0,a[0],0)
Each time out of the team an element, the queue (sum,sum+a[nextid+1],nextid+1), (next,next+a[nextid+1],nextid+1), that is, whether add a[nextid+1] are considered in.
In this way each new element is the next smallest (next), and the M-time is small.
#include <iostream> #include <queue> #include <algorithm>using namespace std;struct a{a () {}a (int s), int n,int ni): sum (s), Next (N), NextID (NI) {}bool operator< (const a& A) Const{return This->next>a.next;} int sum; Current and int next; Add the next element and int nextid; The next element id};int A[10010];int n,m;int Run () {priority_queue<a> q; A Tmp;int Cnt;q.push (A (0,a[0],0)); Cnt=0;while (!q.empty ()) {tmp=q.top (); Q.pop (); if (tmp.nextid>=n) //out of range, A[n], set the end value Continue;q.push (A (tmp.sum,tmp.sum+a[tmp.nextid+1],tmp.nextid+1)); Q.push (A (tmp.next,tmp.next+a[ tmp.nextid+1],tmp.nextid+1)); Cnt++;if (cnt==m) return tmp.next;}} int main () {int t,i,c;scanf ("%d", &t), C=0;while (t--) {scanf ("%d%d", &n,&m), for (i=0;i<n;i++) scanf ("%d" , &a[i]); sort (a,a+n); a[n]=0;printf ("Case #%d:%d\n", ++c,run ());} return 0;}
HDU ACM 4546 Race Difficulty-priority queue