n = int(input())
heights = list(map(int, input().split()))
costs = list(map(int, input().split()))
left_costs = [0] * n
right_costs = [0] * n
stack = []

# Вычисляем стоимости для установки крыш слева
for i in range(n):
    while stack and heights[stack[-1]] < heights[i]:
        stack.pop()
    if stack:
        left_costs[i] = left_costs[stack[-1]] + costs[i]
    else:
        left_costs[i] = costs[i]
    stack.append(i)

stack = []

# Вычисляем стоимости для установки крыш справа
for i in range(n - 1, -1, -1):
    while stack and heights[stack[-1]] < heights[i]:
        stack.pop()
    if stack:
        right_costs[i] = right_costs[stack[-1]] + costs[i]
    else:
        right_costs[i] = costs[i]
    stack.append(i)

# Вычисляем минимальную общую стоимость
min_total_cost = min(left_costs[n - 1], right_costs[0])
for i in range(n - 1):
    min_total_cost = min(min_total_cost, left_costs[i] + right_costs[i + 1])

print(min_total_cost)
