N = int(eval(input()))

h = list(map(int, input().split()))

a = list(map(int, input().split()))



num = 2

while num <= N+1:

    num *= 2



seg = [0]*(2*num-1)



#update(i,x):Aiをxに更新する

def update(i,x):

    k = i + num - 1

    while k >= 0:

        seg[k] = max(x,seg[k])

        k = (k-1)//2



#query(a,b,0,0,num):[a,b)の最大値

def query(a,b,k,l,r):

    if r <= a or b <= l:

        return -1

    elif a <= l and r <= b:

        return seg[k]

    else:

        return max(query(a,b,2*k+1,l,(l+r)//2),query(a,b,2*k+2,(l+r)//2,r))





for i in range(N):

    update(h[i], query(0,h[i],0,0,num) + a[i])



print((max(seg)))
N = int(eval(input()))

h = list(map(int, input().split()))

a = list(map(int, input().split()))



num = 2**((N+1).bit_length())



seg = [0]*(2*num-1)



#update(i,x):Aiをxに更新する

def update(i,x):

    k = i + num - 1

    while k >= 0:

        seg[k] = max(x,seg[k])

        k = (k-1)//2



#query(a,b,0,0,num):[a,b)の最大値

def query(a,b,k,l,r):

    if r <= a or b <= l:

        return -1

    elif a <= l and r <= b:

        return seg[k]

    else:

        return max(query(a,b,2*k+1,l,(l+r)//2),query(a,b,2*k+2,(l+r)//2,r))





for i in range(N):

    update(h[i], query(0,h[i],0,0,num) + a[i])



print((max(seg)))
N = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
num = 2
while num <= N + 1:
    num *= 2
seg = [0] * (2 * num - 1)
# update(i,x):Aiをxに更新する
def update(i, x):
    k = i + num - 1
    while k >= 0:
        seg[k] = max(x, seg[k])
        k = (k - 1) // 2


# query(a,b,0,0,num):[a,b)の最大値
def query(a, b, k, l, r):
    if r <= a or b <= l:
        return -1
    elif a <= l and r <= b:
        return seg[k]
    else:
        return max(
            query(a, b, 2 * k + 1, l, (l + r) // 2),
            query(a, b, 2 * k + 2, (l + r) // 2, r),
        )


for i in range(N):
    update(h[i], query(0, h[i], 0, 0, num) + a[i])
print((max(seg)))

N = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
num = 2 ** ((N + 1).bit_length())
seg = [0] * (2 * num - 1)
# update(i,x):Aiをxに更新する
def update(i, x):
    k = i + num - 1
    while k >= 0:
        seg[k] = max(x, seg[k])
        k = (k - 1) // 2


# query(a,b,0,0,num):[a,b)の最大値
def query(a, b, k, l, r):
    if r <= a or b <= l:
        return -1
    elif a <= l and r <= b:
        return seg[k]
    else:
        return max(
            query(a, b, 2 * k + 1, l, (l + r) // 2),
            query(a, b, 2 * k + 2, (l + r) // 2, r),
        )


for i in range(N):
    update(h[i], query(0, h[i], 0, 0, num) + a[i])
print((max(seg)))

-num = 2 -while num <= N + 1: -    num *= 2 +num = 2 ** ((N + 1).bit_length())