Problem:
 Score : 300 points    Problem Statement We will play a one-player game using a number line and N pieces.  First, we place each of these pieces at some integer coordinate.  Here, multiple pieces can be placed at the same coordinate.  Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:  Move : Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1 .  Note that the coordinates where we initially place the pieces are already regarded as visited.  Find the minimum number of moves required to achieve the objective.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  -10^5 \leq X_i \leq 10^5  X_1, X_2, ..., X_M are all different.         Input Input is given from Standard Input in the following format:  N  M  X_1  X_2  ...  X_M       Output Find the minimum number of moves required to achieve the objective.        Sample Input 1 2 5
10 12 1 2 14      Sample Output 1 5  The objective can be achieved in five moves as follows, and this is the minimum number of moves required.   Initially, put the two pieces at coordinates 1 and 10 .  Move the piece at coordinate 1 to 2 .  Move the piece at coordinate 10 to 11 .  Move the piece at coordinate 11 to 12 .  Move the piece at coordinate 12 to 13 .  Move the piece at coordinate 13 to 14 .        Sample Input 2 3 7
-10 -3 0 9 -100 2 17      Sample Output 2 19       Sample Input 3 100 1
-100000      Sample Output 3 0

Solution (Python):
import sys

N,M = (int(x) for x in input().split())
if N >= M:
  print("0")
  sys.exit()
  
cnt = 0
  
x = list(map(int,input().split()))
x.sort()

cost = {}
for i in range(len(x)-1):
  cost[i] = x[i+1] - x[i]
num = -1
cost = sorted(cost.items(), key=lambda x:x[1],reverse=True)
for i in range(N-1):
  a = cost[i][0]
  cnt += x[a] - x[num+1]
  num = a
cnt += x[len(x)-1] - x[num+1]
print(cnt)

Solution (Python):
N,M = map(int,(input().split()))
X = list(map(int,(input().split())))
if N >= M:
    print(0)
else:
    X.sort()
    gap = []
    for i in range(1,M):
        gap.append(X[i]-X[i-1])
    print(sum(sorted(gap)[0:M-N]))

Solution (Python):
N,M = map(int,(input().split()))
X = list(map(int,(input().split())))
if N >= M:
    print(0)
else:
    X.sort()
    gap = []
    for i in range(1,M):
        gap.append(X[i]-X[i-1])
    s = X[M-1] - X[0]
    gap.sort(reverse=True)
    print(s-sum(gap[0:N-1]))

Solution (Python):
N,M = map(int,(input().split()))
X = list(map(int,(input().split())))
if N >= M:
    print(0)
else:
    X.sort()
    gap = []
    for i in range(1,M):
        gap.append(X[i]-X[i-1])
    s = X[M-1] - X[0]
    gap.sort(reverse=True)
    d = 0
    for i in range(N-1):
        d += gap[i]
    print(s-d)

Solution (Python):
def chunk_list(index_list, data):
  result = []
  start = 0
  for i in index_list:
    result.append(data[start:i+1])
    start = i+2
    print(start)
  result.append(data[start:len(data)])
  return result
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce

def main():
  n, m = input_list()
  if n >= m:
    print(0)
    exit(0)
  x = list(set(input_list()))
  sx = list(sorted(x))
  diffs = []
  for i, xv in enumerate(range(len(x)-1)):
    diff = abs(sx[i+1]-sx[i])
    diffs.append(diff)
  sd = list(sorted(diffs, reverse=True))

  for i in range(n-1):
    sd[i] = 0
    if i == m-1:
      break

  ans = 0
  for a in sd:
    ans += a
  print(ans)
main()

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
Y=[]
for i in range(1,M):
  Y.append(X[i]-X[i-1])

Y.sort()
ikanai = sum(Y[M-1-(N-1):M-1])



if N>=M:
  print(0)
else:
  print(X[-1]-X[0]-ikanai)

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))

if n >= m:
    print(0)
    exit()

diff = sorted([x[i+1] - x[i] for i in range(m-1)], reverse=True)
print(sum(diff[n-1:]))

Solution (Python):
n, m = map(int, input().split())
list_X = list(map(int, input().split()))

list_X.sort()
list_Y = []

for i in range(m-1):
    list_Y.append(list_X[i+1]-list_X[i])

list_Y.sort()

if n >= m:
    print(0)
else:
    print(sum(list_Y[:m-n]))

Solution (Python):
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))

if N >=M:
  print(0)
else:
  diff = sorted([ X[i+1] - X[i] for i in range(M-1) ] ,reverse = True)
  print(sum(diff[N-1:]))

Solution (Python):
import sys
N,M = map(int,input().split())
X = [i for i in map(int,input().split())]
X.sort()
if M == 1 or N>=M:
  print(0)
  sys.exit(0)
res = max(X)-min(X)
Length = []
for i in range(len(X)-1):
  Length.append(X[i+1]-X[i])
Length.sort()
for i in range(N-1):
  res -= Length[len(Length)-1-i]
print(res)

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    X_div = [0] * (M-1)
    for i in range(1,M):
        X_div[i-1] = X[i] - X[i-1]
    X_div.sort(reverse=True)
    m = 0
    for i in range(N-1):
        m += X_div[i]
    print(X[-1]-X[0]-m)

Solution (Python):
def main():
    n, m = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    if n >= m:
        print(0)
        exit()

    X.sort()
    Xd = []
    prev = X[0]
    for i in range(1, m):
        Xd.append(X[i] - prev)
        prev = X[i]

    Xd.sort()
    for i in range(n - 1):
        Xd.pop()

    print(sum(Xd))

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n, m = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    if n >= m:
        print(0)
        exit()

    X.sort()
    Xd = []
    prev = X[0]
    for i in range(1, m):
        Xd.append(X[i] - prev)
        prev = X[i]

    d = 0
    Xd.sort()
    for i in range(n - 1):
        Xd.pop()

    print(sum(Xd))

if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if n >= len(x):
    print(0)
    exit()

ls = []
x.sort()

for i in range(m-1):
    diff = x[i+1] - x[i]
    ls.append(diff)

ls.sort(reverse=True)
ans = sum(ls)

for i in range(n-1):
    ans -= ls[i]

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if n >= len(x):
    print(0)
    exit()

ls = []
x.sort()

for i in range(m-1):
    diff = x[i+1] - x[i]
    ls.append(diff)

ls.sort(reverse=True)
ans = sum(ls)

for i in range(n-1):
    ans -= ls[i]

print(ans)

Solution (Python):
import numpy as np
n,m=map(int,input().split())
x=list(map(int,input().split()))
if n>=m:
    print(0)
else:
    x.sort()
    x=np.array(x)
    L=np.diff(x,1)
    L=np.sort(L)[::-1]
    ans=x[-1]-x[0]-sum(L[:n-1])
    print(ans)

Solution (Python):

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


if n>=m:
    ans=0
else:
    xl.sort()
    bet=[]
    for i in range(1,m):
        bet.append(xl[i]-xl[i-1])
    bet.sort(reverse=True)
    ans=xl[m-1]-xl[0]-(sum(bet[0:n-1]))
print(ans)

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
x_dis=[0 for _ in range(m-1)]
for i in range(m-1):
    x_dis[i]=x[i+1]-x[i]
x_dis.sort(reverse=True)
ans=0
for j in range(m-1):
    if j>=n-1:
        ans+=x_dis[j]
if n>=m:
    ans=0
print(ans)

Solution (Python):
import sys
import heapq
inf = 10**6
n, m = map(int, input().split())

if n >= m:
  print(0)
  sys.exit()

a = [int(x) for x in input().split()]
a.sort()

count = []
heapq.heapify(count)
for i in range(m-1):
  heapq.heappush(count, -a[i+1]+a[i])

ans = 0
for j in range(n-1):
  ans -= heapq.heappop(count)
ans = max(a)-min(a)-ans
print(ans)

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))

X=sorted(X)
between=[0]*(M-1)
answer=0

