mod = 10**9+7

class RMQ:

    def __init__(self, N):

        self.N0 = 2**(N-1).bit_length()

        self.INF = float('inf')

        self.data = [self.INF]*(2*self.N0)

    def update(self, k, x):

        k+=self.N0-1

        self.data[k] = x

        while k>=0:

            k = (k-1)//2

            self.data[k] = min(self.data[2*k+1], self.data[2*k+2])

    def query(self, l, r):

        L = l+self.N0; R = r+self.N0

        s = self.INF

        while L<R:

            if R&1:

                R-=1

                s = min(s, self.data[R-1])

            if L&1:

                s = min(s, self.data[L-1])

                L+=1



            L >>= 1; R>>=1

        return s

N = int(eval(input()))

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

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

R = RMQ(N+1)

dp = [0]*N

dp[0] = a[0]

R.update(h[0], -dp[0])

for i in range(1, N):

    m = -R.query(0, h[i])

    if m == -float('inf'):

        m = 0

    dp[i] = m+a[i]

    R.update(h[i], -dp[i])

print((max(dp)))









    













mod = 10**9+7

class SegmentTree:

    def __init__(self, N, id, fun):

        self.N0 = 2**(N-1).bit_length()

        self.id = id

        self.data = [self.id]*(2*self.N0)

        self.fun = fun

    def update(self, k, x):

        k+=self.N0-1

        self.data[k] = x

        while k>=0:

            k = (k-1)//2

            self.data[k] = self.fun(self.data[2*k+1], self.data[2*k+2])

    def query(self, l, r):

        L = l+self.N0; R = r+self.N0

        s = self.id

        while L<R:

            if R&1:

                R-=1

                s = self.fun(s, self.data[R-1])

            if L&1:

                s = self.fun(s, self.data[L-1])

                L+=1

            L >>= 1; R>>=1

        return s

N = int(eval(input()))

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

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

R = SegmentTree(N+1, 0, max)

dp = [0]*N

dp[0] = a[0]

R.update(h[0], dp[0])

for i in range(1, N):

    m = R.query(0, h[i])

    dp[i] = m+a[i]

    R.update(h[i], dp[i])

print((max(dp)))









    













mod = 10**9 + 7


class RMQ:
    def __init__(self, N):
        self.N0 = 2 ** (N - 1).bit_length()
        self.INF = float("inf")
        self.data = [self.INF] * (2 * self.N0)

    def update(self, k, x):
        k += self.N0 - 1
        self.data[k] = x
        while k >= 0:
            k = (k - 1) // 2
            self.data[k] = min(self.data[2 * k + 1], self.data[2 * k + 2])

    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        s = self.INF
        while L < R:
            if R & 1:
                R -= 1
                s = min(s, self.data[R - 1])
            if L & 1:
                s = min(s, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return s


N = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
R = RMQ(N + 1)
dp = [0] * N
dp[0] = a[0]
R.update(h[0], -dp[0])
for i in range(1, N):
    m = -R.query(0, h[i])
    if m == -float("inf"):
        m = 0
    dp[i] = m + a[i]
    R.update(h[i], -dp[i])
print((max(dp)))

mod = 10**9 + 7


class SegmentTree:
    def __init__(self, N, id, fun):
        self.N0 = 2 ** (N - 1).bit_length()
        self.id = id
        self.data = [self.id] * (2 * self.N0)
        self.fun = fun

    def update(self, k, x):
        k += self.N0 - 1
        self.data[k] = x
        while k >= 0:
            k = (k - 1) // 2
            self.data[k] = self.fun(self.data[2 * k + 1], self.data[2 * k + 2])

    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        s = self.id
        while L < R:
            if R & 1:
                R -= 1
                s = self.fun(s, self.data[R - 1])
            if L & 1:
                s = self.fun(s, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return s


N = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
R = SegmentTree(N + 1, 0, max)
dp = [0] * N
dp[0] = a[0]
R.update(h[0], dp[0])
for i in range(1, N):
    m = R.query(0, h[i])
    dp[i] = m + a[i]
    R.update(h[i], dp[i])
print((max(dp)))

-class RMQ: -    def __init__(self, N): +class SegmentTree: +    def __init__(self, N, id, fun): -        self.INF = float("inf") -        self.data = [self.INF] * (2 * self.N0) +        self.id = id +        self.data = [self.id] * (2 * self.N0) +        self.fun = fun -            self.data[k] = min(self.data[2 * k + 1], self.data[2 * k + 2]) +            self.data[k] = self.fun(self.data[2 * k + 1], self.data[2 * k + 2]) -        s = self.INF +        s = self.id -                s = min(s, self.data[R - 1]) +                s = self.fun(s, self.data[R - 1]) -                s = min(s, self.data[L - 1]) +                s = self.fun(s, self.data[L - 1]) -R = RMQ(N + 1) +R = SegmentTree(N + 1, 0, max) -R.update(h[0], -dp[0]) +R.update(h[0], dp[0]) -    m = -R.query(0, h[i]) -    if m == -float("inf"): -        m = 0 +    m = R.query(0, h[i]) -    R.update(h[i], -dp[i]) +    R.update(h[i], dp[i])