vijos P1097 合并果子

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<queue>
using namespace std;

int n,s;

struct cmp
{
   bool operator ()(int x, int y)
    {
        return x > y;
    }
};
priority_queue<int,vector<int>,cmp>q;

void fun()
{
    while(!q.empty())
        q.pop();
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&s);
            q.push(s);
        }
        int ans=0;
        while(q.size()!=1)
        {
            int t=q.top();
            q.pop();
            t+=q.top();
            q.pop();
            q.push(t);
            ans+=t;
            //printf("%d\n",t);
        }
        printf("%d\n",ans);
        fun();
    }
	return 0;
}

你可能感兴趣的:(vijos P1097 合并果子)