Problem Type : Priority_queue
#include<iostream>
#include<cstdio>
#include<queue>
#define MAX 1000
using namespace std;
priority_queue<int,vector<int>,greater<int> > Q;
int main()
{
int test,i,arr[MAX],num,m,n,sum,c;
while(scanf("%d",&test)&&test)
{
while(!Q.empty())
{
Q.pop();
}
for(i=0;i<test;i++)
{
scanf("%d",&num);
Q.push(num);
}
sum=0;
while(!Q.empty())
{
m=Q.top();
Q.pop();
n= Q.top();
Q.pop();
c=m+n;
sum+=c;
Q.push(c);
if(Q.size()==1)
{
break;
}
}
printf("%d\n",sum);
}
}
#include<iostream>
#include<cstdio>
#include<queue>
#define MAX 1000
using namespace std;
priority_queue<int,vector<int>,greater<int> > Q;
int main()
{
int test,i,arr[MAX],num,m,n,sum,c;
while(scanf("%d",&test)&&test)
{
while(!Q.empty())
{
Q.pop();
}
for(i=0;i<test;i++)
{
scanf("%d",&num);
Q.push(num);
}
sum=0;
while(!Q.empty())
{
m=Q.top();
Q.pop();
n= Q.top();
Q.pop();
c=m+n;
sum+=c;
Q.push(c);
if(Q.size()==1)
{
break;
}
}
printf("%d\n",sum);
}
}
No comments:
Post a Comment