Harryxun/stf_run huggingface dataset:
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
  now = st+1
  temp=[0]
  while(P[now-1] != st+1):
    temp.append(temp[-1]+C[P[now-1]-1])
    now = P[now-1]
    temp.append(temp[-1]+C[P[now-1]-1])
  if len(temp)>=K+1:
    ans=max(ans, max(temp[1:K+1]))
    continue
  if temp[-1]<=0:
    ans = max(ans, (max(temp[1:])))
  else:
    t=temp[-1]*(K//(len(temp)-1))-temp[-1]
    t+=max(max(temp),max([temp[-1]+temp[i+1] for i in range(K%(len(temp)-1))]))
    ans=max(ans, t)
		
print(int(ans))


### FIXED CODE ###

N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

maxscore = -(10 ** 18)

for st in range(N):
	cumsum = [0]
	now = st
	while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	cumsum.append(cumsum[-1] + C[st])
	
	m = len(cumsum) - 1
	loopsum = cumsum[-1]
	
	if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	score = (K // m - 1) * loopsum
	extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
	score += max(extended_cumsum)
	maxscore = max(maxscore, score)
print(maxscore)
x = int(input())
def solve1(x):
    for c in range(1, x+1):
        if 100*c <= x <= 105*c:
            print(1)
            exit()
    print(0)

def solve2(x):
    dp = [0 for _ in range(x+1)]
    for i in range(100, 106):
        dp[i] = 1

    for i in range(100, x+1):
        for j in range(6):
            if dp[i-(100+j)]:
                dp[i] = 1
    print(dp[-1])

solve2(x)

### FIXED CODE ###

x = int(input())
for c in range(1, x+1):
    if 100*c <= x <= 105*c:
        print(1)
        exit()
print(0)
import numpy as np
p = 10**9+7
def pow(X,k):
    if k==0:
        return np.eye(X.shape[0])
    if k==1:
        return X
    if k%2==0:
        return np.dot(pow(X,k//2),pow(X,k//2))%p
    else:
        return np.dot(X,np.dot(pow(X,(k-1)//2),pow(X,(k-1)//2))%p)%p
N,K = map(int,input().split())
A = np.zeros((N,N))
for i in range(N):
    a = np.array(list(map(int,input().split())))
    A[i][:] = a[:]
X = pow(A,K)
cnt = 0
for i in range(N):
    for j in range(N):
        cnt = (cnt+X[i][j])%p
print(int(cnt))

### FIXED CODE ###

p = 10**9+7
def pow(X,k):
    if k==0:
        return [[1 if i==j else 0 for j in range(N)] for i in range(N)]
    if k==1:
        return X
    if k%2==0:
        Y = pow(X,k//2)
        Z = [[0 for _ in range(N)] for _ in range(N)]
        for i in range(N):
            for j in range(N):
                for m in range(N):
                    Z[i][j] = (Z[i][j]+Y[i][m]*Y[m][j])%p
        return Z
    else:
        Y = pow(X,(k-1)//2)
        Z = [[0 for _ in range(N)] for _ in range(N)]
        for i in range(N):
            for j in range(N):
                for m in range(N):
                    Z[i][j] = (Z[i][j]+Y[i][m]*Y[m][j])%p
        Z1 = [[0 for _ in range(N)] for _ in range(N)]
        for i in range(N):
            for j in range(N):
                for m in range(N):
                    Z1[i][j] = (Z1[i][j]+Z[i][m]*X[m][j])%p
        return Z1
N,K = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
X = pow(A,K)
cnt = 0
for i in range(N):
    for j in range(N):
        cnt = (cnt+X[i][j])%p
print(int(cnt))
from collectiojns import deque
n=int(input())
a=list(map(int,input().split()))
b=deque()

for i in range(n):
  b.append(a[i])
  b=[::-1]
print(*b)

### FIXED CODE ###

N=int(input())
A=list(input().split())
s = " ".join(A[::-2]) + " " + " ".join(A[N%2::2])
print(s)
a,b=map(int,input().split())
print(max(max(a,b)-1,min(a,b))+e)

### FIXED CODE ###

a, b = map(int, input().split())
coin = 0
for i in range(2):
    if a > b:
        coin += a
        a -= 1
    else:
        coin += b
        b -= 1

print(coin)
A,B =map(int.input().split())

if(A>B*2):
  aaa=A*B
  print(aaa)
else:
  print(0)
  

### FIXED CODE ###

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

if(A>B*2):
  aaa=A-B*2
  print(aaa)
else:
  print(0)
n=int(input())
t,a=map(int,input().split())
h=list(map(int,input().solit()))
num=0
dist=abs(t-h[0]*0.006)
for i in range(1,n):
    if dist>abs((t-h[i]*0.006)):
        num=i
print(num+1)



### FIXED CODE ###

n=int(input())
t,a=map(int,input().split())
h=list(map(int,input().split()))
num=0
dist=abs(a-(t-h[0]*0.006))
for i in range(1,n):
    if dist>abs(a-(t-h[i]*0.006)):
        num=i
        dist=abs(a-(t-h[i]*0.006))
print(num+1)
H,W = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(b) for b in input().split()] for _ in range(H)]

dp = [[[False]*12802 for _ in range(W)] for _ in range(H)]
t = A[0][0]-B[0][0]
if t < 0:
    dp[0][0][-t*2] = True
    dp[0][0][-t*2-1] = True
elif t > 0:
    dp[0][0][2*t-1] = True
    dp[0][0][2*t] = True
else:
    dp[0][0][0] = True

for i in range(H):
    for j in range(W):
        if i > 0:
            for k in range(12802):
                if dp[i-1][j][k]:
                    if k % 2 == 0:
                        num = -k//2
                    else:
                        num = (k+1)//2
                    num += A[i][j]-B[i][j]
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    num -= (-A[i][j]+B[i][j])*2
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
        if j > 0:
            for k in range(12802):
                if dp[i][j-1][k]:
                    if k % 2 == 0:
                        num = -k//2
                    else:
                        num = (k+1)//2
                    num += A[i][j]-B[i][j]
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    num += (-A[i][j]+B[i][j])*2
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    
for k in range(12802):
    if dp[H-1][W-1][k]:
        ans = k//2 + k%2
        break
        
print(ans)

### FIXED CODE ###

H,W = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(b) for b in input().split()] for _ in range(H)]

C = [[0]*W for _ in range(H)]
num = 80
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j]-B[i][j])
        
dp = [[0]*W for _ in range(H)]
dp[0][0] |= (1<<num+C[0][0]) | (1<<num-C[0][0])

for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] |= (dp[i-1][j]<<num+C[i][j]) | (dp[i-1][j]<<num-C[i][j])
        if j > 0:
            dp[i][j] |= (dp[i][j-1]<<num+C[i][j]) | (dp[i][j-1]<<num-C[i][j])
            
b = list(bin(dp[-1][-1]))[::-1]
ans = 10**9
for i in range(len(b)):
    if b[i] == "1":
        ans = min(ans, abs(num*(H+W-1)-i))

print(ans)
a,b=map(int,raw_input().split())

if a==b
  print "a==b"
elseif a<b
  print "a<b"
else a>b
  print "a>b"

### FIXED CODE ###

a,b=map(int,raw_input().split())

if a==b:
	print "a == b"
elif a<b:
	print "a < b"
else:
	print "a > b"
import numpy

def gcd(a,b,mem):
    str12 = "{}.{}".format(a, b)
    if b==0:
        return a
    elif str12 in mem.keys():
        return mem[str12]
    else:
        mem[str12] = gcd(b,a%b,mem)
        return  mem[str12]




N = int(input())
A = numpy.array([int(x) for x in input().split()])
a2 = list(A)
max = 0
mem = {}
for i,_ in enumerate(A):
    A.pop(i)
    res = A[0]
    for i in A[1:]:
        res = gcd(i,res,mem)
    max = res if res >= max else max
    A = list(a2)


print(max)

### FIXED CODE ###

def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)


gcdL = []
gcdR = []

N = int(input())
A = [int(x) for x in input().split()]
gcdL.append(0)
length_A = len(A)
for i in range(1,length_A):
    gcdL.append(gcd(A[i-1],gcdL[i-1]))


gcdR.append(0)
for i in range(1,length_A):
    gcdR.append(gcd(A[length_A-i],gcdR[i-1]))



list_gcd = []
for i in range(length_A):
        list_gcd.append(gcd(gcdL[i],gcdR[length_A-i-1]))

print(max(list_gcd))
N = int(input())
ll = map(int,input())

print(max(ll)-min(ll))

### FIXED CODE ###

N = int(input())
ll = list(map(int,input().split()))
 

print(max(ll)-min(ll))
a,b,k=map(int,input().split())
count=0
for i in range(1,ab):
  if a%i==0 and b%i==0:
    count+=1
    if count==k:
      print(i)
      exit()
    


### FIXED CODE ###

a,b,k=map(int,input().split())
count=0
for i in range(105):
  j=101-i
  if a%j==0 and b%j==0:
    count+=1
    if count==k:
      print(j)
      exit()
N = int(input())
A = list(map(int, input().split(' ')))
A.sort().reverse()

last_num = 0
first_second_biggest_numpairs = []
for i in range(N):
    if A[i] == last_num:
        if len(first_second_biggest_numpairs) >= 2:
            break
        first_second_biggest_numpairs.append(A[i])
    last_num = A[i]

if len(first_second_biggest_numpairs) == 2:
    print(str(first_second_biggest_numpairs[0] * first_second_biggest_numpairs[1]))

print("0")



### FIXED CODE ###

N = int(input())
A = list(map(int, input().split(' ')))
A = sorted(A)
A = A[::-1]

first_second_biggest_numpairs = []

flag = False
for i in range(N):
    if flag == True:
        flag = False
        continue
    if i + 1 < N:
        if A[i] == A[i + 1]:
            if len(first_second_biggest_numpairs) < 2:
                first_second_biggest_numpairs.append(A[i])
                flag = True
            else:
                break
            
if len(first_second_biggest_numpairs) == 2:
    print(str(first_second_biggest_numpairs[0] * first_second_biggest_numpairs[1]))
else:
    print("0")
x,a,b=map(int,input().split())
if b-a<=0:
  print("delicious")
elif b-a=0 and x-(b-a)>=0:
  print("safe")
else:
  print("dangerous")


### FIXED CODE ###

x,a,b=map(int,input().split())
if b-a<=0:
  print("delicious")
elif b-a>0 and x-(b-a)>=0:
  print("safe")
else:
  print("dangerous")
n,k = map(int,input().split())
a = list(map(int,input().split()))
pos = 0
visit = [0] * n
move = []
loop = []
while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move[k]) > k:
    print(move[k]+1)
