#pragma GCC target("avx2") #pragma GCC optimize("O3,unroll-loops") #include #include const long long sup = 1LL << 60; void chmin(long long* a, long long b) { if (*a > b) *a = b; } long long naive(int N, int X[], int A[]) { if (N == 1) { if (A[1] == 1) return 0; else return -1; } int i, D[201], A_max = 0; for (i = 2; i <= N; i++) D[i] = X[i] - X[i-1]; for (i = 1; i <= N; i++) if (A_max < A[i]) A_max = A[i]; A_max *= 2; int j, jj, k, kk, kkk, cur, prev, L, R; long long dp[2][3][10001], tmp; for (i = 0; i <= 1; i++) for (j = 0; j <= 2; j++) for (k = 0; k <= A_max; k++) dp[i][j][k] = sup; dp[0][0][A[1]*2] = 0; dp[0][1][A[1]*2-1] = 0; dp[0][2][A[1]*2-2] = 0; for (i = 2, cur = 1, prev = 0; i <= N; i++, cur ^= 1, prev ^= 1) { for (j = 0; j <= 2; j++) { for (k = 0; k <= A_max; k++) { if (dp[prev][j][k] == sup) continue; tmp = dp[prev][j][k] + (long long)k * D[i]; dp[prev][j][k] = sup; if (k == 0) continue; for (jj = 0; jj <= 2 - j; jj++) { kk = A[i] * 2 - jj; kkk = 0; R = k + kk - kkk; if (R > A_max) R = A_max; kkk = A[i] * 2 - jj; if (kkk > k) kkk = k; kk = A[i] * 2 - jj - kkk; L = k + kk - kkk; if (L < 0) L = abs(L) % 2; for (kk = L; kk <= R; kk += 2) chmin(&(dp[cur][j+jj][kk]), tmp); } } } } if (dp[prev][2][0] == sup) return -1; else return dp[prev][2][0]; } int main() { int i, N, X[201], A[201], A_max = 0; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(X[i])); for (i = 1; i <= N; i++) scanf("%d", &(A[i])); printf("%lld\n", naive(N, X, A)); fflush(stdout); return 0; }