CodeEff/ECCO huggingface dataset:
# Original solution
# Author: cr4zjh0bp
# Created: Sat Mar 14 11:48:11 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

n, k = nin(2)
x = na()

ans = 0
for i in range(n):
    ans += min(abs(x[i] - k) * 2, abs(x[i]) * 2)

print(ans)

# Optimized solution
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
k = ni()
x = na()

ans = 0
for i in range(n):
    ans += min(abs(x[i]) * 2, abs(x[i] - k) * 2)
print(ans)
# Original solution
N = int(eval(input()))
K = int(eval(input()))
A = list(map(int,input().split()))
ans = 0
for i in range(N):
  ans += min(abs(0-A[i]),abs(K-A[i]))*2
print(ans)

# Optimized solution
N = int(eval(input()))
K = int(eval(input()))
X = list(map(int,input().split()))
ans = 0
for i in range(1,N+1):
  A = abs(X[i-1])
  B = abs(K-X[i-1])
  ans += 2*(min(A,B))
print(ans)
# Original solution
_,k,*x=list(map(int,open(0).read().split()));print((sum(min(t,k-t)*2for t in x)))

# Optimized solution
_,k,t=open(0);print((sum(min(x,int(k)-x)*2for x in map(int,t.split()))))
# Original solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    k = int(input().rstrip('\n'))
    x = list(map(int, input().rstrip('\n').split()))
    t = 0
    for v in x:
        t += (min(v, abs(k - v)) * 2)
    print(t)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    k = int(input().rstrip('\n'))
    x = list(map(int, input().rstrip('\n').split()))
    t = 0
    for i in range(n):
        t += (min(abs(x[i]), abs(k - x[i])) * 2)
    print(t)


if __name__ == '__main__':
    slove()
# Original solution
N = int(eval(input())) # ボールの数
K = int(eval(input())) # Bの位置
xs = [int(i) for i in input().split()] # ボールの位置

half = K / 2

total = 0
for x in xs:
    if x < half:
        total += 2*x
    else:
        total += 2*(K-x)

print(total)


# Optimized solution
N = int(eval(input())) # ボールの数
K = int(eval(input())) # Bの位置
xs = [int(i) for i in input().split()] # ボールの位置

half = K / 2

total = 0
for x in xs:
    if x < half:
        total += x
    else:
        total += (K-x)

# 最後に二倍すればよい
print((2*total))
# Original solution
n = int(eval(input()))
k = int(eval(input()))
x = list(map(int, input().split()))
ans = 0
for xi in x:
    ans += min(xi, k - xi) * 2
print(ans)

# Optimized solution
n = int(eval(input()))
k = int(eval(input()))
ans = 0
x = list(map(int, input().split()))
for v in x:
    ans += min(v, k - v) * 2
print(ans)
# Original solution
n = int(eval(input()))
k = int(eval(input()))
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(abs(i-0),abs(i-k))*2
print(ans)

# Optimized solution
n = int(eval(input()))
k = int(eval(input()))
xl = list(map(int, input().split()))
ans = 0
for i in xl:
    ans+= min(abs(i-0), abs(k-i))*2
print(ans)
# Original solution
N = int(eval(input()))
K = int(eval(input()))
X = list(map(int,input().split()))

ans = 0
k = int(K/2)
for x in X:
  if(x > k):
    ans += (K-x) * 2
  else:
    ans += x * 2
print(ans)