else:
    print(loop[(k-(len(move)-len(loop)))%len(loop)]+1)

### FIXED CODE ###

n,k = map(int,input().split())
a = list(map(int,input().split()))
pos = 0
visit = [0] * n
move = []
loop = []
while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move) > k:
    print(move[k]+1)
else:
    print(loop[(k-(len(move)-len(loop)))%len(loop)]+1)
X,t=map(int,input().split())

if X-t >= o:
  print(X-t)
else:
  print(0)

### FIXED CODE ###

X,t=map(int,input().split())

if X-t >= 0:
  print(X-t)
else:
  print(0)
H=input(), W=input()
H=int(H),W=int(W)
h=input(),w=input() 
h=int(h),w=int()
print((H-h)*(W-w))

### FIXED CODE ###

H,W=map(int,input().split())#標準入力を行う
h,w=map(int,input().split())#標準入力を行う
print((H-h)*(W-w))
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
logging.disable(logging.CRITICAL)

N, K = (int(x) for x in input().split())
L = list(int(x) for x in input().split())

logging.info(L)

counter = 0

for i in range(0, N):
    for j in range(i, N):
        if sum(L[i:j+1]) > K:
            counter = counter + 1

print(counter)

### FIXED CODE ###

X, A = (int(x) for x in input().split())

if X < A:
    print(0)
else:
    print(10)
N = int(input)
for i in range(11):
	money = i * 1000
	if money - N >= 0:
		print(money - N)
		break

### FIXED CODE ###

N = int(input())

for i in range(11):
    money = i * 1000
    if money - N>=0:
        print(money - N)
        break
from collections import defaultdict

MOD = (10 ** 9) + 7

def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)

n = int(raw_input())

ns = []
d = defaultdict(int)

for i in xrange(n):
    a, b = map(int, raw_input().split())
    ns.append((a, b))
    if a and b:
        s = 1 if a * b >= 0 else -1
        g = gcd(abs(a), abs(b))
        m1 = (s * abs(a) / g, abs(b) / g)
        m2 = (-s * abs(b) / g, abs(a) / g)
    elif a == 0 and b == 0:
        continue
    elif a == 0:
        m1 = (1, 0)
        m2 = (0, 1)
    elif b == 0:
        m1 = (0, 1)
        m2 = (1, 0)

    d[m1] += 1
    d[m2] += 0

res = 0
for i in xrange(1 << n):
    fs = []
    flag = True
    for j in xrange(n):
        if i & (1 << j):
            fs.append(ns[j])
    for f1 in fs:
        for f2 in fs:
            (a1, b1) = f1
            (a2, b2) = f2
            if a1 * a2 + b1 * b2 == 0:
                flag = False
                break
        if not flag:
            break
    if flag:
        res += 1
