Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用优先队列妥妥的。代码附上#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <vector> #define ll long long using namespace std; int main() { int n,i,a; priority_queue<int, vector<int>, greater<int> > q; while(scanf("%d",&n)!=EOF) { while(!q.empty()) q.pop(); if(n==1) { scanf("%d",&a); printf("0\n"); } else { for(i=0;i<n;i++) { scanf("%d",&a); q.push(a); } ll sum=0; while(1) { int num=q.top(); q.pop(); num+=q.top(); q.pop(); sum+=num; if(!q.empty()) q.push(num); else break; } printf("%lld\n",sum); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator