<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

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

<|response|>
1. Abridged Problem Statement  
You have N separate chains, where the i-th chain has Li links. In one minute you may open exactly one link (taking it out of its chain), use that open link to connect chains, and then close it. What is the minimum number of minutes required to join all N chains into a single continuous line?

2. Key Observations  
- To reduce the number of separate chains from N down to 1, you must perform exactly N–1 connections, each costing one minute.  
- Each connection consumes one original link: opening a link removes it from the chain it belonged to. So the links used as connectors must come from some of the chains, shrinking those chains.  
- The cheapest links to sacrifice are those of the shortest chains. Spending all links of a small chain both supplies connectors and removes that small chain from the set still needing attachment, leaving the large chains intact to be hooked on.  
- This leads to a greedy two-pointer strategy after sorting the lengths ascending.

3. Full Solution Approach  
1. Read N and the lengths L1…LN; sort them in nondecreasing order.  
2. Use two pointers: left = 0 points at the shortest chain we dismantle for links, right = N−1 points at the largest chain still needing attachment. Keep time = 0.  
3. While left < right:  
   - If lengths[left] > 0: open one link from the left chain (lengths[left]--), use it to absorb the right chain (right--), and increment time. If the left chain is now empty, advance left.  
   - Otherwise (the left chain is exhausted), just advance left.  
4. When left meets right, all chains have been connected; output time.  

This uses exactly one link per merge, prioritizing the smallest chains as connector sources, and runs in O(N log N).

4. C++ Implementation  
```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;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    lengths = sorted(map(int, data[1:]))  # chain lengths, ascending

    left, right = 0, n - 1   # left: source chain for links, right: chain to attach
    time = 0

    while left < right:
        if lengths[left] > 0:
            # open one link from the shortest available chain
            lengths[left] -= 1
            # use it to absorb the largest remaining chain
            right -= 1
            time += 1
            # if this source chain is now empty, move to the next
            if lengths[left] == 0:
                left += 1
        else:
            left += 1

    print(time)

if __name__ == "__main__":
    main()
```