print res - 1

pre = 1
for k in d.keys():
    if k[0] < 0:
        assert (k[1], -k[0]) in d
        continue
    elif k[0] == 0:
        pre = pre * d[k] % MOD
    else:
        k1 = k
        k2 = (-k[1], k[0])

        tot = pow(2, d[k1], MOD) + pow(2, d[k2], MOD) - 1
        pre = pre * tot % MOD
print (pre - 1 + MOD) % MOD


### FIXED CODE ###

from collections import defaultdict

MOD = (10 ** 9) + 7
assert MOD == 1000000007

def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)

n = int(raw_input())

ns = []
d = defaultdict(int)
zero = 0

for i in xrange(n):
    a, b = map(int, raw_input().split())
    ns.append((a, b))
    if a and b:
        s = 1 if a * b >= 0 else -1
        g = gcd(abs(a), abs(b))
        m1 = (s * abs(a) / g, abs(b) / g)
        m2 = (-s * abs(b) / g, abs(a) / g)
    elif a == 0 and b == 0:
        zero += 1
        continue
    elif a == 0:
        m1 = (1, 0)
        m2 = (0, 1)
    elif b == 0:
        m1 = (0, 1)
        m2 = (1, 0)

    d[m1] += 1
    d[m2] += 0

'''
res = 0
for i in xrange(1 << n):
    fs = []
    flag = True
    for j in xrange(n):
        if i & (1 << j):
            fs.append(ns[j])
    for i, f1 in enumerate(fs):
        for f2 in fs[i + 1:]:
            (a1, b1) = f1
            (a2, b2) = f2
            if a1 * a2 + b1 * b2 == 0:
                flag = False
                break
        if not flag:
            break
    if flag:
        res += 1
print res - 1
'''

pre = 1
for k in d.keys():
    if k[0] <= 0:
        assert (k[1], -k[0]) in d
        continue
    else:
        k1 = k
        k2 = (-k[1], k[0])

        tot = pow(2, d[k1], MOD) + pow(2, d[k2], MOD) - 1
        pre = pre * tot % MOD
print (pre - 1 + zero + MOD) % MOD
import java.util.*;
import java.io.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();

        System.out.println( Math.max( (Math.max(a*c,a*d)),(Math.max(b*c,b*d)) ) );



    }
}


### FIXED CODE ###

a, b, c, d = list(map(int, input().split()))
print(max((a*c),(a*d),(b*c),(b*d)))
n = int(input())
print(10**n-9**n-8**n-9**n)

### FIXED CODE ###

a,b,c,d = map(int,input().split())
ac = a*c
ad = a*d
bc = b*c
bd = b*d

if ac>ad and ac>bc and ac>bd:
  print(ac)
elif ad >bc and ad>bd and ad>ac:
  print(ad)
