Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
N=input()
for i in range(1,N+1):
  for j in range(1,N+1):
    if i<j:
      if i*j==N:
        print("Yes")
        sys.exit()
print("No")



Fixed submission:
import sys
N=int(input())
for i in range(1,10):
  for j in range(1,10):
    if i<=j:
      if i*j==N:
        print("Yes")
        sys.exit()
print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N,K,Q = [int(x) for x in input().rstrip().split()]
l = [K for x in range(N)]

for i in range(Q):
    a = int(input())
    for j in range(N):

        if a-1 != j:

            l[j]=l[j]-1
for i in l:
    if 0<i:
     
        print("Yes")
    else:
        print("No")

Fixed submission:
N,K,Q=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(N)]
for i in range(Q):
  A=int(input())
  cnt[A-1]+=1
for i in cnt:
  if 0<(K-Q+i):
    print("Yes")
  else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
a.sort()

while(m):
  m-=1
  now=a.pop()
  a.append(now//2)
  a.sort()

print(sum(a))

Fixed submission:
import heapq as hp
n,m=[int(x) for x in input().rstrip().split()]
a=[-int(x) for x in input().rstrip().split()]

hp.heapify(a)
a.sort()

while(m):
  
  m-=1
  now=hp.heappop(a)
  now=-((-now)//2)
  hp.heappush(a,now)
  
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
a.sort()

while(m):
  m-=1
  now=a.pop()
  ind=bisect.bisect_left(a,(now//2))
  a.insert(ind,(now//2))

print(sum(a))

Fixed submission:
import heapq as hp
n,m=[int(x) for x in input().rstrip().split()]
a=[-int(x) for x in input().rstrip().split()]

hp.heapify(a)
a.sort()

while(m):
  
  m-=1
  now=hp.heappop(a)
  now=-((-now)//2)
  hp.heappush(a,now)
  
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
a.sort()

while(m):
  m-=1
  now=a.pop()
  ind=bisect.bisect_left(a,(now//2))
  a.insert(ind,(now//2))

print(sum(a))

Fixed submission:
import heapq as hp
n,m=[int(x) for x in input().rstrip().split()]
a=[-int(x) for x in input().rstrip().split()]

hp.heapify(a)
a.sort()

while(m):
  
  m-=1
  now=hp.heappop(a)
  now=-((-now)//2)
  hp.heappush(a,now)
  
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
a.sort()
ans=0

while(m):
  m-=1
  now=a.pop()
  ind=bisect.bisect_left(a,int(now/2))
  a.insert(ind,int(now/2))

print(sum(a))

Fixed submission:
import heapq as hp
n,m=[int(x) for x in input().rstrip().split()]
a=[-int(x) for x in input().rstrip().split()]

hp.heapify(a)
a.sort()

while(m):
  
  m-=1
  now=hp.heappop(a)
  now=-((-now)//2)
  hp.heappush(a,now)
  
print(-sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
import collections

N=int(input())
ans=0
s=[input() for x in range(N)]


for i in range(N):
  for j in range(N):
    if i<j:
      if collections.Counter(s[i])==collections.Counter(s[j]):
        ans+=1
print(ans)

Fixed submission:
import collections

N=int(input())
ans=0
s=[input() for x in range(N)]

d={}

for i in range(N):

  temp=str(sorted(s[i]))

  if temp in d.keys():
    ans+=d[temp]
    d[temp]+=1
  else:
    d[temp]=1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S=input()
nagasa=len(S)
R_flag=0
L_flag=0
j=0
n=0
ans=[0 for i in range(nagasa)]
 
for i in range(nagasa):
  now=S[i]
  if now=="R":
    R_flag=1
    j=i
    n=0
    while(R_flag==1):

      if S[j+n]=="L" or nagasa-1==j+n:
        R_flag=0
        break
      
      else:
        n+=1
 
    if n%2==0:
      ans[j+n]+=1
    
    elif n%2==1 and S[j+n]=="R" and nagasa-1==j+n:
      ans[j+n]+=1
    elif n%2==1:
      ans[j+n-1]+=1

 
  else:
    L_flag=1
    j=i
    n=0
    while(L_flag==1):

      if S[j-n]=="R" or j-n==0:
        L_flag=0
        break

      else:
        n+=1
 
    if n%2==1 and S[j-n]=="L" and j-n==0: 
      ans[j-n]+=1
 
    elif n%2==1:
      ans[j-n+1]+=1
 
    elif n%2==0:
      ans[j-n]+=1

 
print(*ans)

Fixed submission:
s=input()
n=len(s)
ans=[0 for i in range(n)]
r=0
l=0

for i in range(n):
  if s[i]=="R":
    r+=1
  else:
    plus=r//2
    ans[i]+=plus
    ans[i-1]+=(r-plus)
    r=0

for i in range(n-1,-1,-1):
  if s[i]=="L":
    l+=1
  else:
    plus=l//2
    ans[i]+=plus
    ans[i+1]+=l-plus
    l=0
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
nagasa=len(S)
R_flag=0
L_flag=0
j=0
n=0
ans=[0 for i in range(nagasa)]

for i in range(nagasa):
  now=S[i]
  if now=="R":
    R_flag=1
    j=i
    n=0
    while(R_flag==1):
      if j+n<nagasa-1:
        n+=1
      if S[j+n]=="L" or nagasa-1==j+n:
        R_flag=0
        break

    if n%2==0: #and nagasa-1!=j+n:
      ans[j+n]+=1
    
    elif n%2==1 and S[j+n]=="R" and nagasa-1==j+n:
      ans[j+n]+=1
    elif n%2==1:
      ans[j+n-1]+=1
    
    # elif j+n==nagasa-1:
    #    ans[nagasa-1]+=1

  else:
    L_flag=1
    j=i
    n=0
    while(L_flag==1):


      if S[j-n]=="R" or j-n==0:
        L_flag=0
        break

      if 1<=j-n:
        n+=1

      


    if n%2==1 and S[j-n]=="L" and j-n==0: #and 0!=j-n:
      ans[j-n]+=1

    elif n%2==1:
      ans[j-n+1]+=1

    elif n%2==0: #and 0!=j-n:
      ans[j-n]+=1
    # elif (j-n)==0:
    #   ans[0]+=1


for s in ans:
  print(s,end=" ")

Fixed submission:
s=input()
n=len(s)
ans=[0 for i in range(n)]
r=0
l=0

for i in range(n):
  if s[i]=="R":
    r+=1
  else:
    plus=r//2
    ans[i]+=plus
    ans[i-1]+=(r-plus)
    r=0

for i in range(n-1,-1,-1):
  if s[i]=="L":
    l+=1
  else:
    plus=l//2
    ans[i]+=plus
    ans[i+1]+=l-plus
    l=0
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
l=[]
for i in range(N):
    a=int(input())
    l.append(a)
len_l=len(l)
now=[]
for i in range(len_l):
    now=l[0:i]+l[i+1:len_l]
    print(max(now))
    

Fixed submission:
import copy
N=int(input())
A=[int(input()) for i in range(N)]
A_f=copy.deepcopy(A)
A_f.sort()
A_max=A_f[N-1]
A_sec=A_f[N-2]

for i in A:
    if i==A_max:
        print(A_sec)
    else:
        print(A_max)
Buggy python submission (verdict: Runtime Error):
l,r=map(int,input().split())
import sys
mod=2019
ans=float('inf')

for i in range(l,r+1):
  for j in range(j+1,r+1):
    ans=min((i*j)%mod,ans)
    if ans==0:
      print(0)
      sys.exit()
print(ans)

Fixed submission:
import sys
L,R=[int(x) for x in input().strip().split()]

tmp=10**9+1
ans=10**9+1

for i in range(L,R,1):
  for j in range(L+1,R+1,1):
    if i<j:
      ans=min((i*j)%2019,ans)
  
      if ans==0:
        print(0)
        sys.exit()
print(ans)


# 332701087 1753412895

Buggy python submission (verdict: Runtime Error):
l,r=map(int,input().split())
import sys
mod=2019
ans=float('inf')

for i in range(l,r):
  for j in range(j+1,r+1):
    ans=min((i%mod)*(j%mod),ans)
    if ans==0:
      print(0)
      sys.exit()
print(ans)

Fixed submission:
import sys
L,R=[int(x) for x in input().strip().split()]

tmp=10**9+1
ans=10**9+1

for i in range(L,R,1):
  for j in range(L+1,R+1,1):
    if i<j:
      ans=min((i*j)%2019,ans)
  
      if ans==0:
        print(0)
        sys.exit()
print(ans)


# 332701087 1753412895

Buggy python submission (verdict: Time Limit Exceeded):
L,R=[int(x) for x in input().strip().split()]

ans=10**9+1


for i in range(L,R+1,1):
  if i==2019:
    break
  for j in range(L,R+1,1):
    if j==2019:
      break

    if i<j:
      ans=min((i*j)%2019,ans)

if ans==10**9+1:
  ans=0
  

print(ans)


  
      





Fixed submission:
import sys
L,R=[int(x) for x in input().strip().split()]

tmp=10**9+1
ans=10**9+1

for i in range(L,R,1):
  for j in range(L+1,R+1,1):
    if i<j:
      ans=min((i*j)%2019,ans)
  
      if ans==0:
        print(0)
        sys.exit()
print(ans)


# 332701087 1753412895

Buggy python submission (verdict: Time Limit Exceeded):
L,R=[int(x) for x in input().strip().split()]

ans=10**9+1
for i in range(L,R+1,1):
  for j in range(L,R+1,1):
    if i<j:
      ans=min(i*j%2019,ans)
print(ans)
  

  
      





Fixed submission:
import sys
L,R=[int(x) for x in input().strip().split()]

tmp=10**9+1
ans=10**9+1

for i in range(L,R,1):
  for j in range(L+1,R+1,1):
    if i<j:
      ans=min((i*j)%2019,ans)
  
      if ans==0:
        print(0)
        sys.exit()
print(ans)


# 332701087 1753412895
Buggy python submission (verdict: Runtime Error):
n,x=[int(x) for x in input().rstrip().split()]
l=[int(x) for x in input().rstrip().rsplit()]

cnt=0
now=0
i=0
while(now<=x):
  cnt+=1
  now+=l[i]
  i+=1
print(cnt)

Fixed submission:
n,x=[int(x) for x in input().rstrip().split()]
l=[int(x) for x in input().rstrip().rsplit()]
 
cnt=1
now=0
for i in l:
  now+=i
  if now<=x:
    cnt+=1    
  else:
    break  
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N,M=[int(x) for x in input().rstrip().split()]
l=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(b)
count=0
ans=0
for i in range(1,N+1):
    for x,y in l:
        if int(x)<=i and i<=int(y):
            count+=1
        else:
            break
        if count==M:
            ans+=1

    count=0
print(ans)







Fixed submission:
N,M=[int(x) for x in input().rstrip().split()]
l=[]
r=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(int(b[0]))
    r.append(int(b[1]))

L=max(l)
R=min(r)

if R<L:
    print(0)
else:

    print(R-L+1)
Buggy python submission (verdict: Runtime Error):
n=int(input())
c=input()
b=c.count("#")
w=c.count(".")

cnt=0
for i in range(w):
  if c[i]=="#":
    cnt+=1
  bb=b-cnt
  ww=w-cnt

ans=min(ww+cnt,b,w)
print(ans)

Fixed submission:
import sys

n=int(input())
c=input()
black=c.count("#")
white=c.count(".")

if black==n:
  print(0)
  sys.exit()

b=0
w=0
now=0
ans=float('inf')

for i in range(n):
  if c[i]=="#":
    b+=1
  w=(i+1)-b

  now=b+(white-w)
  ans=min(ans,now)
  
print(min(ans,white))
Buggy python submission (verdict: Runtime Error):
import itertools
kai,av=[int(x) for x in input().rstrip().split()]
l=[int(x) for x in input().rstrip().split()]
ll=[i for i in range(1,kai+1)]
result=[]
count=0
 
 
for i in ll:
    for j in itertools.combinations(l,r=i):
        if av==(sum(j)/i):
            count+=1
print(count)

Fixed submission:
a=int(input())
b=int(input())
c=int(input())
d=int(input())
ans=0
for i in range(a):
    if 0<b:
        ans+=c
    else:
        ans+=d
    b=b-1
print(ans)

Buggy python submission (verdict: Runtime Error):
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
//	cin>>n>>k;
//	cout<<n<<k<<endl;
//string S[100];

int main(){
	int N,K,X,Y;cin>>N>>K>>X>>Y;
	int ans;

	for(int i=1;i<N+1;i++){
		if(K<i){
			ans+=Y;

		}else{
			ans+=X;
		}
	}
	cout<<ans<<endl;
}

Fixed submission:
a=int(input())
b=int(input())
c=int(input())
d=int(input())
ans=0
for i in range(a):
    if 0<b:
        ans+=c
    else:
        ans+=d
    b=b-1
print(ans)
Buggy python submission (verdict: Runtime Error):
a = input().rstrip().split()
N=int(a[0])
L=int(a[1])
l=[]

for i in range(N):
    b=input()
    l.append()

l.sort()

ans=""
for i in range(N):
    ans+=l[i]
print(ans)

Fixed submission:
a = input().rstrip().split()
N=int(a[0])
L=int(a[1])
l=[]

for i in range(N):
    b=input()
    l.append(b)

l.sort()

ans=""
for i in range(N):
    ans+=l[i]
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np

N = int(input())
s = []
for i in range (N):
    word = input()
    word_lst = []
    for j in range (len(word)):
        word_lst.append(word[j])
    word_lst = np.sort(word_lst)
    words = ""
    for j in range (len(word_lst)):
        words += word_lst[j]
    s.append(words)
#print (s)

count = 0
for i in range (N):
    for j in range (i+1,N):
        #print (i,j)
        #print (s[i],s[j])
        if s[i] == s[j]:
            count += 1
print (count)

Fixed submission:
import numpy as np

A,B = map(int,input().split())
ans = [A+B,A-B,A*B]
ans = np.sort(ans)
print (ans[-1])
Buggy python submission (verdict: Time Limit Exceeded):
input()
a = list(map(int, input().split()))
ans = 1
flag = False
for i in a:
    ans *= i
if ans<1000000000000000001:
    print(ans)
else:
    print(-1)


Fixed submission:
import sys
input()
a = list(map(int, input().split()))
ans = 1
flag = False
for i in a:
    if i==0:
        print(0)
        sys.exit()
for i in a:
    ans *= i
    if ans>1000000000000000001:
        print(-1)
        sys.exit()
if ans<1000000000000000001:
    print(ans)
else:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 ** 9 + 7
K = int(input())
M = len(input())
N = M + K
 
pow25 = [1] * (N + 1)
fac = [1] * (N + 1)
inv = [1] * (N + 1)
for x in range(1, N + 1):
    fac[x] = fac[x - 1] * x % MOD
    pow25[x] = pow25[x - 1] * 25 % MOD
inv[N] = pow(fac[N], MOD-2, MOD)
for x in range(N - 1, -1, -1):
    inv[x] = inv[x + 1] * (x + 1) % MOD
 
def binom(n, k):
    return fac[n] * inv[n - k] % MOD * inv[k] % MOD
 
ans = 0
for k in range(M, N + 1):
    ans += binom(N, k) * pow25[N - k] % MOD
    ans %= MOD
print(ans)

Fixed submission:
MOD = 10 ** 9 + 7
K = int(input())
M = len(input())
N = M + K
 
pow25 = [1] * (N + 1)
fac = [1] * (N + 1)
inv = [1] * (N + 1)
for x in range(1, N + 1):
    fac[x] = fac[x - 1] * x % MOD
    pow25[x] = pow25[x - 1] * 25 % MOD
inv[N] = pow(fac[N], MOD-2, MOD)
for x in range(N - 1, -1, -1):
    inv[x] = inv[x + 1] * (x + 1) % MOD
 
def binom(n, k):
    return fac[n] * inv[n - k] % MOD * inv[k] % MOD
 
ans = 0
for k in range(M, N + 1):
    ans += binom(N, k) * pow25[N - k] % MOD
    ans %= MOD
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
"""

最後に登場したindexで考える
l以下のやつを除く
最後に登場したindexをBITにいれる

bit[i] = 最後に登場したindexがiの色の数

"""

def bitadd(a,w,bit): #aにwを加える(1-origin)
 
    x = a
    while x <= (len(bit)-1):
        bit[x] += w
        x += x & (-1 * x)
 
def bitsum(a,bit): #ind 1～aまでの和を求める
 
    ret = 0
    x = a
    while x > 0:
        ret += bit[x]
        x -= x & (-1 * x)
    return ret

N,Q = map(int,input().split())

c = list(map(int,input().split()))
last = [1] * (N+1)

li = [ [] for i in range(N+2)]
ans = [None] * Q
for i in range(Q):
    L,R = map(int,input().split())
    L += 1
    R += 1
    li[R].append((L,i))

BIT = [0] * (N+2)
bitadd(1,N,BIT)

for i in range(N):

    nc = c[i]
    lastind = last[nc]
    last[nc] = i+2

    bitadd(lastind,-1,BIT)
    bitadd(i+2,1,BIT)

    for l,qind in li[i+2]:
        ans[qind] = bitsum(i+2,BIT) - bitsum(l-1,BIT)

for i in ans:
    print (i)



Fixed submission:

def bitadd(a,w,bit): #aにwを加える(1-origin)
 
    x = a
    while x <= (len(bit)-1):
        bit[x] += w
        x += x & (-1 * x)
 
def bitsum(a,bit): #ind 1～aまでの和を求める
 
    ret = 0
    x = a
    while x > 0:
        ret += bit[x]
        x -= x & (-1 * x)
    return ret

from sys import stdin
N,Q = map(int,stdin.readline().split())

c = list(map(int,stdin.readline().split()))
last = [1] * (N+1)

li = [ [] for i in range(N+2)]
ans = [None] * Q
for i in range(Q):
    L,R = map(int,stdin.readline().split())
    L += 1
    R += 1
    li[R].append((L,i))

BIT = [0] * (N+2)
bitadd(1,N,BIT)

for i in range(N):

    nc = c[i]
    lastind = last[nc]
    last[nc] = i+2

    bitadd(lastind,-1,BIT)
    bitadd(i+2,1,BIT)

    for l,qind in li[i+2]:
        ans[qind] = bitsum(i+2,BIT) - bitsum(l-1,BIT)

print (*ans)
Buggy python submission (verdict: Runtime Error):

N = int(input())

X = input()

o = 0
cnt = 0
for i in X:
    o *= 2
    if i == "1":
        o += 1
        cnt += 1

orev = [o % (cnt+1) , o % (cnt-1)]

for i in range(N):

    nb = int(X[i])

    if nb == 0:
        mod = cnt+1
        now = (orev[0] + pow(2,N-1-i,mod)) % mod

        ans = 1
        while now != 0:
            s = format( now , 'b' )
            one = 0
            for j in s:
                if j == "1":
                    one += 1
            now %= one
            ans += 1

        print (ans)

    else:
        mod = cnt-1
        now = (orev[1] - pow(2,N-1-i,mod)) % mod

        ans = 1
        while now != 0:
            s = format( now , 'b' )
            one = 0
            for j in s:
                if j == "1":
                    one += 1
            now %= one
            ans += 1

        print (ans)

Fixed submission:
import sys
N = int(input())

X = input()

o = 0
cnt = 0
for i in X:
    o *= 2
    if i == "1":
        o += 1
        cnt += 1

if cnt == 0:
    for i in range(N):
        print (1)
    sys.exit()
elif cnt == 1:
    orev = [o % (cnt+1) , None]
else:
    orev = [o % (cnt+1) , o % (cnt-1)]

for i in range(N):

    nb = int(X[i])

    if nb == 0:
        mod = cnt+1
        now = (orev[0] + pow(2,N-1-i,mod)) % mod

        ans = 1
        while now != 0:
            s = format( now , 'b' )
            one = 0
            for j in s:
                if j == "1":
                    one += 1
            now %= one
            ans += 1

        print (ans)

    else:
        if cnt == 1:
            print (0)
            continue
        
        mod = cnt-1
        now = (orev[1] - pow(2,N-1-i,mod)) % mod

        ans = 1
        while now != 0:
            s = format( now , 'b' )
            one = 0
            for j in s:
                if j == "1":
                    one += 1
            now %= one
            ans += 1

        print (ans)
Buggy python submission (verdict: Runtime Error):

N,A,B,C = map(int,input().split())

import sys

s = []

for i in range(N):

    S = input()
    s.append(S)

def dfs(dep,A,B,C):

    if dep == N:
        return []

    if s[dep] == "AB":

        if A == B == 0:
            return None
        elif A > B:
            cat = dfs(dep+1,A-1,B+1,C)
            if cat != None:
                cat.append("B")
                return cat
        else:
            cat = dfs(dep+1,A+1,B-1,C)
            if cat != None:
                cat.append("A")
                return cat

    elif s[dep] == "BC":
        
        if B == C == 0:
            return None
        elif B > C:
            cat = dfs(dep+1,A,B-1,C+1)
            if cat != None:
                cat.append("C")
                return cat
        else:
            cat = dfs(dep+1,A,B+1,C-1)
            if cat != None:
                cat.append("B")
                return cat

    else:

        if A == C == 0:
            return None
        elif A > C:
            cat = dfs(dep+1,A-1,B,C+1)
            if cat != None:
                cat.append("C")
                return cat
        else:
            cat = dfs(dep+1,A+1,B+1,C-1)
            if cat != None:
                cat.append("A")
                return cat

    return None

       
rev = dfs(0,A,B,C)

if rev == None:
    print ("No")
else:
    print ("Yes")
    for i in range(N-1,-1,-1):
        print (rev[i])
    
    

Fixed submission:

N,A,B,C = map(int,input().split())

import sys
sys.setrecursionlimit(500000)

s = []

for i in range(N):

    S = input()
    s.append(S)

def dfs(dep,A,B,C):

    if dep == N:
        return []

    if s[dep] == "AB":

        if A == B == 0:
            return None
        if A >= B:
            cat = dfs(dep+1,A-1,B+1,C)
            if cat != None:
                cat.append("B")
                return cat
        if A <= B:
            cat = dfs(dep+1,A+1,B-1,C)
            if cat != None:
                cat.append("A")
                return cat

    elif s[dep] == "BC":
        
        if B == C == 0:
            return None
        if B >= C:
            cat = dfs(dep+1,A,B-1,C+1)
            if cat != None:
                cat.append("C")
                return cat
        if B <= C:
            cat = dfs(dep+1,A,B+1,C-1)
            if cat != None:
                cat.append("B")
                return cat

    else:

        if A == C == 0:
            return None
        if A >= C:
            cat = dfs(dep+1,A-1,B,C+1)
            if cat != None:
                cat.append("C")
                return cat
        if A <= C:
            cat = dfs(dep+1,A+1,B,C-1)
            if cat != None:
                cat.append("A")
                return cat

    return None

       
rev = dfs(0,A,B,C)

if rev == None:
    print ("No")
else:
    print ("Yes")
    for i in range(N-1,-1,-1):
        print (rev[i])
Buggy python submission (verdict: Runtime Error):

import bisect

def LIS(lis):

    seq = []

    for c in lis:
        ind = bisect.bisect_left(seq,c)

        if ind == len(seq):
            seq.append(c)
        else:
            seq[ind] = c

    return len(seq)


N = int(input())

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

lis = [ [] for i in range(N) ]

for i in range(N-1):

    u,v = map(int,input().split())
    u -= 1
    v -= 1

    lis[u].append(v)
    lis[v].append(u)

ans = [None] * N

def dfs(v,p,ln):

    ind = bisect.bisect_left(ln,a[v])

    if ind == len(ln):
        mt = None
        ln.append(a[v])
    else:
        mt = ln[ind]
        ln[ind] = a[v]

    ans[v] = len(ln)

    for nex in lis[v]:

        if nex != p:
            dfs(nex,v,ln)

    if mt == None:
        del ln[-1]
    else:
        ln[ind] = mt

    return

dfs(0,0,[])

for i in range(N):
    print (ans[i])
    

Fixed submission:

import bisect
import sys
sys.setrecursionlimit(500000)

def LIS(lis):

    seq = []

    for c in lis:
        ind = bisect.bisect_left(seq,c)

        if ind == len(seq):
            seq.append(c)
        else:
            seq[ind] = c

    return len(seq)


N = int(input())

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

lis = [ [] for i in range(N) ]

for i in range(N-1):

    u,v = map(int,input().split())
    u -= 1
    v -= 1

    lis[u].append(v)
    lis[v].append(u)

ans = [None] * N

def dfs(v,p,ln):

    ind = bisect.bisect_left(ln,a[v])

    if ind == len(ln):
        mt = None
        ln.append(a[v])
    else:
        mt = ln[ind]
        ln[ind] = a[v]

    ans[v] = len(ln)

    for nex in lis[v]:

        if nex != p:
            dfs(nex,v,ln)

    if mt == None:
        del ln[-1]
    else:
        ln[ind] = mt

    return

dfs(0,0,[])

for i in range(N):
    print (ans[i])
Buggy python submission (verdict: Runtime Error):

X,Y,A,B,C = map(int,input().split())

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort()
q.sort()
r.sort()

p.reverse()
q.reverse()
r.reverse()

qi = 0
ri = 0

lis = [0]

for i in range(B+C):

    if (qi == Y or qi == B) and ri==C:
        lis.append(float("-inf"))
    elif qi == Y or qi == B:
        lis.append(lis[-1] + r[ri])
        ri += 1
    elif ri == C:
        lis.append(lis[-1] + q[qi])
        qi += 1
    elif q[qi] > r[ri]:
        lis.append(lis[-1] + q[qi])
        qi += 1
    else:
        lis.append(lis[-1] + r[ri])

ans = 0

n = 0
for i in range(X+1):

    if i != 0:
        n += p[i-1]

    ans = max(ans , n + lis[X+Y-i])

print (ans)


Fixed submission:

X,Y,A,B,C = map(int,input().split())

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort()
q.sort()
r.sort()

p.reverse()
q.reverse()
r.reverse()

qi = 0
ri = 0

lis = [0]

for i in range(B+C):

    if (qi == Y or qi == B) and ri==C:
        lis.append(float("-inf"))
    elif qi == Y or qi == B:
        lis.append(lis[-1] + r[ri])
        ri += 1
    elif ri == C:
        lis.append(lis[-1] + q[qi])
        qi += 1
    elif q[qi] > r[ri]:
        lis.append(lis[-1] + q[qi])
        qi += 1
    else:
        lis.append(lis[-1] + r[ri])
        ri += 1

#print (lis)
ans = 0

n = 0
for i in range(X+1):

    if i != 0:
        n += p[i-1]

    if len(lis) > X+Y-i:
        ans = max(ans , n + lis[X+Y-i])

print (ans)