n = int(input())
heights = list(map(int, input().split()))
costs = list(map(int, input().split()))
left_boundaries = [0] * n
right_boundaries = [0] * n
current_left = 0
for i in range(n):
    while current_left > 0 and heights[i] > heights[current_left - 1]:
        current_left = left_boundaries[current_left - 1]
    left_boundaries[i] = current_left
    current_left = i + 1
current_right = n - 1
for i in range(n - 1, -1, -1):
    while current_right < n - 1 and heights[i] > heights[current_right + 1]:
        current_right = right_boundaries[current_right + 1]
    right_boundaries[i] = current_right
    current_right = i - 1
total_cost = 0
for i in range(n):
    total_cost += min(costs[i], costs[left_boundaries[i]], costs[right_boundaries[i]])
print(total_cost)