Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

s = input()
n = int(input())

head = []
div = True
for i in range(n):
    q = input().split(' ')
    if q[0] == '1':
        div = not div
    elif (q[1] == '1' and div) or (q[1] == '2' and not div):
        s = q[2] + s
    else:
        s = s + q[2]


if div:
    print(s)
else:
    s = s[::-1]
    print(s)


### FIXED CODE ###

from collections import deque

s = input()
n = int(input())
ans = deque(s)

div = True
for i in range(n):
    q = input().split(' ')
    if q[0] == '1':
        div = not div
    elif (q[1] == '1' and div) or (q[1] == '2' and not div):
        ans.appendleft(q[2])
    else:
        ans.append(q[2])

if div:
    print(''.join(ans))
else:
    print(''.join(reversed(ans)))
### BUGGY CODE ###

n,k = map(int, input().split())
a = [n]
i = 0
if n%k == 0:
    print(0)
else:
    while i >= 0:
        n = abs(n-k)
        a.append(n)
        i += 1
        if a[i] > a[i-1]:
            print(min(a))
            break

### FIXED CODE ###

n,k = map(int, input().split())
a,b = divmod(n,k)
print(min(b,k-b))
### BUGGY CODE ###

a,b=map(int,input().split())
ans=0
for i in range(a+1):
  for j in range(a+1):
    for k in range(a+1):
      if i+j+k==b:
        ans+=1
print(ans)

### FIXED CODE ###

a,b=map(int,input().split())
ans=0
for i in range(a+1):
  for j in range(a+1):
      if 0<=b-i-j<=a:
        ans+=1
print(ans)
### BUGGY CODE ###

from collections import deque
from bisect import bisect_left
S = input()
# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1
mod = 10**9 + 7

for i in range(len(S)):
  x = S[-(i+1)]
  if x == '?':
    for k in range(10):
      for j in range(13):
        dp[i+1][(mul*k+j)%13] += dp[i][j]
        dp[i+1][(mul*k+j)%13] %= mod
  else:
    k = int(x)
    for j in range(13):
      dp[i+1][(mul*k+j)%13] += dp[i][j]
      dp[i+1][(mul*k+j)%13] %= mod
  mul = mul*10 % 13  
print(dp[len(S)][5])

### FIXED CODE ###

from collections import deque
from bisect import bisect_left
S = input()
# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1
mod = 10**9 + 7

for i in range(len(S)):
  x = S[-(i+1)]
  if x == '?':
    for k in range(10):
      for j in range(13):
        dp[i+1][(mul*k+j)%13] += dp[i][j]
        dp[i+1][(mul*k+j)%13] %= mod
  else:
    k = int(x)
    for j in range(13):
      dp[i+1][(mul*k+j)%13] += dp[i][j]
      dp[i+1][(mul*k+j)%13] %= mod
  mul = mul*10 % 13  
print(dp[len(S)][5])
### BUGGY CODE ###

#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections

N = int(sys.stdin.readline())
a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param

from math import gcd

pairwise = True
setwise = True
acc = a[0]
for i in range(N):
    for j in range(i+1,N):
        b = gcd(a[i],a[j])
        if b != 1:
            pairwise = False
    acc = gcd(acc,a[i])
if acc != 1:
    setwise = False
if pairwise == True:
    print("pairwise coprime")
elif setwise == True:
    print("setwise coprime")
else:
    print("not coprime")


### FIXED CODE ###

import math
from collections import defaultdict
pc = True
n = int(input())
a = list(map(int,input().split()))
g = math.gcd(a[0],a[1])
for i in range(2,n):
    g = math.gcd(g,a[i])
