p186.ans1
======================
1

=================
p186.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<int> lengths;

void read() {
    cin >> n;
    lengths.resize(n);
    cin >> lengths;
}

void solve() {
    // To join all chains into one line we need N - 1 connections, and each
    // connection costs one minute of opening a link. The cheapest links to
    // sacrifice are those of the shortest chains: opening every link of a small
    // chain both spends those links as connectors and removes that chain from
    // the list to be attached. Sort the chains ascending and use two pointers:
    // left points at the chain we are dismantling for links, right at the
    // current largest chain still needing attachment. Each minute we open one
    // link from the left chain (decrementing its remaining length) and use it
    // to absorb the right chain (right--, time++); when the left chain is
    // exhausted we advance left. We stop once left meets right, i.e. all
    // chains are connected.

    sort(lengths.begin(), lengths.end());

    int left = 0, right = n - 1, time = 0;
    while(left < right) {
        if(lengths[left] > 0) {
            lengths[left]--;
            right--;
            time++;
            if(lengths[left] == 0) {
                left++;
            }
        } else {
            left++;
        }
    }

    cout << time << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p186.in1
======================
2
3 4

=================
p186.py
======================
def minimum_time_to_connect_chains():
    import sys

    input = sys.stdin.read
    data = input().split()

    n = int(data[0])
    lengths = list(map(int, data[1:]))
    lengths.sort()

    left, right = 0, n - 1
    time = 0

    while left < right:
        if lengths[left] > 0:
            lengths[left] -= 1
            right -= 1
            time += 1
            if lengths[left] == 0:
                left += 1
        else:
            left += 1

    print(time)


if __name__ == "__main__":
    minimum_time_to_connect_chains()

=================
statement.txt
======================
186. The Chain
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



Smith has N chains. Each chain is the sequence of successively connected links. The length of each chain is known: the first chain contains L1 links, the second - L2, ..., the last one - LN.
He can make a following series of actions in a minute:
1. to unchain one link
2. to remove or to put into the unchained link some other links of any chain
3. to chain the link
Your task is to determine the minimum time which will take the smith to connect all the chains in one line, i.e. the chain will look like a chain made up of successively connected links.

Input
The first line contains natural number N<=100. The second line contains L1, L2, ..., LN (1<=Li<=100, for all i = 1..N).

Output
Output the only integer number - the solution to the problem.

Sample test(s)

Input
2
3 4

Output
1
Author:	Michael R. Mirzayanov
Resource:	ACM International Collegiate Programming Contest 2003-2004
North-Eastern European Region, Southern Subregion
Date:	2003 October, 9





=================