for i in range(1,M):
    between[i-1]=X[i]-X[i-1]
bet=sorted(between)
for i in range(M-N):
    answer+=bet[i]

print(answer)

Solution (Python):
def main():
    n, m = map(int, input().split())
    num_list = list(map(int, input().split()))

    if m <= n:
        print(0)
        exit()

    num_list.sort()

    dist_list = []
    for i in range(1,m):
        dist_list.append(num_list[i]-num_list[i-1])

    dist_list.sort(reverse=True)


    ans = num_list[m-1]-num_list[0]
    for i in range(n-1):
        ans -= dist_list[i]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))


if n>= m:
	print(0)
else:
	x = sorted(x)
	x_interval = [x[i+1]-x[i] for i in range(m-1)]
	x_interval = sorted(x_interval)
	print(sum(x_interval[0:m-n]))

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_interval = []
for i in range(m-1):
    x_interval.append(x[i+1] - x[i])
x_interval.sort()
if m >= n:
    print(sum(x_interval[0:m-n]))
else:
    print(0)

Solution (Python):

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

x = list(map(int, input().split()))
if n >= m:
    print(0)
    exit()
x.sort()
dis = [0] * (m - 1)
for i in range(m - 1):
    dis[i] = x[i + 1] - x[i]

dis.sort()
ans = x[m - 1] - x[0]
for i in range(len(dis) - 1, len(dis)- n, -1):
    ans -= dis[i]

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

ans = [abs(x[i+1] - x[i]) for i in range(m-1)]
ans.sort()

print(sum([ans[i] for i in range(m-n)]))

Solution (Python):
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
B =  sorted(A)
C = list()
total = 0
if N >= M:
    total = 0
else:
    for i in range(M-1):
        C.append(B[i+1]-B[i])
    D = sorted(C)
    E = D[0:(M-N)]
    total = sum(E)
print(total)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()

ans = [abs(x[i+1] - x[i]) for i in range(m-1)]
ans.sort()

print(sum([ans[i] for i in range(m-n)]))

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))

d = []
for i in range(m-1):
    d.append(x[i+1] - x[i])
d.sort(reverse=True)

if n >= m:
    print(0)
else:
    print(sum(d[n-1:]))

Solution (Python):




def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))

    X.sort()
    result = []
    for i in range(1, M):
        result.append(X[i] - X[i - 1])
    result.sort()

    if N >= M:
        print(0)
    else:
        print(sum(result[:M - N]))


if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
ans = [abs(x[i]-x[i+1]) for i in range(m-1)]
ans.sort(reverse=True)
print(sum(ans[n-1:]))

Solution (Python):


N,M = list(map(int, input().rstrip().split()))
X_list = list(map(int, input().rstrip().split()))


X_list.sort()

diff_list=[0]
for i in range(1,len(X_list)):
    diff = X_list[i] - X_list[i-1]
    diff_list.append(diff)

diff_list.sort()

if N > M:
    ans = 0
elif N==1:
    ans = sum(diff_list[1:])
else:
    ex_num = N-1
    ans = sum(diff_list[1:]) - sum( diff_list[-ex_num:] )

print(ans)

Solution (Python):
koma,spot=map(int,input().split())
if koma>=spot:print(0);exit()
l=list(map(int,input().split()))
l.sort()
k=[l[i+1]-l[i]for i in range(spot-1)]
k.sort()
print(sum(k[:spot-koma]))

Solution (Python):
N, M = map(int, input().split())


X = sorted(list(map(int, input().split())))











print(sum(sorted([X[i + 1] - X[i] for i in range(M - 1)])[: max(M - N, 0)]))

Solution (Python):


def submit():
    n, m = map(int, input().split())
    xlist = list(map(int, input().split()))

    if n >= m:
        print(0)
    else:
        xlist.sort()
        dlist = [xlist[i + 1] - xlist[i] for i in range(m - 1)]
        dlist.sort()

        if n == 1:
            print(sum(dlist))
        else:
            print(sum(dlist[:1 - n]))