elif bc>ac and bc>ad and bc>bd:
  print(bc)
else:
  print(bd)
aord = ord("a")
s = list(raw_input())
k = int(input())
i = 0

while k > 0 and i < len(s):
	if k >= (aord + 26 - ord(s[i])):
		k -= (aord + 26 - ord(s[i]))
		s[i] = "a"

	i += 1

s[len(s)-1] = chr(ord(s[len(s)-1]) + k)
print ''.join(s)

### FIXED CODE ###


aord = ord("a")
s = list(raw_input())
k = int(input())
i = 0

while k > 0 and i < len(s):
	if k >= ord('z') - ord(s[i]) + 1:
		if s[i] != "a":
			k -= ord('z') - ord(s[i]) + 1
			s[i] = "a"

	i += 1
k = k%26
s[len(s)-1] = chr(ord(s[len(s)-1]) + k)
print ''.join(s)
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k-1])


### FIXED CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)
a = []
for line in sys.stdin:
    a.append(int(line))

for i in range(len(a)):
	print("Case ",i+1,": ",a[i],sep="")

### FIXED CODE ###

a = []
while True:
    n = input()
    if n == "0":
        break
    a.append(n)


for i in range(len(a)):
	print("Case ",i+1,": ",a[i],sep="")
x^3


### FIXED CODE ###

number = int(input())
print(number**3)
n = int(input())
p= []
AC = 0
WA = 0
TLE = 0
RE = 0

for i in range(n):
  p.append(input())
  
for j in p :
  if j == "AC":
    AC += 1
  elif j == "WA":
    WA += 1
  elif j == "TLE":
    TLE += 1
  else:
    RE += 1
  
print("AC x " + AC)
print("WA x " + WA)
print("TLE x " + TLE)
print("RE x " + RE)
  
  



### FIXED CODE ###

n = int(input())
p= []
AC = 0
WA = 0
TLE = 0
RE = 0
 
for i in range(n):
  p.append(input())
  
for j in p :
  if j == "AC":
    AC += 1
  elif j == "WA":
    WA += 1
  elif j == "TLE":
    TLE += 1
  else:
    RE += 1


  
print("AC x " + str(AC))
print("WA x " + str(WA))
print("TLE x " + str(TLE))
print("RE x " + str(RE))
# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack[-1] == "T" or stack[0] == "":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))


### FIXED CODE ###

# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack == [] or stack[-1] == "T":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))
# coding:utf-8
import readline
import datetime

H1 = int(input())
M1 = int(input())
H2 = int(input())
M2 = int(input())
K = int(input())

while 0 <= H1 < 23 and 0 <= M1 < 60 and 0 <= H2 < 23 and 0 <= M2 < 60:
    if M2 > M1:
        a = (H2 - H1) * 60 + M2 - M1
        print(a - K)
        break
    else:
        M2 < M1
        b = (H2 - H1) * 60 + M1 - M2
        print(b - K)
        break
else:
	H1 = int(input())
	M1 = int(input())
	H2 = int(input())
	M2 = int(input())
	K = int(input())

### FIXED CODE ###

import sys
readline = sys.stdin.buffer.readline

h1, m1, h2, m2, k = map(int, readline().split())
print(60 * (h2 - h1) + m2 - m1 - k)
import sys
input = sys.stdin.readline

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    return fact[n]*inv(fact[r])*inv(fact[n-r])

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        r = j-i+1
        
        if j==N-1:
            r = N-2-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

### FIXED CODE ###

N = int(input())
x = list(map(int, input().split()))
MOD = 10**9+7
D = x[-1]-x[0]
ans = 0
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)

inv_table = [-1]*N

for i in range(N):
    inv_table[i] = pow(i, MOD-2, MOD)
    
for i in range(N-1):
    d = D-(x[i]-x[0])
    ans += d*fact[N-1]*inv_table[i+1]
    ans %= MOD

print(ans)
import heapq
x,y,a,b,c=map(int,input().split())
def _int(x):
    return -int(x)
