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

# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(input())

def main():
    N, K = geta(int)
    h = geta(int)

    INF = 1 << 60
    dp = [INF for _ in range(N)]
    dp[0], dp[1] = 0, abs(h[0] - h[1])

    for i in range(2, N):
        lb = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp

    print(dp[N - 1])

if __name__ == "__main__":
    main()

### FIXED CODE ###

# dp[i]:  minimum possible total cost to reach Stone i.
# dp[1] = 0
# dp[2] = |h[2]-h[1]|
# dp[i+2] = min{ dp[i] + |h[i]-h[i+2]|,  dp[i+1] + |h[i+1]-h[i+2]| }
# dp[0] = INF, h[0] = INF

INF = 10**9

N, K = map(int, input().split())
h = [INF] + list(map(int, input().split()))

dp = [INF]*(N+1)
dp[1] = 0

for i in range(1, N+1):
    for j in range(1, K+1):
        if i+j > N:
            break

        tmp = dp[i] + abs(h[i] - h[i+j])

        if tmp < dp[i+j]:
            dp[i+j] = tmp

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

n = int(input())
As = list(map(int, input().split()))
ans = []
for i in range(n):
  ans.append(0)
for a in As:
  ans[a - 1] = As.index(a) + 1
print(*ans)

### FIXED CODE ###

n = int(input())
As = list(map(int, input().split()))
ans = [0] * n
for a, i in zip(As, range(1, n+1)):
  ans[a - 1] = i
print(*ans)
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
    c = [[] for _ in range(3)] #c = [[]] * 3 とすると全ての要素にappendされてしまうのでNG
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : # 'B'のとき
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])
    
    reigai = 0
    for i in c[0]:
        for j in c[1]:
            s = j - i # i < j と仮定して差を求める。逆の場合は負の値になるのでOK
            if j + s in c[2]:
                reigai += 1
            if i - s in c[2]:
                reigai += 1
            if (i + j) / 2 in c[2]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
 
    c = [[] for _ in range(3)] #c = [[]] * 3 とすると全ての要素にappendされてしまうのでNG
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : # 'B'のとき
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])

    reigai = 0 #先に条件2を満たすものを考えてから、条件1に当てはまるかを判定
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + (j - i)
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                reigai += 1
    print(total - reigai)


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

n,k,q = map(int,input().split())
a = [];
for i in range(q):
    a.append(int(input()))
for i in range(1,n+1):
    wp = a.count(i)
    if k-(q-wp) <= 0:
        print("No")
    else:
        print("Yes")

### FIXED CODE ###

n,k,q = map(int,input().split())
a = []; b = [0]*n
for i in range(q):
    c = int(input())
    a.append(c)
    b[c-1] += 1
for i in b:
    if k-(q-i) <= 0:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

N = int(input())
ABs = []
for i in range(N - 1):
    ABs.append(tuple(map(int, input().split())))

Cs = [0] * (N + 1)
for i in range(N - 1):
    Cs[ABs[i][0]] += 1
    Cs[ABs[i][1]] += 1
K = max(Cs)
print(K)

ABsc = [[False] * (K + 1) for _ in range(N + 1)]
Tr = [[] for _ in range(N + 1)]
for i in range(N - 1):
    p = min(ABs[i][0], ABs[i][1])
    c = max(ABs[i][0], ABs[i][1])
    Tr[p].append((c, i))
import collections

d = collections.deque()
d.append(1)


def minC(i):
    for j in range(1, K + 1):
        if not ABsc[i][j]:
            return j


Cis = [1] * N
while (len(d) > 0):
    p = d.popleft()
    for (c, i) in Tr[p]:
        d.append(c)
        col = minC(p)
        ABsc[p][col] = True
        ABsc[c][col] = True
        Cis[i] = col

for i in range(N - 1):
    print(Cis[i])

### FIXED CODE ###

N = int(input())
ABs = []
for i in range(N - 1):
    ABs.append(tuple(map(int, input().split())))

Cs = [0] * (N + 1)
Tr = [[] for _ in range(N + 1)]
for i in range(N - 1):
    Cs[ABs[i][0]] += 1
    Cs[ABs[i][1]] += 1
    p = min(ABs[i][0], ABs[i][1])
    c = max(ABs[i][0], ABs[i][1])
    Tr[p].append((c, i))
