a=int(input())
counter=0
b=list(map(int,input().split()))
c=list(map(int,input().split()))
high=max(b)
l=True
while l:
    f=b.index(high)
    counter+=c[f]
    b1,b2=b[:f], b[(f+1):]
    c1,c2=c[:f], c[(f+1):]
    if c1==c2:
        counter+=sum(c1)
        l=False
        break

print(counter)
