class SegTree:

    

    def __init__(self, A, intv, initialize = True):

        self.N = len(A)

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

        self.dep = self.N0.bit_length()

        if initialize:

            self.data = [intv]*(self.N0-1) + A + [intv]*(self.N0 - self.N + 1)

            for i in range(self.N0-2, -1, -1):

                self.data[i] = max(self.data[2*i+1], self.data[2*i+2]) 

        else:

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

    

    def segf(self, a, b):

        return max(a, b)

    

    def update(self, k, x):

        k += self.N0-1

        self.data[k] = x

        for _ in range(self.dep - 1):

            k = (k-1)//2

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

    

    def query(self, l, r):

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

        s = 0

        while L < R:

            if R & 1:

                R -= 1

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

            if L & 1:

                

                s = max(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()))



dp = SegTree([0]*(max(H)+1), 0, False)



for h, a in zip(H, A):

    dp.update(h, a+dp.query(0, h))

print((dp.data[0]))

class Segtree:

    def __init__(self, A, intv, initialize = True, segf = max):

        self.N = len(A)

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

        self.intv = intv

        self.segf = segf

        if initialize:

            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)

            for i in range(self.N0-1, 0, -1):

                self.data[i] = self.segf(self.data[2*i], self.data[2*i]) 

        else:

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

        

    def update(self, k, x):

        k += self.N0

        self.data[k] = x

        while k > 0 :

            k = k >> 1

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

    

    def query(self, l, r):

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

        s = self.intv

        while L < R:

            if R & 1:

                R -= 1

                s = self.segf(s, self.data[R])

            if L & 1:

                s = self.segf(s, self.data[L])

                L += 1

            L >>= 1

            R >>= 1

        return s



N = int(eval(input()))

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

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



dp = Segtree([0]*(max(H)+1), 0, False)



for h, a in zip(H, A):

    dp.update(h, a+dp.query(0, h))

print((dp.data[1]))

class SegTree:
    def __init__(self, A, intv, initialize=True):
        self.N = len(A)
        self.N0 = 2 ** (self.N - 1).bit_length()
        self.dep = self.N0.bit_length()
        if initialize:
            self.data = [intv] * (self.N0 - 1) + A + [intv] * (self.N0 - self.N + 1)
            for i in range(self.N0 - 2, -1, -1):
                self.data[i] = max(self.data[2 * i + 1], self.data[2 * i + 2])
        else:
            self.data = [intv] * (2 * self.N0)

    def segf(self, a, b):
        return max(a, b)

    def update(self, k, x):
        k += self.N0 - 1
        self.data[k] = x
        for _ in range(self.dep - 1):
            k = (k - 1) // 2
            self.data[k] = max(self.data[2 * k + 1], self.data[2 * k + 2])

    def query(self, l, r):
        L, R = l + self.N0, r + self.N0
        s = 0
        while L < R:
            if R & 1:
                R -= 1
                s = max(s, self.data[R - 1])
            if L & 1:
                s = max(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()))
dp = SegTree([0] * (max(H) + 1), 0, False)
for h, a in zip(H, A):
    dp.update(h, a + dp.query(0, h))
print((dp.data[0]))

class Segtree:
    def __init__(self, A, intv, initialize=True, segf=max):
        self.N = len(A)
        self.N0 = 2 ** (self.N - 1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv] * self.N0 + A + [intv] * (self.N0 - self.N)
            for i in range(self.N0 - 1, 0, -1):
                self.data[i] = self.segf(self.data[2 * i], self.data[2 * i])
        else:
            self.data = [intv] * (2 * self.N0)

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

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


N = int(eval(input()))
H = list(map(int, input().split()))
A = list(map(int, input().split()))
dp = Segtree([0] * (max(H) + 1), 0, False)
for h, a in zip(H, A):
    dp.update(h, a + dp.query(0, h))
print((dp.data[1]))

-class SegTree: -    def __init__(self, A, intv, initialize=True): +class Segtree: +    def __init__(self, A, intv, initialize=True, segf=max): -        self.dep = self.N0.bit_length() +        self.intv = intv +        self.segf = segf -            self.data = [intv] * (self.N0 - 1) + A + [intv] * (self.N0 - self.N + 1) -            for i in range(self.N0 - 2, -1, -1): -                self.data[i] = max(self.data[2 * i + 1], self.data[2 * i + 2]) +            self.data = [intv] * self.N0 + A + [intv] * (self.N0 - self.N) +            for i in range(self.N0 - 1, 0, -1): +                self.data[i] = self.segf(self.data[2 * i], self.data[2 * i]) -    def segf(self, a, b): -        return max(a, b) - -        k += self.N0 - 1 +        k += self.N0 -        for _ in range(self.dep - 1): -            k = (k - 1) // 2 -            self.data[k] = max(self.data[2 * k + 1], self.data[2 * k + 2]) +        while k > 0: +            k = k >> 1 +            self.data[k] = self.segf(self.data[2 * k], self.data[2 * k + 1]) -        s = 0 +        s = self.intv -                s = max(s, self.data[R - 1]) +                s = self.segf(s, self.data[R]) -                s = max(s, self.data[L - 1]) +                s = self.segf(s, self.data[L]) -dp = SegTree([0] * (max(H) + 1), 0, False) +dp = Segtree([0] * (max(H) + 1), 0, False) -print((dp.data[0])) +print((dp.data[1]))