M = max(a)
class Sieve_of_Eratosthenes:
    def __init__(self, N):
        self.sieve = [-1] * (N+1)
 
        for i in range(2,N+1):
            if self.sieve[i] == -1:
                for j in range(1,1+N//i):
                    self.sieve[i*j] = i
 
    def isprime(self, num):
        if num <= 1:
            return False
        else:
            return self.sieve[num] == num
 
    def factorization(self, num):
        ret = set([])
        while num != 1:
            div = self.sieve[num]
            ret.add(div)
            num //= div
        return ret
 
sofe = Sieve_of_Eratosthenes(M)
ddic = defaultdict()
judge = set([])
 
for i in a:
    if not pc:
        break
    asf = sofe.factorization(i)
    
    if judge & asf != set():
        pc = False
    judge |= asf
if pc:
    print("pairwise coprime")
elif g == 1:
    print("setwise coprime")
else:
    print("not coprime")
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
for _ in range(Q):
  ans = 0
  l, r = map(int, input().split())
  for i in range(l - 1, r - 1):
      if S[i] == "A" and S[i + 1] == "C":
         ans += 1
  print(ans)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
ac = [0, 0]
for i in range(len(S) - 1):
  if S[i] == "A" and S[i + 1] == "C":
    ac.append(ac[-1] + 1)
  else:
    ac.append(ac[-1])
for _ in range(Q):
  l, r = map(int, input().split())
  print(ac[r] - ac[l])
### BUGGY CODE ###

HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])

list1=[]
for i in range(H):
    list2=[]
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        
        list2.append(int(line[j]))
    list1.append(list2)
#print(list1)


    
N=int(input())
#print(N)

for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            
            a=0
            for a in range(H):
                if L in list1[a]:
                    L1=[a+1,list1[a].index(L)+1]
            b=0    
            for b in range(H):
                if L+D in list1[b]:
                    L2=[b+1,list1[b].index(L+D)+1]
            
            count=count+((L1[0]-L2[0])**2)**0.5+((L1[1]-L2[1])**2)**0.5
            L=L+D
        print(int(count))


### FIXED CODE ###

H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])
### BUGGY CODE ###

INF = (10 ** 9) + 7
N, M = map(int, input().split())
aLst = [0] * M

for i in range(M):
    aLst[i] = int(input())
ansLst = [0] * (N + 1)
ans = 0
ansLst[0] = 1
for i in range(1, N + 1):
    if aLst.count(i) == 1:
        continue
    tmp = ansLst[i - 1]
    if i > 1:
        tmp += ansLst[i - 2]
    ansLst[i] = tmp % INF


print(ansLst[N])


### FIXED CODE ###

INF = (10 ** 9) + 7
N, M = map(int, input().split())

aLst = set(int(input()) for _ in range(M))
ansLst = [0] * (N + 1)
ansLst[0] = 1

for i in range(1, N + 1):
    if i in aLst:
        continue
    tmp = ansLst[i - 1]
    if i > 1:
        tmp += ansLst[i - 2]
    ansLst[i] = tmp % INF


print(ansLst[N])
### BUGGY CODE ###

n,k = map(int,input().split())
h = [int(i) for i in input().split()]
dp = [1000000000 for i in range(n)]
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        if i-j < 0 : break
        dp[i] = min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
print(dp[n-1])


### FIXED CODE ###

n,k = map(int,input().split())
h = [int(i) for i in input().split()]
dp = [1000000000 for i in range(n)]
dp[0] = 0
for i in range(n):
    for j in range(1,k+1):
        if i-j < 0 : break
        dp[i] = min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
print(dp[n-1])
### BUGGY CODE ###

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



A = list(A)
for _ in range(Q):
	# replace every element whose value is B with C
	B, C = map(int, input().split())

	for i in range(len(A)):
		if A[i] == B:
			A[i] = C
	print(sum(A))

### FIXED CODE ###

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



A = list(A)

hashmap = {}
s = 0
for i in range(len(A)):
	s += A[i]
	hashmap[A[i]] = hashmap.get(A[i], [0, A[i]]) 
	hashmap[A[i]][0] += 1

for _ in range(Q):
	# replace every element whose value is B with C
	B, C = map(int, input().split())

	if B in hashmap:
		num = hashmap[B][0]
		del hashmap[B]
		s -= num * B
		s += num * C

		hashmap[C] = hashmap.get(C, [0, C])
		hashmap[C][0] += num
	print(s)
### BUGGY CODE ###

n = int(input())
l = []
l[:] = map(int,input().split())
l.sort()

