import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; void main() { try { for (; ; ) { const N = readInt; auto X = new long[N]; foreach (i; 0 .. N) { X[i] = readLong; } auto A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt; } if (N == 1) { writeln((A[0] == 1) ? 0 : -1); continue; } const maxA = A.maxElement; const lim = 2 * maxA + 5; auto crt = new long[][](3, lim); foreach (s; 0 .. 3) { crt[s][] = INF; } // i = 0 foreach (s; 0 .. 3) { for (int k = s; k < lim; k += 2) if (s + (k - s) / 2 == A[0]) { crt[s][k] = k * (X[1] - X[0]); } } debug { if (lim <= 20) { foreach (s; 0 .. 3) { // writefln("%s %s: %s", 0, s, crt[s]); } } } int segN = 1; for (; segN < lim; segN <<= 1) {} auto seg = new long[][][](3, 3, segN << 1); foreach (s; 0 .. 3) foreach (t; 1 .. 3) { seg[s][t][] = INF; } foreach (i; 1 .. N - 1) { foreach (s; 0 .. 3) foreach (t; 1 .. 3) { for (int k = t; k < lim; k += 2) { seg[s][t][segN + k] = crt[s][k]; } foreach_reverse (u; 1 .. segN) { seg[s][t][u] = min(seg[s][t][u << 1], seg[s][t][u << 1 | 1]); } } // a <= k <= b long rangeMin(int s, int t, int a, int b) { chmax(a, 0); chmin(++b, lim); long ret = INF; for (a += segN, b += segN; a < b; a >>= 1, b >>= 1) { if (a & 1) chmin(ret, seg[s][t][a++]); if (b & 1) chmin(ret, seg[s][t][--b]); } return ret; } auto nxt = new long[][](3, lim); foreach (s; 0 .. 3) { nxt[s][] = INF; } // foreach (s; 0 .. 3) { /* foreach (k; 1 .. lim) { const t = (k & 1) ? 1 : 2; for (int l = t; l < lim; l += 2) { if (abs(l - k) / 2 <= A[i] && A[i] <= t + (k - t) / 2 + (l - t) / 2) { chmin(nxt[s][l], crt[s][k]); } } } */ foreach (l; 1 .. lim) { const t = (l & 1) ? 1 : 2; chmin(nxt[s][l], rangeMin(s, t, max(l - 2 * A[i], 2 * A[i] - l), l + 2 * A[i])); } } // +L foreach (s; 0 .. 2) { /* foreach (k; 2 .. lim) { const t = ((k-1) & 1) ? 1 : 2; for (int l = t; l < lim; l += 2) { if (1 + abs(l - (k-1)) / 2 <= A[i] && A[i] <= 1 + t + ((k-1) - t) / 2 + (l - t) / 2) { chmin(nxt[s + 1][l], crt[s][k]); } } } //*/ //* foreach (l; 1 .. lim) { const t = (l & 1) ? 1 : 2; chmin(nxt[s + 1][l], rangeMin(s, t, max(1, l - 2 * (A[i] - 1), 2 * (A[i] - 1) - l) + 1, l + 2 * (A[i] - 1) + 1)); } //*/ } // +R foreach (s; 0 .. 2) { /* foreach (k; 1 .. lim) { const t = (k & 1) ? 1 : 2; for (int l = 1 + t; l < lim; l += 2) { if (1 + abs((l-1) - k) / 2 <= A[i] && A[i] <= 1 + t + (k - t) / 2 + ((l-1) - t) / 2) { chmin(nxt[s + 1][l], crt[s][k]); } } } //*/ //* foreach (l; 1 .. lim) { const t = ((l-1) & 1) ? 1 : 2; chmin(nxt[s + 1][l], rangeMin(s, t, max((l-1) - 2 * (A[i] - 1), 2 * (A[i] - 1) - (l-1)), (l-1) + 2 * (A[i] - 1))); } //*/ } // +L, +R { const s = 0; if (maxA <= 100) { //* foreach (k; 1 .. lim) { const t = ((k-1) & 1) ? 1 : 2; for (int l = 1 + t; l < lim; l += 2) { if (2 + abs((l-1) - (k-1)) / 2 <= A[i] && A[i] <= 2 + t + ((k-1) - t) / 2 + ((l-1) - t) / 2) { chmin(nxt[s + 2][l], crt[s][k]); } } } //*/ } else { //* foreach (l; 1 .. lim) { const t = ((l-1) & 1) ? 1 : 2; chmin(nxt[s + 2][l], rangeMin(s, t, max((l-1) - 2 * (A[i] - 2), 2 * (A[i] - 2) - (l-1)) + 1, (l-1) + 2 * (A[i] - 2) + 1)); } //*/ } } crt = nxt; foreach (s; 0 .. 3) foreach (k; 1 .. lim) { crt[s][k] += k * (X[i + 1] - X[i]); } debug { if (lim <= 20) { foreach (s; 0 .. 3) { // writefln("%s %s: %s", i, s, crt[s]); } } } } // i = N - 1 long ans = INF; foreach (s; 0 .. 3) { for (int k = (2-s); k < lim; k += 2) if ((2-s) + (k - (2-s)) / 2 == A[N - 1]) { chmin(ans, crt[s][k]); debug { if (lim <= 20) { // writefln("%s %s %s: %s", N - 1, s, k, crt[s][k]); } } } } writeln((ans >= INF) ? -1 : ans); debug { if (A.sum <= 10) { int[] ps; foreach (i; 0 .. N) { foreach (_; 0 .. A[i]) { ps ~= i; } } long brt = INF; int[] psm; do { bool ok = true; long cost; foreach (j; 0 .. cast(int)(ps.length) - 1) { ok = ok && (ps[j] != ps[j + 1]); cost += abs(X[ps[j + 1]] - X[ps[j]]); } if (ok) { if (chmin(brt, cost)) { psm = ps.dup; } } } while (ps.nextPermutation); writeln("brt = ", brt, "; ", psm); assert(brt == ans); } } } } catch (EOFException e) { } }