# Optimized solution
N = int(eval(input()))
K = int(eval(input()))
X = list(map(int,input().split()))
cnt = 0
for x in X:
  if(x > K//2):
    cnt += (K-x)*2
  else:
    cnt += x*2
print(cnt)
# Original solution
def resolve():
    N = int(eval(input()))
    K = int(eval(input()))
    x = list(map(int, input().split()))
    
    sum = 0
    for i in range(N):
        if abs(K-x[i]) > x[i]:
            sum += x[i]*2
        else:
            sum += abs(K-x[i])*2
    print(sum)
resolve()


# Optimized solution
def resolve():
    N = int(eval(input()))
    K = int(eval(input()))
    x = list(map(int, input().split()))
    
    sum = 0
    for i in range(N):
        if K-x[i] > x[i]:
            sum += x[i]*2
        else:
            sum += (K-x[i])*2
    print(sum)
resolve()
# Original solution
_ = eval(input())
k = int(eval(input()))
x = [int(x) for x in input().split()]

ans = 0
for i in x:
    ans += min(i, k - i) * 2
else:
    print(ans)

# Optimized solution
_ = eval(input())
k = int(eval(input()))
x = [int(x) for x in input().split()]

ans = 0
for i in x:
    ans += min(i, k - i)
else:
    print((ans * 2))
# Original solution
from collections import Counter
def get_par(x, par_lst):
  if x == par_lst[x]:
    return x
  ret = get_par(par_lst[x], par_lst)
  #par_lst[x] = ret
  return ret

def main():
  while True:
    n, m = list(map(int, input().split()))
    if n == 0:
      break
    par_lst = [i for i in range(n)]
    counter = Counter()
    flag = False
    if m == 0:
      print("yes")
      continue
    for _ in range(m):
      u, v = list(map(int, input().split()))
      u -= 1
      v -= 1
      counter[u] += 1
      counter[v] += 1
      pu, pv = get_par(u, par_lst), get_par(v, par_lst)
      if pu == pv:
        flag = True
      else:
        par_lst[pu] = pv
    
    if counter.most_common()[0][1] > 2:
      flag = True
    if flag:
      print("no")
    else:
      print("yes")
main()


# Optimized solution
from collections import Counter
def get_par(x, par_lst):
  if x == par_lst[x]:
    return x
  ret = get_par(par_lst[x], par_lst)
  par_lst[x] = ret
  return ret

def main():
  while True:
    n, m = list(map(int, input().split()))
    if n == 0:
      break
    par_lst = [i for i in range(n)]
    counter = [0] * n
    flag = False
    for _ in range(m):
      u, v = list(map(int, input().split()))
      u -= 1
      v -= 1
      counter[u] += 1
      counter[v] += 1
      pu, pv = get_par(u, par_lst), get_par(v, par_lst)
      if pu == pv:
        flag = True
      else:
        par_lst[pu] = pv
    
    if max(counter) > 2:
      flag = True
    if flag:
      print("no")
    else:
      print("yes")
main()
# Original solution
import sys
import heapq
import math
N, K = list(map(int, sys.stdin.readline().rstrip().split()))
A = list(map(int, sys.stdin.readline().rstrip().split()))
F = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort()
F.sort()

cost = []
for i in range(N):
    cost.append((-A[i] * F[N - 1 - i], A[i], F[N - 1 - i], A[i]))
heapq.heapify(cost)

while K > 0:
    max_1, a_1, f_1, nokori_1 = heapq.heappop(cost)
    max_2, a_2, f_2, nokori_2 = heapq.heappop(cost)
    if max_1 == 0:
        break
    c = math.ceil((max_2 - max_1 + 1) / f_1)
    num = min(K, c)
    if num <= nokori_1:
        heapq.heappush(cost, (min(max_1 + f_1 * num, 0), a_1, f_1, nokori_1 - num))
        heapq.heappush(cost, (max_2, a_2, f_2, nokori_2))
        K -= num
    else:
        heapq.heappush(cost, (min(max_1 + f_1 * a_1, 0), a_1, f_1, nokori_1 - a_1))
        heapq.heappush(cost, (max_2, a_2, f_2, nokori_2))
        break

print((-cost[0][0]))

# Optimized solution
import sys
import math
N, K = list(map(int, sys.stdin.readline().rstrip().split()))
A = list(map(int, sys.stdin.readline().rstrip().split()))
F = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort()
A = A[::-1]
F.sort()

def test(x):
    res = 0
    for i in range(N):
        t = A[i] * F[i]
        if t > x:
            res += math.ceil((t - x) / F[i])
    
    return res <= K

ng = -1
ok = 10**18

while ng + 1 < ok:
    mid = (ng + ok) // 2
    if test(mid):
        ok = mid
    else:
        ng = mid
print(ok)
# Original solution
import copy
from heapq import heappop, heapify, heappush
import math
N, K = list(map(int, input().split()))
A = [int(i) for i in input().split()]
F = [int(i) for i in input().split()]
ans = 0
A.sort(reverse=True)
F.sort()
s = []
for i in range(N):
    s.append([A[i]*F[i], A[i], F[i]])
s.sort(reverse=True, key=lambda x: x[0])

def f(l, T):
    cur = 0
    num = 0
    S = copy.deepcopy(l)
    while cur <= K:
        if num == N:
            break
        a, b, c = S[num]
        if a <= T:
            break
        cur += math.ceil((a - T)/c)
        num += 1
    if cur <= K:
        return True
    else:
        return False

ok, ng = s[0][0], -1
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    if f(s, mid) == True:
        ok = mid
    else:
        ng = mid
print(ok)

# Optimized solution
def main():
    import copy
    from heapq import heappop, heapify, heappush
    import math
    N, K = list(map(int, input().split()))
    A = [int(i) for i in input().split()]
    F = [int(i) for i in input().split()]
    ans = 0
    A.sort(reverse=True)
    F.sort()
    s = []
    for i in range(N):
        s.append([A[i]*F[i], A[i], F[i]])
    s.sort(reverse=True, key=lambda x: x[0])

    def f(S, T):
        cur = 0
        num = 0
        while cur <= K:
            if num == N:
                break
            a, b, c = S[num]
            if a <= T:
                break
            cur += math.ceil((a - T)/c)
            num += 1
        if cur <= K:
            return True
        else:
            return False

    ok, ng = s[0][0], -1
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if f(s, mid) == True:
            ok = mid
        else:
            ng = mid
    print(ok)

if __name__ == '__main__':
    main()
# Original solution
import math

N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
F = list(map(int, input().split()))

if K >= sum(A):
    print((0))
    exit()

A.sort(reverse=True)
F.sort()

def is_ok(mid):
    cnt = 0
    for i in range(N):
        if A[i] * F[i] <= mid:
            continue
        else:
            cnt += math.ceil((A[i] * F[i] - mid) / F[i]) # A[i]が1減少するごとにF[i]減るのでF[i]で割る  
    
    return cnt <= K

# okは全員がX以下になる最小値に着地するので、A*F<=10**12 なので＋１して最大のケースにも対応する
ok = 10**12 + 1
# 0ではないことが上で保証されてるので、条件を満たさない最大値の0にしておく
ng = 0
while ok - ng > 1:
    
    mid = (ok + ng) // 2
    #print(ok,ng,mid)
    if is_ok(mid):
        ok = mid
    else:
        ng = mid

#print(ok,ng,mid)
print(ok)

# Optimized solution
"""

AtCoder Beginner Contest 144  E - Gluttony


愚直解
・修行の順序は結果に影響しないから、修行を誰に割り振るか：N^K
・メンバーN人をどの問題に当てるか（N人の並べ方）：N!
-> N^K * N! 
間に合わない。

完食にかかる時間のうち最大値がチーム全体の成績なので、
・消化コストAiを修行で下げる
・消化コストAiの大きい人に、食べにくさFiの小さい食べ物を割り当てる
のが良さそう

最大値の最小化、なので、二分探索が使えそうな気もする。

A,F <= 10^6
かかる時間を X 以下にできるか、を考える。
・消化コストが大きい人に、食べにくさが小さい問題を当てるようにする
・Ai * Fi > Xであれば、(Ai - m) * Fi <= X となるような最小のmをカウントする
全メンバーに対しこれを数え上げた時に、
mの合計 > K
であれば、X以下になるように修行することができない（K回までしか修行できず、回数が足りない）ので、最大値の最小はXより上
mの合計 <= K
であれば、X以下になるように修行できるので、最大値の最小はX以下

A*F <= 10^12 までなので、40~50程度繰り返せば行けそう
"""
import math

N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
F = list(map(int, input().split()))

if K >= sum(A):
    print((0))
    exit()

A.sort(reverse=True)
F.sort()

def is_ok(mid):
    cnt = 0
    for i in range(N):
        if A[i] * F[i] <= mid:
            continue
        else:
            cnt += math.ceil((A[i] * F[i] - mid) / F[i]) # A[i]が1減少するごとにF[i]減るのでF[i]で割る  
    
    return cnt <= K

# okは全員がX以下になる最小値に着地するので、A*F<=10**12 なので＋１して最大のケースにも対応する
ok = 10**12 + 1
# 0ではないことが上で保証されてるので、条件を満たさない最大値の0にしておく
ng = 0
while ok - ng > 1:
    
    mid = (ok + ng) // 2
    #print(ok,ng,mid)
    if is_ok(mid):
        ok = mid
    else:
        ng = mid

#print(ok,ng,mid)
print(ok)
# Original solution
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
F=list(map(int,input().split()))

if N==1:
    ans=max(A[0]-K,0)*F[0]
else:
    F.sort(reverse=True) # 降順で固定
    A.sort()

#    print(f'F={F}')
#    print(A)

    while K>0 and A[-1]>0:
#        print(f'A={A}')
        S=sorted([(A[i]*F[i],i) for i in range(N)],reverse=True)
#        print(f'S={S}') #DB
        xym=S[0][0];xys=S[1][0]
        m=S[0][1];s=S[1][1]
        if xym==xys>0:
            A[m]-=1
            K-=1
        else:
            d=xym-xys
            p=min((d+(F[m]-1))//F[m],K,A[m])
            A[m]-=p # 負にならない
            K-=p
        A.sort()
    S=sorted([(A[i]*F[i],i) for i in range(N)],reverse=True)
    ans=max(S)[0]

print(ans)

# Optimized solution
def p(F,A,s): # スコアをsにするために最小限必要な修行の回数
    res=0
    for i in range(N):
        res+=max(A[i]-s//F[i],0)
    return res

N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
F=list(map(int,input().split()))

F.sort(reverse=True) # 降順で固定
A.sort() # 昇順で固定

l=0
r=max([F[i]*A[i] for i in range(N)])

while r-l>1:
    m=(l+r)//2
    if p(F,A,m)<=K:
        r=m
    else:
        l=m

if r>1 or p(F,A,0)>K:
    ans=r
else:
    ans=0

print(ans)
# Original solution
import math
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
a.reverse()
f.sort()
totalnum=sum(a)
maxproduct=0
for i in range(n):
  maxproduct=max(f[i]*a[i],maxproduct)
l1=0
r1=maxproduct
while True:
  l2=l1
  r2=(l1+r1)//2
  if l2==r2:
    break
  counter=0
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    r1=r2
  else:
    l1=r2
if l1==r1:
  print(r2)
else:
  counter=0
  r2=l1
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    print(l1)
  else:
    print(r1)

# Optimized solution
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
a.reverse()
f.sort()
maxproduct=0
for i in range(n):
  maxproduct=max(f[i]*a[i],maxproduct)
l1=0
r1=maxproduct
while True:
  l2=l1
  r2=(l1+r1)//2
  if l2==r2:
    break
  counter=0
  for i in range(n):
    c=r2//f[i]
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    r1=r2
  else:
    l1=r2
if l1==r1:
  print(r2)
else:
  counter=0
  r2=l1
  for i in range(n):
    c=r2//f[i]
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    print(l1)
  else:
    print(r1)
# Original solution
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
a.reverse()
f.sort()
maxproduct=0
for i in range(n):
  maxproduct=max(f[i]*a[i],maxproduct)
l1=0
r1=maxproduct
while True:
  l2=l1
  r2=(l1+r1)//2
  if l2==r2:
    break
  counter=0
  for i in range(n):
    c=r2//f[i]
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    r1=r2
  else:
    l1=r2
if l1==r1:
  print(r2)
else:
  counter=0
  r2=l1
  for i in range(n):
    c=r2//f[i]
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    print(l1)
  else:
    print(r1)

# Optimized solution
import sys
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
if sum(a)<=k:
  print((0))
  sys.exit()
pointer=0
l=0
r=0
for i in range(n):
  r=max(r,a[i]*f[i])
while l+1<r:
  try1=(r+l)//2
  required=0
  for i in range(n):
    required+=(max(0,a[i]-try1//f[i]))
  if required>k:
    l=try1
  else:
    r=try1
required=0
for i in range(n):
  required+=(max(0,a[i]-l//f[i]))
if required>k:
  print(r)
else:
  print(l)
# Original solution
import sys
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
if sum(a)<=k:
  print((0))
  sys.exit()
pointer=0
l=0
r=0
for i in range(n):
  r=max(r,a[i]*f[i])
while l+1<r:
  try1=(r+l)//2
  required=0
  for i in range(n):
    required+=(max(0,a[i]-try1//f[i]))
  if required>k:
    l=try1
  else:
    r=try1
required=0
for i in range(n):
  required+=(max(0,a[i]-l//f[i]))
if required>k:
  print(r)
else:
  print(l)

# Optimized solution
import sys
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
if sum(a)<=k:
  print((0))
  sys.exit()
pointer=0
l=0
r=0
for i in range(n):
  r=max(r,a[i]*f[i])
while l+1<r:
  try1=(l+r)//2
  required=0
  for i in range(n):
    required+=(max(0,a[i]-try1//f[i]))
  if required>k:
    l=try1
  else:
    r=try1
required=0
for i in range(n):
  required+=(max(0,a[i]-l//f[i]))
if required>k:
  print(r)
else:
  print(l)
# Original solution
import heapq
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())), reverse=True)

K = min(K, sum(A))
X = [[-a * f, a, f] for a, f in zip(A, F)]

heapq.heapify(X)
cnt = 0
while K:
    cost, a, f = heapq.heappop(X)

    if a == 0:
        heapq.heappush(X, [-a * f, new_a, f])
        continue

    new_a = a - 1
    heapq.heappush(X, [-new_a * f, new_a, f])
    K -= 1

X.sort()
print((-X[0][0]))


# Optimized solution
from math import ceil
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())), reverse=True)

K = min(K, sum(A))
X = [[a, f] for a, f in zip(A, F)]

ok, ng = 10 ** 12 + 1, -1
while abs(ok - ng) > 1:
    m = (ok + ng) // 2
    k_cost = 0
    for a, f in X:
        if a * f <= m:
            continue
        else:
            k_cost += max(0, ceil(-(m / f - a)))

    if k_cost <= K:
        ok = m
    else:
        ng = m

print(ok)
# Original solution
import sys
N, K = map(int,input().split())
Alist = list(map(int,input().split()))
Flist = list(map(int,input().split()))
Alist.sort()
sum_Alist = 0
for i in range(N):
    sum_Alist += Alist[i]
print(Alist,Flist,file=sys.stderr)
if sum_Alist <= K:
    print(0)
    exit()
maxtime = max(Alist)*max(Flist)
mintime = -1
print(maxtime,mintime,file=sys.stderr)
while mintime+1 < maxtime:
    mid = (mintime+maxtime)//2
    sum_midlist = 0
    max_midlist = 0
    midlist = [0 for i in range(N)]
    for i in range(N):
        midlist[i] = mid//Flist[i]
    midlist.sort()
    for i in range(N):
        if midlist[i] < Alist[i]:
            sum_midlist += Alist[i]-midlist[i]
    #print(maxtime,mintime,sum_midlist,file=sys.stderr)
    if sum_midlist <= K:
        maxtime = mid
    else:
        mintime = mid
print(maxtime)


# Optimized solution
import sys
N, K = map(int,input().split())
Alist = list(map(int,input().split()))
Flist = list(map(int,input().split()))
Alist.sort()
sum_Alist = 0
for i in range(N):
    sum_Alist += Alist[i]
print(Alist,Flist,file=sys.stderr)
if sum_Alist <= K:
    print(0)
    exit()
maxtime = max(Alist)*max(Flist)
mintime = -1
print(maxtime,mintime,file=sys.stderr)
midlist = [0 for i in range(N)]
while mintime+1 < maxtime:
    mid = (mintime+maxtime)//2
    sum_midlist = 0
    max_midlist = 0
    for i in range(N):
        midlist[i] = mid//Flist[i]
    midlist.sort()
    for i in range(N):
        if midlist[i] < Alist[i]:
            sum_midlist += Alist[i]-midlist[i]
    #print(maxtime,mintime,sum_midlist,file=sys.stderr)
    if sum_midlist <= K:
        maxtime = mid
    else:
        mintime = mid
print(maxtime)
# Original solution
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())))[::-1]
B = sum(A)
## 修行前の最短: M
M = max([a*f for a, f in zip(A, F)])

def is_possible(x):
    return B - sum([min(a, x//f) for a, f in zip(A, F)]) <= K

## 2分探索
left = -1
right = M
while left + 1 < right:
    mid = (left + right)//2
    if is_possible(mid):
        right = mid
    else:
        left = mid

print(right)

# Optimized solution
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())))[::-1]

## 修行前の最短: M
M = max([a*f for a, f in zip(A, F)])

## 積の最小をxにできるか。
## x//f以下である必要がある。
def is_possible(x):
    return sum([max(0, a-x//f) for a, f in zip(A, F)]) <= K

## 2分探索
left = -1
right = M
while left + 1 < right:
    mid = (left + right)//2
    if is_possible(mid):
        right = mid
    else:
        left = mid

print(right)
# Original solution
import heapq

n, k = list(map(int, input().split()))
a_list = [int(i) for i in input().split()]
f_list = [int(i) for i in input().split()]
a_list.sort()
f_list.sort(reverse=True)

heap_list = []
for i in range(n):
    heap_list.append((-a_list[i] * f_list[i], f_list[i]))

heapq.heapify(heap_list)    
for i in range(k):
    max_v, f = heapq.heappop(heap_list)
    if max_v < 0:
        max_v = min(max_v + f, 0)
    heapq.heappush(heap_list, (max_v, f))
max_v, f = heapq.heappop(heap_list)
print((-max_v))

# Optimized solution
n, k = list(map(int, input().split()))
a_list = [int(i) for i in input().split()]
f_list = [int(i) for i in input().split()]
a_list.sort(reverse=True)
f_list.sort()

if k >= sum(a_list):
    print((0))
    exit()

point = []
for i in range(n):
    point.append(a_list[i] * f_list[i])

import math
def binary_search(point, k):
    left = 0
    right = 10 ** 12
    while left + 1< right:
        ans = 0
        center = (left + right) // 2
        for i in range(n):
            if point[i] > center:
                ans += a_list[i] - (center // f_list[i])
        if ans <= k:
            right = center
        else:
            left = center
    return right    
    
value = binary_search(point, k)
print(value)
# Original solution
import math

N,K,*AF = list(map(int, open(0).read().split()))
A = sorted(AF[:N])
F = sorted(AF[N:],reverse=True)
left = 0
right = max(A) * max(F)
while left != right:
    mid = (left+right) // 2
    if sum([max(A[i]-math.floor(mid/F[i]),0) for i in range(N)]) <= K:
        right = mid
    else:
        left = mid + 1
print(left)

# Optimized solution
import math

N,K,*AF = list(map(int, open(0).read().split()))
A = sorted(AF[:N])
F = sorted(AF[N:],reverse=True)
left = 0
right = max(A) * max(F)
while left != right:
    mid = (left+right) // 2
    temp = 0
    for i in range(N):
        temp += max(A[i]-math.floor(mid/F[i]),0)
        if temp > K:
            left = mid + 1
            break
    else:
        right = mid
print(left)
# Original solution
import sys
input = sys.stdin.readline
import heapq

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

    A_s = sorted(A, reverse=True)
    F_s = sorted(F)
    C = []
    C_h = []
    for i in range(N):
        x = A_s[i] * F_s[i]
        C.append(x)
        heapq.heappush(C_h, -x)

    for _ in range(K):
        x = -heapq.heappop(C_h)
        i = C.index(x)
        A_s[i] -= 1
        y = A_s[i] * F_s[i]
        C[i] = y
        heapq.heappush(C_h, -y)

    x = -heapq.heappop(C_h)
    print(x)











if __name__ == '__main__':
    main()



# Optimized solution
import sys
input = sys.stdin.readline

def check(A, F, mid, k):
    for i in range(len(A)):
        if A[i] * F[i] > mid:
            x = A[i] - mid // F[i]
            k -= x
        if k < 0:
            return False
    return True



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

    A_s = sorted(A, reverse=True)
    F_s = sorted(F)

    l = -1
    r = 10 ** 13
    while r - l != 1:
        mid = (r + l) // 2
        if check(A_s, F_s, mid, K):
            r = mid
        else:
            l = mid

    print(r)











if __name__ == '__main__':
    main()