CodeEff/ECCO huggingface dataset:
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()

    t = [[0]*(k+1) for _ in range(n)]
    z = t[-1]
    for i in range(a[-1]+1):
        z[i] = 1

    for i in range(n-2,-1,-1):
        s = 0
        z = t[i]
        y = t[i+1]
        c = a[i] + 1
        for j in range(c):
            s += y[j]
            s %= mod
            z[j] = s

        for j in range(c,k+1):
            s += y[j] - y[j-c]
            s %= mod
            z[j] = s

    return t[0][-1]


print(main())


# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    l = k + 1

    y = [1] * (a[-1]+1) + [0] * (l-a[-1]-1)

    for i in range(n-2,-1,-1):
        z = [None] * l
        s = 0
        c = a[i] + 1
        for j in range(c):
            z[j] = s = (s + y[j]) % mod

        for j in range(c,l):
            z[j] = s = (s + y[j] - y[j-c]) % mod

        y = z

    return (y[-1] + mod) % mod


print(main())
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

dp = [[0] * (k + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    dp[i + 1][0] = dp[i][0]
    for j in range(1, k+1):
        dp[i + 1][j] = (dp[i + 1][j - 1] + dp[i][j]) % mod
    for j in range(k, a[i], -1):
        dp[i + 1][j] = (dp[i + 1][j] - dp[i + 1][j - a[i] - 1]) % mod
print((dp[n][-1]))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

f = [0] * (k + 1)
f[0] = 1
for i in range(n):
    for j in range(1, k+1):
        f[j] = (f[j] + f[j - 1]) % mod # 1/(1-x):累積和
    for j in range(k, a[i], -1): # 1-x^(a[i]+1) a[i]+1次したの項を引く
        f[j] = (f[j] - f[j - a[i] - 1]) % mod
print((f[-1]))
# Original solution
def main():
  e,M=enumerate,10**9+7
  n,k,*a=list(map(int,open(0).read().split()))
  dp=[[0]*(k+1)for _ in range(n+1)]
  dp[0][0]=1
  for b,d,a in zip(dp,dp[1:],a):
    a+=1
    t=0
    for j,v in e(b[:a]):
      t+=v
      t%=M
      d[j]=t
    for j,(w,v)in e(list(zip(b,b[a:])),a):
      t+=v-w
      t%=M
      d[j]=t
  print(t)
main()

# Optimized solution
def main():
  M=10**9+7
  n,k,*a=list(map(int,open(0).read().split()))
  dp=[[1]+[0]*k]
  for b,i in zip(dp,a):
    t,d=0,[]
    for v in b[:i+1]:
      t+=v
      t%=M
      d+=t,
    for w,v in zip(b,b[i+1:]):
      t+=v-w
      t%=M
      d+=t,
    dp+=d,
  print(t)
main()
# Original solution
from sys import stdin
from itertools import accumulate
input = stdin.readline

MOD = 10**9+7
N, K = list(map(int,input().split()))
a = list(map(int,input().split()))

p = list(accumulate(a))

dp = [0] * (K+1)
dp[0] = 1
prefix = [0]+[1]*(K+1)
for i in range(N):
    new_dp = [0]*(K+1)
    for j in range(min(p[i],K)+1):
        new_dp[j] = prefix[j+1] - prefix[max(0,j-a[i])]
    dp = new_dp[:]
    prefix = [i % MOD for i in [0]+list(accumulate(dp))]

print((dp[K]%MOD))


# Optimized solution
from sys import stdin
from itertools import accumulate
input = stdin.readline

MOD = 10**9+7
N, K = list(map(int,input().split()))
*a, = list(map(int,input().split()))

p = list(accumulate(a))

dp = [0] * (K+1)
dp[0] = 1
prefix = [0]+[1]*(K+1)
for i in range(N):
    new_dp = [0]*(K+1)
    for j in range(min(p[i],K)+1):
        new_dp[j] = prefix[j+1] - prefix[max(0,j-a[i])]
    dp = new_dp[:]
    prefix = [i % MOD for i in [0]+list(accumulate(dp))]

print((dp[K]%MOD))
# Original solution
def main():

    N, K = list(map(int, input().split()))
    a = list(map(int, input().split()))
    dp = [[0]*(K+1) for i in range(N+1)]
    import collections
    mod = 10**9 + 7
    # dp[i][j] i人目まで配って、残りがj個の方法の数
    s = [1]*(K+2)
    s[-1] = 0
    tmp = 0
    for i in range(1, N+1):
        next = [0]*(K+2)
        for j in range(K, -1, -1):
            tmp = s[j] - s[min(K, j+a[i-1]) + 1]
            tmp %= mod
            dp[i][j] = tmp
            next[j] = (next[j+1]+tmp) % mod
        s = list(next)

    print(tmp)


main()


# Optimized solution
def main():

    N, K = list(map(int, input().split()))
    a = list(map(int, input().split()))
    dp = [[0]*(K+1) for i in range(N+1)]
    import collections
    dp[0][K] = 1
    mod = 10**9 + 7
    # dp[i][j] i人目まで配って、残りがj個の方法の数
    s = [0]*(K+2)
    for i in range(1, N+1):
        tmp = 0
        for j in range(K, -1, -1):
            s[j] = (s[j+1] + dp[i-1][j]) % mod
        for j in range(K, -1, -1):
            tmp = s[j] - s[min(K, j+a[i-1]) + 1]
            tmp %= mod
            dp[i][j] = tmp

    print((dp[-1][0]))


main()
# Original solution
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    u = [1] + [0] * K
    v = [1] + [0] * K
    s = 0
    for i in A:
        u, v = v, u
        s += i
        t = 0
        for k in range(i):
            t += u[k]
            v[k] = t
        for k in range(i, min(s, K) + 1):
            t += u[k]
            v[k] = t
            t -= u[k - i]
    print((v[K] % (10 ** 9 + 7)))

main()


# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    u = [1] + [0] * K
    v = [1] + [0] * K
    s = 1
    for a in A:
        u, v = v, u
        s += a
        c = 0
        for i, t in enumerate(u[:a + 1]):
            c += t
            v[i] = c
        for i, t, tt in zip(list(range(a + 1, K + 1)), u[a + 1:s], u):
            c += t - tt
            v[i] = c
    print((v[K] % (10 ** 9 + 7)))

main()
# Original solution
import sys
readline = sys.stdin.readline

N,K = list(map(int, readline().split()))
A = list(map(int,readline().split()))
DIV = 10 ** 9 + 7

# dp[i][j] : i人目までにj個配るときの場合の数
# 数式変形で以下
# dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - Ai-1 - 1]

dp = [[0] * (K + 1) for i in range(N + 1)]
for i in range(N + 1):
  dp[i][0] = 1 # 0個配る方法は常に1通り
  
for i in range(1, N + 1):
  for j in range(1, K + 1):
    if j - A[i - 1] - 1 >= 0:
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - A[i - 1] - 1]) % DIV
    else:
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % DIV
      
