Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(a, b, i=0, l=0, r=n2):
        if b <= l or r <= a:
            return 0

        if a <= l and r <= b:
            return data[i]

        m = (l + r) // 2
        li = (i << 1) + 1
        return max(get_max(a, b, li, l, m), get_max(a, b, li + 1, m, r))

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(0, i) + aaa[i])

    return get_max(0, n2)


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))


Fixed submission:
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(k):
        i = k + offset
        ret = data[i]
        while i:
            if i % 2 == 0:
                ret = max(ret, data[i - 1])
            i = (i - 1) // 2
        return ret

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(i) + aaa[i])

    return data[0]


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
class SegmentTree:
    def __init__(self, n, sentinel=0):
        self.n = 1 << n.bit_length()
        self.offset = self.n - 1
        m = (self.n << 1) - 1
        self.data = [sentinel] * m
        self.sentinel = sentinel

    def update(self, k, x):
        i = k + self.offset
        self.data[i] = max(self.data[i], x)
        while i:
            i = (i - 1) // 2
            self.data[i] = max(self.data[i], x)

    def get_max(self, a, b, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return self.sentinel

        if a <= l and r <= b:
            return self.data[i]

        m = (l + r) // 2
        li = (i << 1) + 1
        return max(self.get_max(a, b, li, l, m), self.get_max(a, b, li + 1, m, r))


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
srt = sorted((h, i) for i, h in enumerate(hhh))
lst = SegmentTree(n, 0)

for h, i in srt:
    lst.update(i, lst.get_max(0, i) + aaa[i])
    # print(lst.data)
print(lst.get_max(0, lst.n))


Fixed submission:
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(k):
        i = k + offset
        ret = data[i]
        while i:
            if i % 2 == 0:
                ret = max(ret, data[i - 1])
            i = (i - 1) // 2
        return ret

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(i) + aaa[i])

    return data[0]


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
class LazySegmentTree:
    """
    Range Max Query
    Node must be positive.
    """

    def __init__(self, n, sentinel=0):
        self.n = 1 << (n + 1).bit_length()
        m = (self.n << 1) - 1
        self.data = [sentinel] * m
        self.lazy = [sentinel] * m
        self.flag = [False] * m
        self.sentinel = sentinel

    def _eval(self, i, l, r):
        if self.flag[i]:
            self.data[i] = max(self.data[i], self.lazy[i])
            if r - l > 1:
                li = (i << 1) + 1
                self.lazy[li] = max(self.lazy[li], self.lazy[i])
                self.lazy[li + 1] = max(self.lazy[li + 1], self.lazy[i])
                self.flag[li] = self.flag[li + 1] = True
            self.flag[i] = False

    def update(self, a, b, x, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return

        self._eval(i, l, r)

        if a <= l and r <= b:
            self.lazy[i] = x
            self.flag[i] = True
            self._eval(i, l, r)
        else:
            m = (l + r) // 2
            li = (i << 1) + 1
            self.update(a, b, x, li, l, m)
            self.update(a, b, x, li + 1, m, r)
            self.data[i] = max(self.data[li:li + 2])

    def get_max(self, a, b, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return self.sentinel

        self._eval(i, l, r)

        if a <= l and r <= b:
            return self.data[i]

        m = (l + r) // 2
        li = (i << 1) + 1
        return max(self.get_max(a, b, li, l, m), self.get_max(a, b, li + 1, m, r))


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
srt = sorted((h, i) for i, h in enumerate(hhh))
lst = LazySegmentTree(n, 0)

for h, i in srt:
    lst.update(i, lst.n, lst.get_max(0, i) + aaa[i])
    # print(lst.data)
    # print(lst.lazy)
    # print(lst.flag)
print(lst.get_max(0, n))


Fixed submission:
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(k):
        i = k + offset
        ret = data[i]
        while i:
            if i % 2 == 0:
                ret = max(ret, data[i - 1])
            i = (i - 1) // 2
        return ret

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(i) + aaa[i])

    return data[0]


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
class LazySegmentTree:
    """
    Range Max Query
    Node must be positive.
    """

    def __init__(self, n, sentinel=0):
        self.n = n
        self.n2 = 1 << (n + 1).bit_length()
        m = (self.n2 << 1) - 1
        self.data = [sentinel] * m
        self.lazy = [sentinel] * m
        self.flag = [False] * m
        self.sentinel = sentinel

    def _eval(self, i, l, r):
        if self.flag[i]:
            self.data[i] = max(self.data[i], self.lazy[i])
            if r - l > 1:
                li = (i << 1) + 1
                self.lazy[li] = max(self.lazy[li], self.lazy[i])
                self.lazy[li + 1] = max(self.lazy[li + 1], self.lazy[i])
                self.flag[li] = self.flag[li + 1] = True
            self.flag[i] = False

    def update(self, a, b, x, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return

        self._eval(i, l, r)

        if a <= l and r <= b:
            self.lazy[i] = x
            self.flag[i] = True
            self._eval(i, l, r)
        else:
            m = (l + r) // 2
            li = (i << 1) + 1
            self.update(a, b, x, li, l, m)
            self.update(a, b, x, li + 1, m, r)
            self.data[i] = max(self.data[li:li + 2])

    def get_max(self, a, b, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return self.sentinel

        self._eval(i, l, r)

        if a <= l and r <= b:
            return self.data[i]

        m = (l + r) // 2
        li = (i << 1) + 1
        return max(self.get_max(a, b, li, l, m), self.get_max(a, b, li + 1, m, r))


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
srt = sorted((h, i) for i, h in enumerate(hhh))
lst = LazySegmentTree(n, 0)

for h, i in srt:
    lst.update(i, lst.n2, lst.get_max(0, i) + aaa[i])
    # print(lst.data)
    # print(lst.lazy)
    # print(lst.flag)
print(lst.get_max(0, n))


Fixed submission:
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(k):
        i = k + offset
        ret = data[i]
        while i:
            if i % 2 == 0:
                ret = max(ret, data[i - 1])
            i = (i - 1) // 2
        return ret

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(i) + aaa[i])

    return data[0]


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
class LazySegmentTree:
    """
    Range Max Query
    Node must be positive.
    """

    def __init__(self, n, sentinel=0):
        self.n = n
        m = 1 << ((n + 1).bit_length() + 1)
        self.data = [sentinel] * m
        self.lazy = [sentinel] * m
        self.flag = [False] * m
        self.sentinel = sentinel

    def _eval(self, i, l, r):
        if self.flag[i]:
            self.data[i] = max(self.data[i], self.lazy[i])
            if r - l > 1:
                li = (i << 1) + 1
                self.lazy[li] = max(self.lazy[li], self.lazy[i])
                self.lazy[li + 1] = max(self.lazy[li + 1], self.lazy[i])
                self.flag[li] = self.flag[li + 1] = True
            self.flag[i] = False

    def update(self, a, b, x, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return

        self._eval(i, l, r)

        if a <= l and r <= b:
            self.lazy[i] = x
            self.flag[i] = True
            self._eval(i, l, r)
        else:
            m = (l + r) // 2
            li = (i << 1) + 1
            self.update(a, b, x, li, l, m)
            self.update(a, b, x, li + 1, m, r)
            self.data[i] = max(self.data[li:li + 2])

    def get_max(self, a, b, i=0, l=0, r=None):
        if r is None:
            r = self.n

        if b <= l or r <= a:
            return self.sentinel

        self._eval(i, l, r)

        if a <= l and r <= b:
            return self.data[i]

        m = (l + r) // 2
        li = (i << 1) + 1
        return max(self.get_max(a, b, li, l, m), self.get_max(a, b, li + 1, m, r))


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
srt = sorted((h, i) for i, h in enumerate(hhh))
lst = LazySegmentTree(n, 0)

for h, i in srt:
    lst.update(i, n, lst.get_max(0, i) + aaa[i])
    # print(lst.data)
    # print(lst.lazy)
    # print(lst.flag)
print(lst.get_max(0, n))


Fixed submission:
def solve(n, hhh, aaa):
    n2 = 1 << n.bit_length()
    offset = n2 - 1
    data = [0] * ((n2 << 1) - 1)

    def update(k, x):
        i = k + offset
        data[i] = max(data[i], x)
        while i:
            i = (i - 1) // 2
            data[i] = max(data[i], x)

    def get_max(k):
        i = k + offset
        ret = data[i]
        while i:
            if i % 2 == 0:
                ret = max(ret, data[i - 1])
            i = (i - 1) // 2
        return ret

    srt = sorted((h, i) for i, h in enumerate(hhh))
    for h, i in srt:
        update(i, get_max(i) + aaa[i])

    return data[0]


n = int(input())
hhh = list(map(int, input().split()))
aaa = list(map(int, input().split()))
print(solve(n, hhh, aaa))