-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzad1_ms.cpp
More file actions
50 lines (44 loc) · 921 Bytes
/
Copy pathzad1_ms.cpp
File metadata and controls
50 lines (44 loc) · 921 Bytes
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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// let: minimal element exists
int get_min(queue<int> *sums, const std::vector<int> r, int *i)
{
if (sums->empty() || sums->front() > r[*i]){
int ret = r[*i];
(*i)++;
return ret;
}
int ret = sums->front();
sums->pop();
return ret;
}
int robakołak(const std::vector<int> r)
{
queue<int> sums;
// current index in r
int i = 0;
while (!sums.size() > 1 || i < r.size())
{
int a = get_min(&sums, r, &i);
int b = get_min(&sums, r, &i);
if (a == b)
sums.push(a - 1);
else
sums.push(a + b-1);
}
return sums.front();
}
int main()
{
int n;
cin >> n;
vector<int> wilkolaki(n);
for (int i = 0; i < n; i++)
{
cin >> wilkolaki[i];
}
int w = robakołak(wilkolaki);
cout << w << endl;
}