K = max(Cs)
print(K)

import collections

d = collections.deque()
d.append(1)

Pc = [0] * (N + 1)
Nc = [1] * (N + 1)
ABsc = [0] * (N + 1)
Cis = [1] * N
while (len(d) > 0):
    p = d.popleft()
    for (c, i) in Tr[p]:
        d.append(c)
        col = Nc[p]
        if Pc[p] == col:
            col += 1
        Nc[p] = col + 1
        Pc[c] = col
        Cis[i] = col

for i in range(N - 1):
    print(Cis[i])
### BUGGY CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
a = []
for i in p:
  a.append((i+1)/2)
ans = 0
for f in range(n - k + 1):
  ans = max([ans, sum(a[f:f+k])])
print(ans)  

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
a = [0] * n 
asum = 0
for i in range(n):
  a[i] = (p[i]+1)/2 + asum
  asum = a[i]
ans = a[k-1]
for f in range(1, n-k+1):
  ans = max([ans, a[f+k-1] - a[f-1]])
print(ans)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split(" ")))
# print(n,a)
for j in range(1,n+1):
	a1=[i for i in a if i == j]
	print(len(a1))

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split(" ")))
countlist=[0 for i in range(n)]
for j in range(n-1):
  countlist[a[j]-1]+=1
for i in range(n):
	print(countlist[i])
### BUGGY CODE ###

N = int(input())
Ali = list(map(int,input().split()))
L = Ali[:]

L.sort()
li = []
for i in range(N):
    li.append(Ali.index(L[i])+1)

print(' '.join(map(str, li)))


### FIXED CODE ###

from collections import defaultdict
d = defaultdict(int)
N = int(input())
A = list(map(int, input().split()))
for i, v in enumerate(A):
    d[i + 1] = v
ans = []
d_swap = {v: k for k, v in d.items()}
sd = sorted(d_swap.items())
for i, v in enumerate(sd):
    ans.append(v[1])
print(' '.join(map(str, ans)))
### BUGGY CODE ###

import scipy.misc as scm
import sys

N = int(sys.stdin.readline().strip())
a = list(map(lambda x: int(x), sys.stdin.readline().strip().split(" ")))

n = max(a)
max_m = -1
max_c = -1

for m in a:
    s = scm.comb(n, m)
    if m == n:
        continue
    if max_c < s:
        max_c = s
        max_m = m
print(str(n) + " " + str(max_m))

### FIXED CODE ###

import sys
import numpy as np

N = int(sys.stdin.readline().strip())
a = list(map(lambda x: int(x), sys.stdin.readline().strip().split(" ")))

index = np.argmax(a)
n = a.pop(index)

m = -1
tmp = n

for e in a:
    if abs((n/2) - e) < tmp:
        tmp = abs((n/2) - e)
        m = e
        
print(str(n) + " " + str(int(m)))
### BUGGY CODE ###

#!/usr/bin/python3
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)


K = int(input()) + 1
K_a = K
K_b = K
K_c = K
sum = 0

for x in range(1, K_a):
    for y in range(1, K_b):
        for z in range(1, K_c):
            sum += gcd(x, y, z)
print(sum)

### FIXED CODE ###

#!/usr/bin/python3
import itertools
import math
from functools import reduce


def gcd(a, b, c):
    return math.gcd(math.gcd(a, b), c)

K = int(input()) + 1
sum = 0

List = [ i for i in range(1, K)]

for v in itertools.combinations_with_replacement(List, 3):
    g = gcd(v[0],v[1],v[2])
    if v[0] == v[1] and v[1] == v[2]:
        sum += g
    elif v[0] == v[1] or v[0] == v[2] or v[1] == v[2]:
        sum += g * 3
    else:
        sum += g * 6

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

# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    res = []

    for i in range(N-1):
        for j in range(i+1,N):
            res.append(A[i]*A[j])

    res.sort()

    print(res[K-1])



main()


### FIXED CODE ###

