Problem:
Score : 1600 points

Problem Statement

There are A slimes lining up in a row.
Initially, the sizes of the slimes are all 1.

Snuke can repeatedly perform the following operation.

Choose a positive even number M. Then, select M consecutive slimes and form M / 2 pairs from those slimes as follows: pair the 1-st and 2-nd of them from the left, the 3-rd and 4-th of them, ..., the (M-1)-th and M-th of them. Combine each pair of slimes into one larger slime. Here, the size of a combined slime is the sum of the individual slimes before combination. The order of the M / 2 combined slimes remain the same as the M / 2 pairs of slimes before combination.

Snuke wants to get to the situation where there are exactly N slimes, and the size of the i-th (1 ≤ i ≤ N) slime from the left is a_i.
Find the minimum number of operations required to achieve his goal.

Note that A is not directly given as input. Assume A = a_1 + a_2 + ... + a_N.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve Snuke's goal.

Sample Input 1

2
3 3

Sample Output 1

2

One way to achieve Snuke's goal is as follows.
Here, the selected slimes are marked in bold.

(1, 1, 1, 1, 1, 1) → (1, 2, 2, 1)

(1, 2, 2, 1) → (3, 3)

Sample Input 2

4
2 1 2 2

Sample Output 2

2

One way to achieve Snuke's goal is as follows.

(1, 1, 1, 1, 1, 1, 1) → (2, 1, 1, 1, 1, 1)

(2, 1, 1, 1, 1, 1) → (2, 1, 2, 2)

Sample Input 3

1
1

Sample Output 3

0

Sample Input 4

10
3 1 4 1 5 9 2 6 5 3

Sample Output 4

10

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
using namespace std::chrono;

#define rep(i,a,b) for(int i = a;i<(b);++i)
#define trav(a,v) for(auto& a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;

const ll big = 1000000007ll;
const ll big2 = 1000000009ll;
ll n,m,k,q,w,h;

vector<vector<ll> > C(100001, vector<ll>());

vector<ll> P;

vector<ll> N0;
vector<ll> N1;

ll f(ll i, ll j){
    if(i == 1)return 0;
    if(i%2 == 0){
        C[j].push_back(0);
        return 1ll+f(i/2,j);
    }
    C[j].push_back(1);
    return 1ll+f(i-1,j);
}

ll DP[100001][101] = {0};

ll dp(ll i, ll w){

    ll sum = N0[i]+w;

    //cout << i << " " << w << "\n";

    if(i == n-1)return sum;
    if(DP[i][w] != -1)return DP[i][w];


    ll ans = big*big;

    for(ll c1 = max(0ll, N1[i+1]-sum); c1 <= N1[i+1]; c1++){
        ans = min(ans, N1[i+1]-c1 +  max(0ll, (sum-N1[i+1]+c1) - (N1[i+1]-c1+N0[i+1]) ) + dp(i+1,c1));
    }

    DP[i][w] = ans;

    //cout << i << " " << w << "   -   " << ans << "\n";

    return ans;

}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("autput.txt","w",stdout);
    ll a,b,c,d;


    cin >> n;
   // n = 100000;
    ll ans = 0;
    ll prev = 0;
    ll ma = 0;
    for(ll c1 = 0; c1 < n; c1++){
        cin >> a;
        //a = 3;
        ma = max(ma,f(a,c1));
        P.push_back(0);
        N0.push_back(0);
        N1.push_back(0);
        for(ll c2 = 0; c2 < sz(C[c1]); c2++){

            if(C[c1][c2] == 0){
                N0[c1]++;
            }else{
                N1[c1]++;
            }

        }

    }


    for(ll c1 = 0; c1 < n; c1++){
        for(ll c2 = 0; c2 < ma; c2++){
            DP[c1][c2] = -1;
        }
    }

/*
    for(ll c1 = 0; c1 < 1; c1++){
       for(ll c2 = 0; c2 < sz(C[c1]); c2++){
        cout << C[c1][c2] << " ";

       }
       cout << "\n";

    }
    */
/*
    for(ll c1 = 0; c1 < ma; c1++){

        bool have = 0;
        for(ll c2 = 0; c2 < n; c2++){

            if(P[c2] >= C[c2].size()){
                ans += have;
                have = 0;
            }
            else{

                if(have == 0){
                    have = 1;
                }
                else{
                    if(C[c2][P[c2]] == 1){
                        have = 0;
                        ans++;
                    }
                }
                P[c2]++;
            }

        }
        ans += have;

    }
*/

/*
    for(ll c1 = 0; c1 < n; c1++){

        if(c1 == n-1){
            ans += N0[c1]+N1[c1];

        }else{


            ll n0 = N0[c1+1];

            if(N1[c1] <= n0){
                n0 -= N1[c1];
                N1[c1] = 0;
            }
            else{
                N1[c1] -= n0;
               n0 = 0;
            }

            if(N0[c1] <= n0){
               n0 -= N0[c1];
                N0[c1] = 0;
            }
            else{
                N0[c1] -= n0;
               n0 = 0;
            }

            N1[c1+1] = max(0ll, N1[c1+1] - N0[c1] - N1[c1]);

            ans += N0[c1]+N1[c1];

        }

    }
*/

    ans = dp(0,N1[0]);
    cout << ans << "\n";


    return 0;
}