if __name__ == "__main__":
    submit()

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
mi=X[0]
ma=X[len(X)-1]
l=[]
c=0


for i in range(M-1):
    l.append(X[i+1]-X[i])
l.sort()
for i in range(len(l)-(N-1)):
    c=c+l[i]
print(c)

Solution (Python):
import numpy as np

n, m = [int(i) for i in input().split()]
x = np.array([int(i) for i in input().split()])

res = 0
if n < m:
    x.sort()
    d = np.diff(x)
    d = np.sort(d)[::-1]
    res = max(x) - min(x) - sum(d[:n-1])

print(res)

Solution (Python):

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

cuml=[]
if n>=m:
    print(0)
else:
    xl.sort()
    rng=xl[m-1]-xl[0]
    ans=rng
    for i in range(1,m):
        cuml.append(xl[i]-xl[i-1])
    cuml.sort()
    for i in range(n-1):
        ans-=cuml[m-2-i]
    print(ans)

Solution (Python):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
b = []
for i in range(m-1):
  b.append(a[i+1] - a[i])
b.sort(reverse=True)
del b[:n-1]
print(sum(b))

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()

if n >= m:
    print(0)
    exit()
sub = [0]*(m-1)
for i in range(m-1):
    sub[i] = x[i+1]-x[i]
sub.sort()

ass = 0
for i in range(m-n):
    ass += sub[i]
print(ass)

Solution (Python):
N,M=map(int, input().split())
point=list(map(int,input().split()))
point.sort()
d=[point[i+1]-point[i] for i in range(M-1)]
d.sort()
if N<M:
    print(sum(d[:M-N]))
else:
    print(0)

Solution (Python):
import numpy as np

def a():
  n, m = list(map(int,input().split()))
  x = list(map(int,input().split()))
  if(m <= n):
    return(0)
  x = np.array(sorted(x))
  d = x[1:] - x[:-1]
  d.sort()
  return((x[-1] - x[0]) - sum(d[m-n:]))
print(a())

Solution (Python):
N, M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]

X.sort()
gap = [X[i + 1] - X[i] for i in range(M - 1)]
gap.sort()



if M <= N:
    ans = 0
else:
    ans = sum(gap[0 : M - N])

print(ans)

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

def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if N >= M:
        print(0)
    else:
        X.sort()
        diff = [y-x for x,y in zip(X[0:M-1], X[1:])]
        diff.sort()
        ans = sum(diff[:M-N])
        print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if n >= m:
    print(0)
    exit(0)

x.sort()
x_dis = []

for i in range(m-1):
    x_dis.append(x[i+1]-x[i])

x_dis.sort(reverse=True)
num = 0

for i in range(n-1):
    num += x_dis[i]

print(x[m-1]-x[0]-num)

Solution (Python):
n, m = map(int, input().split())
x = [int(i) for i in input().split()]
if n >= m:
    print(0)
    exit()
x.sort()
dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
dis.sort()
for _ in range(n-1): dis.pop(-1)
print(sum(dis))

Solution (Python):
if __name__ == '__main__':
	N, M = map(int, input().split())
	Xlst = [int(a) for a in input().split()]
	Xlst.sort()
	lst = []
	for i in range(M-1):
		lst.append(Xlst[i+1] - Xlst[i])
	lst.sort()
	if N >= M:
		print(0)
	else:
		print(sum(lst[0:M-N]))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N >= M:
    print(0)
    exit()

X.sort()
L = []
for i in range(1,M):
    L.append(X[i]-X[i-1])
L.sort(reverse=True)

tmp = 0
for i in range(N-1):
    tmp += L[i]
print(X[-1]-X[0] - tmp)

Solution (Python):
N, M = map(int, input().split())
X = [int(x) for x in input().split()]

if N >= M :
    print(0)
    exit()

X.sort()
Y = [X[i+1]-X[i] for i in range(M-1)]
Y.sort()

if N == 1 :
    print(sum(Y))
else :
    print(sum(Y[:1-N]))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int,input().split()))

