Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and b by their father. The array a is given to Devu and b to his brother. 

As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b. 

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting. 


-----Input-----

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 10^5). The second line will contain n space-separated integers representing content of the array a (1 ≤ a_{i} ≤ 10^9). The third line will contain m space-separated integers representing content of the array b (1 ≤ b_{i} ≤ 10^9).


-----Output-----

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.


-----Examples-----
Input
2 2
2 3
3 5

Output
3

Input
3 2
1 2 3
3 4

Output
4

Input
3 2
4 5 6
1 2

Output
0



-----Note-----

In example 1, you can increase a_1 by 1 and decrease b_2 by 1 and then again decrease b_2 by 1. Now array a will be [3; 3] and array b will also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu's condition are 3.

In example 3, you don't need to do any operation, Devu's condition is already satisfied.

Difficulty: MEDIUM_HARD
Tags: Sorting

Amortized analysis
Skill types: Sorting

Amortized analysis

Starter code:

Solutions:
import sys
import math

def solve():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a.sort()
	b.sort()
	bcost = [0] * m
	for i in range(m - 2, -1, -1):
		valstoright = m - i - 1
		bcost[i] = bcost[i + 1] + valstoright * (b[i + 1] - b[i])
	acost = [0] * n
	for i in range(1, n):
		valstoleft = i
		acost[i] = acost[i - 1] + valstoleft * (a[i] - a[i - 1])
	res = 10000000000000000
	for i in range(m):
		amax = n - 1
		amin = 0
		while amin < amax:
			avg = (amin + amax + 1) // 2
			if a[avg] > b[i]:
				amax = avg - 1
			else:
				amin = avg
		res = min(res, bcost[i] + acost[amin] + (amin + 1) * (b[i] - a[amin]))
	for i in range(n):
		bmax = m - 1
		bmin = 0
		while bmin < bmax:
			avg = (bmax + bmin) // 2
			if b[avg] < a[i]:
				bmin = avg + 1
			else:
				bmax = avg
		res = min(res, bcost[bmin] + acost[i] + (m - bmin) * (b[bmin] - a[i]))
	return max(res, 0)
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
print(solve())


(n, m) = map(int, input().split())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))[::-1]
ans = 0
for i in range(min(n, m)):
	if b[i] > a[i]:
		ans += b[i] - a[i]
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
arr = sorted(list(zip(a, [1] * n)) + list(zip(b, [2] * m)))
ans = 10 ** 18
(less_sum, less_cnt) = (0, 0)
(more_sum, more_cnt) = (sum(b), m)
for (x, y) in arr:
	if y == 1:
		less_sum += x
		less_cnt += 1
	else:
		more_sum -= x
		more_cnt -= 1
	ans = min(ans, less_cnt * x - less_sum + (more_sum - more_cnt * x))
print(ans)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return list(map(int if arg == 0 else str, input().split()))
(n, m) = mapi()
a = list(mapi())
b = list(mapi())
a.sort()
b.sort(reverse=True)
res = 0
i = 0
while i < n and i < m and (a[i] < b[i]):
	res += b[i] - a[i]
	i += 1
print(res)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def steps(x):
	ans = 0
	for i in a:
		ans += max(0, x - i)
	for i in b:
		ans += max(0, i - x)
	return ans
(n, m) = value()
a = array()
b = array()
low = 1
high = 10 ** 9
while low <= high:
	mid1 = low + (high - low) // 3
	mid2 = high - (high - low) // 3
	if steps(mid1) < steps(mid2):
		high = mid2 - 1
	else:
		low = mid1 + 1
print(steps(mid1))


(n, m) = map(int, input().split())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
b.reverse()
Res = 0
for i in range(min(n, m)):
	if a[i] < b[i]:
		Res += b[i] - a[i]
print(Res)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort(reverse=True)
ans = 0
for i in range(min(n, m)):
	if a[i] < b[i]:
		ans += b[i] - a[i]
	else:
		continue
