Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
N = int(input())
fact = factorization(N)
if fact[0][0] == 1:
    print(0)
    sys.exit()
ans = 0
for f, num in fact:
    for i in range(1, num+1):
        if num < i:
            break
        num -= i
        ans += 1
print(ans)

Fixed submission:
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def main():
    import sys
    N = int(input())
    fact = factorization(N)
    if fact[0][0] == 1:
        print(0)
        sys.exit()
    ans = 0
    for f, num in fact:
        for i in range(1, num+1):
            if num < i:
                break
            num -= i
            ans += 1
    print(ans)
main()

Buggy python submission (verdict: Runtime Error):
import sys
N = int(input())
fact = factorization(N)
if fact[0][0] == 1:
    print(0)
    sys.exit()
print(fact)
ans = 0
for f, num in fact:
    print(f'{f=}, {num=}')
    for i in range(1, num+1):
        print(f'{i=}')
        if num < i:
            break
        num -= i
        ans += 1
print(ans)

Fixed submission:
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def main():
    import sys
    N = int(input())
    fact = factorization(N)
    if fact[0][0] == 1:
        print(0)
        sys.exit()
    ans = 0
    for f, num in fact:
        for i in range(1, num+1):
            if num < i:
                break
            num -= i
            ans += 1
    print(ans)
main()
Buggy python submission (verdict: Runtime Error):
N = int(Input[0])
a = list(map(int, Input[1].split()))

target = 1
for ai in a:
    if ai == target:
        target += 1

if target == 1:
    print(-1)
else:
    print(N-target+1)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))

target = 1
for ai in a:
    if ai == target:
        target += 1

if target == 1:
    print(-1)
else:
    print(N-target+1)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))

    ans = 0
    for ia in range(N):
        for ib in range(ia+1, N-1):
            a = L[ia]
            b = L[ib]

            c_min = max(a-b, b-a)
            c_max = a + b

            for ic in range(ib+1, N):
                c = L[ic]
                if c >= c_max:
                    break
                if c <= c_min:
                    continue
                ans += 1

    print(ans)
main()

Fixed submission:
from bisect import bisect_left, bisect_right

def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))

    ans = 0
    for ia in range(N):
        for ib in range(ia+1, N-1):
            a = L[ia]
            b = L[ib]

            c_min = max(a-b, b-a)
            c_max = a + b
            ic_begin = bisect_right(L, c_min, lo=ib+1)
            ic_end   = bisect_left( L, c_max, lo=ib+1)

            ans += ic_end - ic_begin

    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

def triangle_exists(a, b, c):
    return ((a < b + c) and 
            (b < c + a) and
            (c < a + b))

def main():
    N = int(input())
    L = list(map(int, input().split()))

    ans = 0
    for a,b,c in combinations(L, 3):
        if triangle_exists(a, b, c):
            ans += 1
    print(ans)

main()

Fixed submission:
from bisect import bisect_left, bisect_right

def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))

    ans = 0
    for ia in range(N):
        for ib in range(ia+1, N-1):
            a = L[ia]
            b = L[ib]

            c_min = max(a-b, b-a)
            c_max = a + b
            ic_begin = bisect_right(L, c_min, lo=ib+1)
            ic_end   = bisect_left( L, c_max, lo=ib+1)

            ans += ic_end - ic_begin

    print(ans)
main()
Buggy python submission (verdict: Time Limit Exceeded):
def evaluate(directions):
    ans = 0
    for i, d in enumerate(directions):
        if 0 <= i+d and i+d <= N-1:
            if directions[i] == directions[i+d]:
                ans += 1
    return ans

def find_lr(directions):
    N = len(directions)
    for i in range(1, N):
        if directions[i-1] != directions[i]:
            l = i
            break
    else:
        l = None
        return None, None
    
    for i in range(l+1, N):
        if directions[i-1] != directions[i]:
            r = i
            break
    else:
        r = N
    return l, r

