local function lower_bound(ary,key)
    local n=#ary
    if n==0 then
        return 1 
    end
    if key<=ary[1] then
        return 1
    end
    if key>ary[n] then
        return n+1
    end
    local min,max=1,n
    while 1<max-min do
        local mid=math.floor((min+max)/2)
        if key>ary[mid] then
            min=mid
        else
            max=mid
        end
    end
    return max
end

----------

local n=io.read("n")
local h,a={},{}
for i=1,n do
    h[i]=io.read("n")
end
for i=1,n do
    a[i]=io.read("n")
end

local INF=10^20
local hp={}
local ap={}
for i=1,n do
    hp[i]=INF
    ap[i]=0
end

for i=1,n do
    local H,A=h[i],a[i]
    local k=lower_bound(hp,H)
    hp[k]=H
    ap[k]=math.max(ap[k],A)
end

local total=0
for i=1,n do
    total=total+ap[i]
end
print(total)