n = int(input())
heights = list(map(int, input().split()))
costs = list(map(int, input().split()))

dp = [float('inf')] * n
leftMax = [0] * n
rightMax = [0] * n

# Заполняем массивы leftMax и rightMax
leftMax[0] = heights[0]
rightMax[n-1] = heights[n-1]
for i in range(1, n):
    leftMax[i] = max(leftMax[i-1], heights[i])
for i in range(n-2, -1, -1):
    rightMax[i] = max(rightMax[i+1], heights[i])

dp[0] = costs[0]

for i in range(1, n):
    for j in range(i, -1, -1):
        if heights[i] == leftMax[i] or heights[j] == rightMax[j]:
            dp[i] = min(dp[i], dp[j-1] + costs[i])

print(dp[n-1])
