Guest User

Untitled

a guest
Jun 5th, 2019
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN=1000001;
  5. int cnt[MAXN]={};
  6. queue<long> orgQ, addQ;
  7. inline long LeastNumber(long rem=0){
  8.   if(orgQ.empty() or addQ.empty()==0 and addQ.front()<orgQ.front())
  9.     rem=addQ.front(), addQ.pop();
  10.   else
  11.     rem=orgQ.front(), orgQ.pop();
  12.   return rem;
  13. }
  14. static inline int gc() {
  15.   static char buf[1 << 20], *p = buf, *end = buf;
  16.   if (p == end) {
  17.     if ((end = buf + fread(buf, 1, 1 << 20, stdin)) == buf) return EOF;
  18.     p = buf;
  19.   }
  20.   return *p++;
  21. }
  22. template<typename T>
  23. static inline bool gn(T &_){
  24.   register int c = gc(); register T __ = 1; _ = 0;
  25.   while(!isdigit(c) and c!=EOF and c!='-') c = gc();
  26.   if(c == '-') { __ = -1; c = gc(); }
  27.   if(c == EOF) return false;
  28.   while(isdigit(c)) _ = _ * 10 + c - '0', c = gc();
  29.   _ *= __;
  30.   return true;
  31. }
  32. template <typename T, typename ...Args>
  33. static inline bool gn(T &x, Args& ...args){return gn(x) and gn(args...);}
  34. int main(){
  35.   int N, x;
  36.   while(gn(N) and N){
  37.     // init
  38.     memset(cnt,0,sizeof(cnt));
  39.     while(orgQ.empty()==0) orgQ.pop();
  40.     while(addQ.empty()==0) addQ.pop();
  41.     // CountSort
  42.     for(int i=0;i<N;i++)
  43.       gn(x),
  44.       cnt[x]++;
  45.     for(int i=1;i<MAXN;i++)
  46.       for(int j=0;j<cnt[i];j++)
  47.         orgQ.push(i);
  48.     long sum=0, min_one, min_two;
  49.     for(int t=1; t<N; t++){
  50.       min_one=LeastNumber(),
  51.       min_two=LeastNumber(),
  52.       sum+=min_one+min_two;
  53.       addQ.push(min_one+min_two);
  54.     }
  55.     printf("%ld\n",sum);
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment