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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>

uint32_t add(uint32_t a, uint32_t b) {
    std::vector<uint32_t> toReturn;
    if (a == b)
        return a + 1;
    return std::max(a, b);
}

int main() {
    std::vector<uint32_t> coins;
    std::string a,line;
    std::getline(std::cin,a);
    uint32_t number;
    std::getline(std::cin, line);
    std::istringstream stream(line);
    while (stream >> number)
        coins.push_back(number);




    std::sort(coins.begin(), coins.end());
    while (coins.size() > 1) {
        uint32_t currCoin = coins[0];
        uint32_t nOfCurrCoins = 1;
        while (currCoin == coins[nOfCurrCoins] && coins.size() > nOfCurrCoins)
            nOfCurrCoins++;
        uint32_t n = nOfCurrCoins - nOfCurrCoins / 2;
        for (uint32_t i = 0; i < n; i++) {
            uint32_t coin = add(coins[0], coins[1]);
            coins.insert(coins.begin() + nOfCurrCoins, coin);
            coins.erase(coins.begin());
            coins.erase(coins.begin());
            nOfCurrCoins = nOfCurrCoins - 2;
        }
    }
    std::cout << coins[0];
}