x.sort()
dis = [0] * (m - 1)

for i in range(m - 1):
  dis[i] = x[i + 1] - x[i]

dis.sort()


ans = sum(dis[:max(m-n, 0)])
print(ans)

Solution (Python):
n,m = [int(e) for e in input().split()]
a = sorted([int(e) for e in input().split()])
l=sum(sorted([y-x for x,y in zip(a,a[1:])],reverse=True)[:n-1])
print(max(a)-min(a)-l)

Solution (Python):
def mi():return map(int,input().split())
n,m=mi()
x=sorted(list(mi()))
d=sorted([x[i+1]-x[i] for i in range(m-1)])
print(sum(d[:max(m-n,0)]))

Solution (Python):
n,m=map(int,input().split())
xlist = list(map(int,input().split()))

xlist.sort()

diflist=[]


for i in range(m-1):
    diflist.append(xlist[i+1]-xlist[i])
diflist.sort()

if n >= m:
    print(0)
else:
    print(sum(diflist[:(m-n)]))

Solution (Python):
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if n>=m:
  print(0)
else:
  dif_l=[]
  for i in range(m-1):
    dif_l.append(l[i+1]-l[i])
  dif_l.sort()
  sum=0
  for j in range(m-n):
    sum+=dif_l[j]
  print(sum)

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
l=[x[i]-x[i-1] for i in range(1,m)]
l.sort(reverse=True)
ans=x[-1]-x[0]-sum(l[:n-1])
print(ans)

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()
diff_X = []
for i in range(M-1):
  tmp = X[i+1] - X[i]
  diff_X.append(tmp)

diff_X.sort(reverse=True)
ans = sum(diff_X[N-1:])
print(ans)

Solution (Python):
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())))[::-1]
sbn = sorted([abs(Xlist[i] - Xlist[i+1]) for i in range(M-1)])[::-1]
if N >= M:
    print(0)
else:
    print(sum(sbn[N-1:]))

Solution (Python):
N,M=map(int,input().split());X=sorted([int(i) for i in input().split()]);print(sum(sorted([X[i+1]-X[i] for i in range(M-1)],reverse=True)[N-1:M-1]))

Solution (Python):
n, m, *spot = map(int, open(0).read().split())
spot.sort()
dist = [spot[i+1]-spot[i] for i in range(m-1)]
dist.sort(reverse = True)
print(sum(dist[n-1:]) if dist else 0)

Solution (Python):









import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int,ipti().split()))
    x = list(map(int,ipti().split()))
    x.sort()

    dist = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dist.sort()

    if m <= n:
        print(0)
    else:
        print(sum(dist[:m-n]))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = map(int, sys.stdin.readline().split())
    lines = list(map(int, sys.stdin.readline().split()))
    lines.sort()

    cost = {}
    for i in range(len(lines) - 1):
        tmp = lines[i + 1] - lines[i]
        cost[i] = tmp

    sortedCost = sorted(cost.items(), key=lambda x: x[1], reverse=True)
    ret = 0
    for i, c in sortedCost[N - 1:]:
        ret += c

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
N, M = map(int, input().split(' '))
X =[int(i) for i in input().split(' ')]
if M > N:
    X.sort()
    X_n = X.copy()

    X_n.insert(0, X[0])

    del X_n[-1]

    X_section  = [X[i] - X_n[i] for i in range(len(X))]

    X_section.sort(reverse = True)

    X_result = X_section[N-1:]

    result = sum(X_result)

    print(result)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
Y = []

X.sort()
for i in range(1, M):
    Y.append(X[i] - X[i-1])

Y.sort()
if N >= M:
    print(0)
else:
    print(sum(Y[:M-N]))

Solution (Python):
n,m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
d_l = []
for i in range(len(x)-1):
    dif=x[i+1] - x[i]
    d_l.append(dif)
d_l.sort()
if n==1:
    print(sum(d_l))
else:
    print(sum(d_l[:-(n-1)]))

