-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddall.cpp
More file actions
49 lines (40 loc) · 1.02 KB
/
Copy pathaddall.cpp
File metadata and controls
49 lines (40 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* File: addall.cpp
* Author: shijiex
*
* Created on November 23, 2013, 1:04 AM
*/
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
int main(int argc, char** argv) {
int node =0 ;
while (scanf("%d\n", &node) != EOF){
if(node ==0) break;
priority_queue<int, vector<int>, greater<int> > costTree;
for (int i = 0; i < node; i++) {
int digit = 0;
scanf("%d", &digit);
costTree.push(digit);
}
int cost = 0;
while(!costTree.empty()){
int sum = 0;
int a = costTree.top();
costTree.pop();
if(!costTree.empty()){
int b = costTree.top();
costTree.pop();
sum = a+b;
cost+=sum;
costTree.push(sum);
}else{
sum = a;
}
}
cout<<cost<<endl;
}
return 0;
}