#include using namespace std; typedef long long int64; inline void chmin(int64 &a, int64 b) { a = min(a, b); } inline void chmax(int64 &a, int64 b) { a = max(a, b); } const int64 INF = 1LL << 55; int N, A[2003]; int64 dp[2003][2003][2]; int main() { cin >> N; assert(1 <= N && N <= 2000); for(int i = 1; i <= N; i++) { cin >> A[i]; assert(0 <= A[i] && A[i] <= 1000000000); } fill_n(**dp, 2003 * 2003 * 2, INF); dp[2][N][0] = A[1]; dp[1][N - 1][1] = A[N]; for(int i = N - 2; i >= 0; i--) { for(int j = 1; j + i <= N; j++) { const int L = j, R = j + i; chmin(dp[L + 1][R][0], dp[L][R][0] + 1); chmin(dp[L + 1][R][0], dp[L][R][1] + i + 1); chmax(dp[L + 1][R][0], A[L]); chmin(dp[L][R - 1][1], dp[L][R][0] + i + 1); chmin(dp[L][R - 1][1], dp[L][R][1] + 1); chmax(dp[L][R - 1][1], A[R]); } } int64 ret = INF; for(int i = 1; i <= N + 1; i++) { chmin(ret, dp[i][i - 1][0]); chmin(ret, dp[i][i - 1][1]); } cout << ret << endl; }