forked from IElgohary/Uva-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10954 - Add All.cpp
77 lines (66 loc) · 1.22 KB
/
10954 - Add All.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*input
3
1 2 3
4
1 2 3 4
0
*/
#include <stdio.h>
#include <cmath>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
#define FOR(a , b) for ( int i = a ; i < b ; i++ )
#define FOR2(a , b) for ( int j = a ; j < b ; j++ )
#define sz() size()
#define printArr(array,n) for(int i = 0 ; i < n ; i++) cout << array[i]<<" ";
typedef long long ll;
typedef pair<int,int> ii;
int main()
{
int n;
ios::sync_with_stdio(false);
cin >> n;
int sum[5000];
priority_queue<int,vector<int>,greater<int> > que;
while(n != 0)
{
FOR(0,n)
{
int tmp;
cin >> tmp;
que.push(tmp);
}
int i = 0;
while (que.size() > 1)
{
int a , b;
a = que.top();
que.pop();
b = que.top();
que.pop();
que.push(a + b);
sum[i++]= a+b;
//cout << "nums: "<< a << ' '<< b << " sum: "<< a+b <<endl;
}
int res = 0;
FOR2(0, i)
res += sum[j];
cout << res << endl;
que.pop();
cin >> n;
}
}