import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def cnt(x,A,An,Ap,N,Nz):
    res = 0
    if x >= 0:
        res += N * Nz
    res += np.searchsorted(A,x//Ap,side='right').sum()
    res += N * len(An) - np.searchsorted(A,-(-x//An),side='left').sum()
    res -= np.count_nonzero(A*A<=x)
    return res//2


def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    A = np.array(A)
    A.sort()

    An = A[A<0]
    Az = A[A==0]
    Ap = A[A>0]

    Nz = np.count_nonzero(A==0)

    INF = 10 ** 18

    l = - INF - 1
    r = INF

    while r-l > 1:
        m = (l+r)//2
        if cnt(m,A,An,Ap,N,Nz) >= K:
            r = m
        else:
            l = m

    print(r)





main()
### BUGGY CODE ###

s = list(input())

count = 0
check_count = -1
while True:
    if count == check_count:
        break
    else:
        check_count = count
    for index in range(len(s) - 1):
        if s[index] == "B" and s[index + 1] == "W":
            s[index] = "W"
            s[index + 1] = "B"
            count += 1

print(count)

### FIXED CODE ###

s = list(input())

count = 0
w_cnt = 0

for index in range(len(s))[::-1]:
    if s[index] == "W":
        w_cnt += 1
    else:
        count += w_cnt

print(count)
### BUGGY CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,q = inpl()
g = [[]for i in range(n)]
c = [0 for i in range(n)]
seen = set()
for i in range(n-1):
    a,b = inpl()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    a,b = inpl()
    c[a-1] += b
def dfs(node):
    for v in g[node]:
        if v in seen:
            continue
        c[v] += c[node]
        seen.add(v)
        dfs(v) 

seen.add(0)
dfs(0)
for i in c:
    print(i)

### FIXED CODE ###

n,q = map(int,input().split())
ki = [0] * (n+1)
c = [0] * (n+1)
for i in range(n-1):
    a,b = map(int,input().split())
    ki[b] = a
for i in range(q):
    p,x = map(int,input().split())
    c[p] += x

for i in range(2,n+1):
    c[i] += c[ki[i]]
print(*c[1:])
### BUGGY CODE ###

from itertools import combinations
N = int(input())
A = list(map(int, input().split()))
_sum = 0
k = 10**9+7
com = combinations(A,2)
for AA in com:
    _sum += ((AA[0]%k)*(AA[1]%k))%k
    _sum %= k
print(_sum%k)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
mod = 1000000007
S = 0
_sum = 0
for i in range(N):
    S += A[i]
    S %= mod
for i in range(N):
    S -= A[i]
    if S<0:
        S += mod
    _sum += A[i]*S
    _sum %= mod

print(_sum)
### BUGGY CODE ###

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

dp = [0] * n

dp[0]=0
for i in range(1, n):
    dp[i] = min([dp[j]+abs(h[i]-h[j]) for j in range(max(i-k, 0), i)])

print(dp[n-1])

### FIXED CODE ###

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

dp = [0] * n

dp[0]=0
for i in range(1, n):
    dp[i] = min([dp[j]+abs(h[i]-h[j]) for j in range(max(i-k, 0), i)])

print(dp[n-1])
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
for i in range(1,N+1):
  print(A.count(i))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = [0]*N
for i in A:
  ans[i-1] += 1
for i in range(N):
  print(ans[i])
### BUGGY CODE ###

def is_prime(x):

    if x == 2 : return True

    for i in range(2, x):
        if x % i == 0:
            return False

    return True


n = int(input())
res = 0

for _ in range(n):
    x = int(input())
    if is_prime(x): res += 1

print(res)

### FIXED CODE ###

import math
def is_prime(n):
    if n in [2, 3, 5]:
        return True
    for i in range(2, int(math.sqrt(n))+1):
        if n % i == 0:
            return False
        
    return True

n = int(input())
nums = [int(input()) for i in range(n)]
res = len([i for i in nums if is_prime(i)])
print(res)
### BUGGY CODE ###

from collections import deque

N = int(input())

neighbor = [[0]*N for _ in range(N)]
edge = []

for _ in range(N-1):
    a, b = map(int, input().split())
    neighbor[a-1][b-1] = 1
    neighbor[b-1][a-1] = 1
    edge.append([a-1, b-1])

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    visited = [0]*N
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        visited[p] = 1
        extend_points = [idx for idx in range(len(neighbor[p])) if neighbor[p][idx] > 0 and visited[idx] == 0]
        for q in extend_points:
            shortest_distance_list[q] = shortest_distance_list[p]+1
        next_points.extend(extend_points)

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")


### FIXED CODE ###

from collections import deque

N = int(input())

neighbor = [[] for _ in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    neighbor[a-1].append(b-1)
    neighbor[b-1].append(a-1)

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        for idx in neighbor[p]:
            if shortest_distance_list[idx] == -1:
                shortest_distance_list[idx] = shortest_distance_list[p]+1
                next_points.append(idx)

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")
### BUGGY CODE ###

from functools import reduce

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

from functools import reduce

def gcd(a, b):
    if a<b:
      a,b = b,a
    while b != 0:
        a, b = b, a % b
    return a

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

G,L = A[0], 1
for i in range(N-1):
  G = gcd(G,A[i+1])
  L = L * A[i+1] // G
  #print(G,L)
  
print(G)

### FIXED CODE ###

from fractions import gcd

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

'''
def gcd(a, b):
    if a<b:
      a,b = b,a
    while b != 0:
        a, b = b, a % b
    return a
'''

G = A[0]
for i in range(N-1):
  G = gcd(G,A[i+1])
  
print(G)
### BUGGY CODE ###

n=int(input())
ans=0
for i in range(1,n+1):
  X=[]
  for j in range(1,i+1):
    if i%j==0:
      X.append(j)
  ans+=i*len(X)
print(ans)

### FIXED CODE ###

n=int(input())
ans=0
for i in range(1,n+1):
  ans+=(i+i*(n//i))*(n//i)//2
print(ans)
### BUGGY CODE ###

import functools
import operator
n = int(input())

a = list(map(int, input().split()))


pre = functools.reduce(operator.mul, a)
if pre <= 1000000000000000000:
    print(pre)
else:
    print(-1)


### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)

    if 0 in A:
        print('0')
        return
    prod=1
    for a in A:
        prod*=a
        if prod> 1000000000000000000:
            print('-1')
            return
    print(prod)

ans=main()
### BUGGY CODE ###

N, K = map(int, input().split())
h = [int(i) for i in input().split()]

INF = float('inf')
dp = [INF] * N
dp[0] = 0
dp[1] = abs(h[1] - h[0])

for i in range(2, N):
    tmp = INF
    step = max(0, i-K)
    for j in range(step, i):
        tmp = min(tmp, dp[j] + abs(h[i] - h[j]))
    dp[i] = tmp

print(dp[N-1])


### FIXED CODE ###

N, K = map(int, input().split())
h = [int(i) for i in input().split()]

INF = float('inf')
dp = [INF] * N
dp[0] = 0
dp[1] = abs(h[1] - h[0])

for i in range(2, N):
    tmp = INF
    step = max(0, i-K)
    for j in range(step, i):
        tmp = min(tmp, dp[j] + abs(h[i] - h[j]))
    dp[i] = tmp

print(dp[N-1])
### BUGGY CODE ###

n, k, q = map(int, input().split())
As = [int(input()) for i in range(q)]
points = [k for i in range(n)]
for i in range(q):
    solver = As[i] - 1
    for j in range(len(points)):
        if j == solver:
            continue
        else:
            points[j] -= 1
for i in range(len(points)):
    if points[i] <= 0:
        print("No")
    else:
        print("Yes")


### FIXED CODE ###

n, k, q = map(int, input().split())
As = [int(input()) for i in range(q)]
points = {i: k for i in range(n)}
accepted = [0] * n

for i in range(q):
    accepted[As[i]-1] += 1

for i in range(n):

    if k - q + accepted[i] <= 0:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

N = int(input())
S = input()

cnt = 0
for i in range(1000):
  c = str(i).zfill(3)
  f = 0
  for j in range(N):
    if S[j] == c[f]:
      f += 1
    if f == 3:
      break
  if f == 3:
    cnt += 1
print(cnt)


### FIXED CODE ###

N = int(input())
S = input()

cnt = 0
for i in range(1000):
  c = str(i).zfill(3)
  if c[0] in S:
        index1 = S.index(c[0])
        if c[1] in S[index1+1:]:
            index2 = index1 + 1 + S[index1+1:].index(c[1])
            if c[2] in S[index2+1:]:
                cnt+=1
print(cnt)
### BUGGY CODE ###

n,m = list(map(int, input().split()))
x = []
for i in range(m):
  x.append(list(map(str,input().split())))
  
qu = []
AC,WA = 0,0
for i in x:
  qu.append(i[0])

qu = list(set(qu))
for i in qu:
  x_target = [j for j in x if j[0] == i]
  
  #ACがあるかどうか、あった場合そのインデックス
  if [i,"AC"] in x:
    index = x_target.index([i,"AC"])
    WA += index
    AC += 1
  else:
    pass
  
print(AC, WA)

### FIXED CODE ###

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

ac = [0]*n
wa = [0]*(n+1)
pena = 0

for i in range(m):
  ques, state = input().split()
  ques = int(ques)-1
  
  if (state=="AC") and (ac[ques]==0):
    ac[ques] = 1
    pena += wa[ques]
  elif (state=="WA") and (ac[ques]==0):
    wa[ques] += 1
    
print(sum(ac), pena)
### BUGGY CODE ###

N,K=map(int,input().split())
A=list(map(int,input().split()))
s=[]
for j in range(K-1,N):
    score=1
    for i in range(j-K+1,j+1):
        score*=A[i]
    s.append(score)
    
for i in range(len(s)-1):
    if s[i]<s[i+1]:
        print("Yes")
    else:
        print("No")

### FIXED CODE ###

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N-K):
    if A[i]<A[i+K]:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

import sys
import bisect

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def cc(v, pp):
	if v == pp[-1]:
		return False, -1
	#print(pp)		
	for p in pp:
		if v % p == 0:
			return False, 0
		if v < 2*p:
			return True, 0
	return True, 0
			

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	al.sort()
	pp=[al[0]]
	n_same=0
	n_same_set=set()
	for v in al[1:]:
		tf, vv = cc(v, pp)
		if tf:
			pp.append(v)
		if vv == -1 and pp[-1] not in n_same_set:
			n_same_set.add(pp[-1])
			n_same += vv
	print(len(pp) + n_same)
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

### FIXED CODE ###

import sys
import collections

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	al = lin()
	al.sort()
	
	unique_al = list(set(al))
	unique_al.sort()
	
	al_counter = collections.Counter(al)
	ok=[v for v in al_counter.keys() if al_counter[v] == 1]
	d = {v:i for i,v in enumerate(ok)}
	max_v = 10**6+1
	for check in unique_al:
		for v in range(2*check, max_v, check):
			if v in d:
				ok[d[v]]=0
				
	return sum([v > 0 for v in ok ])
		
	
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
### BUGGY CODE ###

w, h = map(int, raw_input().split())
p = []
q = []
for i in range(w):
    p.append(int(raw_input()))
for i in range(h):
    q.append(int(raw_input()))

wl = w
hl = h

cost = 0

while(wl + hl > 0):
    if len(p) > 0:
        mp = min(p)
    else:
        mp = 10 ** 8 + 1
    if len(q) > 0:
        mq = min(q)
    else:
        mq = 10 ** 8 + 1
    if mp < mq:
        cost += (hl + 1) * mp
        p.pop(p.index(mp))
        wl -= 1
    else:
        cost += (wl + 1) * mq
        q.pop(q.index(mq))
        hl -= 1

print cost

        


### FIXED CODE ###

w, h = map(int, raw_input().split())
p = []
q = []
for i in xrange(w):
    p.append(int(raw_input()))
for i in xrange(h):
    q.append(int(raw_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
### BUGGY CODE ###

string = str(input())
n = 0

for _ in range(len(string)):

    try:
        i = string.index("W")
        n = n + i
        string = string[:i] + string[i+1:]
    
    except ValueError:
        break

print(n)


### FIXED CODE ###

string = str(input())

Wi = [i for i, x in enumerate(string) if x == 'W']
Wi = [x - i for i, x in enumerate(Wi)]

print(sum(Wi))