#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);


vector<int> ps;
void init(int sup) {
	vector<bool> isp(sup + 1,true);
	for (int i = 2; i <= sup; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j <= sup; j += i) {
			isp[j] = false;
		}
	}
}
ll calc(ll n, ll m) {
	if (m == 0)return n;
	return calc(n, m - 1) + calc(n / ps[m-1], m - 1);
}


void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n) {
		cin >> a[i];
	}
	int ans = 0;
	int pre = 0;
	rep(i, n) {
		//cout << i << " " << ans<<" "<<pre << "\n";
		int t = 1;
		int tmp = 0;
		while (2 * t <= a[i]) {
			t *= 2; tmp++;
		}
		if (t == a[i]) {
			if (pre < tmp) {
				ans += tmp - pre;
			}
			pre = tmp;
		}
		else {
			if (pre < tmp + 1) {
				ans += tmp + 1 - pre;
				pre = tmp + 1;
			}
			else {
				pre = tmp;
			}
		}
	}
	cout << ans << "\n";
}




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	solve();
	stop
		return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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.

Examples

Input

2
3 3


Output

2


Input

4
2 1 2 2


Output

2


Input

1
1


Output

0


Input

10
3 1 4 1 5 9 2 6 5 3


Output

10

### Response

```cpp
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);


vector<int> ps;
void init(int sup) {
	vector<bool> isp(sup + 1,true);
	for (int i = 2; i <= sup; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j <= sup; j += i) {
			isp[j] = false;
		}
	}
}
ll calc(ll n, ll m) {
	if (m == 0)return n;
	return calc(n, m - 1) + calc(n / ps[m-1], m - 1);
}


void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n) {
		cin >> a[i];
	}
	int ans = 0;
	int pre = 0;
	rep(i, n) {
		//cout << i << " " << ans<<" "<<pre << "\n";
		int t = 1;
		int tmp = 0;
		while (2 * t <= a[i]) {
			t *= 2; tmp++;
		}
		if (t == a[i]) {
			if (pre < tmp) {
				ans += tmp - pre;
			}
			pre = tmp;
		}
		else {
			if (pre < tmp + 1) {
				ans += tmp + 1 - pre;
				pre = tmp + 1;
			}
			else {
				pre = tmp;
			}
		}
	}
	cout << ans << "\n";
}




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	solve();
	stop
		return 0;
}

```