c = 0
for i in range(0,n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if(l[i]+l[j]>l[k]):
                c+=1
            else:
                continue
print(c)


### FIXED CODE ###

from bisect import bisect_right

n = int(input())
l = list(map(int,input().split()))
l.sort() # 昇順にソート

count = 0
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1, n-1):
        ab = a+l[j]
        # bisect_left(a, x): x を a に挿入できる点を返す
        # cの最大値は a+b-1
        k = bisect_right(l, ab-1, j+1, n)
        count += k-(j+1)
print(count)
### BUGGY CODE ###

N = int(input())
S = input()
ans = 0
for i in range(1,N):
    c = 0
    cc = 0
    for j in range(N-i):
        if S[j] == S[i+j]:
            cc += 1
        else:
            c = max(cc,c)
            cc = 0
    c = max(cc,c)
    c = min(i,c)
    ans = max(ans,c)
print(ans)


### FIXED CODE ###

N = int(input())
S = input()
ans = 0
for i in range(1,N):
    c = 0
    cc = 0
    for j in range(N-i):
        if S[j] == S[i+j]:
            cc += 1
        else:
            c = max(cc,c)
            cc = 0
    c = max(cc,c)
    c = min(i,c)
    ans = max(ans,c)
print(ans)
### BUGGY CODE ###

import sys
aa=list(map(int,sys.stdin.read().split()))
aa=aa[1:]
n=len(aa)
bb=[]
for i in range(n):
    if not bb:
        bb.append(aa[i])
    else:
        if bb[0] >= aa[i]:
            bb = [aa[i]] + bb
        else:
            for j in range(len(bb)):
                if bb[-(j+1)] < aa[i]:
                    bb[-(j+1)] = aa[i]
                    bb = bb[:-(j+1)] + sorted(bb[-(j+1):])
                    break
print(len(bb))

### FIXED CODE ###

import sys
aa=list(map(int,sys.stdin.read().split()))
aa=aa[1:aa[0]+1]
n=len(aa)
import bisect
bb=[]
for i in range(n):
    if not bb:
        bb.append(aa[-i-1])
    else:
        if bb[-1] <= aa[-i-1]:
            bb.append(aa[-i-1])
        else:
            j = bisect.bisect_right(bb,aa[-i-1])
            bb[j] = aa[-i-1]
print(len(bb))
### BUGGY CODE ###

n,k =map(int,input().split())
s =[]
for i in range(k):
    l,r=map(int,input().split())
    s+=(list(range(l,r+1)))
S= set(s)

mod = 998244353

step = [0] * (n+1)

step[0]=1
for i in range(n):
    for s in S:
        if i+s<=n:
            step[i+s]=(step[i]+step[i+s])%mod
        else:
            break
            # print(step)
# print(step)
print(step[n-1]%mod)

### FIXED CODE ###

mod = 998244353
n,k =map(int,input().split())
step =[0]*(n+1)
# step[0]=1
step[1]=1
stepsum=[0]*(n+1)
stepsum[1]=1
l=[0]*k
r=[0]*k
for i in range(k):
    l[i],r[i]=map(int,input().split())

for i in range(2,n+1):
    for j in range(k):
        li = i - r[j]
        ri = i - l[j]
        if ri <= 0:
            continue
        # li = max(1,li)
        step[i] += stepsum[ri] - stepsum[max(0,li-1)]
        # step[i] %= mod
        # print(step)
    stepsum[i] = ( stepsum[i-1] + step[i] )%mod


print(step[n]%mod)
### BUGGY CODE ###

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [True] * n
c = Counter(a)
for i in range(n):
    if b[i]:
        for j in range(i+1, n):
            if a[j] % a[i] == 0 and i != j:
                b[j] = False
ans = 0
for i in range(n):
    if b[i] and c[a[i]] == 1:
        ans += 1
print(ans)

### FIXED CODE ###

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a.sort()
dp = [0] * a[-1]
c = Counter(a)
for i in a:
    for j in range(i, a[-1]+1, i):
        dp[j-1] += 1
ans = 0
for i in a:
    if dp[i-1] == 1 and c[i]:
        ans += 1
