Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #include <cstdio>
- #include <cstdlib>
- #include <cctype>
- #include <cmath>
- using namespace std;
- typedef unsigned long long LL;
- #define FOR(k,a,b) for(int k(a); k < (b); ++k)
- #define FORD(k,a,b) for(int k(a-1); k >= (b); --k)
- #define REP(k,a) for(int k=0; k < (a); ++k)
- #define ABS(a) ((a)>0?(a):-(a))
- int main()
- {
- #ifdef HOME
- freopen ("in.txt","r",stdin);
- // freopen ("out.txt","w",stdout);
- #endif
- int T,N,K,A[500],act[500],res;
- scanf("%d",&T);
- REP(testcase,T)
- {
- res=0;
- scanf("%d %d",&N,&K);
- REP(i,N)
- {
- act[i]=0;
- scanf("%d",A+i);
- }
- sort(A,A+N,greater<int>());
- REP(iiii,K)
- {
- int best=1e9,bestidx=0;
- REP(i,N)
- {
- if(act[i]==-1)
- continue;
- int cost=A[i]-act[i];
- REP(j,i)
- if(act[j]!=-1)
- cost+=max(A[i]-act[j],0);
- if(cost<best)
- {
- best=cost;
- bestidx=i;
- }
- }
- res+=best;
- REP(j,bestidx)
- if(act[j]!=-1)
- {
- act[j]=max(act[j],A[bestidx]);
- }
- act[bestidx]=-1;
- }
- printf("%d\n",res);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment