Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
h = np.array(list(map(int, input().split())), dtype='int64')
a = np.array(list(map(int, input().split())), dtype='int64')

dp = np.zeros((n, 2), dtype='int64')
dp[0][0] = a[0]
dp[0][1] = a[0]

for i in range(1, n):
    h_ = h[:i]
    js = np.where(h_ < h[i])[0]
    if len(js) == 0:
        dp[i][1] = a[i]
    else:
        dp[i][1] = np.max(np.array(dp[js, 1])) + a[i]
    dp[i][0] = max(dp[i][1], dp[i-1][0])

print(dp[-1][0])


Fixed submission:
from math import ceil, log

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


class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get(self, x: int, y: int) -> int:  # [x, y)
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


dp = SegmentTree([0] * (n+1), 0, max)

for i in range(n):
    v = dp.get(0, h[i]) + a[i]
    if v > dp.get(h[i], h[i]+1):
        dp.set(h[i], v)

print(dp.get(0, n+1))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
h = np.array(list(map(int, input().split())), dtype='int64')
a = np.array(list(map(int, input().split())), dtype='int64')

dp = np.zeros((n, 2), dtype='int64')
dp[0][0] = a[0]
dp[0][1] = a[0]

for i in range(1, n):
    h_ = h[:i]
    js = np.where(h_ < h[i])[0]
    if len(js) == 0:
        dp[i][1] = a[i]
    else:
        dp[i][1] = max(dp[js, 1]) + a[i]
    dp[i][0] = max(dp[i][1], dp[i-1][0])

print(dp[-1][0])


Fixed submission:
from math import ceil, log

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


class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get(self, x: int, y: int) -> int:  # [x, y)
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


dp = SegmentTree([0] * (n+1), 0, max)

for i in range(n):
    v = dp.get(0, h[i]) + a[i]
    if v > dp.get(h[i], h[i]+1):
        dp.set(h[i], v)

print(dp.get(0, n+1))