def solve(directions):
    l, r = find_lr(directions)
    if l is None:
        return False
    else:
        if l is not None:
            for i in range(l, r):
                directions[i] *= -1
        return True 
    
N, K = map(int, input().split())
S = input()
directions = [1 if s=="R" else -1 for s in S]
for k in range(K):
    result = solve(directions)
    if result == False:
        break
    
print(evaluate(directions))

Fixed submission:
def main():
    N, K = map(int, input().split())
    S = input()
    score = 0
    for i in range(1, N):
        if S[i-1] == S[i]:
            score += 1
    print(min(N-1, score + 2*K))
main()
Buggy python submission (verdict: Time Limit Exceeded):
print(sum([i for i in range(int(input()))]))

Fixed submission:
N = int(input())
print(N*(N-1)//2)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

def dfs(now, parent, total):
    result[now] = total + counter[now]
    for child in tree[now]:
        if child == parent:
            continue
        dfs(child, result[now])

        
N, Q = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

tree = [[] for _ in range(N+1)]
counter = [0 for _ in range(N+1)]

for a,b in edge:
    tree[a].append(b)
    tree[b].append(a)

for p,x in P:
    counter[p] += x

result = [0]*(N+1)
dfs(1, 0, 0)

print(' '.join(map(str, result[1:])))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

def dfs(now, parent, total):
    result[now] = total + counter[now]
    for child in tree[now]:
        if child == parent:
            continue
        dfs(child, now, result[now])

N, Q = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

tree = [[] for _ in range(N+1)]
counter = [0 for _ in range(N+1)]

for a,b in edge:
    tree[a].append(b)
    tree[b].append(a)

for p,x in P:
    counter[p] += x

result = [0]*(N+1)
dfs(1, 0, 0)

print(' '.join(map(str, result[1:])))
Buggy python submission (verdict: Runtime Error):
def nC2(n):
    x = 1
    for i in range(2, n+1):
        x *= i
    return x//2

def main():
    N = int(input())
    S = [sorted(input()) for _ in range(N)]
    counter = {}
    for s in S:
        if s in counter:
            counter[s] += 1
        else:
            counter[s] = 1
    ans = sum([nC2(v) for v in counter.values()])
    print(ans)

main()

Fixed submission:
def nC2(n):
    if n < 2:
        return 0
    return n * (n-1) // 2

def main():
    N = int(input())
    S = [''.join(sorted(input())) for _ in range(N)]
    counter = {}
    for s in S:
        if s in counter:
            counter[s] += 1
        else:
            counter[s] = 1
    ans = sum([nC2(v) for v in counter.values()])
    print(ans)

main()
Buggy python submission (verdict: Runtime Error):
def main():
    num_monster = 0
    res = 0  # 余力
    for i in range(N):
        a = A[i] - res  # 前の人が倒しといてくれた分を引く
        b = B[i]
        if b <= a:  # 目の前の敵を倒し切れるかあやしい
            num_monster += b
            res = 0
        elif b <= a+A[i+1]:  # 次の敵の一部まで倒せる
            num_monster += b
            res = b - a
        else:  # 次の敵まで全部倒せる
            num_monster += a+A[i+1]
            res = A[i+1]
    print(num_monster)
main()

Fixed submission:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    num_monster = 0
    res = 0  # 余力
    for i in range(N):
        a = A[i] - res  # 前の人が倒しといてくれた分を引く
        b = B[i]
        if b <= a:  # 目の前の敵を倒し切れるかあやしい
            num_monster += b
            res = 0
        elif b <= a+A[i+1]:  # 次の敵の一部まで倒せる
            num_monster += b
            res = b - a
        else:  # 次の敵まで全部倒せる
            num_monster += a+A[i+1]
            res = A[i+1]
    print(num_monster)
main()
Buggy python submission (verdict: Runtime Error):
difficulties = sorted(d)
l = len(difficulties)//2 - 1
r = len(difficulties)//2
print(difficulties[r] - difficulties[l])

Fixed submission:
def main():
    N = int(input())
    d = list(map(int, input().split()))
    difficulties = sorted(d)
    l = len(difficulties)//2 - 1
    r = len(difficulties)//2
    print(difficulties[r] - difficulties[l])
main()
Buggy python submission (verdict: Runtime Error):
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = int(fct((b-i)+(i-1)) / fct(b-i) / fct(i-1)) % MOD
        if r >= i-1:
            p_red = int(fct((r-(i-1))+i) / fct(r-(i-1)) / fct(i)) % MOD
        else:
            p_red = 0
        print((p_blue * p_red) % MOD)
main()

Fixed submission:
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = (fct((b-i)+(i-1)) // fct(b-i) // fct(i-1)) % MOD
        if r >= i-1:
            p_red = (fct((r-(i-1))+i) // fct(r-(i-1)) // fct(i)) % MOD
        else:
            p_red = 0
        print((p_blue * p_red) % MOD)
main()

Buggy python submission (verdict: Runtime Error):
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = int(fct((b-i)+(i-1)) / fct(b-i) / fct(i-1)) % MOD
        if r >= i-1:
            p_red = int(fct((r-(i-1))+i) / fct(r-(i-1)) / fct(i)) % MOD
        else:
            p_red = 0
        print((p_blue * p_red) % MOD)
main()

Fixed submission:
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = (fct((b-i)+(i-1)) // fct(b-i) // fct(i-1)) % MOD
        if r >= i-1:
            p_red = (fct((r-(i-1))+i) // fct(r-(i-1)) // fct(i)) % MOD
        else:
            p_red = 0
        print((p_blue * p_red) % MOD)
main()

Buggy python submission (verdict: Runtime Error):
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = int(fct((b-i)+(i-1)) / fct(b-i) / fct(i-1)) % MOD
        p_red = 0
        if r >= i-1 and i-1 >= 1:
            n = i - 1
            p_red += int(fct((r-n+(n-1))) / fct(r-n) / fct(n-1)) % MOD  
        if r >= i:
            n = i
            p_red += int(fct((r-n)+(n-1)) / fct(r-n) / fct(n-1)) * 2 % MOD
        if r >= i+1:
            n = i + 1
            p_red += int(fct((r-n)+n-1) / fct(r-n) / fct(n-1)) % MOD
        print((p_blue * p_red) % MOD)
main()

Fixed submission:
from math import factorial as fct
def main():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    b = K
    r = N - K
    for i in range(1, K+1):
        p_blue = (fct((b-i)+(i-1)) // fct(b-i) // fct(i-1)) % MOD
        if r >= i-1:
            p_red = (fct((r-(i-1))+i) // fct(r-(i-1)) // fct(i)) % MOD
        else:
            p_red = 0
        print((p_blue * p_red) % MOD)
main()
Buggy python submission (verdict: Runtime Error):
from fractions import gcd

lcm = int(C*D / gcd(C, D))
nA = A - (int(A/C) + int(A/D) - int(A/lcm))  
nB = B - (int(B/C) + int(B/D) - int(B/lcm))
print(nB - nA)

Fixed submission:
from fractions import gcd

A, B, C, D = map(int, input().split())
A -= 1
lcm = int(C*D / gcd(C, D))
nA = A - (A//C + A//D - A//lcm)  
nB = B - (B//C + B//D - B//lcm)
print(nB - nA)
Buggy python submission (verdict: Runtime Error):
class Graph():
    def __init__(self):
        self.adjacency_dict = {}

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
    
    def add_edge(self, v1, v2, dist):
        self.adjacency_dict[v1].append([v2, dist])
        self.adjacency_dict[v2].append([v1, dist])
    
    def next_edges(self, v):
        return self.adjacency_dict[v]

    def __str__(self):
        return self.adjacency_dict.__str__()
      
N = int(input())
dists = [list(map(int, input().split())) for _ in range(N-1)]

graph = Graph()
for i in range(1, N+1):
    graph.add_vertex(i)
for v1, v2, dist in dists:
    graph.add_edge(v1, v2, dist)
    
def dfs(start, group):
    if visited[start] is not None:
        return
    visited[start] = group
    for v, dist in graph.next_edges(start):
        if dist % 2 == 0:
            dfs(v, group)
        else:
            dfs(v, 1-group)

visited = [None]*(N+1)
dfs(1, 0)

for i in visited[1:]:
  print(i)

Fixed submission:
import sys
sys.setrecursionlimit(100000)

class Graph():
    def __init__(self):
        self.adjacency_dict = {}

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
    
    def add_edge(self, v1, v2, dist):
        self.adjacency_dict[v1].append([v2, dist])
        self.adjacency_dict[v2].append([v1, dist])
    
    def next_edges(self, v):
        return self.adjacency_dict[v]

N = int(input())
dists = [list(map(int, input().split())) for _ in range(N-1)]

graph = Graph()
for i in range(1, N+1):
    graph.add_vertex(i)
for v1, v2, dist in dists:
    graph.add_edge(v1, v2, dist)

def dfs(start, group):
    if visited[start] is not None:
        return
    visited[start] = group
    for v, dist in graph.next_edges(start):
        if dist % 2 == 0:
            dfs(v, group)
        else:
            dfs(v, 1-group)

visited = [None]*(N+1)
dfs(1, 0)
for i in visited[1:]:
    print(i)
Buggy python submission (verdict: Runtime Error):
class ATherefore:
    def solve(self, tc=0):
        n = int(input())
        if n % 10 in [2, 4, 5, 7, 9]:
            print("hon")
        elif n % 10 in [0, 1, 6, 8]:
            print("pon")
        else:
            print("bon")

x = Atherefore()
x.solve()

Fixed submission:
class ATherefore:
    def solve(self, tc=0):
        n = int(input())
        if n % 10 in [2, 4, 5, 7, 9]:
            print("hon")
        elif n % 10 in [0, 1, 6, 8]:
            print("pon")
        else:
            print("bon")


x = ATherefore()
x.solve()
Buggy python submission (verdict: Runtime Error):
# @author 

import sys

class DTeleporter:
    def solve(self, tc=0):
        n, k = [int(_) for _ in input().split()]
        a = [int(_) - 1 for _ in input().split()]
        done = dict()
        cur = 0
        steps = 0
        while cur not in done and steps < k:
            done[cur] = steps
            cur = a[cur]
            steps += 1

        d = steps - done[cur]
        steps = steps + ((k - steps) // d) * d

        while steps < k:
            cur = a[cur]
            steps += 1

        print(cur + 1)


solver = DTeleporter()
input = sys.stdin.readline

solver.solve()


Fixed submission:
# @author 

import sys

class DTeleporter:
    def solve(self, tc=0):
        n, k = [int(_) for _ in input().split()]
        a = [int(_) - 1 for _ in input().split()]
        done = dict()
        cur = 0
        steps = 0
        while cur not in done and steps < k:
            done[cur] = steps
            cur = a[cur]
            steps += 1
        
        if steps == k:
            print(cur + 1)
            return

        d = steps - done[cur]
        steps = steps + ((k - steps) // d) * d

        while steps < k:
            cur = a[cur]
            steps += 1

        print(cur + 1)


solver = DTeleporter()
input = sys.stdin.readline

solver.solve()
Buggy python submission (verdict: Runtime Error):
import math


def solve_even(n):
    assert(n % 2 == 0)
    k = n // 2
    return leg(k)


def leg(k):
    return sum(k // 5 ** i for i in range(1, int(math.log(k) / math.log(5)) + 1))


n = int(input())

if n % 2 == 0:
    print(solve_even(n))
else:
    if n == 1:
        print(0)
    else:
        print(solve_even(n + 1) - leg(n // 2))

# (2k + 1)!! = (2k + 2)! / (2^(k+1)*(k+1)!)


Fixed submission:
import math


def solve_even(n):
    assert(n % 2 == 0)
    k = n // 2
    return leg(k)


def leg(k):
    return sum(k // 5 ** i for i in range(1, int(math.log(k) / math.log(5)) + 1))


n = int(input())

if n % 2 == 0:
    if n == 0:
        print(0)
    else:
        print(solve_even(n))
else:
    if n == 1:
        print(0)
    else:
        print(solve_even(n + 1) - leg(n // 2 + 1))

# (2k + 1)!! = (2k + 2)! / (2^(k+1)*(k+1)!)
Buggy python submission (verdict: Runtime Error):
from math import *

n, k = [int(item) for item in input().split()]
ans = 0
for p in range(1, n + 1):

    if p >= k:
        ans += 1;
    else:
        x = ceil(log2(k / p))
        ans += 1.0 / (1 << x);
print(ans / n)


Fixed submission:
from math import *

n, k = [int(item) for item in input().split()]
ans = 0
for p in range(1, n + 1):

    if p >= k:
        ans += 1;
    else:
        x = ceil(log2(k) - log2(p))
        ans += 1.0 / (1 << x);
print(ans / n)
Buggy python submission (verdict: Runtime Error):
n = int(input())

ans = 0

for i in range(1, int(n ** .5) + 1):
    if n % i == 0:
        m = n // i - 1
        if n % m == n // m:
            ans += m

print(ans)


Fixed submission:
n = int(input())

ans = 0

for i in range(1, int(n ** .5) + 1):
    if n % i == 0:
        m = n // i - 1
        if m > 0 and n % m == n // m:
            ans += m

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
v = [int(item) for item in input().split()]
c = [int(item) for item in input().split()]

d = [[v[i], c[i]] for i in range(n)]
d.sort(key=lambda x: x[0] - x[1], reverse=True)

ans = 0
i = 0
while d[i][0] - d[i][1] > 0:
    ans += d[i][0] - d[i][1]
    i += 1

print(ans)


Fixed submission:
n = int(input())
v = [int(item) for item in input().split()]
c = [int(item) for item in input().split()]

d = [[v[i], c[i]] for i in range(n)]
d.sort(key=lambda x: x[0] - x[1], reverse=True)

ans = 0
i = 0
while i < n and d[i][0] - d[i][1] > 0:
    ans += d[i][0] - d[i][1]
    i += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
# @author 

import sys

class GLongestPath:
    def dfs(self, s):
        if self.done[s]:
            return self.dp[s]

        self.done[s] = 1

        ans = 0
        for x in self.adj[s]:
            ans = max(ans, 1 + self.dfs(x))

        self.dp[s] = ans
        return ans

    def solve(self):
        from collections import defaultdict
        n, m = [int(_) for _ in input().split()]
        self.adj = defaultdict(list)
        self.dp = [0] * (n + 1)
        self.done = [0] * (n + 1)
        for i in range(m):
            u, v = [int(_) for _ in input().split()]
            self.adj[u].append(v)

        ans = 0
        for i in range(1, n + 1):
            ans = max(ans, self.dfs(i))

        print(ans)

solver = GLongestPath()
input = sys.stdin.readline

solver.solve()


Fixed submission:
# @author 

import sys

class GLongestPath:
    def dfs(self, s):
        if self.done[s]:
            return self.dp[s]

        self.done[s] = 1

        ans = 0
        for x in self.adj[s]:
            ans = max(ans, 1 + self.dfs(x))

        self.dp[s] = ans
        return ans

    def solve(self):
        import sys
        sys.setrecursionlimit(10 ** 5 + 5)
        from collections import defaultdict
        n, m = [int(_) for _ in input().split()]
        self.adj = defaultdict(list)
        self.dp = [0] * (n + 1)
        self.done = [0] * (n + 1)
        for i in range(m):
            u, v = [int(_) for _ in input().split()]
            self.adj[u].append(v)

        ans = 0
        for i in range(1, n + 1):
            ans = max(ans, self.dfs(i))

        print(ans)

solver = GLongestPath()
input = sys.stdin.readline

solver.solve()
Buggy python submission (verdict: Time Limit Exceeded):
# @author 

import sys

class NSlimes:
    def solve(self):
        n = int(input())
        a = [None] + [int(_) for _ in input().split()]

        dp = [[float('inf')] * (n + 1) for _ in range(n + 1)]
        for i in range(1, n + 1):
            dp[i][i] = 0
        for i in range(n + 1, 0, -1):
            for j in range(1, n + 1):
                for k in range(i, j):
                    dp[i][j] = min(dp[i][k] + dp[k + 1][j] + sum(a[p] for p in range(i, j + 1)), dp[i][j])

        print(dp[1][n])


        # Greedy, not working

        # b = a[:]
        # ans = 0
        # for i in range(n - 1):
        #     idx = -1
        #     mn = float('inf')
        #     for j in range(n - 1 - i):
        #         if b[j] + b[j + 1] < mn:
        #             mn = b[j] + b[j + 1]
        #             idx = j
        #     assert(idx != -1)
        #     b[idx] += b[idx + 1]
        #     ans += b[idx]
        #     del b[idx + 1]
        # assert(len(b) == 1)
        # print(ans)

solver = NSlimes()
input = sys.stdin.readline

solver.solve()


Fixed submission:
# @author 

import sys

class NSlimes:
    def solve(self):
        n = int(input())
        a = [None] + [int(_) for _ in input().split()]

        dp = [[float('inf')] * (n + 1) for _ in range(n + 1)]
        for i in range(1, n + 1):
            dp[i][i] = 0
        for i in range(n + 1, 0, -1):
            for j in range(1, n + 1):
                s = sum(a[p] for p in range(i, j + 1))
                for k in range(i, j):
                    dp[i][j] = min(dp[i][k] + dp[k + 1][j] + s, dp[i][j])

        print(dp[1][n])


        # Greedy, not working

        # b = a[:]
        # ans = 0
        # for i in range(n - 1):
        #     idx = -1
        #     mn = float('inf')
        #     for j in range(n - 1 - i):
        #         if b[j] + b[j + 1] < mn:
        #             mn = b[j] + b[j + 1]
        #             idx = j
        #     assert(idx != -1)
        #     b[idx] += b[idx + 1]
        #     ans += b[idx]
        #     del b[idx + 1]
        # assert(len(b) == 1)
        # print(ans)

solver = NSlimes()
input = sys.stdin.readline

solver.solve()
Buggy python submission (verdict: Time Limit Exceeded):
# @author 

import sys

class QFlowers:
    def solve(self):
        n = int(input())
        h = [int(_) for _ in input().split()]
        a = [int(_) for _ in input().split()]

        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            dp[i] = a[i - 1]
            for j in range(i):
                if h[i - 1] > h[j - 1]:
                    dp[i] = max(dp[i], dp[j] + a[i - 1])

        # print(dp)
        print(dp[-1])

solver = QFlowers()
input = sys.stdin.readline

solver.solve()


Fixed submission:
# @author 

import sys

class QFlowers:
    def solve(self):
        n = int(input())
        h = [int(_) for _ in input().split()]
        a = [int(_) for _ in input().split()]

        # h = [(h[i], i) for i in range(n)]
        # h.sort()

        def update(i, x):
            while i < n + 1:
                if x > dp[i]:
                    dp[i] = x
                i += i & -i

        def get(i):
            mx = 0
            while i > 0:
                if dp[i] > mx:
                    mx = dp[i]
                i -= i & -i
            return mx

        dp = [0] * (n + 1)
        for i, he in enumerate(h):
            update(he, get(he - 1) + a[i])

        # print(dp)
        print(max(dp))

solver = QFlowers()
input = sys.stdin.readline

solver.solve()