Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MAXN=1000001;
- int cnt[MAXN]={};
- queue<long> orgQ, addQ;
- inline long LeastNumber(long rem=0){
- if(orgQ.empty() or addQ.empty()==0 and addQ.front()<orgQ.front())
- rem=addQ.front(), addQ.pop();
- else
- rem=orgQ.front(), orgQ.pop();
- return rem;
- }
- static inline int gc() {
- static char buf[1 << 20], *p = buf, *end = buf;
- if (p == end) {
- if ((end = buf + fread(buf, 1, 1 << 20, stdin)) == buf) return EOF;
- p = buf;
- }
- return *p++;
- }
- template<typename T>
- static inline bool gn(T &_){
- register int c = gc(); register T __ = 1; _ = 0;
- while(!isdigit(c) and c!=EOF and c!='-') c = gc();
- if(c == '-') { __ = -1; c = gc(); }
- if(c == EOF) return false;
- while(isdigit(c)) _ = _ * 10 + c - '0', c = gc();
- _ *= __;
- return true;
- }
- template <typename T, typename ...Args>
- static inline bool gn(T &x, Args& ...args){return gn(x) and gn(args...);}
- int main(){
- int N, x;
- while(gn(N) and N){
- // init
- memset(cnt,0,sizeof(cnt));
- while(orgQ.empty()==0) orgQ.pop();
- while(addQ.empty()==0) addQ.pop();
- // CountSort
- for(int i=0;i<N;i++)
- gn(x),
- cnt[x]++;
- for(int i=1;i<MAXN;i++)
- for(int j=0;j<cnt[i];j++)
- orgQ.push(i);
- long sum=0, min_one, min_two;
- for(int t=1; t<N; t++){
- min_one=LeastNumber(),
- min_two=LeastNumber(),
- sum+=min_one+min_two;
- addQ.push(min_one+min_two);
- }
- printf("%ld\n",sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment