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

stack = []
min_cost = 0

for i in range(n - 1, -1, -1):
    current_column = (heights[i], costs[i])
    while stack and stack[-1][0] < current_column[0]:
        popped_column = stack.pop()
        min_cost += popped_column[1]
    if stack:
        min_cost += current_column[1]
    else:
        min_cost = current_column[1]
    stack.append(current_column)

print(min_cost)