print(ans)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	(n, m) = mi()
	a = li()
	b = li()
	a.sort()
	b.sort()
	if b[-1] <= a[0]:
		print(0)
		return
	pre = [0] * n
	for i in range(1, n):
		pre[i] += pre[i - 1]
		pre[i] += i * (a[i] - a[i - 1])
	suff = [0] * m
	for i in range(m - 2, -1, -1):
		suff[i] += suff[i + 1]
		suff[i] += (m - i - 1) * (b[i + 1] - b[i])

	def fun(m1):
		x = bisect(a, m1) - 1
		y = bisect(b, m1) - 1
		ans = 0
		if x == n:
			x -= 1
		ans += pre[x]
		ans += (m1 - a[x]) * (x + 1)
		if y >= m - 1:
			return ans
		if y == 0:
			y -= 1
		ans += (b[y + 1] - m1) * (m - y - 1)
		ans += suff[y + 1]
		return ans
	l = 1
	r = int(1000000000.0 + 5)
	while l <= r:
		mid = l + (r - l) // 2
		x = fun(mid)
		if mid == 1:
			x1 = fun(mid + 1)
			if x > x1:
				l = mid + 1
				ans = x1
			else:
				print(max(x, 0))
				return
		x1 = fun(mid - 1)
		x2 = fun(mid + 1)
		if x1 < x2:
			if x1 < x:
				r = mid - 1
				ans = x1
			else:
				print(max(x, 0))
				return
		elif x2 < x:
			l = mid + 1
		else:
			print(max(x, 0))
			return

def __starting_point():
	if file:
		if path.exists('input.txt'):
			sys.stdin = open('input.txt', 'r')
			sys.stdout = open('output.txt', 'w')
		else:
			input = sys.stdin.readline
	solve()
__starting_point()


def costo(a, b, t):
	resultado = 0
	for elem in a:
		resultado += max(t - elem, 0)
	for elem in b:
		resultado += max(elem - t, 0)
	return resultado
(m, n) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
(inf, sup) = (min(a), max(b))
while inf < sup:
	t = (inf + sup) // 2
	if costo(a, b, t + 1) - costo(a, b, t) >= 0:
		sup = t
	else:
		inf = t + 1
print(costo(a, b, inf))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_min = min(a)
b_max = max(b)
if a_min >= b_max:
	print(0)
else:
	a = sorted([i for i in a if i < b_max])
	n = len(a)
	aa = [0] * (n + 1)
	for i in range(n):
		aa[i + 1] = aa[i] + a[i]
	b = sorted([i for i in b if i > a_min])
	m = len(b)
	bb = [0] * (m + 1)
	for i in range(m - 1, -1, -1):
		bb[i] = bb[i + 1] + b[i]
	output = float('inf')
	i = 0
	j = 0
	while i < n or j < m:
		if i == n:
			k = b[j]
		elif j == m:
			k = a[i]
		else:
			k = min(a[i], b[j])
		while i < n and a[i] <= k:
			i += 1
		while j < m and b[j] <= k:
			j += 1
		output = min(output, k * (i - m + j) - aa[i] + bb[j])
	print(int(output))


def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))

	def f(m):
		ans = 0
		for i in a:
			ans += max(0, m - i)
		for i in b:
			ans += max(0, i - m)
		return ans
	l = 0
	r = 10 ** 9 + 3
	while r - l > 2:
		m1 = l + (r - l) // 3
		m2 = m1 + (r - l) // 3
		if f(m1) >= f(m2):
			l = m1
		else:
			r = m2
	print(min(f(l), f(l + 1), f(r)))
main()


def read(mode=2):
	inputs = input().strip()
	if mode == 0:
		return inputs
	if mode == 1:
		return inputs.split()
	if mode == 2:
		return [int(x) for x in inputs.split()]

def write(s='\n'):
	if isinstance(s, list):
		s = ' '.join(s)
	s = str(s)
	print(s, end='')
(n, m) = read()
a = read()
b = read()
s = [(0, 2)] + [(i, 0) for i in a] + [(i, 1) for i in b]
s.sort()
t = sum(b)
al = 0
br = m
mn = t
for i in range(1, n + m + 1):
	t += (al - br) * (s[i][0] - s[i - 1][0])
	mn = min(mn, t)
	if s[i][1]:
		br -= 1
	else:
		al += 1
print(mn)


(n, m) = map(int, input().split())
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
a.sort()
b.sort(reverse=True)
s = 0
for i in range(min(n, m)):
	if b[i] > a[i]:
		s += b[i] - a[i]
print(s)


def getIndexA(a, value, l, r):
	ans = -1
	while l <= r:
		m = int((l + r) / 2)
		if a[m] < value:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def getIndexB(b, value, l, r):
	ans = -1
	while l <= r:
		m = int((l + r) / 2)
		if b[m] > value:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def main():
	string = input().split()
	n = int(string[0])
	m = int(string[1])
	a = [int(value) for value in input().split()]
	b = [int(value) for value in input().split()]
	a.sort()
	b.sort(reverse=True)
	sum_a = [0] * n
	sum_a[0] = a[0]
	for i in range(1, n):
		sum_a[i] = sum_a[i - 1] + a[i]
	sum_b = [0] * m
	sum_b[0] = b[0]
	for i in range(1, m):
		sum_b[i] = sum_b[i - 1] + b[i]
	if a[0] >= b[0]:
		print('0')
		return
	l = a[0]
	r = b[0]
	ans = 9223372036854775807
	for i in range(n):
		mid = a[i]
		index_a = i
		index_b = getIndexB(b, mid, 0, m - 1)
		req_sum_a = mid * (index_a + 1) - sum_a[index_a]
		req_sum_b = 0
		if index_b != -1:
			req_sum_b = req_sum_b + sum_b[index_b]
		req_sum_b = req_sum_b - mid * (index_b + 1)
		if req_sum_a + req_sum_b <= ans:
			ans = req_sum_b + req_sum_a
	for i in range(m):
		mid = b[i]
		index_a = getIndexA(a, mid, 0, n - 1)
		index_b = i
		req_sum_a = mid * (index_a + 1)
		if index_a != -1:
			req_sum_a = req_sum_a - sum_a[index_a]
		req_sum_b = sum_b[index_b] - mid * (index_b + 1)
		if req_sum_a + req_sum_b <= ans:
			ans = req_sum_b + req_sum_a
	print(ans)
main()


def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
(m, n) = map(int, input().split())
b = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
b.sort()
tot = 9999999999999999999999999999999
s = SegmentTree(a)
s1 = SegmentTree(b)
for i in range(n):
	c = binarySearchCount(b, m, a[i])
	ans = c * a[i] - s1.query(0, c - 1) - (n - 1 - i) * a[i] + s.query(i + 1, n - 1)
	tot = min(ans, tot)
for i in range(m):
	c = countGreater(a, n, b[i])
	ans = -c * b[i] + s.query(n - c, n - 1) - s1.query(0, i - 1) + i * b[i]
	tot = min(ans, tot)
print(tot)


(n, m) = list(map(int, input().split()))
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()), reverse=True)
ans = 0
for i in range(min(n, m)):
	ans += max(0, b[i] - a[i])
print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(n, m) = invr()
a = inara()
b = inara()
a.sort()
b.sort(reverse=True)

def f(val):
	res = 0
	for x in a:
		if x <= val:
			res += val - x
		else:
			break
	for x in b:
		if x >= val:
			res += x - val
		else:
			break
	return res
hi = int(1000000000.0)
lo = 1
ans = -1
while hi >= lo:
	m1 = lo + (hi - lo) // 3
	m2 = hi - (hi - lo) // 3
	if f(m1) <= f(m2):
		hi = m2 - 1
		ans = f(m1)
	else:
		lo = m1 + 1
print(ans)


(n, m) = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
a.sort()
b.sort()
c = list(set(a + b))
c.sort()
ans = 1e+18
less = 0
big = sum(b)
(i1, k1) = (0, 0)
(i2, k2) = (0, len(b))
for border in c:
	while i1 < len(a) and a[i1] < border:
		less += a[i1]
		i1 += 1
		k1 += 1
	while i2 < len(b) and b[i2] < border:
		big -= b[i2]
		i2 += 1
		k2 -= 1
	ans = min(ans, border * k1 - less + big - border * k2)
print(int(ans))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, trunc
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(n, m) = mp()
arr = lmp()
barr = lmp()
arr.sort()
barr.sort(reverse=True)
c = 0
for i in range(min(m, n)):
	c += max(0, barr[i] - arr[i])
print(c)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())), reverse=True)
ans = 0
for i in range(min(n, m)):
	ans += max(0, b[i] - a[i])
print(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations
from bisect import *

def main():
	(n, m) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	b = sorted(map(int, input().split()), reverse=True)
	su = 0
	for i in range(min(n, m)):
		if a[i] < b[i]:
			su += b[i] - a[i]
	print(su)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin
(n, m) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
l = min(min(a), min(b))
r = max(max(a), max(b))
ans = 10 ** 20
while l <= r:
	lo = l + (r - l) // 3
	hi = r - (r - l) // 3
	cou = 0
	for i in a:
		cou += max(0, lo - i)
	for j in b:
		cou += max(0, j - lo)
	cou1 = 0
	for i in a:
		cou1 += max(0, hi - i)
	for j in b:
		cou1 += max(0, j - hi)
	if cou1 > cou:
		r = hi - 1
	elif cou1 < cou:
		l = lo + 1
	else:
		l = lo + 1
		r = hi - 1
	ans = min(ans, cou, cou1)
print(ans)


R = lambda : map(int, input().split())
(n, m) = R()
a = sorted(R())
b = sorted(R())
b.reverse()
ans = 0
for i in range(min(n, m)):
	if b[i] > a[i]:
		ans += b[i] - a[i]
print(ans)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
int1 = lambda x: int(x) - 1
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
ilelec = lambda : map(int1, input().split())
alelec = lambda : list(map(int1, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
(n, m) = ilele()
A = alele()
B = alele()
A.sort()
B.sort()
Ans = float('inf')
if A[0] >= B[-1]:
	print(0)
	exit(0)
C = list(set(A + B))
D = [0] + list(accumulate(A))
E = [0] + list(accumulate(B))
for x in C:
	a = bisect.bisect_left(A, x)
	c = x * a - D[a]
	b = bisect.bisect_left(B, x)
	d = E[-1] - E[b] - (m - b) * x
	Ans = min(Ans, c + d)
print(Ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_left as bl, bisect as br
from collections import Counter
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l1 = []
l2 = []
d1 = Counter(a)
d2 = Counter(b)
l = []
for i in d1:
	l1.append(i)
	l.append(i)
for i in d2:
	l2.append(i)
	l.append(i)
l1.sort()
l2.sort()
p1 = []
p2 = []
c = 0
d = 0
for i in l1:
	c += d1[i] * i
	d += d1[i]
	p1.append([c, d])
c = 0
d = 0
for i in l2:
	c += d2[i] * i
	d += d2[i]
	p2.append([c, d])
l = list(set(l))
l.sort()
ans = 10 ** 14
for i in l:
	x = bl(l1, i)
	c = 0
	if x != 0:
		c = p1[x - 1][1] * i - p1[x - 1][0]
	x = br(l2, i)
	d = 0
	if x != 0:
		d = p2[-1][0] - p2[x - 1][0] - i * (p2[-1][1] - p2[x - 1][1])
	else:
		d = p2[-1][0] - i * p2[-1][1]
	ans = min(ans, c + d)
print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(n, m) = invr()
a = inara()
b = inara()
a.sort()
b.sort(reverse=True)
dpA = [0] * (n + 1)
for i in range(n):
	dpA[i + 1] = dpA[i] + a[i]
dpB = [0] * (m + 1)
for i in range(m):
	dpB[i + 1] = dpB[i] + b[i]

def Find(val, flag):
	if flag == True:
		lo = 0
		hi = n - 1
		ans = 0
		while hi >= lo:
			mid = (hi + lo) // 2
			if a[mid] <= val:
				ans = val * (mid + 1) - dpA[mid + 1]
				assert ans >= 0
				lo = mid + 1
			else:
				hi = mid - 1
		return ans
	else:
		lo = 0
		hi = m - 1
		ans = 0
		while hi >= lo:
			mid = (hi + lo) // 2
			if b[mid] >= val:
				ans = dpB[mid + 1] - val * (mid + 1)
				assert ans >= 0
				lo = mid + 1
			else:
				hi = mid - 1
		return ans

def f(val):
	return Find(val, True) + Find(val, False)
hi = int(1000000000.0)
lo = 1
ans = -1
while hi >= lo:
	m1 = lo + (hi - lo) // 3
	m2 = hi - (hi - lo) // 3
	if f(m1) <= f(m2):
		hi = m2 - 1
		ans = f(m1)
	else:
		lo = m1 + 1
print(ans)


(n, m) = map(int, input().split())
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
a.sort()
b.sort()
i = j = 0
s = t = sum(b)
while True:
	if i == n or (j < m and a[i] > b[j]):
		if j == m:
			break
		x = b[j]
		j += 1
	else:
		x = a[i]
		i += 1
	t -= x
	s = min(t - x * (m - j - i), s)
print(s)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
(n, m) = mapi()
a = list(mapi())
b = list(mapi())
res = 10 ** 18
l = 1
r = 10 ** 9
cnt = 0

def check(k):
	ans = 0
	for i in a:
		if i < k:
			ans += k - i
	for i in b:
		if i > k:
			ans += i - k
	return ans
while r - l >= 0 and cnt <= 64:
	cnt += 1
	mid1 = l + (r - l) // 3
	mid2 = r - (r - l) // 3
	if mid1 - l < 0 or r - mid1 < 0 or mid2 - l < 0 or (r - mid2 < 0):
		break
	ans1 = check(mid1)
	ans2 = check(mid2)
	res = min(res, ans1, ans2)
	if ans1 < ans2:
		r = mid2
	else:
		l = mid1
print(res)