import heapq import sys 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) print(ans) exit() INF = 10**18 dp2 = [INF for _ in range(N)] stack = [(0, 0)] while stack: sco, i = heapq.heappop(stack) 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] heapq.heappush(stack, (dp2[j], j+1)) print(dp2[-1])