Solution (Python):
n, m, *a = map(int, open(0).read().split());a.sort()
print(sum(sorted([abs(a[i+1]-a[i])for i in range(m-1)])[:max(0,m-n)]))

Solution (Python):
n,m = map(int, input().split())
x = list(map(int, input().split()))

if n>=m:
    print(0)
else:
    x.sort()

    diff = [[] for i in range(m-1)]
    for i in range(m-1):
        diff[i] = x[i+1]-x[i]
    diff.sort()

    for i in range(n-1):
        diff.pop(-1)

    print(sum(diff))

Solution (Python):


coma, prot = map(int, input().split())
nums = list(map(int, input().split()))


if coma >= prot:
    print(0)
    exit()

nums.sort()

intervals = []
for i in range(prot-1):
    intervals.append(nums[i+1] - nums[i])

intervals.sort()
li = len(intervals)
print(sum(intervals[:li-(coma-1)]))

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()

ans = 0
if M > N:
    dist = []
    for i in range(1,M):
        dist.append(abs(X[i]-X[i-1]))
    dist.sort()
    for i in range(M-N):
        ans += dist[i]

print(ans)

Solution (Python):
n, m = map(int, input().split())
x = sorted(map(int, input().split()))
delx = []
for i in range(m-1):
    delx.append(abs(x[i+1] - x[i]))
delx.sort()

if n >= m:
    print(0)
else:
    print(sum(delx[:m-n]))

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
y=[x[i]-x[i-1] for i in range(1,m)]
y.sort()
print(0 if n>=m else sum(y[:(m-n)]))

Solution (Python):
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
ans = 0
X.sort()
if N >= M:
  print(0)
  exit()
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(N-1):
  xx[j] = 0
ans = sum(xx)
print(ans)

Solution (Python):

import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,M = LMIIS()
    X = LMIIS()
    X.sort()
    place = [False]*M
    if N>=M:
        print(0)
        return
    
    diff = []
    pre = X[0]

    for i,x in enumerate(X):
        diff.append((x-pre,i))
        pre = x

    
    sorted_d = sorted(diff,reverse=True)
    
    
    N -= 1 
    place[0] = True
    num = 0
    for d,i in sorted_d:
        if num == N:
            break
        place[i] = True
        num += 1
    
    
    sum_l = 0
    for i,b in enumerate(place):
        if b:
            pass
        else:
            d,di = diff[i]
            
            sum_l += d
    print(sum_l)
        


if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
x = [int(i) for i in input().split()]
x.sort()
memo = []
for i in range(m-1):
    memo.append(x[i+1]-x[i])
memo.sort()
ans = 0
for i in range(m-n):
    ans += memo[i]
print(ans)

Solution (Python):
def c_streamline():
    N, M = [int(i) for i in input().split()]
    X = sorted([int(i) for i in input().split()])

    interval = sorted([X[i + 1] - X[i] for i in range(M - 1)], reverse=True)
    return sum(interval[N - 1:])  

print(c_streamline())

Solution (Python):
N, M = list(map(int,input().split()))
H = list(map(int,input().split()))
H = sorted(H,reverse = False) 

if N == 1:
    print(abs(H[M-1] - H[0]))
else:
    A = []
    for i in range(M-1):
        A.append(H[i+1] - H[i])
    A = sorted(A,reverse = False) 
    print(sum(A[:-N+1]))

Solution (Python):
n,m,*a=map(int,open(0).read().split())
a.sort()
b=[i-j for i,j in zip(a[1:],a)]
b.sort()
print(sum(b[:max(0,m-n)]))

Solution (Python):
n,m = map(int,input().split())

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

t.sort()
memo = []
for i in range(m-1):
    memo.append(t[i+1]-t[i])
memo.sort()

ans = 0
for i in range(m-n):
    
    ans += memo[i]
print(ans)

Solution (Python):
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
C = []
if m <= n:
	print(0)
	exit()
for i in range(1, m):
	C.append(abs(X[i] - X[i-1]))
C.sort()
for i in range(n-1):
	C[m-2-i] = 0
