import sys import pypyjit pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(10**8) input = sys.stdin.readline N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) dp = [[[0, 0] for _ in range(N)] for _ in range(N)] for i in range(N): dp[i][i][0] = A[i] for i in range(N): for j in range(i+1, N): dp[i][j][0] = dp[i][j-1][0] ^ A[j] dp[i][j][1] += dp[i][j-1][1] + X[j]-X[j-1] #print(dp) ans = sum(A) INF = 10**18 dp2 = [INF for _ in range(N)] stack = [(0, 0)] while stack: i, sco = stack.pop() for j in range(i, N): if sco+dp[i][j][0]+dp[i][j][1] < dp2[j]: dp2[j] = sco+dp[i][j][0]+dp[i][j][1] stack.append((j+1, dp2[j])) print(dp2[-1])