import heapq import sys input = sys.stdin.readline N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) dp1 = [[0 for _ in range(N)] for _ in range(N)] dp2 = [[0 for _ in range(N)] for _ in range(N)] for i in range(N): dp1[i][i] = A[i] for i in range(N): for j in range(i+1, N): dp1[i][j] = dp1[i][j-1] ^ A[j] dp2[i][j] = dp2[i][j-1] + X[j]-X[j-1] #print(dp) INF = sum(A) dp3 = [INF for _ in range(N)] stack = [(0, 0)] while stack: sco, i = heapq.heappop(stack) for j in range(i, N): if sco+dp1[i][j]+dp2[i][j] < dp3[j]: dp3[j] = sco+dp1[i][j]+dp2[i][j] heapq.heappush(stack, (dp3[j], j+1)) print(dp3[-1])