-
Notifications
You must be signed in to change notification settings - Fork 0
/
10954 Add all.cpp
executable file
·84 lines (69 loc) · 1.38 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
78
79
80
81
82
83
84
#include <iostream>
#include <cstdio>
#include <queue>
#define reads() freopen("input.txt", "r", stdin)
#define writes() freopen("output.txt", "w", stdout)
using namespace std;
int main()
{
int i, n, l, temp, total;
//reads();
//writes();
while (cin >> n && n)
{
priority_queue < int, vector<int>, greater<int> > q;
total = 0;
while (n--)
{
cin >> temp;
q.push(temp);
}
temp = 0;
while (q.size() > 1)
{
temp = q.top();
q.pop();
temp += q.top();
q.pop();
total += temp;
q.push(temp);
}
cout << total << endl;
}
return 0;
}
/*
I submitted this version. It takes more time than the solution above.
int main()
{
int i, n, l;
vector<int> v;
reads();
writes();
int temp;
while (cin >> n && n)
{
v.clear();
for (i = 0; i < n; i++)
{
cin >> temp;
v.push_back(temp);
}
sort(v.begin(), v.end());
temp = 0;
while (n > 1)
{
l = v[0] + v[1];
temp += l;
v.erase(v.begin());
v.erase(v.begin());
v.push_back(l);
n--;
sort(v.begin(), v.end());
}
cout << temp << endl;
}
return 0;
}
*/
/*