#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if (y < x) x = y; } template static void amax(T &x, U y) { if (x < y) x = y; } int main() { int N; while (~scanf("%d", &N)) { vector A(N); for (int i = 0; i < N; ++ i) scanf("%d", &A[i]); vector dpL(N, vi(N, INF)), dpR = dpL; dpL[0][N - 1] = A[0]; dpR[0][N - 1] = A[N - 1]; for (int len = N - 1; len >= 1; -- len) { rer(i, 0, N - len) { int j = i + len - 1; int xL = INF, xR = INF; if (i > 0) { amin(xL, dpL[i - 1][j] + 1); amin(xR, dpL[i - 1][j] + (j - i + 1)); } if (j < N - 1) { amin(xL, dpR[i][j + 1] + (j - i + 1)); amin(xR, dpR[i][j + 1] + 1); } dpL[i][j] = max(A[i], xL); dpR[i][j] = max(A[j], xR); } } int ans = INF; rep(i, N) amin(ans, dpL[i][i]); printf("%d\n", ans); } return 0; }