import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import log,gcd input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) INF = 10**18 N = int(input()) X = li() A = li() M = max(A) assert M <= 200 if N==1: if A[0]==1: print(0) else: print(-1) exit() dp = [[INF]*(M+3) for p in range(3)] dp[0][A[0]] = (X[1]-X[0]) * (2 * A[0] - 2) dp[1][A[0]] = (X[1]-X[0]) * (2 * A[0] - 1) dp[2][A[0]] = (X[1]-X[0]) * (2 * A[0]) for i in range(1,N-1): ndp = [[INF]*(M+3) for p in range(3)] for j in range(1,M+3): #端におかない if j!=1: for k in range(-2*min(A[i],j-1),1): if A[i]+j+k < M+3: ndp[0][A[i]+j+k] = min(ndp[0][A[i]+j+k],dp[0][j]+(X[i+1]-X[i])*(2*(A[i]+j+k)-2)) ndp[1][A[i]+j+k] = min(ndp[1][A[i]+j+k],dp[1][j]+(X[i+1]-X[i])*(2*(A[i]+j+k)-1)) ndp[2][A[i]+j+k] = min(ndp[2][A[i]+j+k],dp[2][j]+(X[i+1]-X[i])*(2*(A[i]+j+k))) #片端におく for k in range(-(2*min(A[i],j)-1),1): if A[i]+j+k < M+3: for p in range(2): ndp[p][A[i]+j+k] = min(ndp[p][A[i]+j+k],dp[1][j]+(X[i+1]-X[i])*(2*(A[i]+j+k)-2+p)) for p in range(1,3): ndp[p][A[i]+j+k] = min(ndp[p][A[i]+j+k],dp[2][j]+(X[i+1]-X[i])*(2*(A[i]+j+k)-2+p)) #p2,両端におく if 2 <= A[i]: for k in range(-(2*min(A[i],j+1)-2),-1): if A[i]+j+k < M+3: for p in range(3): ndp[p][A[i]+j+k] = min(ndp[p][A[i]+j+k],dp[2][j]+(X[i+1]-X[i])*(2*(A[i]+j+k)-2+p)) dp = ndp res = 10**17 for p in range(3): if p <= A[-1]: res = min(res,dp[p][A[-1]-p+1]) if res==10**17: print(-1) else: print(res)