import java.util.*; public class nya { private static final int inf = 987654321; private static final int invalid = -1; private void solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] s = new int[n+1]; for(int i=0; i<=n; i++) { s[i] = sc.nextInt(); } int[][] dp = new int[n+1][s[n]+1]; for(int i=0; i<=n; i++) { for(int j=0; j<=s[i]; j++) { dp[i][j] = inf; } for(int j=s[i]+1; j<=s[n]; j++) { dp[i][j] = invalid; } } for(int i=0; i<=n; i++) { dp[i][0] = i + 1; } for(int j=0; j<=s[0]; j++) { dp[0][j] = j + 1; } for(int i=1; i<=n; i++) { for(int j=1; j<=s[i]; j++) { if(dp[i][j] == invalid) { continue; } if(dp[i-1][j] != invalid) { dp[i][j] = dp[i-1][j] + 1; } for(int k=0; k