#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;


int main()
{
    int n;
    cin >> n;

    vector<int> a;
    int temp, previous;
    cin >> temp;
    if (n != 1)
        for (int i = 1; i < n; i++)
        {
            previous = temp;
            cin >> temp;
            if (temp != previous)
            {
                a.push_back(previous);
            }
        }
    a.push_back(temp);

    int c = 0;
    while (true)
    {
        /*for (int i = 0; i < a.size(); i++)
            cout << a[i] << " ";
        cout << endl;*/

        vector<int>::iterator pos = max_element(a.begin(), a.end());
        int max_val = *pos;

        (*pos)--;

        if ((pos != a.end() - 1) && (*pos == *(pos + 1)))
        {
            a.erase(pos + 1);
        }
        if ((pos != a.begin()) && (*pos == *(pos - 1)))
        {
            a.erase(pos - 1);
        }

        c++;

        if (max_val == 1)
            break;
    }

    cout << c;
}