print((dp[N][K]))

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

N,K = list(map(int, readline().split()))
A = list(map(int,readline().split()))
DIV = 10 ** 9 + 7

# dp[i][j] : i人目までにj個配るときの場合の数
# dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + ... + dp[i - 1][j - A[i - 1]]
# j に j - 1を代入
# dp[i][j - 1] = dp[i - 1][j - 1] + dp[i - 1][j - 2] + ... + dp[i - 1][j - A[i - 1] - 1]
# 上から下を引く
# dp[i][j] - dp[i][j - 1] = dp[i - 1][j] - dp[i - 1][j - A[i - 1] - 1]
# dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - Ai-1 - 1]

dp = [[0] * (K + 1) for i in range(N + 1)]
for i in range(N + 1):
  dp[i][0] = 1 # 0個配る方法は常に1通り
  
for i in range(1, N + 1):
  for j in range(1, K + 1):
    if j - A[i - 1] - 1 >= 0: # j個配れる場合 (j >= A[i - 1] + 1)
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - A[i - 1] - 1]) % DIV
    else:
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % DIV
      
print((dp[N][K]))
# Original solution
import sys
input = sys.stdin.readline

n,k=list(map(int, input().split())) 
a=list(map(int, input().split()))
mod=10**9+7
dp=[[0]*(k+1) for i in range(n+1)]

for i in range(n+1):
    dp[i][0]=1



for i in range(1,n+1):
    for j in range(1,k+1):
        if j-1-a[i-1]>=0:
            dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1-a[i-1]]
            dp[i][j]%mod
        
        else:
            dp[i][j]=dp[i][j-1]+dp[i-1][j]   
            dp[i][j]%mod

print((dp[-1][-1]%mod))
    
    

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

n,k=list(map(int, input().split())) 
a=list(map(int, input().split()))
mod=10**9+7
dp=[[0]*(k+1) for i in range(n+1)]

for i in range(n+1):
    dp[i][0]=1



for i in range(1,n+1):
    for j in range(1,k+1):
        if j-1-a[i-1]>=0:
            dp[i][j]=(dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1-a[i-1]])%mod
        
        else:
            dp[i][j]=(dp[i][j-1]+dp[i-1][j])%mod

print((dp[-1][-1]%mod))
# Original solution


import collections


def solve():

    A, B = list(map(int, input().split()))
    print((A*B-(A+B-1)))


if __name__ == "__main__":
    solve()


# Optimized solution
A, B = list(map(int, input().split()))

print(((A-1)*(B-1)))
# Original solution
a,b=list(map(int, input().split()))
print(((a-1)*(b-1)))

# Optimized solution
a,b=list(map(int,input().split()))
print((~-a*~-b))
# Original solution
import subprocess
A,B = list(map(int, subprocess.check_output("dd").split()))
print((A-1)*(B-1))


# Optimized solution
import sys
A,B = list(map(int, sys.stdin.read().split()))
print((A-1)*(B-1))
# Original solution
a,b=list(map(int,input().split()))
print((~-a*~-b))

# Optimized solution
A, B = list(map(int, input().split()))
print((A*B-(A+B-1)))
# Original solution
import sys

# 関数 solve は，もちろん，問題に応じて書き換える
def solve(a, b):
    s=int(a)-1
    r=int(b)-1
    return s*r

# ここから下は，入力・出力形式が同じであれば，変えなくて良い．
def readQuestion():
    line = sys.stdin.readline().rstrip()
    [str_a, str_b] = line.split(' ')
    return (int(str_a), int(str_b))

def main():
    a, b = readQuestion()
    answer = solve(a, b)
    print(answer)
    
if __name__ == '__main__':
    main()

# Optimized solution
import sys

def solve(a, b):
    # Uncomment the following if you want to check the input.
    # print('a =', a)
    # print('b =', b)
    s=int(a)-1
    r=int(b)-1
    return s*r


def readQuestion():
    ws = sys.stdin.readline().strip().split()
    a = int(ws[0])
    b = int(ws[1])
    return (a, b)

def main():
    print((solve(*readQuestion())))

# Uncomment before submission
main()
# Original solution
a,b=list(map(int,input().split()))
c=(a*b)-(b+a-1)
print(c)

# Optimized solution
a,b=list(map(int,input().split()))
print((a*b-a-b+1))
# Original solution
eval('print(('+input().replace(' ','-1)*(')+'-1))')

# Optimized solution
a,b=list(map(int,input().split()));print((~-a*~-b))
# Original solution
print((eval('~-'+'*~-'.join(input().split()))))

# Optimized solution
a,b=list(map(int,input().split()))
print((~-a*~-b))
# Original solution
A,B=list(map(int,input().split()))
print(((A-1) * (B-1)))

# Optimized solution
print((eval('('+input().replace(' ','-1)*(')+'-1)')))
# Original solution
a, b = list(map(int, input().split()))
print(((a-1) * (b-1)))

# Optimized solution
a,b=list(map(int,input().split()));print((a*b-a-b+1))
# Original solution
a,b=list(map(int,input().split()))
print((a*b-a-b+1))

# Optimized solution
A,B=list(map(int,input().split()))
print((A*B-A-B+1))
# Original solution
a, b = list(map(int, input().split()))
print(((a - 1) * (b - 1)))

# Optimized solution
A, B = list(map(int, input().split()))
print(((A - 1) * (B - 1)))
# Original solution
import sys
sys.setrecursionlimit(100000)
S = input()
T = ""
t = ['dream','dreamer','erase','eraser']
def rec(T,S):
    if len(S) < len(T):
        return False
    if S==T:
        return True
    elif S[len(T)] == "e":
        return rec(T+t[2],S) or rec(T+t[3],S)
    elif S[len(T)] == "d":
        return rec(T+t[0],S) or rec(T+t[1],S)
    else:
        return False
if rec(T,S):
    print("YES")
else:
    print("NO")


# Optimized solution
S = input().split()[0]
t = ['dreameraser','dreamerase','dreamer','dream','eraser','erase']
while len(S) > 0:
    for i in t:
        if S[-len(i):]==i:
            S = S[:-len(i)]
            break
        elif i == t[-1]:
            print("NO")
            exit()
print("YES")
# Original solution
N=int(input())

A=list(map(int,input().split()))
B=[]

for i in range(N):
    if(N%2==0):
        if(i%2==0):
            B.append(A[i])
        else:
            B.insert(0,A[i])
    else:
        if(i%2==0):
            B.insert(0,A[i])
        else:
            B.append(A[i])

for i in range(N):
    print(B[i] if i==0 else " "+str(B[i]),end="")
print()


# Optimized solution
N=int(input())

A=list(map(int,input().split()))
B=[]
C=[]

for i in range(N):
    if(N%2==0):
        if(i%2==0):
            B.append(A[i])
        else:
            C.append(A[i])
    else:
        if(i%2==0):
            C.append(A[i])
        else:
            B.append(A[i])

C.reverse()
for i in range(len(B)):
    C.append(B[i])

for i in range(N):
    print(C[i] if i==0 else " "+str(C[i]),end="")
print()
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

ans = [A[0]]
for i in range(1, N):
    if i % 2 == 0:
        ans.append(A[i])
    else:
        ans.insert(0, A[i])

if N % 2 == 1:
    ans = ans[::-1]

print((" ".join(list(map(str, ans)))))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

ans = []
if N % 2 == 0:
    l1 = A[0::2]
    l2 = A[1::2]
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = A[0::2]
    l2 = A[1::2]
    l1 = l1[::-1]
    ans = l1+l2

print((" ".join(list(map(str, ans)))))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

ans = []
if N % 2 == 0:
    l1 = A[0::2]
    l2 = A[1::2]
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = A[0::2]
    l2 = A[1::2]
    l1 = l1[::-1]
    ans = l1+l2

print((" ".join(list(map(str, ans)))))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

ans = []

l1 = A[0::2]
l2 = A[1::2]
if N % 2 == 0:
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = l1[::-1]
    ans = l1+l2

print((" ".join(list(map(str, ans)))))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

ans = []

l1 = A[0::2]
l2 = A[1::2]
if N % 2 == 0:
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = l1[::-1]
    ans = l1+l2

print((" ".join(list(map(str, ans)))))


# Optimized solution
N = int(eval(input()))
A = list(input().split())

ans = []

l1 = A[0::2]
l2 = A[1::2]
if N % 2 == 0:
    l2 = l2[::-1]
    ans = l2+l1
else:
    l1 = l1[::-1]
    ans = l1+l2

print((" ".join(ans)))
# Original solution
N = int(eval(input()))
a = list(map(int,input().split()))
ans = [a[0]]
for i,j in enumerate(a[1:]):
    if(N%2==0):
        if(i%2==0):
            ans.insert(0,j)
        else:
            ans.append(j)
    else:
        if(i%2==1):
            ans.insert(0,j)
        else:
            ans.append(j)
print((" ".join(map(str,ans))))

# Optimized solution
N = int(eval(input()))
a = list(map(int,input().split()))

ans = [a[0]]
check = N%2==0
for i,j in enumerate(a[1:]):
    if(check):
        if(i%2==0):
            ans.insert(0,j)
        else:
            ans.append(j)
    else:
        if(i%2==1):
            ans.insert(0,j)
        else:
            ans.append(j)
print((" ".join(map(str,ans))))
# Original solution
import sys
import collections
import math

n = int(eval(input()))
A = [int(x) for x in input().split()]

R = [0] * n

idx = 0
for i in range(n-1, -1, -2):
    R[idx] = A[i]
    idx += 1

for i in range(n % 2, n, +2):
    R[idx] = A[i]
    idx += 1

print((" ".join([str(i) for i in R])))

sys.exit(0)


# Optimized solution

import sys
import collections
import math
from collections import Counter
from collections import deque