print(ans)
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]



    xxxx = 0
    while True:
        xxxx += 1
        imos = [0] * (N + 2)
        if xxxx == 100 or xxxx == K + 1:
            break
        for i, a in enumerate(A):
            imos[max(0, i + 1 - a)] += 1
            imos[min(N + 1, i + 1 + a + 1)] -= 1

        ruiseki = [0] * (N + 2)
        for i in range(N + 2):
            ruiseki[i] = imos[i] + ruiseki[i - 1]

        for i, v in enumerate(ruiseki[1:N + 1]):
            A[i] = v

    print(*ruiseki[1:N + 1])



if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]



    xxxx = 0
    while True:
        xxxx += 1
        imos = [0] * (N + 2)
        if xxxx == 100 or xxxx == K + 1:
            break
        for i, a in enumerate(A):
            imos[max(0, i + 1 - a)] += 1
            imos[min(N + 1, i + 1 + a + 1)] -= 1

        ruiseki = [0] * (N + 2)
        for i in range(N + 2):
            ruiseki[i] = imos[i] + ruiseki[i - 1]

        for i, v in enumerate(ruiseki[1:N + 1]):
            A[i] = v

    print(*ruiseki[1:N + 1])



if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math
k = int(input())
ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += math.gcd(x,math.gcd(y,z))
print(ans)

### FIXED CODE ###

import math
k = int(input())
ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += math.gcd(x,math.gcd(y,z))
print(ans)
### BUGGY CODE ###

import math
import itertools
import sys

n=int(input())
all=1
for i in range(1,n+1):
    all*=i
print(all%(10**9+7))

### FIXED CODE ###

import sys
import math
import itertools
MOD=10**9+7
n = int(input())
ans=1
for i in range(1, n + 1):
    ans =ans%MOD*i
print(ans%MOD)
### BUGGY CODE ###

from collections import Counter

N, P = list(map(int, input().split()))
S = [int(c) for c in input().rstrip()]


if P in [2, 5]:
    total = 0
    for i, x in enumerate(S):
        if x % P == 0:
            # If P == 2
            # all substring ending here with last digit even
            # If P == 5
            # all substring ending here with last digit 5 or 0
            total += i + 1
    print(total)
else:
    # Note P is relatively prime with 10

    # If S is '1234'
    # Want suffixes:
    # s[4] = 1234
    # s[3] =  234
    # s[2] =   34
    # s[1] =    4
    # s[0] =    0
    #
    # Then s[j] - s[i] gives the value of the number int(S[-j:-i]) * 10**(N -i)
    # Since P doesn't divide 10, if P divides s[j] - s[i] it also divides the number

    suf = [0]
    power = 1
    for x in reversed(S):
        suf.append(suf[-1] + x * power)
        power *= 10
    if False:
        # Check
        N = len(S)
        for i in range(N):
            for j in range(i + 1, N + 1):
                print(
                    i,
                    j,
                    int("".join(map(str, S[N - j : N - i]))) * (10 ** i),
                    suf[j] - suf[i],
                )
        print(suf)

    # Count number of s[j] - s[i] pairs that are 0 mod P
    total = 0
    counts = Counter()
    for x in suf:
        x = x % P
        total += counts[x]
        counts[x] += 1
    print(total)


### FIXED CODE ###

from collections import Counter

N, P = list(map(int, input().split()))
S = [int(c) for c in input().rstrip()]


if P in [2, 5]:
    total = 0
    for i, x in enumerate(S):
        if x % P == 0:
            # If P == 2
            # all substring ending here with last digit even
            # If P == 5
            # all substring ending here with last digit 5 or 0
            total += i + 1
    print(total)
else:
    # Note P is relatively prime with 10

    # If S is '1234'
    # Want suffixes:
    # s[4] = 1234
    # s[3] =  234
    # s[2] =   34
    # s[1] =    4
    # s[0] =    0
    #
    # Then s[j] - s[i] gives the value of the number int(S[-j:-i]) * 10**(N -i)
    # Since P doesn't divide 10, if P divides s[j] - s[i] it also divides the number

    CHECK = False
    suf = [0]
    power = 1
    for x in reversed(S):
        suf.append(suf[-1] + x * power)
        power *= 10
        if not CHECK:
            # Don't mod when checking
            suf[-1] %= P
            power %= P
    if CHECK:
        # Check
        N = len(S)
        for i in range(N):
            for j in range(i + 1, N + 1):
                print(
                    i,
                    j,
                    int("".join(map(str, S[N - j : N - i]))) * (10 ** i),
                    suf[j] - suf[i],
                )
        print(suf)

    # Count number of s[j] - s[i] pairs that are 0 mod P
    total = 0
    counts = Counter()
    for x in suf:
        x = x % P
        total += counts[x]
        counts[x] += 1
    print(total)
### BUGGY CODE ###

p = int(input())
*A, = map(int, input().split())
B = [0]*p

C = [0]*(p+1)
C[0] = 1
D = [0]*(p+1)
zeros = [0]*(p+1)

Y = [0]*(p+1)

R = [0]*p
for i in range(p):
    v = 0
    b = 1
    for j in range(i):
        v += Y[j] * b % p
        b = b * (i-j) % p
    Y[i] = y = (A[i]-v) * pow(b, p-2, p) % p

    for j in range(i+1):
        R[j] += y * C[j] % p

    D[:] = zeros
    for j in range(p):
        D[j] -= C[j]*i
    for j in range(p):
        D[j+1] += C[j]
    C, D = D, C
for i in range(p):
    R[i] %= p
print(*R)

### FIXED CODE ###

def solve():
    p = int(input())
    *A, = map(int, input().split())
    B = [0]*p

    C = [0]*p
    C[0] = 1
    zeros = [0]*p

    Y = [0]*p
    R = [0]*p
    for i in range(p):
        v = 0
        b = 1
        for j in range(i):
            v += Y[j] * b
            b = b * (i-j) % p
        Y[i] = y = (A[i]-v) * pow(b, p-2, p) % p

        for j in range(i+1):
            R[j] += y * C[j]

        if i < p-1:
            C[1:] = ((b - a*i) % p for a, b in zip(C[1:], C))
            C[0] = 0
    for i in range(p):
        R[i] %= p
    print(*R)
solve()
### BUGGY CODE ###

import sys


def main():
	input = sys.stdin.readline
	N, M, L = list(map(int,input().split()))
	P = [list(map(int,input().split())) for i in range(M)]
	Q = int(input())
	st = [list(map(int,input().split())) for i in range(Q)]

	neighbor = [[10**10]*N for i in range(N)]
	for s,e,d in P:
		# if d<=L:
		# 	neighbor[s-1].append((e-1,d))
		# 	neighbor[e-1].append((s-1,d))
		if d<=L:
			neighbor[s-1][e-1] = neighbor[e-1][s-1] = d

	# start, end = 2,1

	fuellist = [0]*N
	def bfs(start, end):
		fuel = [[10000000,L+1] for i in range(N)]
		fuel[start][0] = 0
		fuel[start][1] = L
		queue = [start]
		while len(queue)>0:
			nq = []						# next target node
			nq_append = nq.append
			for i in queue:				# target node
				oc, om = fuel[i]
				for j, d in enumerate(neighbor[i]):	# next node
					if d==10**10: continue
					if om<d:
						nc = oc+1
						nm = L-d
					else:
						nc = oc
						nm = om-d

					# print(i,j,nc,nm)

					if (nc<fuel[j][0]) or (nc==fuel[j][0] and nm>fuel[j][1]):
						fuel[j][0], fuel[j][1] = nc, nm
						nq_append(j)

			queue = nq					# update target node

		ans = fuel[end][0]
		if ans==10000000: ans = -1

		fuellist[start] = fuel

		print(ans)


	for s, t in st:
		start, end = s-1, t-1
		if fuellist[start]==0:
			bfs(start,end)
		else:
			ans = fuellist[start][end][0]
			if ans==10000000: ans = -1
			print(ans)


if __name__ == '__main__':
	main()

### FIXED CODE ###

import sys


def main():
	input = sys.stdin.readline
	N, M, L = list(map(int,input().split()))
	P = [list(map(int,input().split())) for i in range(M)]
	Q = int(input())
	st = [list(map(int,input().split())) for i in range(Q)]

	neighbor = [[] for i in range(N)]
	for s,e,d in P:
		if d<=L:
			neighbor[s-1].append((e-1,d))
			neighbor[e-1].append((s-1,d))
		# if d<=L:
		# 	neighbor[s-1][e-1] = neighbor[e-1][s-1] = d

	# print(neighbor)
	# start, end = 2,1

	fuellist = [0]*N
	def bfs(start, end):
		# fuel = [[10000000,L+1] for i in range(N)]
		fuelc, fuelm = [10000000]*N, [L+1]*N
		# fuel[start][0] = 0
		# fuel[start][1] = L
		fuelc[start], fuelm[start] = 0, L
		queue = [start]
		while len(queue)>0:
			nq = []						# next target node
			nq_append = nq.append
			for i in queue:				# target node
				oc, om = fuelc[i], fuelm[i]
				for j, d in neighbor[i]:
				# for j, d in enumerate(neighbor[i]):	# next node
					# if d==10**10: continue
					if om<d:
						nc = oc+1
						nm = L-d
					else:
						nc = oc
						nm = om-d

					# print(i,j,nc,nm)

					if nc<fuelc[j]:
						fuelc[j], fuelm[j] = nc, nm
						nq_append(j)
					elif nc==fuelc[j] and nm>fuelm[j]:
						fuelm[j] = nm
						nq_append(j)

			queue = nq					# update target node

		# ans = fuel[end][0]
		ans = fuelc[end]
		if ans==10000000: ans = -1

		fuellist[start] = fuelc

		print(ans)

	for s, t in st:
		start, end = s-1, t-1

		if fuellist[start]!=0:
			ans = fuellist[start][end]
			if ans==10000000: ans = -1
			print(ans)
		elif fuellist[end]!=0:
			ans = fuellist[end][start]
			if ans==10000000: ans = -1
			print(ans)
		else:
			bfs(start,end)


if __name__ == '__main__':
	main()
### BUGGY CODE ###

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

h.sort()
for i in range(k):
    if sum(h) == 0:
        break
    else:
        h.pop(-1)

print(sum(h))

### FIXED CODE ###

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

h.sort()
ans = 0

if n <= k:
    h.clear()
else:
    for i in range(k):
            h.pop(-1)

print(sum(h))
### BUGGY CODE ###

N = int(input())
A = input().split()

num = 0

for i in range(N):
  for j in range(i+1, N):
    num += int(A[i])*int(A[j])

num = num % (10**9 + 7)
print(num)


### FIXED CODE ###

N = int(input())
Al = input().split()
A = list( map((lambda x: int(x)), Al))
num = 0
ma = 10**9 + 7
li = 0

for i in range(N-1, 0, -1):
  li += A[i]
  num += A[i-1] * li

num = num % ma
print(num)
### BUGGY CODE ###

# coding: utf-8

# k = int(input())
k = 999983
befo = 0

for i in range(k):
    n = befo * 10 + 1
    befo = n
    if (7 * n) % k == 0:
        print(i+1)
        exit(0)
print(-1)


### FIXED CODE ###

# coding: utf-8

k = int(input())
n = 7 % k

for i in range(k+1):
    if n % k == 0:
        print(i+1)
        exit(0)
    n = (((n % k) * (10 % k)) + 7 % k) % k
print(-1)
### BUGGY CODE ###

import heapq


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    f = list(map(int, input().split()))

    a.sort()
    f.sort(reverse=True)

    h = []
    for a_e, f_e in zip(a, f):
        heapq.heappush(h, [-a_e * f_e, a_e, f_e])

    for _ in range(k):
        if not h:
            break

        m_e, a_e, f_e = heapq.heappop(h)
        if a_e != 1:
            heapq.heappush(h, [-(a_e-1) * f_e, a_e - 1, f_e])

    if h:
        ans, _, _ = heapq.heappop(h)
        ans = -ans
    else:
        ans = 0

    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    f = list(map(int, input().split()))

    a.sort()
    f.sort(reverse=True)

    l = -1
    r = 10 ** 13

    def test(x):
        cnt = 0
        for ae, fe in zip(a, f):
            cnt += max(0, ae - x // fe)

        return cnt <= k

    while l + 1 < r:
        m = (l + r) // 2
        if test(m):
            r = m
        else:
            l = m

    ans = r
    print(ans)


if __name__ == "__main__":
    main()