iscsi

Ciel and Eggs

Dec 3rd, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cctype>
  12. #include <cmath>
  13. using namespace std;
  14. typedef unsigned long long LL;
  15. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  16. #define FORD(k,a,b) for(int k(a-1); k >= (b); --k)
  17. #define REP(k,a) for(int k=0; k < (a); ++k)
  18. #define ABS(a) ((a)>0?(a):-(a))
  19.  
  20. int main()
  21. {
  22. #ifdef HOME
  23.     freopen ("in.txt","r",stdin);
  24. //  freopen ("out.txt","w",stdout);
  25. #endif
  26.     int T,N,K,A[500],act[500],res;
  27.     scanf("%d",&T);
  28.     REP(testcase,T)
  29.     {
  30.         res=0;
  31.         scanf("%d %d",&N,&K);
  32.         REP(i,N)
  33.         {
  34.             act[i]=0;
  35.             scanf("%d",A+i);
  36.         }
  37.         sort(A,A+N,greater<int>());
  38.         REP(iiii,K)
  39.         {
  40.             int best=1e9,bestidx=0;
  41.             REP(i,N)
  42.             {
  43.                 if(act[i]==-1)
  44.                     continue;
  45.                 int cost=A[i]-act[i];
  46.                 REP(j,i)
  47.                     if(act[j]!=-1)
  48.                         cost+=max(A[i]-act[j],0);
  49.                 if(cost<best)
  50.                 {
  51.                     best=cost;
  52.                     bestidx=i;
  53.                 }
  54.             }
  55.             res+=best;
  56.             REP(j,bestidx)
  57.             if(act[j]!=-1)
  58.             {
  59.                 act[j]=max(act[j],A[bestidx]);
  60.             }
  61.             act[bestidx]=-1;
  62.         }
  63.         printf("%d\n",res);
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment