class SegmentTree():



    def __init__(self, n):

        self.size = 1

        while self.size < n:

            self.size *= 2

        self.node = [0] * (2*self.size - 1)



    def update(self, i, val):

        i += (self.size - 1)

        self.node[i] = val

        while i > 0:

            i = (i - 1) // 2

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



    def get_max(self, begin, end):

        begin += (self.size - 1)

        end += (self.size - 1)

        s = 0

        while begin < end:

            if (end - 1) & 1:

                end -= 1

                s = max(s, self.node[end])

            if (begin - 1) & 1:

                s = max(s, self.node[begin])

                begin += 1

            begin = (begin - 1) // 2

            end = (end - 1) // 2

        return s





n = int(eval(input()))

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

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

st = SegmentTree(n+1)



dp = [0]*(n+1)



for i in range(n):

    #i番目の花を残さないとき

    res1 = st.get_max(0, n+1) 

    #i番目の花を残すとき

    res2 = st.get_max(0, h[i]) + a[i]

    

    dp[i+1] = max(res1, res2)

    st.update(h[i], res2)

print((dp[n]))
class SegmentTree():

    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する

    update: i番目をvalに変更する

    get_min: 区間[begin, end)の最小値を求める

    """

    def __init__(self, n):

        self.n = n

        self.INF = 0

        self.size = 1

        while self.size < n:

            self.size *= 2

        self.node = [self.INF] * (2*self.size - 1)



    def update(self, i, val):

        i += (self.size - 1)

        self.node[i] = val

        while i > 0:

            i = (i - 1) // 2

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



    def get_max(self, begin, end):

        begin += (self.size - 1)

        end += (self.size - 1)

        s = self.INF

        while begin < end:

            if (end - 1) & 1:

                end -= 1

                s = max(s, self.node[end])

            if (begin - 1) & 1:

                s = max(s, self.node[begin])

                begin += 1

            begin = (begin - 1) // 2

            end = (end - 1) // 2

        return s



n = int(eval(input()))

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

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



memo = {}

for i in range(n):

    memo[h[i]-1] = i

st = SegmentTree(n)



for i in range(n):

    ind = memo[i]

    st.update(ind, st.get_max(0, ind) + a[ind])

ans = 0

for i in range(n):

    ans = max(ans, st.get_max(i, i+1))

print(ans)
class SegmentTree:
    def __init__(self, n):
        self.size = 1
        while self.size < n:
            self.size *= 2
        self.node = [0] * (2 * self.size - 1)

    def update(self, i, val):
        i += self.size - 1
        self.node[i] = val
        while i > 0:
            i = (i - 1) // 2
            self.node[i] = max(self.node[2 * i + 1], self.node[2 * i + 2])

    def get_max(self, begin, end):
        begin += self.size - 1
        end += self.size - 1
        s = 0
        while begin < end:
            if (end - 1) & 1:
                end -= 1
                s = max(s, self.node[end])
            if (begin - 1) & 1:
                s = max(s, self.node[begin])
                begin += 1
            begin = (begin - 1) // 2
            end = (end - 1) // 2
        return s


n = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
st = SegmentTree(n + 1)
dp = [0] * (n + 1)
for i in range(n):
    # i番目の花を残さないとき
    res1 = st.get_max(0, n + 1)
    # i番目の花を残すとき
    res2 = st.get_max(0, h[i]) + a[i]
    dp[i + 1] = max(res1, res2)
    st.update(h[i], res2)
print((dp[n]))

class SegmentTree:
    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する
    update: i番目をvalに変更する
    get_min: 区間[begin, end)の最小値を求める
    """

    def __init__(self, n):
        self.n = n
        self.INF = 0
        self.size = 1
        while self.size < n:
            self.size *= 2
        self.node = [self.INF] * (2 * self.size - 1)

    def update(self, i, val):
        i += self.size - 1
        self.node[i] = val
        while i > 0:
            i = (i - 1) // 2
            self.node[i] = max(self.node[2 * i + 1], self.node[2 * i + 2])

    def get_max(self, begin, end):
        begin += self.size - 1
        end += self.size - 1
        s = self.INF
        while begin < end:
            if (end - 1) & 1:
                end -= 1
                s = max(s, self.node[end])
            if (begin - 1) & 1:
                s = max(s, self.node[begin])
                begin += 1
            begin = (begin - 1) // 2
            end = (end - 1) // 2
        return s


n = int(eval(input()))
h = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = {}
for i in range(n):
    memo[h[i] - 1] = i
st = SegmentTree(n)
for i in range(n):
    ind = memo[i]
    st.update(ind, st.get_max(0, ind) + a[ind])
ans = 0
for i in range(n):
    ans = max(ans, st.get_max(i, i + 1))
print(ans)

+    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する +    update: i番目をvalに変更する +    get_min: 区間[begin, end)の最小値を求める +    """ + +        self.n = n +        self.INF = 0 -        self.node = [0] * (2 * self.size - 1) +        self.node = [self.INF] * (2 * self.size - 1) -        s = 0 +        s = self.INF -st = SegmentTree(n + 1) -dp = [0] * (n + 1) +memo = {} -    # i番目の花を残さないとき -    res1 = st.get_max(0, n + 1) -    # i番目の花を残すとき -    res2 = st.get_max(0, h[i]) + a[i] -    dp[i + 1] = max(res1, res2) -    st.update(h[i], res2) -print((dp[n])) +    memo[h[i] - 1] = i +st = SegmentTree(n) +for i in range(n): +    ind = memo[i] +    st.update(ind, st.get_max(0, ind) + a[ind]) +ans = 0 +for i in range(n): +    ans = max(ans, st.get_max(i, i + 1)) +print(ans)