n = int(eval(input()))
A = [int(x) for x in input().split()]

Q = deque()

f = True
if n % 2 == 0:
    f = not f

for a in A:
    if f:
        Q.appendleft(a)
    else:
        Q.append(a)
    f = not f

print((" ".join([str(q) for q in Q])))

sys.exit(0)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))

even = []
odd = []
for i in range(0,n):
	if i % 2 == 0:
		odd.append(a[i])
	else:
		even.append(a[i])

res = []
if n % 2 == 0:
	even = reversed(even)
	res.extend(even)
	res.extend(odd)
else:
	odd = reversed(odd)
	res.extend(odd)
	res.extend(even)

print((" ".join(map(str,res))))

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))

if n == 1:
	print((a[0]))
elif n % 2 == 0:
	print((" ".join(map(str, a[1::2][::-1] + a[0::2]))))
else:
	print((" ".join(map(str, a[0::2][::-1] + a[1::2]))))
# Original solution
n = int(eval(input()))
*a, = list(map(int, input().split()))

b = []
for i in range(n):
    b.append(a[i])
    b.reverse()

print((" ".join(map(str, b))))


# Optimized solution
n = int(eval(input()))
*a, = list(map(int, input().split()))

b = list(reversed(a[1::2]))+a[0::2]
print((" ".join(map(str, b if n%2 == 0 else reversed(b)))))
# Original solution
N=input()
b=[]
P=input().split()
for i in P:
    b.append(i)
    b=b[::-1]
print(" ".join(b))

# Optimized solution
N=int(input())
P=input().split()
b=P[-1::-2]+P[N%2::2]
print(" ".join(b))
# Original solution
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
if a % 2 == 0:
    for i in range(len(ar)-1,0,-2):
        br.append(ar[i])
    for i in range(0,len(ar)-1,+2):
        br.append(ar[i])
else:
    for i in range(len(ar)-1,-1,-2):
        br.append(ar[i])
    for i in range(1,len(ar)-1,+2):
        br.append(ar[i])
for i in range(a-1):
    print(str(br[i]),end = " ")
print(str(br[a-1]))


# Optimized solution
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
for i in range(a-1,-1,-2):
    br.append(ar[i])
if a % 2 == 0:
    n = 0
else:
    n = 1
for i in range(n,a-1,2):
    br.append(ar[i])
for i in range(a-1):
    print(br[i],end=" ")
print(br[a-1])
# Original solution
n = int(input())
a = input().split()

b = []
for i in range(n):
  if i % 2:
    b.append(a[i])
  else:
    b.insert(0, a[i])

if n % 2:
  print(" ".join(b))
else:
  print(" ".join(b[::-1]))


# Optimized solution
n = int(input())
a = input().split()

b = []
c = []
for i in range(n):
  if i % 2:
    b.append(a[i])
  else:
    # b.insert(0, a[i])
    c.append(a[i])

b = c[::-1] + b

if n % 2:
  print(" ".join(b))
else:
  print(" ".join(b[::-1]))
# Original solution
from itertools import count
def get_weed(h):
    d10 = h[1] - h[0]
    d21 = h[2] - h[1]
    d32 = h[3] - h[2]
    if d10 == d21 == d32:
        for hi, expect in zip(h, count(h[0], d10)):
            if hi != expect:
                return hi

    d43 = h[4] - h[3]

    if d21 == d32 == d43:
        return h[0]
    elif h[2] - h[0] == d32 == d43:
        return h[1]
    elif d10 == h[3] - h[1] == d43:
        return h[2]
    elif d10 == d21 == h[4] - h[2]:
        return h[3]

import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    h = list(map(int, f.readline().split()))
    print((get_weed(h)))

# Optimized solution
from itertools import count
def get_weed(h):
    d = [h[i + 1] - h[i] for i in range(3)]
    if d[0] == d[1] == d[2]:
        for hi, expect in zip(h, count(h[0], d[0])):
            if hi != expect:
                return hi

    for i in range(4):
        hh = h[:i] + h[i + 1:]
        d = [hh[i + 1] - hh[i] for i in range(3)]
        if d[0] == d[1] == d[2]:
            return h[i]

import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    h = list(map(int, f.readline().split()))
    print((get_weed(h)))
# Original solution
from collections import defaultdict
from heapq import *
from array import *

def read():
    return int(eval(input()))

def reads(sep=None):
    return list(map(int, input().split(sep)))

def mst_prim(adj, n):
    pred = defaultdict(lambda: -1)
    key = defaultdict(lambda: float('inf'))
    key[0] = 0
    pq = [(key[v], v) for v in range(n)]
    heapify(pq)
    in_pq = array('B', (True for _ in range(n)))
    while pq:
        _,u = heappop(pq)
        in_pq[u] = False
        for v, v_cost in adj[u]:
            if in_pq[v]:
                weight = v_cost
                if weight < key[v]:
                    pred[v] = u
                    key[v] = weight
                    heappush(pq, (weight, v))
                    in_pq[v] = True
    return (pred, key)

def main():
    w, h = reads()
    p = [read() for _ in range(w)]
    q = [read() for _ in range(h)]

    adj = defaultdict(set)
    for x in range(w+1):
        for y in range(h+1):
            if x < w:
                adj[x+y*(w+1)].add((x+1+y*(w+1), p[x]))
                adj[x+1+y*(w+1)].add((x+y*(w+1), p[x]))
            if y < h:
                adj[x+y*(w+1)].add((x+(y+1)*(w+1), q[y]))
                adj[x+(y+1)*(w+1)].add((x+y*(w+1), q[y]))
    print((sum(mst_prim(adj, (w+1)*(h+1))[1].values())))

main()


# Optimized solution
def read():
    return int(eval(input()))

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    w, h = reads()
    p = sorted([read() for _ in range(w)])[::-1]
    q = sorted([read() for _ in range(h)])[::-1]

    pi = w-1
    qi = h-1
    res = 0
    while 0<=pi and 0<=qi:
        if p[pi] < q[qi]:
            res += p[pi] * (h+1)
            pi -= 1
            w -= 1
        else:
            res += q[qi] * (w+1)
            qi -= 1
            h -= 1
    res += sum(p[:pi+1]) + sum(q[:qi+1])
    print(res)

main()
# Original solution
W, H = list(map(int, input().split()))
P = [(int(eval(input())), 0) for i in range(W)]
Q = [(int(eval(input())), 1) for i in range(H)]

E = sorted(P + Q)
used = [W + 1, H + 1]

ans = 0
for e, f in E:
    ans += e * used[1 - f]
    used[f] -= 1

print(ans)


# Optimized solution
from itertools import accumulate
from bisect import bisect_left
W, H = list(map(int, input().split()))
W_cost = sorted([int(eval(input())) for i in range(W)])
H_cost = [int(eval(input())) for i in range(H)]


# 横は全部辺を張り、縦は各区間1本
W_cost_sum = sum(W_cost)
ans = W_cost_sum * (H + 1) + sum(H_cost)


W_acc_cost = [0] + list(accumulate(W_cost))
for h in H_cost:
    hx = bisect_left(W_cost, h)
    ans -= (W_cost_sum - W_acc_cost[hx]) - ((W - hx) * h)

print(ans)
# Original solution
w, h = list(map(int, input().split()))
p = []
q = []
for i in range(w):
    p.append(int(input()))
for i in range(h):
    q.append(int(input()))

wl = w
hl = h

cost = 0

p.sort()
q.sort()

p.append(10 ** 8 + 1)
q.append(10 ** 8 + 1)

while(wl + hl > 0):
    if p[0] < q[0]:
        cost += (hl + 1) * p[0]
        p.pop(0)
        wl -= 1
    else:
        cost += (wl + 1) * q[0]
        q.pop(0)
        hl -= 1

print(cost)


# Optimized solution
w, h = list(map(int, input().split()))
p = []
q = []
for i in range(w):
    p.append(int(input()))
for i in range(h):
    q.append(int(input()))

wl = w
hl = h

cost = 0

p.sort()
q.sort()

p.append(10 ** 8 + 1)
q.append(10 ** 8 + 1)

pc = 0
qc = 0

while(wl + hl - pc - qc> 0):
    if p[pc] < q[qc]:
        cost += (hl - qc + 1) * p[pc]
        pc += 1
    else:
        cost += (wl - pc + 1) * q[qc]
        qc += 1

print(cost)