print(sum(C))

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
sub = []
for i in range(m - 1):
    sub.append(x[i+1] - x[i])
sub.sort()
ans = 0
for i in range(m - n):
    ans += sub[i]
print(ans)

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_sa = []
for i in range(len(x)-1):
    x_sa.append(abs(x[i]-x[i+1]))
x_sa.sort()
ans = 0
if n >= m:
    print(0)
else:
    for i in range(m-n):
        ans += x_sa[i]
    print(ans)

Solution (Python):
n,m = map(int,input().split())
x_lst = list(map(int,input().split()))
x_lst.sort()

if n>=m:
    answer = 0
else:
    remainder_lst = []
    for i in range(m-1):
        x1 = x_lst[i]
        x2 = x_lst[i+1]

        remainder = x2 - x1
        remainder_lst.append(remainder)

    remainder1_lst = sorted(remainder_lst, reverse=True)
    delete = 0

    for i in range(n-1):
        remainder1 = remainder1_lst[i]
        delete += remainder1
    answer =  sum(remainder_lst) - delete

print(answer)

Solution (Python):
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
if n>=m:
    print(0)
else:
    D = [0]*(m-1)
    for i in range(1, m):
        D[i-1] = abs(X[i] - X[i-1])
    D.sort()
    print(sum(D[:m-n]))

Solution (Python):
import heapq as h

N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
if N>=M:
    print(0)
else:
    data=[]
    h.heapify(data)
    for i in range(0,M-1):
        h.heappush(data,-X[i+1]+X[i])
    s=0
    for i in range(0,N-1):
        s+=(-1)*h.heappop(data)
    print(X[M-1]-X[0]-s)

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))

if n >= m:
    print(0)
else:
    diff = sorted([abs(x[i-1] - x[i]) for i in range(1, m)], reverse=True)
    print(sum(diff[n-1:]))

Solution (Python):
n,m=map(int,input().split())
if n>=m:
    print(0)

else:
    x=list(map(int,input().split()))
    x.sort()
    y=[]
    for i in range(m-1):
        y.append(x[i+1]-x[i])
    y.sort(reverse=True)
    ans=x[-1]-x[0]
    for i in range(n-1):
        ans-=y[i]
    print(ans)

Solution (Python):
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
if n >= m:
    print(0)
    exit(0)
s = []
for i in range(m-1):
    s.append(x[i+1]-x[i])
s.sort(reverse=True)
print(x[-1]-x[0]-sum(s[0:n-1]))

Solution (Python):
n, m, *X = map(int, open(0).read().split())
X.sort()
if n >= m:
  print(0)
else:
  Y = [0] * m
  for i in range(m-1):
      Y[i] = X[i+1] - X[i]
  Y.sort()
  print(sum(Y[:m-n+1]))

Solution (Python):
N , M = (map(int , input().split()))
X = list(map(int , input().split()))
sorted_x = sorted(X)
total_kukann = 0
kukann_kyori = []
a = N-1
for i in range(len(sorted_x)-1):
    kukann_kyori.append(sorted_x[i + 1] - sorted_x[i])
    total_kukann += sorted_x[i + 1] - sorted_x[i]
kukann_kyori.sort()
if N >= M:
    total_kukann = 0
else:
    for j in range(1,a+1):
        total_kukann -= kukann_kyori[-j]
print(total_kukann)

Solution (Python):
import sys
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))

if n >= len(x):
    print(0)
    sys.exit()
lis = []    
for i in range(m-1):
    lis.append(abs(x[i+1]-x[i]))
lis = sorted(lis,reverse = True)    
print(sum(lis) - sum(lis[:n-1]))

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
dists = [x[i+1] - x[i] for i in range(m-1)]
dists.sort()
if n>=m:
    print(0)
else:
    print(sum(dists[:m-n]))

Solution (Python):

N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
D=[X[i+1]-X[i] for i in range(M-1)]
D.sort()
print(sum(D[:M-N]) if N<M else 0)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
X = np.array(input().split(), dtype=np.int64)
X.sort()
print(sum(sorted(X[1:] - X[:-1], reverse=True)[(N - 1):]))