p=list(map(_int,input().split()))
q=list(map(_int,input().split()))
r=list(map(_int,input().split()))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)
ans=0
for i in range(x):
    if len(r)==0:
        _p=heapq.heappop(p)
        ans-=_p
        continue
    _p,_r=heapq.heappop(p),heapq.heappop(r)
    if _p<_r:
        ans-=_p
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(p,_p)
for i in range(y):
    if len(r)==0:
        _q=heapq.heappop(q)
        ans-=_q
    _q,_r=heapq.heappop(q),heapq.heappop(r)
    if _q<_r:
        ans-=_q
        heapq.heappush(r,_r)
    else:
        ans-=_r
        heapq.heappush(q,_q)
print(ans)
    


### FIXED CODE ###

import heapq
x,y,a,b,c=map(int,input().split())
def _int(x):
    return -int(x)
p=list(map(_int,input().split()))
q=list(map(_int,input().split()))
r=list(map(_int,input().split()))
heapq.heapify(p)
heapq.heapify(q)
heapq.heapify(r)

ans=[]
heapq.heapify(ans)
for i in range(x):
    _p=heapq.heappop(p)
    heapq.heappush(ans,-_p)
for i in range(y):
    _q=heapq.heappop(q)
    heapq.heappush(ans,-_q)
#ansは小さい順、rは大きい順
for i in range(x+y):
    if len(r)==0:break
    _ans=heapq.heappop(ans)
    _r=-heapq.heappop(r)
    if _r>=_ans:
        heapq.heappush(ans,_r)
    else:
        heapq.heappush(ans,_ans)
        break
print(sum(ans))
import heapq

n, q = map(int, input().split())

lv =[[] for _ in range(n)]
ans=[0] * (n)

def dfs(v,p=-1):
    for u in lv[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)

for _ in range(n-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    lv[a].append(b)
    lv[b].append(a)

for _ in range(q):
    p,x = map(int,input().split())
    p-=1
    ans[p] += x

dfs(0)

for i in ans:print(i)

### FIXED CODE ###

I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
 
 
from sys import stdin,setrecursionlimit
#from copy import deepcopy
 
setrecursionlimit(10**6)
input=stdin.readline
 
def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)
 
n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
 
for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    
 
dfs(0)
 
print(*counter)
N = int(input())
if N[-1] == 2 or N[-1] == 4 or N[-1] == 5 or N[-1] == 7 or N[-1] == 9:
    print("hon")
elif N[-1] == 0 or N[-1] == 1 or N[-1] == 6 or N[-1] == 8:
    print("pon")
else:
    print("bon")

### FIXED CODE ###

N = input()
n = N[-1]
if n == "2" or n == "4" or n == "5" or n == "7" or n == "9":
    print("hon")
elif n == "0" or n == "1" or n == "6" or n == "8":
    print("pon")
else:
    print("bon")
if (int(input())*int(input()))%2==0:
  print('Even')
else:
  print('Odd')


### FIXED CODE ###

a,b=map(int,input().split())
if (a*b)%2==0:
  print('Even')
else:
  print('Odd')
N, K = map(int, input().split())
list0 = [list(map(int, input().split())) for i in range(2*K-1)]
listd = list0[1::2]
listf = listd[0]
count = 0
for i in range(1, K-1):
  listf = listf + listd[i]

for i in range(1, N):
  if i not in listf:
    count = count + 1

print(count)

### FIXED CODE ###

N, K = map(int, input().split())
ans = [0]*N
for i in range(K):
  d = int(input())
  for j in list(map(int, input().split())):
    ans[j-1] = 1
print(ans.count(0))
import collections
import math
n=int(input())
s=[]
for i in range(n):
    sl=list(input())
    sl.sort()
    s.append("".join(sl))

l=collections.Counter(s).values()
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

c=0
for i in range(len(l)):
    if l[i]!=1:c+=combinations_count(l[i],2)
print(c)

### FIXED CODE ###

import collections
import math
n=int(input())
s=[]
for i in range(n):
    sl=list(input())
    sl.sort()
    s.append("".join(sl))

l=list(collections.Counter(s).values())
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

c=0
for i in range(len(l)):
    k=int(l[i])
    if k!=1:c+=combinations_count(k,2)
print(c)