-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_all.cpp
More file actions
74 lines (72 loc) · 1.7 KB
/
add_all.cpp
File metadata and controls
74 lines (72 loc) · 1.7 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
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
#include <iostream>
#include <queue>
#include <functional>
#include <vector>
/*
int main()
{
while (true)
{
int m;
std::cin >> m;
if (m == 0)
{
break; // break when we read in input 0
}
// idea: go in, push all to min heap. Loop till size==1: each loop pop twice => get 2 min
// add them together to get y, add y to count, and now push y back. lets say we start with 3+3 =>
// pop empty and get 6=> push 6 back. we are done here but size == 1 => thats the stop condi
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
for (int i = 1; i <= m; i++)
{
int x;
std::cin >> x;
pq.push(x);
}
int n, k, j;
int ans;
while (pq.size() > 1)
{
n = pq.top();
pq.pop();
k = pq.top();
pq.pop();
j = n + k;
ans = ans + j;
pq.push(j);
}
std::cout << ans << std::endl;
}
return 0;
}*/
int main()
{
while (true)
{
int n;
std::cin >> n;
if (n == 0)
{
break;
}
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
for (int i = 1; i <= n; i++)
{
int x;
std::cin >> x;
pq.push(x);
}
long long ans = 0;
while ((int)pq.size() > 1)
{
int top1 = pq.top();
pq.pop();
int top2 = pq.top();
pq.pop();
ans += top1 + top2;
pq.push(top1 + top2);
}
std::cout << ans << std::endl;
}
return 0;
}