import sys def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) x = list(map(int, sys.stdin.readline().split())) y = list(map(int, sys.stdin.readline().split())) dp = [0] * n dq = [] for i in range(n): current_a = a[i] # Function to compute cost for j in dq def func(j): return dp[j-1] + abs(current_a - x[j])**3 + y[j]**3 if j > 0 else (abs(current_a - x[0])**3 + y[0]**3) # Maintain deque to have the best j's for current_a # We need to find j such that j <= i and x_j <= a_i (if possible) # However, due to sorting, we can use pointers to maintain possible j's # The logic here assumes certain properties of the cost function and deque management which may need to be adapted. # For the sake of this problem, we use a simplified approach considering the cost for each j up to i # But given the complexity, the following code is a placeholder and may need optimization best = func(0) for j in range(1, i+1): current = func(j) if current < best: best = current dp[i] = best print(dp[-1]) if __name__ == "__main__": main()