$n$ boys and $m$ girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from $1$ to $n$ and all girls are numbered with integers from $1$ to $m$. For all $1 \leq i \leq n$ the minimal number of sweets, which $i$-th boy presented to some girl is equal to $b_i$ and for all $1 \leq j \leq m$ the maximal number of sweets, which $j$-th girl received from some boy is equal to $g_j$.

More formally, let $a_{i,j}$ be the number of sweets which the $i$-th boy give to the $j$-th girl. Then $b_i$ is equal exactly to the minimum among values $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ and $g_j$ is equal exactly to the maximum among values $b_{1,j}, b_{2,j}, \ldots, b_{n,j}$.

You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of $a_{i,j}$ for all $(i,j)$ such that $1 \leq i \leq n$ and $1 \leq j \leq m$. You are given the numbers $b_1, \ldots, b_n$ and $g_1, \ldots, g_m$, determine this number. 


-----Input-----

The first line contains two integers $n$ and $m$, separated with space — the number of boys and girls, respectively ($2 \leq n, m \leq 100\,000$). The second line contains $n$ integers $b_1, \ldots, b_n$, separated by spaces — $b_i$ is equal to the minimal number of sweets, which $i$-th boy presented to some girl ($0 \leq b_i \leq 10^8$). The third line contains $m$ integers $g_1, \ldots, g_m$, separated by spaces — $g_j$ is equal to the maximal number of sweets, which $j$-th girl received from some boy ($0 \leq g_j \leq 10^8$).


-----Output-----

If the described situation is impossible, print $-1$. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.


-----Examples-----
Input
3 2
1 2 1
3 4

Output
12
Input
2 2
0 1
1 0

Output
-1
Input
2 3
1 0
1 1 2

Output
4


-----Note-----

In the first test, the minimal total number of sweets, which boys could have presented is equal to $12$. This can be possible, for example, if the first boy presented $1$ and $4$ sweets, the second boy presented $3$ and $2$ sweets and the third boy presented $1$ and $1$ sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $12$.

In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.

In the third test, the minimal total number of sweets, which boys could have presented is equal to $4$. This can be possible, for example, if the first boy presented $1$, $1$, $2$ sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $4$.

Difficulty: MEDIUM
Tags: Constructive algorithms

Sorting

Amortized analysis

Implementation

Greedy algorithms

Mathematics
Skill types: Sorting

Amortized analysis

Greedy algorithms

Starter code:

Solutions:
import sys
import math
import collections
from pprint import pprint as pp
mod = 1000000007
MAX = 10 ** 10

def vector(size, val=0):
	vec = [val for i in range(size)]
	return vec

def matrix(rowNum, colNum, val=0):
	mat = []
	for i in range(rowNum):
		collumn = [val for j in range(colNum)]
		mat.append(collumn)
	return mat
(n, m) = map(int, input().split())
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
a.sort(reverse=True)
b.sort(reverse=True)
if max(a) > min(b):
	print(-1)
	sys.exit()
ans = sum(b) + m * sum(a[1:])
if b[m - 1] != a[0]:
	ans += a[0] - a[1]
print(ans)


(n, m) = list(map(int, input().split()))
b = list(map(int, input().split()))
g = list(map(int, input().split()))
x = max(b)
y = min(g)
if x > y:
	print(-1)
elif x == y:
	print(sum(b) * m + sum(g) - x * m)
else:
	(m1, m2) = (0, 0)
	for c in b:
		if c >= m1:
			(m1, m2) = (c, m1)
		elif c >= m2:
			m2 = c
	print(sum(b) * m + sum(g) - x * (m - 1) - m2)


(n, m) = [int(x) for x in input().split()]
pir = [int(x) for x in input().split()]
ant = [int(x) for x in input().split()]
ats = sum(pir) * m
did = max(pir)
antmaz = min(ant)
if antmaz < did:
	print(-1)
	return
pir.sort(reverse=True)
ant.sort()
for i in range(1, m):
	ats += ant[i] - did
if ant[0] != did:
	ats += ant[0] - pir[1]
print(ats)


from sys import stdin, setrecursionlimit
from bisect import bisect_right
setrecursionlimit(15000)

def get_gdict(arr):
	gdict = dict()
	for i in range(len(arr)):
		if arr[i] in gdict:
			gdict[arr[i]] += 1
		else:
			gdict[arr[i]] = 1
	return gdict

def initial_check(barr, garr):
	for i in garr:
		if i < barr[-1]:
			return False
	return True
(n, m) = list(map(int, stdin.readline().split()))
barr = list(map(int, stdin.readline().split()))
garr = list(map(int, stdin.readline().split()))
barr.sort()
garr.sort()
ans = 0
gdict = get_gdict(garr)
if initial_check(barr, garr):
	count = m
	b = n - 1
	g = m - 1
	while count > 0:
		tempb = [barr[b]] * m
		for i in range(len(tempb)):
			if count <= 0:
				for j in range(i, m):
					ans += tempb[b]
				break
			if tempb[i] in gdict:
				gdict[tempb[i]] -= 1
				ans += tempb[i]
				count -= 1
				if gdict[tempb[i]] == 0:
					del gdict[tempb[i]]
			else:
				if i == 0:
					ans += tempb[i]
					continue
				for k in range(g, -1, -1):
					if garr[k] in gdict:
						ans += garr[k]
						gdict[garr[k]] -= 1
						if gdict[garr[k]] == 0:
							del gdict[garr[k]]
						g = k - 1
						count -= 1
						break
		b -= 1
	while b >= 0:
		ans += m * barr[b]
		b -= 1
	print(ans)
else:
	print(-1)


(n, m) = input().split()
boy_min = input().split()
gir_max = input().split()
for i in range(len(boy_min)):
	boy_min[i] = int(boy_min[i])
for j in range(len(gir_max)):
	gir_max[j] = int(gir_max[j])
boy_min.sort()
gir_max.sort()
if gir_max[0] < boy_min[-1]:
	print(-1)
else:
	total = sum(gir_max[1:])
	if gir_max[0] == boy_min[-1]:
		total += gir_max[0]
		for i in range(len(boy_min) - 1):
			total += boy_min[i] * len(gir_max)
	else:
		total += boy_min[-1]
		total += gir_max[0]
		total += boy_min[-2] * (len(gir_max) - 1)
		for i in range(len(boy_min) - 2):
			total += boy_min[i] * len(gir_max)
	print(total)


(n, m) = list(map(int, input().split()))
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort()
g.sort()
if b[-1] > g[0]:
	print(-1)
elif b[-1] == g[0]:
	print(sum(g) + m * (sum(b) - b[-1]))
elif n == 1:
	print(-1)
else:
	print(sum(g) + b[-1] + b[-2] * (m - 1) + m * (sum(b) - b[-1] - b[-2]))


from sys import stdin, stdout
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys

class merge_find:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	global factorial_modP, fac_warmup
	if fac_warmup:
		return
	factorial_modP = [1 for _ in range(fac_warmup_size + 1)]
	for i in range(2, fac_warmup_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warmup = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	global fac_warmup, factorial_modP
	if not fac_warmup:
		warm_up_fac(MOD)
		fac_warmup = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def prefix_sum(li):
	sm = 0
	res = []
	for i in li:
		sm += i
		res.append(sm)
	return res

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
import heapq, itertools
pq = []
entry_finder = {}
REMOVED = '<removed-task>'

def add_task(task, priority=0):
	if task in entry_finder:
		remove_task(task)
	count = next(counter)
	entry = [priority, count, task]
	entry_finder[task] = entry
	heapq.heappush(pq, entry)

def remove_task(task):
	entry = entry_finder.pop(task)
	entry[-1] = REMOVED

def pop_task():
	while pq:
		(priority, count, task) = heapq.heappop(pq)
		if task is not REMOVED:
			del entry_finder[task]
			return task
	raise KeyError('pop from an empty priority queue')
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_serach(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
testingMode = False
fac_warmup_size = 10 ** 5 + 100
optimiseForReccursion = False
from math import factorial

def main():
	(n, m) = get_tuple()
	boys = get_list()
	girls = get_list()
	boys.sort(reverse=True)
	min_girl = min(girls)
	max_boy = boys[0]
	res = (sum(boys) - max_boy) * m + sum(girls)
	if max_boy < min_girl:
		print(res + max_boy - boys[1])
	elif max_boy == min_girl:
		print(res)
	else:
		print(-1)
if TestCases:
	for i in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


import math
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
first_max = 0
second_max = 0
for i in range(n):
	if b[i] < first_max and b[i] > second_max:
		second_max = b[i]
	if b[i] >= first_max:
		second_max = first_max
		first_max = b[i]
first_min = min(g)
if first_max > first_min:
	print(-1)
else:
	total = sum(b) * m + sum(g) - m * first_max + (first_max - second_max) * (first_min != first_max)
	print(total)


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort(reverse=True)
b.sort()
if b[0] < a[0]:
	print(-1)
elif b[0] == a[0]:
	s = sum(b)
	for i in range(1, n):
		s += a[i] * m
	print(s)
else:
	s = sum(b) + a[0] + (m - 1) * a[1]
	for i in range(2, n):
		s += a[i] * m
	print(s)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.sort()
l2.sort()
l2 = l2[::-1]
if n == 1:
	if l1[0] != min(l2):
		print(-1)
	else:
		print(sum(l2))
elif max(l1) > min(l2):
	print(-1)
else:
	ans = 0
	l1 = l1[::-1]
	if min(l2) == l1[0]:
		print(sum(l2) + (sum(l1) - l1[0]) * m)
	elif min(l2) != l1[0]:
		print(sum(l2) + l1[0] + sum(l1[1:]) * m - l1[1])


import math
import bisect
import heapq
from collections import defaultdict

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	elif p == 3:
		return list(input())
	else:
		return int(input())
(n, m) = f(2)
a = sorted(f(2))
b = sorted(f(2))
mn = a[-1]
mx = b[0]
smb = sum(b)
sma = sum(a)
if mx < mn:
	print(-1)
else:
	if a[-1] != b[0]:
		res = a[-1] + smb - b[0] + (sum(a) - a[-1]) * m - a[n - 2] + b[0]
	else:
		res = smb + (sum(a) - a[-1]) * m
	print(res)


(nBoys, nGirls) = map(int, input().split())
boys = sorted(map(int, input().split()), reverse=True)
girls = sorted(map(int, input().split()))
if boys[0] > girls[0]:
	print(-1)
	return
ret = sum(girls)
for i in range(1, nBoys):
	ret += boys[i] * nGirls
if boys[0] != girls[0]:
	ret -= girls[0] - boys[0]
	ret += girls[0] - boys[1]
print(ret)


(n, m) = map(int, input().split())
lisn = list(map(int, input().split()))
lism = list(map(int, input().split()))
lisn.sort()
lism.sort()
if lism[0] < lisn[-1]:
	print(-1)
else:
	ans = 0
	if lism[0] == lisn[-1]:
		for i in range(len(lisn) - 1):
			ans += lisn[i] * len(lism)
		for i in range(len(lism)):
			ans += lism[i]
	else:
		for i in range(len(lisn) - 1):
			ans += lisn[i] * len(lism)
		for i in range(len(lism)):
			ans += lism[i]
		ans += lisn[-1] - lisn[n - 2]
	print(ans)


(n, m) = map(int, input().split())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
if max(a) > min(b):
	print(-1)
else:
	a.sort()
	b.sort()
	ans = sum(b)
	for i in range(n - 2):
		ans += a[i] * m
	if a[n - 1] != b[0]:
		ans += a[n - 1] + a[n - 2] * (m - 1)
	else:
		ans += a[n - 2] * m
	print(ans)


def main():
	(n, m) = list(map(int, input().split()))
	b = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b.sort()
	g.sort()
	if b[-1] > g[0]:
		print(-1)
		return
	if b[-1] == g[0]:
		print(sum(g) + m * (sum(b) - b[-1]))
		return
	if n == 1:
		print(-1)
		return
	print(sum(g) + b[-1] + b[-2] * (m - 1) + m * (sum(b) - b[-1] - b[-2]))
main()


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if max(b) > min(g):
	print(-1)
else:
	maxi = 0
	maxi2 = 0
	for guy in b:
		if guy > maxi:
			(maxi2, maxi) = (maxi, guy)
		elif guy > maxi2:
			maxi2 = guy
	sumi = m * sum(b) + sum(g) - m * maxi + maxi - maxi2
	if maxi in g:
		sumi -= maxi - maxi2
	print(sumi)


def main():
	from sys import stdin, stdout

	def read():
		return stdin.readline().rstrip('\n')

	def read_array(sep=None, maxsplit=-1):
		return read().split(sep, maxsplit)

	def read_int():
		return int(read())

	def read_int_array(sep=None, maxsplit=-1):
		return [int(a) for a in read_array(sep, maxsplit)]

	def write(*args, **kwargs):
		sep = kwargs.get('sep', ' ')
		end = kwargs.get('end', '\n')
		stdout.write(sep.join((str(a) for a in args)) + end)

	def write_array(array, **kwargs):
		sep = kwargs.get('sep', ' ')
		end = kwargs.get('end', '\n')
		stdout.write(sep.join((str(a) for a in array)) + end)
	(n, m) = read_int_array()
	bmin = read_int_array()
	gmax = read_int_array()
	bmin.sort()
	gmax.sort()
	max_boy = bmin[-1]
	min_girl = gmax[0]
	if max_boy > min_girl:
		write(-1)
	elif max_boy == min_girl:
		bmin.pop()
		out = sum(gmax) + sum((x * m for x in bmin))
		write(out)
	else:
		bmin.pop()
		out = sum(gmax) - min_girl + max_boy
		out += min_girl + bmin[-1] * (m - 1)
		bmin.pop()
		out += sum((x * m for x in bmin))
		write(out)
main()


t = 1

def solve():
	l = 0
	ans = 0
	if max(l1) > min(l2):
		return -1
	for i in range(n):
		y = 0
		for j in range(l, m):
			if j == m - 1 and y == m - 1:
				if l1[i] == l2[j]:
					y += 1
					ans += l2[j]
			elif l1[i] <= l2[j]:
				y += 1
				ans += l2[j]
			else:
				break
		l += y
		ans += (m - y) * l1[i]
	if l != m:
		return -1
	return ans
	return ans
for _ in range(t):
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l2.sort(reverse=True)
	l1.sort(reverse=True)
	print(solve())


from sys import stdin
(n, m) = map(int, stdin.readline().strip().split())
s = list(map(int, stdin.readline().strip().split()))
s1 = list(map(int, stdin.readline().strip().split()))
if min(s1) < max(s):
	print(-1)
	exit(0)
s.sort()
s1.sort()
ans = 0
if s1[0] != s[-1]:
	ans += s1[0]
	ans += s[-2] * (m - 1)
	ans += sum(s1[1:])
	ans += s[-1]
	for i in range(n - 2):
		ans += s[i] * m
else:
	ans += sum(s1)
	for i in range(n - 1):
		ans += s[i] * m
print(ans)


(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
s = sorted(list(map(int, input().split())))
if a[-1] > s[0]:
	print(-1)
elif a[-1] == s[0]:
	print(sum(a[:-1]) * m + sum(s))
else:
	print(sum(a[:-2]) * m + a[-2] * (m - 1) + sum(s) + a[-1])


I = lambda : map(int, input().split())
(n, m) = I()
(a, b) = (sorted(I(), reverse=1), sorted(I()))
s = sum(b)
for i in range(1, n):
	s += a[i] * m
if a[0] > min(b):
	s = -1
elif a[0] != b[0]:
	s += a[0] - a[1]
print(s)


(n, m) = map(int, input().split())
(b, g) = (sorted(map(int, input().split())), sorted(map(int, input().split())))
print(-1 if b[-1] > g[0] else m * sum(b) + sum((gj - b[-1] for gj in g)) + (b[-1] < g[0]) * (b[-1] - b[-2]))


import heapq
for _ in range(1):
	(n, m) = map(int, input().split())
	b = list(map(int, input().split()))
	h = list(map(int, input().split()))
	w = min(h)
	for i in range(m):
		h[i] = -1 * h[i]
	heapq.heapify(h)
	b.sort()
	if b[-1] > w:
		print(-1)
		continue
	ans = 0
	i = n - 1
	while i > -1:
		cnt = 0
		som = 0
		f = 0
		if len(h) > 0:
			prev = 1
			while -1 * h[0] >= b[i]:
				x = heapq.heappop(h)
				prev = x
				if -1 * x == b[i]:
					f = 1
				som += -1 * x
				cnt += 1
				if len(h) == 0:
					break
		if f == 0 and cnt == m:
			cnt -= 1
			som += prev
			heapq.heappush(h, prev)
		ans = ans + (m - cnt) * b[i] + som
		i -= 1
	if len(h) > 0:
		print(-1)
	else:
		print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if max(a) > min(b):
	print(-1)
	exit()
a.sort(reverse=True)
b.sort()
j = m - 1
i = 0
ans = 0
while j >= 0:
	if j == 0:
		if b[j] == a[i]:
			ans += b[j]
			flag = 1
			break
		else:
			ans += a[i]
			flag = 0
			break
	if b[j] >= a[i]:
		ans += b[j]
		j -= 1
for i in range(1, n):
	ans += a[i] * m
if flag:
	print(ans)
else:
	print(ans + b[0] - a[1])


(n, m) = (int(x) for x in input().split())
boys_out = sorted([int(x) for x in input().split()], reverse=True)
girls_in = sorted([int(x) for x in input().split()])
max_boy = max(boys_out)
ans = 0
for boy in boys_out:
	ans += boy * m
count = 0
i = 0
for girl in girls_in:
	if girl < max_boy:
		print(-1)
		quit()
	if girl > max_boy:
		if count == m - 1:
			count = 0
			i += 1
		if i >= n:
			print(-1)
			quit()
		ans += girl - boys_out[i]
		count += 1
print(ans)


(n, m) = [int(t) for t in input().split(' ')]
b = [int(t) for t in input().split(' ')]
g = [int(t) for t in input().split(' ')]
count = 0
max_b = max(b)
min_sat = False
rest_b = sum(b) - max_b
for gi in g:
	count += gi + rest_b
	min_sat |= gi == max_b
if not min_sat:
	b = sorted(b, reverse=True)
	count += b[0] - b[1]
impossible = max(b) > min(g)
print(-1 if impossible else count)


(n, m) = [int(x) for x in input().split()]
a = sorted([int(x) for x in input().split()])
b = [int(x) for x in input().split()]
if max(a) < min(b):
	print(sum(a) * m + sum(b) - a[-1] * (m - 1) - a[-2])
elif max(a) == min(b):
	print(sum(a) * m + sum(b) - a[-1] * m)
else:
	print(-1)


(N, M) = map(int, input().split())
A = sorted([int(a) for a in input().split()])[::-1]
B = sorted([int(a) for a in input().split()])
if A[0] > B[0]:
	print(-1)
else:
	ans = 0
	for a in A:
		ans += a * M
	i = 0
	j = M - 1
	for b in B:
		if b > A[0]:
			ans += b - A[i]
			j -= 1
			if j == 0:
				i += 1
				j = M - 1
	print(ans)


(n, m) = map(int, input().strip().split())
boy_give = [int(i) for i in input().strip().split()]
girl_recv = [int(i) for i in input().strip().split()]
boy_give.sort(reverse=True)
girl_min = min(girl_recv)
girl_sum = sum(girl_recv)
girl_recv = set(girl_recv)
ans = 0
has = True
complete = False
if boy_give[0] > girl_min:
	has = False
elif boy_give[0] in girl_recv:
	ans += girl_sum
	complete = True
else:
	ans += girl_sum - girl_min + boy_give[0]
if has:
	if complete:
		for i in range(1, n):
			if boy_give[i] > girl_min:
				has = False
				break
			ans += boy_give[i] * m
	else:
		if boy_give[1] > girl_min:
			has = False
		else:
			ans += girl_min + boy_give[1] * (m - 1)
		for i in range(2, n):
			if boy_give[i] > girl_min:
				has = False
				break
			ans += boy_give[i] * m
	if has:
		print(ans)
	else:
		print(-1)
else:
	print(-1)


import sys
import bisect
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
B = list(map(int, input().split()))
G = list(map(int, input().split()))
B.sort()
G.sort()
if max(B) > min(G):
	print(-1)
	return
ANS = sum(B) * m
MAX = B[-1]
first = m
for i in range(m):
	if G[i] > MAX:
		first = i
		break
if first == 0:
	ANS += sum(G[1:]) - MAX * (m - 1)
	ANS += G[0] - B[-2]
	print(ANS)
	return
else:
	ANS += sum(G[first:]) - MAX * (m - first)
	print(ANS)


(n, m) = map(int, input().split())
minRows = list(map(int, input().split()))
maxCols = list(map(int, input().split()))

def secondMax(arr):
	arr.sort()
	if len(arr) > 1:
		return arr[-2]
maxCols.sort(reverse=True)
minRows.sort()

def solve():
	maxmin = minRows[-1]
	minmax = maxCols[-1]
	if maxmin > minmax:
		print(-1)
		return
	ans = 0
	for i in minRows:
		ans += i * m
	for i in range(m):
		ans += maxCols[i] - maxmin
	if maxmin != minmax and n > 1:
		ans += maxCols[-1] - minRows[-2]
		ans += minRows[-1] - maxCols[-1]
	print(ans)
solve()


(n, m) = map(int, input().split())
boys = list(map(int, input().split()))
girls = list(map(int, input().split()))
boys.sort()
girls.sort()
res = 0
if boys[-1] > girls[0]:
	print(-1)
	exit(0)
for b in boys:
	res += b * len(girls)
if boys[-1] == girls[0]:
	for g in girls:
		res += g - boys[-1]
else:
	res += girls[0] - boys[-2]
	for g in girls[1:]:
		res += g - boys[-1]
print(res)


import bisect

def party_sweet(b, g):
	maxb = max(b)
	ming = min(g)
	if maxb > ming:
		return -1
	elif maxb == ming:
		return (sum(b) - maxb) * len(g) + sum(g)
	else:
		return sum(b) * len(g) + sum(g) - maxb * (len(g) - 1) - sorted(b)[-2]

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


(n, m) = map(int, input().split())
b = sorted(list(map(int, input().split())), reverse=True)
g = sorted(list(map(int, input().split())), reverse=True)
j = 0
ans = 0
for j in range(m - 1):
	if b[0] > g[j]:
		print(-1)
		exit(0)
	ans += g[j]
if b[0] > g[m - 1]:
	print(-1)
	exit(0)
elif b[0] == g[m - 1]:
	ans += g[m - 1]
	for i in range(1, n):
		ans += b[i] * m
else:
	ans += b[0] + g[m - 1]
	ans += b[1] * (m - 1)
	for i in range(2, n):
		ans += b[i] * m
print(ans)


def go():
	(n, m) = map(int, input().split(' '))
	b = sorted([int(i) for i in input().split(' ')], reverse=True)
	g = sorted([int(i) for i in input().split(' ')])
	if b[0] > g[0]:
		return -1
	if m == 1:
		return -1
	total = 0
	for i in range(n):
		total += b[i] * m
	j = 0
	c = 0
	for i in range(m):
		if j >= n:
			return -1
		if c >= m - 1:
			j += 1
			c = 0
		if g[i] != b[j]:
			total += g[i] - b[j]
			c += 1
	return total
print(go())


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def computeLPSArray(pat, M, lps):
	len = 0
	lps[0]
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
m = 10 ** 9 + 7
t = 1
while t:
	t -= 1
	(n, m) = map(int, fastinput().split())
	a = list(map(int, fastinput().split()))
	b = list(map(int, fastinput().split()))
	a.sort()
	b.sort(reverse=True)
	total = 0
	for i in a:
		total += m * i
	for i in b[:-1]:
		if i < a[-1]:
			print(-1)
			exit()
		else:
			total += i - a[-1]
	if b[-1] == a[-1]:
		print(total)
	elif b[-1] > a[-1]:
		print(total + b[-1] - a[-2])
	else:
		print(-1)


from sys import exit
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if max(b) > min(g):
	print(-1)
	exit(0)
b.sort()
res = sum(g) + sum(b[:-1]) * m
if b[-1] in g:
	print(res)
else:
	print(res + b[-1] - b[-2])


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dop = sum(a) * m
a.sort()
b.sort()
if a[-1] > b[0]:
	print(-1)
else:
	ans = 0
	for i in range(m):
		ans += b[i]
	if a[-1] == b[0]:
		ans -= a[-1] * m
	else:
		ans -= a[-1] * (m - 1)
		ans -= a[-2]
	ans = ans + dop
	print(ans)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort()
g.sort()
if b[len(b) - 1] <= g[0]:
	c = sum(b) * m
	if g[0] != b[len(b) - 1]:
		c = c + g[0] - b[len(b) - 2]
	for i in range(1, m):
		c = c + g[i] - b[len(b) - 1]
else:
	c = -1
print(c)


from collections import Counter, deque as dq
from sys import stdin
input = stdin.readline

def f(a, b):
	cnt = Counter(b)
	B = len(b)
	if max(a) > min(b):
		return -1
	a = sorted(a)
	b = sorted(b)
	t = 0
	for i in a:
		t += i * B
	for i in b[1:]:
		t += i - a[-1]
	if b[0] > a[-1]:
		t += b[0] - a[-2]
	return t
(a, b) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
print(f(a, b))


def main():
	(n, m) = map(int, input().split())
	b = [int(x) for x in input().split()]
	g = [int(x) for x in input().split()]
	b.sort(reverse=True)
	g.sort(reverse=True)
	if b[0] > g[-1]:
		print('-1')
		return
	ans = 0
	for i in range(m - 1):
		ans += g[i]
	ans += b[0]
	if g[-1] > b[0]:
		ans += g[-1]
		ans += b[1] * (m - 1)
	else:
		ans += b[1] * m
	for i in range(2, n):
		ans += b[i] * m
	print(ans)
main()


(n, m) = map(int, input().split())
boys = list(map(int, input().split()))
girls = list(map(int, input().split()))
boys = sorted(boys, reverse=True)
minA = min(girls)
c = 0
if boys[0] > minA:
	print(-1)
else:
	check = 0 if boys[0] not in girls else 1
	c += sum(girls)
	for i in range(1, n):
		if not check and boys[i] <= boys[0]:
			c += boys[0]
			c += boys[i] * (m - 1)
			check = 1
		else:
			c += boys[i] * m
	print(c)


import sys
(num_b, num_g) = map(int, input().split(' '))
B = list(map(int, input().split(' ')))
G = list(map(int, input().split(' ')))
max_b = max(B)
min_g = min(G)
if max_b > min_g:
	print(-1)
	sys.exit(0)
max_b = 0
second_max_b = 0
for (i, b) in enumerate(B):
	if b >= max_b:
		second_max_b = max_b
		max_b = b
	elif b >= second_max_b:
		second_max_b = b
total = 0
for b in B:
	total += b * num_g
for g in G:
	total += g - max_b
if max_b != min_g:
	total += max_b - second_max_b
print(total)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort()
g.sort()
if b[-1] > g[0]:
	print(-1)
	import sys
	sys.exit(0)
a = 0
a += sum(g) - g[0]
if g[0] == b[-1]:
	a += g[0]
	a += m * sum(b[:-1])
	print(a)
else:
	a += g[0]
	a += m * sum(b[:-2]) + (m - 1) * b[-2] + b[-1]
	print(a)


import collections
import heapq
import sys
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
has_min = [False] * n
chosen = [0] * n
necessary_candy = sum((m * boy for boy in b))
g.sort(reverse=True)
b = list(zip((-boy for boy in b), range(n)))
heapq.heapify(b)
wasted_candy = 0
for i in range(len(g)):
	while len(b) > 0 and -b[0][0] <= g[i] and (not (has_min[b[0][1]] or chosen[b[0][1]] < m - 1 or -b[0][0] == g[i])):
		heapq.heappop(b)
	if len(b) == 0 or -b[0][0] > g[i]:
		print(-1)
		sys.exit()
	chosen_boy = b[0]
	if g[i] == -chosen_boy[0]:
		has_min[chosen_boy[1]] = True
	chosen[chosen_boy[1]] += 1
	wasted_candy += g[i] - -chosen_boy[0]
print(necessary_candy + wasted_candy)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.sort()
b.sort()
if a[-1] > b[0]:
	print(-1)
	exit()
if a[-1] == b[0]:
	print(sum(b) + sum(a[:-1]) * m)
else:
	print(sum(b) + a[-1] + sum(a[:-1]) * m - a[-2])


import sys
(n, m) = map(int, input().split())
boys = sorted(map(int, sys.stdin.readline().split()), reverse=True)
girls = sorted(map(int, sys.stdin.readline().split()), reverse=True)
if min(girls) < max(boys):
	print(-1)
else:
	total = m * sum(boys)
	i = 0
	j = 0
	while i < m and girls[i] > max(boys):
		total += sum([girls[k] - boys[j] for k in range(i, min(i + m - 1, m))])
		j += 1
		i += m - 1
	print(total)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
b = [int(k) for k in input().split()]
g = [int(k) for k in input().split()]
if max(b) > min(g):
	print(-1)
elif max(b) == min(g):
	print(sum(b) * m + sum(g) - max(b) * m)
else:
	b.sort()
	print(sum(b) * m + sum(g) - max(b) * (m - 1) - b[-2])


def main():
	(n, m) = map(int, input().split())
	b = sorted([int(i) for i in input().split()])
	g = sorted([int(i) for i in input().split()])
	(x, y) = (b[-1], g[0])
	if x > y:
		print(-1)
	elif x == y:
		print(sum(b) * m + sum(g) - x * m)
	else:
		print(sum(b) * m + sum(g) - x * (m - 1) - b[-2])
main()


import sys

def second_largest(numbers):
	count = 0
	m1 = m2 = float('-inf')
	for x in numbers:
		count += 1
		if x > m2:
			if x >= m1:
				(m1, m2) = (x, m1)
			else:
				m2 = x
	return m2 if count >= 2 else None
(n, m) = map(int, input().split())
boys = list(map(int, input().split()))
girls = list(map(int, input().split()))
firstMax = max(boys)
secondMax = second_largest(boys)
minGrills = min(girls)
minSum = 0
if firstMax > minGrills:
	print(-1)
	sys.exit()
elif firstMax == minGrills:
	minSum = m * (sum(boys) - firstMax) + sum(girls)
elif n == 1:
	print(-1)
	sys.exit()
else:
	minSum = m * sum(boys) + sum(girls) - (m - 1) * firstMax - secondMax
print(minSum)


x = input()
x = x.split()
n = int(x[0])
m = int(x[1])
boys = input()
girls = input()
boys = boys.split()
boys = [int(x) for x in boys]
girls = girls.split()
girls = [int(x) for x in girls]
tot = 0
for i in range(n):
	tot = tot + boys[i]
boys.sort()
girls.sort()
tot = tot * m
temp = 0
i = m - 1
j = n - 1
curr = m
while i >= 0 and j >= 0:
	if curr == 1:
		if girls[i] > boys[j]:
			curr = m
			temp += boys[j]
			j -= 1
			continue
		elif girls[i] < boys[j]:
			temp = -1
			j = -1
			break
	temp += girls[i]
	curr -= 1
	if curr == 0:
		j -= 1
		curr = m
	i -= 1
if j >= 0:
	temp += curr * boys[j]
j -= 1
while j >= 0:
	temp += m * boys[j]
	j -= 1
if temp < tot:
	print(-1)
else:
	print(temp)


input()
b = sorted(list(map(int, input().split())))
g = sorted(list(map(int, input().split())))
if b[-1] > g[0] or len(b) == 1:
	print(-1)
elif len(g) == 1:
	if all((i == g[0] for i in b)):
		print(g[0] * len(b))
	else:
		print(-1)
else:
	s = 0
	s += sum(g)
	if b[-1] != g[0]:
		s += b[-1]
		s -= b[-2]
	for i in range(len(b) - 1):
		s += b[i] * len(g)
	print(s)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
ind = 0
p = m - 1
ans = sum(b) * m
b.sort(reverse=1)
g.sort(reverse=1)
for i in range(m):
	if g[i] < b[0]:
		print(-1)
		exit()
	if g[i] > b[0]:
		if ind == n:
			print(-1)
			exit()
		ans += g[i] - b[ind]
		p -= 1
		if p == 0:
			ind += 1
			p = m - 1
print(ans)


(n, m) = map(int, input().split())
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
p = mx = 0
mn = 1000000001
s = 0
rez = 0
for i in range(n):
	if b[i] > mx:
		mx = b[i]
		p = i
for x in c:
	mn = min(mn, x)
if mx > mn:
	print(-1)
else:
	mx1 = mx
	mx = 0
	if mx1 != mn:
		for i in range(n):
			s += b[i]
			if i != p:
				mx = max(mx, b[i])
		rez = s + mn - mx
		for i in range(m):
			if c[i] == mn:
				p = i
	else:
		p = m + 1
		for i in b:
			s += i
	for i in range(m):
		if i != p:
			rez += s + c[i] - mx1
	print(rez)


from sys import stdin, stdout

def take_input():
	return [int(x) for x in stdin.readline().rstrip().split()]

def min_sweets(b, g, b_sweet, g_sweet):
	if len(b_sweet) == 0 or len(g_sweet) == 0:
		return -1
	total_sum = 0
	max_b = b_sweet[0]
	second_max_b = -1
	max_ind = 0
	for (index, el) in enumerate(b_sweet):
		if el > max_b:
			max_b = el
			max_ind = index
		total_sum += el * g
	for (index, el) in enumerate(b_sweet):
		if el <= max_b and el > second_max_b and (max_ind != index):
			second_max_b = el
	min_g = g_sweet[0]
	for i in g_sweet:
		if i < min_g:
			min_g = i
		total_sum += i
	if max_b > min_g:
		return -1
	if max_b == min_g:
		total_sum -= max_b * g
	if max_b < min_g:
		total_sum = total_sum - max_b * (g - 1)
		total_sum = total_sum - second_max_b
	return total_sum
(b, g) = take_input()
b_sweet = take_input()
g_sweet = take_input()
stdout.write(str(min_sweets(b, g, b_sweet, g_sweet)) + '\n')


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
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())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if min(g) < max(b):
	print(-1)
else:
	a = [0] * n
	b.sort()
	g.sort()
	a[-1] = m - 1
	if b[-1] == g[0]:
		a[-1] += 1
	else:
		a[-2] = 1
	r = sum(g)
	for i in range(n):
		r += (m - a[i]) * b[i]
	print(r)


(n, m) = map(int, input().split())
b = [int(s) for s in input().split()]
g = [int(s) for s in input().split()]
ans = 0
(maxb2, maxb) = sorted(b)[-2:]
ming = min(g)
if maxb > ming:
	ans = -1
else:
	ans += sum(b) * m
	ans += sum(g) - ming - maxb * (m - 1)
	if ming > maxb:
		ans += ming - maxb2
print(ans)


from decimal import Decimal
(n, m) = input().split(' ')
n = int(n)
m = int(m)
s = input()
b = [int(x) for x in s.split(' ')]
s = input()
g = [int(x) for x in s.split(' ')]
b = sorted(b)
g = sorted(g)
value = 0
d = Decimal()
bad = False
presents = set()
p = m - 1
if g[0] < b[-1]:
	print(-1)
else:
	for i in reversed(range(0, n)):
		proc = 0
		while p >= 0 and g[p] >= b[i] and (m - proc > 1 or g[p] == b[i]):
			value += Decimal(g[p])
			proc += 1
			p -= 1
		value += Decimal(b[i]) * (m - proc)
	print(value)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if max(b) > min(g):
	print(-1)
else:
	total = m * sum(b)
	b.sort()
	g.sort()
	while len(g) > 0:
		current = 0
		count = 1
		if len(b) > 0:
			current = b.pop()
		while len(g) > 0 and g[-1] > current and (count < m):
			total += g[-1] - current
			g.pop()
			count += 1
		while len(g) > 0 and g[-1] == current:
			g.pop()
	print(total)


(n, m) = map(int, input().split())
b = [int(x) for x in input().split()]
g = [int(x) for x in input().split()]
b1 = max(b)
g1 = min(g)
c = b[:]
c.remove(b1)
max2 = max(c)
if b1 > g1:
	print(-1)
elif b1 == g1:
	print(sum(b) * m + sum(g) - b1 * m)
else:
	print(sum(b) * m - b1 * m + sum(g) + b1 - max2)


arr = input()
(N, M) = [int(x) for x in arr.split(' ')]
arr = input()
B = [int(x) for x in arr.split(' ')]
arr = input()
G = [int(x) for x in arr.split(' ')]
s = 0
for i in range(N):
	s += B[i] * M
if min(G) < max(B):
	print(-1)
else:
	B.sort(reverse=True)
	G.sort(reverse=True)
	ans = s
	for i in range(M - 1):
		ans += G[i] - B[0]
	if G[M - 1] > B[0]:
		ans += G[M - 1] - B[1]
	print(ans)


rd = lambda : map(int, input().split())
(n, m) = rd()
(a, b) = (list(rd()), list(rd()))
a.sort(reverse=True)
b.sort()
if a[0] > b[0]:
	print(-1)
else:
	d = [0] * n
	ans = 0
	j = m - 1
	for i in range(n):
		ans += m * a[i]
		while j - 1 >= 0 and b[j - 1] > a[i]:
			j -= 1
		d[i] = min(m - j, m - 1)
	(i, j) = (0, m - 1)
	while i < n and j >= 0:
		while j >= 0 and a[0] >= b[j]:
			j -= 1
		while d[i] > 0 and j >= 0:
			ans = ans - a[i] + b[j]
			d[i] -= 1
			j -= 1
		i += 1
	print(-1 if i >= n and j >= 0 else ans)


(n, m) = map(int, input().split())
a = []
b = []
ma = 0
macount = 0
mi = 1000000000000000000000000000
su = 0
for el in map(int, input().split()):
	if el > ma:
		ma = el
		macount = 1
	elif el == ma:
		macount += 1
	a.append(el)
for el in map(int, input().split()):
	mi = min(el, mi)
	b.append(el)
	su += el
if ma > mi:
	print(-1)
elif ma == mi or macount > 1:
	f = True
	for i in range(n):
		if a[i] == ma and f:
			f = False
		else:
			su += a[i] * m
	print(su)
else:
	secmax = 0
	for el in a:
		if el > secmax and el < ma:
			secmax = el
	f = True
	for i in range(n):
		if a[i] == ma and f:
			f = False
		else:
			su += a[i] * m
	print(su + ma - secmax)


from sys import stdin
sys_input = stdin.readline

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

def ii():
	return int(si())

def sti():
	return si().split()

def iti():
	return map(int, sti())

def sli():
	return list(si())

def ili():
	return list(iti())

def main():
	B.sort(reverse=True)
	G.sort(reverse=True)
	if B[0] > G[-1]:
		print(-1)
		return
	boy_capacities = [M - 1] * N
	current_capable_boy_index = 0
	result = sum(B) * M
	for (j, g) in enumerate(G):
		yet = True
		while yet:
			if B[current_capable_boy_index] < g and boy_capacities[current_capable_boy_index] > 0:
				result += g - B[current_capable_boy_index]
				boy_capacities[current_capable_boy_index] -= 1
				yet = False
			elif B[current_capable_boy_index] == g:
				result += g - B[current_capable_boy_index]
				yet = False
			else:
				current_capable_boy_index += 1
				if current_capable_boy_index > N - 1:
					print(-1)
					return
	print(result)
	return
(N, M) = iti()
B = ili()
G = ili()
main()


(n, m) = list(map(int, input().split()))
b = list(map(int, input().split()))
g = list(map(int, input().split()))
y = max(b)
r = sum(b)
z = m * r
flag = 0
if max(b) > min(g):
	print(-1)
else:
	flag = 1
	b.sort()
	u = 0
	v = 0
	for i in g:
		if i > y:
			u += 1
			t = i - y
			z += t
		else:
			v += 1
			pass
	if u > 1 and v == 0:
		b.pop()
		z = z + (y - b[-1])
	print(z)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.sort()
l2.sort()
if l2[0] < l1[n - 1]:
	print(-1)
	exit()
ans = sum(l1) * m
for i in range(m - 1, 0, -1):
	ans += l2[i] - l1[n - 1]
if l2[0] != l1[n - 1]:
	ans += l2[0] - l1[n - 2]
print(ans)


(n, m) = map(int, input().split())
B = list(map(int, input().split()))
G = list(map(int, input().split()))
if min(G) < max(B):
	print(-1)
	exit(0)
cnt = 0
z = max(B)
y = 0
f = 1
f2 = 0
for i in B:
	if i != z or f2:
		y = max(y, i)
	else:
		f2 = 1
for i in G:
	if i == z:
		f = 0
	cnt += i - z
if f:
	cnt += z - y
print(cnt + sum(B) * m)


(n, m) = map(int, input().split())
Boys = [int(x) for x in input().split()]
Girls = [int(x) for x in input().split()]
if min(Girls) < max(Boys):
	print(-1)
else:
	Boys.sort(reverse=True)
	Girls.sort(reverse=True)
	Ans = 0
	for i in range(n):
		Ans += Boys[i] * m
	g = 0
	b = 0
	bins = 1
	while g < m:
		Ans += Girls[g] - Boys[b]
		g += 1
		bins += 1
		if bins == m:
			if g < m and Girls[g] - Boys[b] <= 0:
				g += 1
			bins = 1
			b += 1
	print(Ans)


def main():
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	k = list(map(int, input().split()))
	l.sort()
	s = sum(l) * m
	e = k[0]
	cnt = 0
	for i in range(m):
		if k[i] < e:
			e = k[i]
		if k[i] > l[n - 1]:
			s += k[i] - l[n - 1]
			cnt += 1
	if e < l[n - 1]:
		print(-1)
		return
	if cnt == m:
		if n >= 2:
			s += l[n - 1] - l[n - 2]
	print(s)
	return
main()


(n, m) = [int(i) for i in input().split()]
mins = [int(i) for i in input().split()]
maxs = [int(i) for i in input().split()]
temp = mins[:]
temp.sort()
pivot = temp[-1]
pivot2 = temp[-2]
ans = sum(mins) * m
ans += sum(maxs)
ans -= pivot * (m - 1) + pivot2
bad = False
q = min(maxs)
if q < pivot:
	print(-1)
elif q == pivot:
	print(ans + pivot2 - pivot)
else:
	print(ans)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
mx = max(b)
mn = min(g)
if mn >= mx:
	b.sort()
	g.sort()
	sb = sum(b)
	sg = sum(g)
	ans = sb * m
	if mn == mx:
		ans = ans - m * mx + sg
	else:
		ans = ans - b[n - 1] * (m - 1) + sg - g[0] - b[n - 2] + g[0]
	print(ans)
else:
	print('-1')


(n, m) = [int(i) for i in input().split()]
boys = sorted([int(i) for i in input().split()], reverse=True)
girls = sorted([int(i) for i in input().split()])
soma = 0
minGirl = girls[0]
impossivel = False
for i in range(len(boys)):
	if minGirl < boys[i]:
		impossivel = True
		break
	soma += m * boys[i]
if not impossivel:
	flag = False
	for i in range(len(girls) - 1):
		if girls[i] == boys[0]:
			flag = True
		soma += girls[i] - boys[0]
	if flag:
		soma += girls[len(girls) - 1] - boys[0]
	elif len(boys) > 1 and len(girls) > 1:
		soma += girls[len(girls) - 1] - boys[1]
	elif minGirl != min(boys):
		impossivel = True
	if impossivel:
		print(-1)
	else:
		print(soma)
else:
	print(-1)


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
a = list(map(int, input().split()))
l.sort(reverse=True)
a.sort(reverse=True)
if a[-1] < l[0] or (n == 1 and l[0] != a[-1]) or (m == 1 and l[0] != a[0]):
	print(-1)
	exit()
mus = sum(a)
musl = sum(l)
musl -= l[0]
if a[-1] == l[0]:
	print(mus + musl * m)
else:
	mus -= a[-1]
	print(mus + musl * m - l[1] + a[-1] + l[0])


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(n, m) = map(int, input().split())
b = sorted(map(int, input().split()))
g = sorted(map(int, input().split()))
if b[n - 1] > g[0]:
	print(-1)
	exit()
ans = 0
found = False
for i in range(n - 1):
	ans += b[i] * m
ans += sum(g)
for j in range(m):
	if g[j] == b[-1]:
		found = True
if not found:
	ans -= b[-2]
	ans += b[-1]
print(ans)


from bisect import bisect_left as lb
(n, m) = map(int, input().split())
b = sorted(map(int, input().split()))
g = sorted(map(int, input().split()))
if b[-1] > g[0]:
	print(-1)
	exit()
res = sum(g) + m * sum(b)
(lo, hi) = (0, m)
for i in range(n - 1, -1, -1):
	j = lb(g, b[i], lo, hi)
	rem = hi - j
	if rem == m and b[i] != g[j]:
		j += 1
		if j == hi:
			print(-2)
			exit()
		rem -= 1
	res -= rem * b[i]
	hi = j
print(res)


def read_nums():
	return [int(x) for x in input().split()]

def main():
	(n, m) = read_nums()
	boys = sorted(read_nums(), reverse=True)
	girls = sorted(read_nums(), reverse=True)
	if min(girls) < max(boys):
		print(-1)
		return
	set_boys = set(boys)
	cum = 0
	index = 0
	count = 0
	for g in girls:
		if g in set_boys:
			continue
		cum += g - boys[index]
		count += 1
		if count == m - 1:
			index += 1
			count = 0
	print(sum(boys) * m + cum)
main()


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

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

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

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def solve():
	(n, m) = num_input()
	arr = sorted(list_input(), reverse=True)
	brr = sorted(list_input())
	ans = 0
	flag = False
	prev = 0
	for i in range(n):
		indx = bisect.bisect_left(brr, arr[i])
		if indx != 0:
			print(-1)
			return
		if brr[0] == arr[i]:
			ans += sum(brr)
			flag = True
			break
		if i == 0:
			ans += sum(brr[:m - 1]) + arr[i]
			prev = brr[-1]
		if i == 1:
			ans += (m - 1) * arr[i] + prev
			prev = 0
			break
	if flag:
		for i in range(1, n):
			ans += m * arr[i]
		print(ans)
	else:
		if prev != 0:
			print(-1)
			return
		for i in range(2, n):
			ans += m * arr[i]
		print(ans)
t = 1
for _ in range(t):
	solve()


from sys import exit
(N, M) = map(int, input().split())
B = list(map(int, input().split()))
G = list(map(int, input().split()))
B.sort()
mB = B[-1]
m2B = B[-2]
mG = min(G)
if mB > mG:
	print(-1)
	exit()
if mB == mG:
	print(sum(B) * M + sum(G) - mB * M)
	exit()
print(sum(B) * M + sum(G) - mB * M + mB - m2B)


(b, g) = list(map(int, input().split()))
boys = sorted(list(map(int, input().split())), reverse=True)
girls = sorted(list(map(int, input().split())), reverse=True)
if boys[0] > girls[g - 1]:
	print(-1)
else:
	ans = len(girls) * sum(boys)
	if b == 1:
		if girls[g - 1] > boys[0]:
			ans = -1
		else:
			for i in range(g - 1):
				ans += girls[i] - boys[0]
	else:
		cur_boy = 0
		cur_girl = 0
		num_exhausted = 0
		if girls[g - 1] > boys[0]:
			for i in range(g - 1):
				ans += girls[i] - boys[0]
			ans += girls[g - 1] - boys[1]
		else:
			for i in range(g - 1):
				ans += girls[i] - boys[0]
	print(ans)


(n, m) = map(int, input().split())
arr1 = sorted(list(map(int, input().split())), reverse=True)
arr2 = sorted(list(map(int, input().split())), reverse=True)
if arr2.count(0) != 0:
	if n == arr1.count(0):
		print(sum(arr2))
	else:
		print(-1)
else:
	max1 = arr1[0]
	ans = sum(arr1) * m
	flag = True
	for i in range(m):
		if arr2[i] < max1:
			flag = False
	if flag == False:
		print(-1)
	else:
		for i in range(m - 1):
			ans += arr2[i] - arr1[0]
		if arr2[-1] == arr1[0]:
			ans += arr2[-1] - arr1[0]
		else:
			ans += arr2[-1] - arr1[1]
		print(ans)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort()
g.sort()
if b[-1] > g[0]:
	print(-1)
	exit()
if b[-1] == g[0]:
	ans = 0
	for i in range(n - 1):
		ans += b[i] * m
	ans += sum(g)
	print(ans)
else:
	ans = 0
	for i in range(n - 2):
		ans += b[i] * m
	ans += g[0] + b[n - 2] * (m - 1)
	ans += b[-1] + sum(g[1:])
	print(ans)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
(n, m) = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
if a[-1] > b[0]:
	print(-1)
elif a[-1] == b[0]:
	print(sum(b) + sum(a[:-1]) * m)
else:
	print(sum(b) + a[-1] + sum(a[:-1]) * m - a[-2])


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.sort()
l2.sort()
m1 = max(l1)
m2 = min(l2)
s1 = sum(l1)
s2 = sum(l2)
if m2 < m1:
	print(-1)
elif l2[0] > l1[n - 1]:
	print(m * s1 - l1[n - 2] - (m - 1) * l1[n - 1] + s2)
else:
	print(m * s1 - m * l1[n - 1] + s2)


def main():
	(n, m) = list(map(int, input().split()))
	bs = list(map(int, input().split()))
	gs = list(map(int, input().split()))
	num_sweets = m * sum(bs)
	b = sorted(bs)
	del bs
	remaining_slots = [m - 1] * n
	for (j, g) in enumerate(gs):
		lval_index = n - 1
		while lval_index >= 0:
			if b[lval_index] > g:
				return -1
			if b[lval_index] == g or remaining_slots[lval_index]:
				break
			lval_index -= 1
		if lval_index < 0:
			return -1
		else:
			b_val = b[lval_index]
			if b_val != g:
				remaining_slots[lval_index] -= 1
				num_sweets = num_sweets + (g - b_val)
	return num_sweets
num_sweets = main()
print(num_sweets)


from bisect import bisect
(n, m) = map(int, input().split())
b = sorted(map(int, input().split()))
g = sorted(map(int, input().split()))
if g[0] > b[-1]:
	print(sum(g) + b[-1] + b[-2] * (m - 1) + m * sum(b[:-2]))
elif g[0] == b[-1]:
	i = bisect(g, b[-1])
	print(sum(g) + m * sum(b[:-1]))
else:
	print(-1)


import sys
import collections
import math
import heapq
import bisect
from operator import itemgetter

def getint():
	return int(input())

def getints():
	return [int(x) for x in input().split(' ')]
(n, m) = getints()
b = getints()
g = getints()
result = 0
(bMax, bMax2, bSum) = (-1, -1, 0)
for (i, bb) in enumerate(b):
	bSum += bb
	if bb > bMax:
		(bMax2, bMax) = (bMax, bb)
	elif bb > bMax2:
		bMax2 = bb
(gMin, gSum) = (float('inf'), 0)
for (j, gg) in enumerate(g):
	gSum += gg
	if gg < gMin:
		gMin = gg
if bMax > gMin:
	result = -1
else:
	result = bSum * m
	result += gSum
	result -= bMax * m
	if gMin > bMax:
		result += bMax - bMax2
print(str(result))


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

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

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')

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

def mpint():
	return map(int, sys.stdin.readline().split(' '))

def itg():
	return int(sys.stdin.readline())
import random

class TreapMultiSet(object):
	root = 0
	size = 0

	def __init__(self, data=None):
		if data:
			data = sorted(data)
			self.root = treap_builder(data)
			self.size = len(data)

	def add(self, key):
		self.root = treap_insert(self.root, key)
		self.size += 1

	def remove(self, key):
		self.root = treap_erase(self.root, key)
		self.size -= 1

	def discard(self, key):
		try:
			self.remove(key)
		except KeyError:
			pass

	def ceiling(self, key):
		x = treap_ceiling(self.root, key)
		return treap_keys[x] if x else None

	def higher(self, key):
		x = treap_higher(self.root, key)
		return treap_keys[x] if x else None

	def floor(self, key):
		x = treap_floor(self.root, key)
		return treap_keys[x] if x else None

	def lower(self, key):
		x = treap_lower(self.root, key)
		return treap_keys[x] if x else None

	def max(self):
		return treap_keys[treap_max(self.root)]

	def min(self):
		return treap_keys[treap_min(self.root)]

	def __len__(self):
		return self.size

	def __nonzero__(self):
		return bool(self.root)
	__bool__ = __nonzero__

	def __contains__(self, key):
		return self.floor(key) == key

	def __repr__(self):
		return 'TreapMultiSet({})'.format(list(self))

	def __iter__(self):
		if not self.root:
			return iter([])
		out = []
		stack = [self.root]
		while stack:
			node = stack.pop()
			if node > 0:
				if right_child[node]:
					stack.append(right_child[node])
				stack.append(~node)
				if left_child[node]:
					stack.append(left_child[node])
			else:
				out.append(treap_keys[~node])
		return iter(out)

class TreapHashMap(TreapMultiSet):

	def __init__(self, data=None):
		if data:
			self.map = dict(data)
			super(TreapHashMap, self).__init__(self.map.keys())
		else:
			self.map = dict()

	def __setitem__(self, key, value):
		if key not in self.map:
			super(TreapHashMap, self).add(key)
		self.map[key] = value

	def __getitem__(self, key):
		return self.map[key]

	def add(self, key):
		raise TypeError('add on TreapHashMap')

	def get(self, key, default=None):
		return self.map.get(key, default=default)

	def remove(self, key):
		self.map.pop(key)
		super(TreapHashMap, self).remove(key)

	def discard(self, key):
		if key in self.map:
			self.remove(key)

	def __contains__(self, key):
		return key in self.map

	def __repr__(self):
		return 'TreapHashMap({})'.format(list(self))
left_child = [0]
right_child = [0]
treap_keys = [0]
treap_prior = [0.0]

def treap_builder(sorted_data):

	def build(begin, end):
		if begin == end:
			return 0
		mid = (begin + end) // 2
		root = treap_create_node(sorted_data[mid])
		left_child[root] = build(begin, mid)
		right_child[root] = build(mid + 1, end)
		ind = root
		while True:
			lc = left_child[ind]
			rc = right_child[ind]
			if lc and treap_prior[lc] > treap_prior[ind]:
				if rc and treap_prior[rc] > treap_prior[rc]:
					(treap_prior[ind], treap_prior[rc]) = (treap_prior[rc], treap_prior[ind])
					ind = rc
				else:
					(treap_prior[ind], treap_prior[lc]) = (treap_prior[lc], treap_prior[ind])
					ind = lc
			elif rc and treap_prior[rc] > treap_prior[ind]:
				(treap_prior[ind], treap_prior[rc]) = (treap_prior[rc], treap_prior[ind])
				ind = rc
			else:
				break
		return root
	return build(0, len(sorted_data))

def treap_create_node(key):
	treap_keys.append(key)
	treap_prior.append(random.random())
	left_child.append(0)
	right_child.append(0)
	return len(treap_keys) - 1

def treap_split(root, key):
	left_pos = right_pos = 0
	while root:
		if key < treap_keys[root]:
			left_child[right_pos] = right_pos = root
			root = left_child[root]
		else:
			right_child[left_pos] = left_pos = root
			root = right_child[root]
	(left, right) = (right_child[0], left_child[0])
	right_child[left_pos] = left_child[right_pos] = right_child[0] = left_child[0] = 0
	return (left, right)

def treap_merge(left, right):
	(where, pos) = (left_child, 0)
	while left and right:
		if treap_prior[left] > treap_prior[right]:
			where[pos] = pos = left
			where = right_child
			left = right_child[left]
		else:
			where[pos] = pos = right
			where = left_child
			right = left_child[right]
	where[pos] = left or right
	node = left_child[0]
	left_child[0] = 0
	return node

def treap_insert(root, key):
	if not root:
		return treap_create_node(key)
	(left, right) = treap_split(root, key)
	return treap_merge(treap_merge(left, treap_create_node(key)), right)

def treap_insert_unique(root, key):
	if not root:
		return (treap_create_node(key), False)
	(left, right) = treap_split(root, key)
	if left and treap_keys[left] == key:
		return (treap_merge(left, right), True)
	return (treap_merge(treap_merge(left, treap_create_node(key)), right), False)

def treap_erase(root, key):
	if not root:
		raise KeyError(key)
	if treap_keys[root] == key:
		return treap_merge(left_child[root], right_child[root])
	node = root
	while root and treap_keys[root] != key:
		parent = root
		root = left_child[root] if key < treap_keys[root] else right_child[root]
	if not root:
		raise KeyError(key)
	if root == left_child[parent]:
		left_child[parent] = treap_merge(left_child[root], right_child[root])
	else:
		right_child[parent] = treap_merge(left_child[root], right_child[root])
	return node

def treap_ceiling(root, key):
	while root and treap_keys[root] < key:
		root = right_child[root]
	if not root:
		return 0
	min_node = root
	min_key = treap_keys[root]
	while root:
		if treap_keys[root] < key:
			root = right_child[root]
		else:
			if treap_keys[root] < min_key:
				min_key = treap_keys[root]
				min_node = root
			root = left_child[root]
	return min_node

def treap_higher(root, key):
	while root and treap_keys[root] <= key:
		root = right_child[root]
	if not root:
		return 0
	min_node = root
	min_key = treap_keys[root]
	while root:
		if treap_keys[root] <= key:
			root = right_child[root]
		else:
			if treap_keys[root] < min_key:
				min_key = treap_keys[root]
				min_node = root
			root = left_child[root]
	return min_node

def treap_floor(root, key):
	while root and treap_keys[root] > key:
		root = left_child[root]
	if not root:
		return 0
	max_node = root
	max_key = treap_keys[root]
	while root:
		if treap_keys[root] > key:
			root = left_child[root]
		else:
			if treap_keys[root] > max_key:
				max_key = treap_keys[root]
				max_node = root
			root = right_child[root]
	return max_node

def treap_lower(root, key):
	while root and treap_keys[root] >= key:
		root = left_child[root]
	if not root:
		return 0
	max_node = root
	max_key = treap_keys[root]
	while root:
		if treap_keys[root] >= key:
			root = left_child[root]
		else:
			if treap_keys[root] > max_key:
				max_key = treap_keys[root]
				max_node = root
			root = right_child[root]
	return max_node

def treap_min(root):
	if not root:
		raise ValueError('min on empty treap')
	while left_child[root]:
		root = left_child[root]
	return root

def treap_max(root):
	if not root:
		raise ValueError('max on empty treap')
	while right_child[root]:
		root = right_child[root]
	return root
from collections import Counter

def solve():
	(n, m) = mpint()
	boys = sorted(mpint())
	girls = TreapHashMap(Counter(mpint()))
	if boys[-1] > girls.min():
		return -1
	ans = 0
	while boys:
		b = boys.pop()
		k = m
		if b in girls and girls[b]:
			ans += b * girls[b]
			k -= girls[b]
			girls.remove(b)
		else:
			ans += b
			k -= 1
		for _ in range(k):
			if not girls:
				break
			g_max = girls.max()
			if b > g_max:
				break
			girls[g_max] -= 1
			if not girls[g_max]:
				girls.remove(g_max)
			ans += g_max
			k -= 1
		ans += b * k
	return ans
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
print(solve())


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
Sum = 0
for j in range(n):
	Sum += b[j] * m
b.sort()
for i in range(m):
	Sum += max(0, g[i] - b[-1])
if min(g) < max(b):
	print(-1)
elif min(g) == max(b):
	print(Sum)
else:
	Sum += b[-1] - b[-2]
	print(Sum)


(n, m) = map(int, input().split())
mn = sorted(map(int, input().split()), reverse=True)
mx = list(map(int, input().split()))
if min(mx) < mn[0]:
	print(-1)
else:
	r = m * sum(mn[1:]) + sum(mx)
	if mn[0] not in mx:
		r += mn[0] - mn[1]
	print(r)


from sys import stdin, stdout
from collections import Counter
(n, m) = stdin.readline().strip().split(' ')
b = list(map(int, stdin.readline().strip().split(' ')))
g = list(map(int, stdin.readline().strip().split(' ')))
if min(g) < max(b):
	stdout.write('-1\n')
else:
	ans = 0
	b.sort()
	g.sort()
	b.reverse()
	if b[0] == g[0]:
		ans += sum(g) - g[0] + b[0]
		b = b[1:]
	else:
		ans += sum(g) - g[0] + b[0]
		ans += g[0] + (len(g) - 1) * b[1]
		b = b[2:]
	db = Counter(b)
	for i in db:
		ans += db[i] * i * len(g)
	stdout.write(str(ans))


from collections import defaultdict

def solve():
	if b[-1] > g[0]:
		return -1
	if b[-1] in g:
		ans = sum(g) + m * b[-2] + m * sum(b[:-2])
	else:
		ans = sum(g) + b[-1] + (m - 1) * b[-2] + m * sum(b[:-2])
	occ_b = defaultdict(int)
	for x in b[:-1]:
		occ_b[x] += 1
	already = 0
	for x in g:
		if x in occ_b and occ_b[x] != -1:
			ans -= x
			occ_b[x] = -1
			already += 1
	if already > 0:
		if b[-1] in g:
			ans += already * b[-1]
		else:
			ans += (already - 1) * b[-1] + b[-2]
	return ans
(n, m) = [int(item) for item in input().split()]
b = [int(item) for item in input().split()]
g = [int(item) for item in input().split()]
b.sort()
g.sort()
print(solve())


mx1 = 0
mx2 = 0
sum = 0

def parsare(s, lista):
	number = ''
	for l in s:
		if l != ' ':
			number += l
		else:
			lista.append(int(number))
			number = ''
	lista.append(int(number))
row1 = []
row2 = []
row3 = []
sir = input()
parsare(sir, row1)
sir = input()
parsare(sir, row2)
sir = input()
parsare(sir, row3)
for nr in row2:
	sum += nr * row1[1]
	if nr > mx1:
		mx2 = mx1
		mx1 = nr
	elif nr > mx2:
		mx2 = nr
ok = True
ok2 = False
for nr in row3:
	dif = nr - mx1
	if dif < 0:
		ok = False
		break
	elif dif == 0:
		ok2 = True
	sum += dif
if ok and ok2:
	print(sum)
elif ok:
	print(sum + mx1 - mx2)
else:
	print(-1)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l1.sort()
l2.sort()
s1 = sum(l1)
s2 = sum(l2)
if l2[0] < l1[n - 1]:
	print(-1)
elif l2[0] > l1[n - 1]:
	print(m * s1 - l1[n - 2] - (m - 1) * l1[n - 1] + s2)
else:
	print(m * s1 - m * l1[n - 1] + s2)


R = lambda : list(map(int, input().split()))
(n, m) = R()
a = R()
b = R()
a.sort()
b.sort()
M = a[n - 1]
test = 0
s = 0
for j in range(m):
	if b[j] < M:
		test = 1
		break
if test == 1:
	print(-1)
else:
	k = 0
	for i in range(m):
		if b[i] == a[n - 1]:
			k = 1
	if k == 1:
		s = sum(b) + (sum(a) - a[n - 1]) * m
		print(s)
	else:
		s = sum(b) + (sum(a) - a[n - 1] - a[n - 2]) * m + a[n - 2] * (m - 1) + a[n - 1]
		print(s)


R = lambda : map(int, input().split())
(n, m) = R()
a = list(R())
b = list(R())
a.sort()
b.sort()
t = sum(a) * m + sum(b) - a[-1] * m
if a[-1] > b[0]:
	print(-1)
elif a[-1] == b[0]:
	print(t)
else:
	print(t + a[-1] - a[-2])


l = input().split(' ')
n = int(l[0])
m = int(l[1])
B = input().split(' ')
B = [int(x) for x in B]
J = input().split(' ')
J = [int(x) for x in J]
J.sort()
B.sort()
if J[0] < B[-1]:
	print(-1)
	exit()
k = n - 1
sm = sum(B)
l = (sm - B[-1]) * m
s = l
for i in J:
	s += i
if B[-1] != J[0]:
	s = s + B[-1] - B[-2]
print(s)


def main():
	(n, m) = map(int, input().split())
	boys = list(map(int, input().split()))
	girls = list(map(int, input().split()))
	if min(girls) < max(boys):
		print(-1)
		return
	sweets = 0
	boys.sort(reverse=True)
	girls.sort(reverse=True)
	if min(girls) == max(boys):
		sweets = sum(girls)
		for i in range(1, n):
			sweets += m * boys[i]
	else:
		sweets = sum(girls) - girls[-1] + boys[0]
		sweets += (m - 1) * boys[1]
		sweets += girls[-1]
		for i in range(2, n):
			sweets += boys[i] * m
	print(sweets)
main()


from collections import *
from math import *
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
j = m - 1
ans = 0
if a[-1] > b[0]:
	print(-1)
else:
	for i in range(n - 1, -1, -1):
		ct = 0
		if j >= 0:
			while j >= 0 and a[i] <= b[j]:
				if j == 0 and i == n - 1:
					ans += a[i]
					if a[i] != b[j]:
						ct += 1
						break
				else:
					ans += b[j]
				ct += 1
				j -= 1
		ans += (m - ct) * a[i]
	print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if min(b) < max(a):
	print(-1)
else:
	a.sort()
	if a[-1] == min(b):
		ans = sum(b) + sum(a[:-1]) * m
	else:
		ans = sum(b) + a[-2] * (m - 1) + a[-1] + sum(a[:-2]) * m
	print(ans)


(N, M) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if max(b) > min(g):
	ans = -1
elif max(b) == min(g):
	ans = M * sum(b)
	maxi = max(b)
	for i in range(M):
		if maxi == g[i]:
			continue
		else:
			ans += g[i] - maxi
else:
	ans = M * sum(b)
	b.sort(reverse=True)
	for i in range(M):
		if i == 0:
			ans += g[i] - b[1]
		else:
			ans += g[i] - b[0]
print(ans)


(n, m) = map(int, input().split())
boys = list(map(int, input().split()))
girls = list(map(int, input().split()))
boys.sort(reverse=True)
girls.sort(reverse=True)
s = sum(boys)
ma = max(boys)
res = 0
for i in range(0, m):
	if girls[i] < ma:
		print(-1)
		exit(0)
	res += s
	if girls[i] == ma:
		girls[i] = 0
j = 0
usage = 0
for i in range(0, m):
	if usage == m - 1:
		j += 1
	if j >= n:
		print(-1)
		exit(0)
	res += max(0, girls[i] - boys[j])
	usage += 1
print(res)


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')
import sys
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort()
g.sort()
if g[0] < b[n - 1]:
	print(-1)
	sys.exit()
ans = 0
ans = 0
t = n - 1
i = 0
l = [0] * n
g.reverse()
while i < m:
	if g[i] > b[t] and l[t] < m - 1:
		l[t] += 1
		ans += g[i]
	elif g[i] == b[t] and l[t] < m:
		l[t] += 1
		ans += g[i]
	else:
		t -= 1
		i -= 1
	i += 1
for i in range(n):
	ans += b[i] * (m - l[i])
print(ans)


def solve(B, G):
	if max(B) > min(G):
		return -1
	result = sum(B) * len(G)
	result += sum(G) - max(B) * len(G)
	if max(B) != min(G):
		B.sort()
		result -= B[-2]
		result += B[-1]
	return result
(N, M) = map(int, input().split())
B = list(map(int, input().split()))
G = list(map(int, input().split()))
print(solve(B, G))


(a, b) = (int(i) for i in input().split())
c = [int(i) for i in input().split()]
d = [int(i) for i in input().split()]
leng = len(d)
e = sum(c) * leng
c.sort()
d.sort()
if c[-1] > d[0]:
	print(-1)
else:
	count = 0
	for i in range(len(d)):
		count += d[i]
	if c[-1] == d[0]:
		count -= c[-1] * leng
	else:
		count -= c[-1] * (leng - 1)
		count -= c[-2]
	ans = e + count
	print(ans)


(n, m) = list(map(int, input().split()))
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b = sorted(b, reverse=True)
g = sorted(g, reverse=True)
s = 0
if min(g) < max(b):
	print(-1)
else:
	for i in b:
		s += i * m
	c = m - 1
	k = 0
	for i in g:
		if i == b[k]:
			continue
		if c == 0:
			k += 1
			c = m - 1
		if c != 0 and i > b[k]:
			c -= 1
			s += i - b[k]
	print(s)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
a.sort(reverse=True)
t = a[0]
t1 = a[1]
k = 10 ** 9
work = True
work2 = True
for i in b:
	if i > t:
		c += i
	elif i == t:
		c += i
		work2 = False
	else:
		work = False
		break
	k = min(k, i)
s = sum(a) - t
if not work:
	print(-1)
elif work2:
	c = c - k + t
	print(s * len(b) + c - t1 + k)
else:
	print(s * len(b) + c)


from collections import deque
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.sort(reverse=True)
g.sort(reverse=True)
g = deque(g)
if max(b) > min(g):
	print(-1)
else:
	t = 0
	for e in b:
		t += e * m
		special = len(g) == m and g[-1] >= e
		i = 0
		while i < len(g) - special and g[0] >= e:
			t += g[0] - e
			g.popleft()
		if special and g[-1] == e:
			g.popleft()
	print(t)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
a.reverse()
b.reverse()
if a[0] > b[-1]:
	print(-1)
else:
	que = []
	flag = True
	if b[-1] == a[0]:
		s = sum(b)
		flag = False
	else:
		s = sum(b) - b[-1] + a[0]
	if flag:
		s += (sum(a) - a[0]) * m + b[-1] - a[1]
	else:
		s += (sum(a) - a[0]) * m
	print(s)


(n, m) = map(int, input().split())
B = list(map(int, input().split()))
G = list(map(int, input().split()))
B.sort(reverse=True)
G.sort(reverse=True)
if G[-1] == B[0]:
	ans = sum(G)
	for i in range(1, n):
		ans += m * B[i]
elif G[-1] < B[0]:
	print(-1)
	exit()
else:
	ans = sum(G[0:m - 1]) + B[0]
	if B[1] > G[-1]:
		print(-1)
		exit()
	ans += G[-1] + (m - 1) * B[1]
	for i in range(2, n):
		ans += m * B[i]
print(ans)


def getn():
	return int(input())

def getns():
	return [int(x) for x in input().split()]
(n, m) = getns()
ns = getns()
ms = getns()
if max(ns) > min(ms):
	print(-1)
	quit()
ans = sum(ns) * m
if max(ns) == min(ms):
	ans += sum(ms) - max(ns) * m
	print(ans)
	quit()
ns.sort()
ans += sum(ms) - max(ns) * m
ans += ns[-1] - ns[-2]
print(ans)


(n, m) = map(int, input().split())
b = []
g = []
str = input()
b = list(map(int, str.split(' ')))
str = input()
g = list(map(int, str.split(' ')))
b.sort(reverse=True)
g.sort(reverse=True)
candies = 0
if b[0] > g[m - 1]:
	print(-1)
elif b[0] < g[m - 1]:
	for i in range(m - 1):
		candies = candies + g[i]
	candies = candies + b[0] + b[1] * (m - 1) + g[m - 1]
	for i in range(n - 2):
		candies = candies + b[i + 2] * m
	print(candies)
else:
	for i in range(m):
		candies = candies + g[i]
	for i in range(n - 1):
		candies = candies + b[i + 1] * m
	print(candies)


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
mab = max(b)
mig = min(g)
if mab > mig:
	print(-1)
	exit()
b = sorted(b, reverse=True)
g = sorted(g)
num = 0
j = 0
for i in range(n):
	k = 0
	l = 1
	while j < m and k < m - l and (b[i] <= g[j]):
		if b[i] == g[j]:
			l = 0
		num += g[j]
		j += 1
		k += 1
	num += b[i] * (m - k)
print(num)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 5)
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
if min(g) < max(b):
	print(-1)
	exit()
hash = defaultdict(int)
for i in b:
	hash[i] += m
b = list(set(b))
for i in range(len(b)):
	b[i] = -b[i]
ansi = []
seti = set()
se = set()
ans = 0
for (c, i) in enumerate(g):
	if i in hash and hash[i] != 0:
		hash[i] -= 1
		ans += i
		seti.add(c)
		se.add(i)
heapify(b)
for (c, i) in enumerate(g):
	flag = 0
	if c not in seti:
		while b:
			z = heappop(b)
			z = -z
			if hash[z] == 1 and z not in se:
				continue
			if hash[z] == 0:
				continue
			hash[i] += 1
			hash[z] -= 1
			heappush(b, -z)
			flag = 1
			break
		if flag == 0:
			print(-1)
			exit()
for i in hash:
	ans += hash[i] * i
print(ans)


from collections import Counter

def sum_counter(cnt):
	return sum((cnt[k] * k for k in cnt.keys()))

def main():
	(n, m) = map(int, input().split())
	b = Counter(map(int, input().split()))
	g = Counter(map(int, input().split()))
	ba = max(b.keys())
	gi = min(g.keys())
	if ba > gi:
		print('-1')
	else:
		res = (sum_counter(b) - ba) * m + sum_counter(g)
		if ba < gi and b[ba] == 1:
			res += ba - max(b.keys() - {ba})
		print(res)
main()


(n, m) = map(int, input().strip().split(' '))
b = list(map(int, input().strip().split(' ')))
g = list(map(int, input().strip().split(' ')))
b.sort()
b = b[::-1]
g.sort()
g = g[::-1]
impossible = False
if b[0] > g[-1]:
	impossible = True
result = 0
boy_max = b[0]
for girl in g[:-1]:
	result += girl
result += boy_max
if g[-1] > boy_max:
	result += g[-1] + b[1] * (m - 1)
else:
	result += b[1] * m
result += sum(b[2:]) * m
if impossible:
	result = -1
print(result)