#include #include #include using namespace std; void chmin(long long &x, long long y) { x = min(x, y); } int main() { int n; cin >> n; vector a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } static long long dp[2020][2020][2]; const long long inf = 1e18; fill_n(**dp, 2020 * 2020 * 2, inf); dp[2][n][0] = a[1]; dp[1][n - 1][1] = a[n]; for (int d = n - 2; d >= 0; d--) { for (int l = 1; l + d <= n; l++) { int r = l + d; chmin(dp[l + 1][r][0], max(l - (l - 1), a[l] - dp[l][r][0]) + dp[l][r][0]); chmin(dp[l][r - 1][1], max(r - (l - 1), a[r] - dp[l][r][0]) + dp[l][r][0]); chmin(dp[l + 1][r][0], max((r + 1) - l, a[l] - dp[l][r][1]) + dp[l][r][1]); chmin(dp[l][r - 1][1], max((r + 1) - r, a[r] - dp[l][r][1]) + dp[l][r][1]); } } long long ans = 1e18; for (int i = 1; i <= n + 1; i++) { chmin(ans, dp[i][i - 1][0]); chmin(ans, dp[i][i - 1][1]); } cout << ans << endl; }