#ifdef ONLINE_JUDGE
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <list>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define LL long long
#define ULL unsigned long long
#define PI (atan(1) * 4)
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');
#define MAX_LOG 20

int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, 1, 0, -1};

int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

bool AreEqual(double a, double b) {
	return (fabs(a - b) < 1e-10);
}

template <class T>
void ReadNo(T &_value) {
	T _sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		ch == '-' ? _sign = -1 : _sign = 1 ;
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= _sign;
}

template <class T>
void WriteNo(T _value, char _ch = ' ') {
	cout << _value << _ch;
}

int N, MAX_DIM;
LL best;
vector<vector<int>> rmq;
vector<int> biggestPower2;


void prepare_rmq() {
	int i, j, step;
	
	for (i = 2; i <= MAX_DIM; ++i) {
		biggestPower2[i] = biggestPower2[i >> 1] + 1;
	}
	
	for (j = 1; (1 << j) <= N; ++j) {
		for (i = 0; i + (1 << j) - 1 < N; ++i) {
			step = 1 << (j - 1);
			rmq[j][i] = rmq[j - 1][i];
			if (rmq[0][rmq[j - 1][i + step]] > rmq[0][rmq[j][i]]) {
				rmq[j][i] = rmq[j - 1][i + step];
			}
//			rmq[j][i] = max(rmq[j - 1][i], rmq[j - 1][i + step]);
		}
	}
}

int get_max(int L, int R) {
	int lg = R - L + 1, pw;
	pw = biggestPower2[lg];
//	return max(rmq[pw][L], rmq[pw][R - (1 << pw) + 1]);
	if (rmq[0][rmq[pw][L]] > rmq[0][rmq[pw][R - (1 << pw) + 1]]) {
		return rmq[pw][L];
	}
	return rmq[pw][R - (1 << pw) + 1];
}

LL GetBest(int v1, int p1, int v2, int p2) {
	LL ans = (LL) v1 + v2 - floor(log2(p2 - p1));
	return ans;
}

int main(){
	string fileInput = "sum";
#ifdef INFOARENA
	freopen((fileInput + ".in").c_str(), "r", stdin);
	freopen((fileInput + ".out").c_str(), "w", stdout);
#else
#ifndef ONLINE_JUDGE
	freopen("/Users/duxar/Workplace/Xcode Projects/Selectie/Selectie/input", "r", stdin);
#endif
#endif
	
	int i, j, p2, v2;
	ReadNo(N);
	MAX_DIM = N;
	rmq.resize(MAX_LOG, vector <int> (MAX_DIM, 0));
	biggestPower2.resize(MAX_DIM, 0);
	for (i = 0; i < N; ++i) {
		ReadNo(rmq[0][i]);
	}
	prepare_rmq();
	for (i = 0; i < N; ++i) {
		int aux = i - 1;
//		cout << i << '\n';
		for (j = 1; j <= i && aux >= 0; j *= 2) {
//			cout << aux << ' ' << aux + j - 1 << '\n';
			p2 = get_max(aux, aux + j - 1);
			best = max(best, GetBest(rmq[0][p2], p2, rmq[0][i], i));
			aux -= j * 2;
		}
//		j *= 2;
		if (aux + j - 1 < i && aux + j - 1 >= 0) {
			p2 = get_max(0, aux + j - 1);
//			cout << 0 << ' ' << aux + j - 1 << '\n';
			best = max(best, GetBest(rmq[0][p2], p2, rmq[0][i], i));
		}
	}
	WriteNo(best, '\n');
	
	return 0;
}