Solution (Python):
def main():
    n, m = map(int, input().split())
    *x, = map(int, input().split())
    if n >= m: return 0
    x = sorted(x)
    sec_x = [abs(x[i] - x[i+1]) for i in range(m-1)]
    sec_x = sorted(sec_x)
    ans = sum(sec_x[:(m-1 - (n-1))])
    return ans

print(main())

Solution (Python):
n,m=map(int,input().split())
X=sorted(list(map(int,input().split())))

A=[]
for i in range(m-1):
    A.append(abs(X[i]-X[i+1]))

A.sort()
ans=0
for i in range(m-n):
    ans +=A[i]
print(ans)

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split())) 
X.sort()

if N >= M:
  print(0)
else:
  m = M - N
  diff = []
  for xl, xr in zip(X, X[1:]):
    diff.append(xr - xl)
  
  diff.sort()
  print(sum(diff[:m]))

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))

if n >= m or m == 1:
    print(0)
else:
    move = sorted([x[i] - x[i - 1] for i in range(1, m)])

    ans = sum(move[:-n + 1]) if n > 1 else sum(move)
    print(ans)

Solution (Python):
n,m = map(int, input().split())

if n >= m:
    print(0)
    exit()

l = list(map(int, input().split()))
l.sort()
x =[]
for i in range(m-1):
    x.append(l[i+1]-l[i])
x.sort()
z = []
for j in range(len(x)-n+1):
    z.append(x[j])
print(sum(z))

Solution (Python):
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
if m==1:
	dx=[0]
else:
	dx=sorted([x[i+1]-x[i] for i in range(m-1)],reverse=True)
if n==1:
	print(x[-1]-x[0])
else:
	print(x[-1]-x[0]-sum(dx[0:n-1]))

Solution (Python):
import heapq

N, M = map(int, input().split())

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

a.sort()
if M!=1:
    d = max(a) - min(a)

    da = [-a[i]+a[i-1] for i in range(1,M)]

    heapq.heapify(da)

    for i in range(N-1):
        if not len(da):break
        d += heapq.heappop(da)
else:d = 0
print(d)

Solution (Python):
import numpy as np
N,M,*X=map(int,open(0).read().split())
X.sort()
a=np.array(X)
d=np.diff(a)
d.sort()
all=X[-1]-X[0]
left=d[::-1][:N-1].sum() 
print(all-left)

Solution (Python):
import numpy as np
N,M,*X=map(int,open(0).read().split())
X.sort()
a=np.array(X)
d=np.diff(a)
d.sort()
all=X[-1]-X[0]
left=d[-1:-N:-1].sum()
print(all-left)

Solution (Python):
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()

edges = [0] * (m-1)
for i in range(m-1):
    edges[i] = abs(x[i+1] - x[i])

edges.sort(reverse=True)
ans = sum(edges[n-1:])
print(ans)

Solution (Python):
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))

if n-m>=0: print(0)
else:
    l=[x[i+1]-x[i] for i in range(len(x)-1)] 
    ll=sorted(l, reverse=True)
    print(x[-1]-x[0]-sum(ll[0:n-1]))

Solution (Python):
n,m=map(int,input().split())
*a,=map(int,input().split())
a.sort()
d=[a[i+1]-a[i] for i in range(m-1)]
d=sorted(d,reverse=True)
if m<=n:
	print(0)
else:
	print(sum(d[(n-1):]))

Solution (Python):
N,M=map(int,input().split())
X=sorted(set(list(map(int,input().split()))))
s=[]
for i in range(M-1):
    s.append(X[i+1]-X[i])
s=sorted(s)[::-1]
print(sum(s[N-1:]))

Solution (Python):
N, M = map(int, input().split())
X = sorted([int(x) for x in input().split()])

D = [0] * M
for i in range(M-1):
    D[i] = X[i+1] - X[i]
D.sort(reverse=True)
D = D[N-1:]
print(sum(D))