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):
N,M = map(int,input().split())
ls = list(map(int,input().split()))

if N >= M:
    print(0)

else:
    ls.sort()
    long1 = ls[-1]-ls[0]

    ls2 = []
    for i in range(1,M):
        ls2.append(ls[i]-ls[i-1])

    ls2.sort(reverse=True)
    des = 0
    for i in range(N-1):
        des += ls2.pop(0)

    print(long1-des)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, m, *x = map(int, read().split())
    if n >= m:
        print(0)
        sys.exit()
    x.sort()
    sa = []
    pre = x[-1]
    for xe in x:
        sa.append(abs(xe - pre))
        pre = xe
    sa.sort()
    num = m - n
    r = sum(sa[:num])
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)




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

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

X = list(map(int, readline().split()))
X.sort()

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

L.sort(reverse=True)

res = X[M-1] - X[0] - sum(L[:N-1])

print(res)

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

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

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 

 










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


X.sort()

ans = X[M-1] - X[0]
tmp = []

if N==1:
    print(ans)
    sys.exit()


for i in range(1,M):
    tmp2 = X[i] - X[i-1]
    if len(tmp) < N-1:
        tmp.append(tmp2)
    else:
        tmp3 = min(tmp)
        if tmp3 < tmp2:
            tmp.remove(tmp3)
            tmp.append(tmp2)

print(ans - sum(tmp))

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

x=sorted(x)
l=[0]*(m-1)
for i in range(m-1):
    l[i]=x[i+1]-x[i]
    
l=sorted(l)[::-1]
dist=x[m-1]-x[0]-sum(l[:n-1])
print(dist)

Solution (Python):
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
val = []
if n >= m:
    print(0)
    exit()

for i in range(m-1):
    p = x[i+1]-x[i]
    val.append(p)
val.sort()
print(sum(val[:m-n]))

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    diff = []
    for i in range(1, m):
        diff.append(x[i]-x[i-1])
    diff.sort(reverse=True)
    print(x[-1]-x[0]-sum(diff[:n-1]))
        
if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
X = tuple(map(int, input().split()))
X = sorted(X)
dx = []
for px, bx in zip(X[:m], X[1:]):
    dx.append(bx-px)
if n >= m:
    print(0)
else:
    dx = sorted(dx, reverse=True)
    print(sum(dx[n-1:]))

Solution (Python):
import sys
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
if M == 1:
    print(0)
    sys.exit()
X.sort()
_X = [X[i + 1] - X[i] for i in range(M - 1)]
_X.sort()
res = sum(_X)
for i in range(min(N - 1, M - 1)):
    res -= _X[M - 2 - i]
print(res)

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

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

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

for i in range(m-1):
  d[i] = x[i+1] - x[i]
d.sort(reverse=True)
mx = x[-1] - x[0]
r = n - 1
dsum = sum(d[:r])
print(mx - dsum)

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

a=sorted(a)
b=[]

for x in range(m-1):
  c=a[x+1]-a[x]
  b.append(c)
  

b=sorted(b)
if n>=m:
  print(0)
  
else:
  nn=m-n
  ans=0
  for u in range(nn):
    ans+=b[u]
    
  print(ans)

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

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

L = [0] * (M - 1)
for i in range(1, M):
    L[i - 1] = X[i] - X[i - 1]

L = sorted(L, reverse=True)

print(sum(L[N - 1:]))

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

x.sort()
dis = [0] * (m)
for i in range(1, m):
    dis[i] = abs(x[i] - x[i - 1])
dis.sort()

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

Solution (Python):
import sys
from operator import itemgetter

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def iif(n): return [int(input()) for _ in range(n)]
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def ss(): return input().split()

def main():
    mod = 1000000007
    
    N, M = mi()
    if N >= M:
        print(0)
        return

    X = lmi()
    X.sort()

    X_dis = []
    for i in range(len(X) - 1):
        X_dis.append(X[i+1] - X[i])
    X_dis.sort()

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

    return

main()

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


x.sort()
diff = []
f = x[0]
if m <= n:
    print(0)
else:

    for i in x[1:]:
        diff.append(i-f)
        f = i

    diff.sort()
    if n == 1:
        print(sum(diff))
    else:
        print(sum(diff[:-n+1]))

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

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

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

X = sorted(X)

anslis = []


if M <= N:
    print(0)
    sys.exit()


    
for i in range(M-1):
    ans = abs(X[i+1] - X[i])
    anslis.append(ans)
    ans = 0

anslis = sorted(anslis)

if N==1:
    print(sum(anslis))
else:
    print(sum(anslis[0:M-N]))

Solution (Python):
import sys
N,M = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
if N >= M:
    print(0)
    sys.exit()
X = sorted(X)
dis = [0 for x in range(M-1)]
for i in range(M-1):
    dis[i] = X[i+1] - X[i]
dis.sort()
dis.reverse()
for i in range(N-1):
    dis[i] = 0
print(sum(dis))

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

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

org_kyori =[]
for i in range(len(arr)-1):
    org_kyori.append(arr[i+1]-arr[i])

kyori =sorted(org_kyori)

ans = sum(kyori)
for i in range(N-1):
    ans -= kyori[-1*(i+1)]

print(ans)

Solution (Python):


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

x.sort()
length = []
if n >= m:
    print(0)
else:
    
    for i in range(m - 1):
        length.append(x[i + 1] - x[i])
    length.sort()
    tmp = sum(length[(m - n):])
    print(x[m-1]-x[0] - tmp)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial





def solve():
    n, m = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    a.sort()
    ans = a[-1] - a[0]
    b = []
    for i in range(m-1):
        b.append(a[i+1] - a[i])
    b.sort(reverse=True)
    for i in range(min(len(b), n-1)):
        ans -= b[i]
    print(ans)
        

t = 1

for case in range(1,t+1):
    ans = solve()


"""


"""

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

Solution (Python):





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

    if N >= M:
        return 0

    if N == 1:
        return X[-1] - X[0]

    ds = [None] * (M-1)
    for i in range(M-1):
        ds[i] = [X[i+1] - X[i], i]

    ds_sort = sorted(ds, key=lambda x: x[0], reverse=True)
    jump = ds_sort[:N-1]
    jump = sorted(jump, key=lambda x: x[1])

    ans = 0
    j = 0
    for i in range(M-1):
        if j < len(jump) and i == jump[j][1]:
            j += 1
            continue

        ans += ds[i][0]

    return ans


print(main())

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

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

nums = [0]*(m-1)
for i in range(m-1): nums[i] = x[i+1] - x[i]
nums = sorted(nums)
print(sum(nums[:m-1-(n-1)]))

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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

    N, M = il()
    X = sorted(il())

    cnt = []
    for n in range(M - 1):
        cnt.append(abs(X[n] - X[n + 1]))
    cnt = sorted(cnt, reverse=True)
    print(sum(cnt[N-1:]))


if __name__ == '__main__':
    main()

Solution (Python):
N,M = (int(X) for X in input().split())
if N>=M:
    print(0)
else:
    P   = sorted(int(X) for X in input().split())
    Mov = sorted([P[X+1]-P[X] for X in range(0,M-1)],reverse=True)
    print(sum(Mov[(N-1):]))

Solution (Python):
N,M = (int(X) for X in input().split())
if N>=M: print(0)
else:
    P   = sorted(int(X) for X in input().split())
    Mov = sorted(P[X+1]-P[X] for X in range(0,M-1))
    if N==1: print(sum(Mov))
    else: print(sum(Mov[:1-N]))

Solution (Python):


import sys

def main():
    input = sys.stdin.readline
    n,m = map(int, input().split())
    x_list = list(map(int, input().split()))
    if n >= m:
        print(0)
        exit(0)
    sabun_list = []
    x_list.sort()
    for i,x in  enumerate(x_list):
        if i == 0:
            continue
        sabun_list.append(x - x_list[i-1])
    result = 0
    sum_max_sabun = 0
    sabun_list.sort(reverse=True)
    sum_max_sabun = sum(sabun_list[:n-1])
    
    result = (x_list[-1] - x_list[0]) - sum_max_sabun

    print(result)

    return


if __name__ == '__main__':
    main()

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

Solution (Python):
N, M = map(int,input().split())
X = list(map(int,input().split()))
if len(X)<2:
    print(0)
else:    
    X.sort()

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

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

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

Solution (Python):
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n,m = readInts()
x = readInts()
x.sort()

if m==1:
  print(0)
  exit()

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

ans = x[-1]-x[0]
for i in range(min(n-1,len(intval))):
  ans-=intval[i]

print(ans)

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    N, M = [int(item) for item in input().split()]
    Xs = [int(item) for item in input().split()]
    if N < M:
        Xs.sort()
        ds = [Xs[i] - Xs[i-1] for i in range(1, M)]
        ds.sort()
        
        print(sum(ds[:M-N]))

    else:
        print(0)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    N, M = [int(item) for item in input().split()]
    Xs = [int(item) for item in input().split()]
    if N < M:
        Xs.sort()
        ds = [Xs[i] - Xs[i-1] for i in range(1, M)]
        ds.sort()
        
        print(sum(ds[:M-N]))

    else:
        print(0)


if __name__ == "__main__":
    resolve()

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

if M <= N:
  ans = 0
else:
  X.sort()
  li = []
  for i in range(M-1):
    li.append(X[i+1]-X[i])
  li.sort()
  li = li[:M-N]
  ans = sum(li)
  
print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]





N, M = mi()
X = sorted(li())

sa = []

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

sa = sorted(sa)

print(sum(sa[:max(M-N, 0)]))

Solution (Python):
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
if N >= M:
    ans = 0
    print(ans)
else:
    X.sort()
    diff = [X[i+1] - X[i] for i in range(M-1)]
    diff.sort()
    for j in range(M-N):
        ans += diff[j]
    print(ans)

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

sys.setrecursionlimit(10**6)
def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


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


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N, M = zz()
X = zz()
X = sorted(X)
if (M <= N):
    print(0)
    exit()
distance = [0]*(M-1)
for i in range(M-1):
    
    distance[i] = X[i+1] - X[i]
distance = sorted(distance, reverse=True)
distance = distance[N-1:]
print(sum(distance))

Solution (Python):
import sys
input = sys.stdin.readline

N,M = list(map(int,input().split()))
X = sorted(list(map(int,input().split())))
Xsub = []
for i in range(M-1):
    Xsub.append(X[i+1]-X[i])
    
Xsub = sorted(Xsub)

if N > 1:
    print(sum(Xsub[:-(N-1)] ))   
else:
    print(sum(Xsub))

Solution (Python):

def solve():
    if N >= M:
        return 0
    elif N == 1:
        cnt = abs(X[-1] - X[0])
        return cnt
    L = [0] * (M-1)
    
    for i in range(len(L)):
        L[i] = X[i+1] - X[i]
    L.sort()
    return sum(L[:M-N])



if __name__=="__main__":
    N,M = list(map(int, input().split()))
    X = list(map(int, input().split()))
    X.sort()
    print(solve())

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


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

    N, M = il()
    X = sorted(il())

    diff = []
    for n in range(M - 1):
        diff.append(abs(X[n] - X[n + 1]))
    diff = sorted(diff, reverse=True)
    ret = sum(diff[N-1:])
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline

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

lst=sorted(map(int,input().split()))
sa=[]
for i in range(m-1):
    sa.append(lst[i+1]-lst[i])
sa=sorted(sa)
print(sum(sa[:m-n]))

Solution (Python):
import math
import collections
import fractions
import itertools

def solve():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    x.sort()
    if n >= m:
        print(0)
    else:
        diff = [abs(x[i] - x[i+1]) for i in range(m-1)]
        diff.sort(reverse=True)
        ans = sum(diff)
        for i in range(n-1):
            ans -= diff[i]
        print(ans)
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import heapq
n,m = map(int, raw_input().split(' '))

xis = map(int, raw_input().split())
xis.sort()
distances = [v- u for u,v in zip(xis,xis[1:])]
distances.sort(key = lambda x:-x)
print sum(distances[n-1:] or [0])

Solution (Python):
import heapq
n,m = map(int, raw_input().split(' '))


if n >= m:
	print 0
else:
	xis = map(int, raw_input().split())
	xis.sort()


	distances = [v- u for u,v in zip(xis,xis[1:])]
	distances.sort(key = lambda x:-x)
	print sum(distances[n-1:] or [0])

Solution (Python):

import sys
input = sys.stdin.readline
N,M = (int(i) for i in input().split())
X = list(map(int, input().split()))
if N >=M:
    ans =0
else:
    X.sort()
    dif =[-1]*(M-1)
    for i in range(M-1):
        dif[i] =X[i+1] -X[i]
    dif.sort()
    ans =dif[:M-N]
    ans =sum(ans)
print(ans)

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

ruisekisa = [0]*(M-1)
for i in range(0,M-1,1):
    ruisekisa[i]=X[i+1]-X[i]

ruisekisa.sort()
print(sum(ruisekisa[0:max(M-N,0)]))

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

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

ruisekisa = [0]*(M-1)
for i in range(0,M-1,1):
    ruisekisa[i]=X[i+1]-X[i]

ruisekisa.sort()
print(sum(ruisekisa[0:M-N]))

Solution (Python):
import sys
N,M = map(int,input().split())
if N >= M:
    print(0)
    sys.exit()
    
lc = list(map(int,input().split()))
lc.sort()


diff = []
for i in range(M-1):
    diff.append(lc[i+1]-lc[i])
    

diff.sort()
diff_min = diff[:M-N]
print(sum(diff_min))

Solution (Python):
N,M=map(int,input().split())
R=[0]*(M-1)
L=list(map(int,input().split()))
L=sorted(L)
for i in range(M-1):
  R[i]=abs(L[i+1]-L[i])
c=max(L)-min(L)
R=sorted(R,reverse=True)
print(c-sum(R[:min(N-1,len(R))]))

Solution (Python):
n, m, *xx = map(int, open(0).read().split())

xx.sort()

diff = [a - b for a, b in zip(xx[1:], xx[:-1])]

diff.sort(reverse=True)

print(sum(diff[n-1:]))

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

Solution (Python):
import sys

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

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

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


def main():
    N, M = mi()
    X = list(mi())
    X.sort()
    if N >= M:
        print(0)
        return
    dif = [(X[i+1]-X[i], i) for i in range(M-1)]
    dif.sort(reverse=True)
    koma = [-1]+[dif[i][1] for i in range(N-1)]+[M-1]
    print(sum(X[koma[i+1]]-X[koma[i]+1] for i in range(N)))

if __name__ == '__main__':
    main()

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
x = sorted(LIST())
dx = [x[i+1]-x[i] for i in range(m-1)]
dx.sort(reverse=True)

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

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

Solution (Python):
n, m = map(int, input().split())
X = sorted(list(map(int, input().split())))
if n >= m : print(0)
else:
    d = sorted([i-j for i, j in zip(X[1:], X)])
    print(sum(d[:m-n]))

Solution (Python):
import numpy as np




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

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

Y = sorted(Y, key=lambda x: (x[1]))
Y.reverse()

I = list(map(lambda x: x[0], Y[0:min(N-1, len(Y))]))
I.sort()

Z = []
last = 0
for i in range(0,min(N-1, len(Y))):
    if last < I[i]:
        Z.append(X[last:I[i]])
        last = I[i]
Z.append(X[last:])
if len(Z) == 0:
    Z.append(X)

s = 0
for array in Z:
    if len(array) > 0:
        u = max(array)
        l = min(array)
        s += u - l

print(s)

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

if N >= M:
    print(0)
else:
    D = []
    for i in range(len(X)-1):
        D.append(abs(X[i]-X[i+1]))
    D = list(sorted(D))
    print(sum(D[:M-N]))

Solution (Python):
import itertools
n,m=map(int, input().split())
x=[int(i) for i in input().split()]
x.sort()
if(n>=m):
    print(0)
    exit()
dist = abs(x[-1]-x[0])

diff = []

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

diff.sort(key = lambda x:x[0],reverse = True)
for i in range(n-1):
    dist-=diff[i][0]
print(dist)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, M = in_nn()
    X = sorted(in_nl())

    X_diff = [0] * (M - 1)
    for i in range(M - 1):
        X_diff[i] = X[i + 1] - X[i]

    X_diff.sort(reverse=True)
    
    
    N = min(N, M)
    ans = X[-1] - X[0] - sum(X_diff[:N - 1])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
sabun = []
for i in range(1,m):
    sabun.append(x[i]-x[i-1])
sabun.sort(reverse=True)

print(sum(sabun[n-1:]))

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


for i in range(M - 1):
    diff.append(abs(X[i] - X[i + 1]))





diff = sorted(diff, reverse=True)



ans = (X[-1] - X[0]) - sum(diff[:N - 1])

print(ans)

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

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

count=[0]*(M-1)
i=1
while i<M:
    count[i-1]=X[i]-X[i-1]
    i+=1
count.sort()

print(sum(count[:max(M-N,0)]))

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

if N >= M:
    print(0)

else:
    X.sort()
    D = []
    for i in range(M-1):
        D.append(abs(X[i] - X[i+1]))

    D.sort()
    print(sum(D[0 : M-N]))

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N, M = I()
X = l()
X.sort()
kyori = []

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

if M == 1:
    print(0)
    sys.exit()
for i in range(len(X)-1):
    s = X[i+1]-X[i]
    kyori.append(s)

kyori.sort()
kyori.reverse()

for j in range(N-1):
    kyori.pop(0)

print(sum(kyori))

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

diff = sorted(diff, reverse=True)

dist = x[-1] - x[0]
if dist < n:
    print(0)
    exit()

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

for i in range(n-1):
    dist -= diff[i]

print(dist)

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

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

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

sa = []

for i in range(1, m):
    sa.append(abs(X[i] - X[i - 1]))

sa.sort(reverse=True)


print((max(X) - min(X)) - sum(sa[:n-1]))

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

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


ans.sort()

for i in range(n-1):
    ans[-1-i]=0
print(sum(ans))

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

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

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

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

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

Solution (Python):


import math



from collections import deque
from collections import Counter




n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
d=[0]*m
for i in range(1,m):
  d[i]=a[i]-a[i-1]
d.sort(reverse=True)
print(sum(d[n-1:]))

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

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

d = [0] * (M - 1)

if (N >= M):
    print(0)
    exit()
else:
    for i in range(M):
        if (i == M - 1):
            break
        else:
            d[i] = X[i + 1] - X[i]

d.sort(reverse=True)
count = 0
for i in d:
    if (N > 1):
        N -= 1
        continue
    elif(N == 1):
        count += i

print(count)

Solution (Python):
def main():
    N, M = map(int, input().split())
    X = [j for j in map(int, input().split())]
    if N >= M:
        print(0)
        exit()
    X.sort()
    s = max(X)-min(X)
    d = [X[i+1]-X[i] for i in range(M-1)]
    d.sort()
    if N == 1:
        print(s)
    else:
        print(s-sum(d[-N+1:]))


main()

Solution (Python):


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

x = sorted(x)
diff = [0 for _ in range(m-1)]
for i in range(m-1):
    diff[i] = x[i+1] - x[i]

diff = sorted(diff, reverse=True)
ans = sum(diff[n-1:])
print(ans)

Solution (Python):
n,m = map(int, input().split(" "))
xList = [int(i) for i in input().split(" ")]
xList = sorted(xList)
dList = [0]
for i in range(len(xList) -1) :
    dList.append(xList[i+1] - xList[i])
dList = sorted(dList, reverse=True)    

for i in range(n-1) :
    dList[i] = 0
    if(i == len(dList) -1) :
        break
print(sum(dList))

Solution (Python):
import numpy as np
N, M = map(int, input().split())
X = list(map(int, input().split()))
X_sort = np.array(sorted(X))

diff_arr = X_sort[1:] - X_sort[:M-1]
print(sum(sorted(diff_arr, reverse = True)[N-1:]))

Solution (Python):
n, m = map(int, input().split())
apple = list(map(int, input().split()))
orange = []
count = 0
if n >= m:
    count = 1
else:
    apple = sorted(apple)
    for i in range(m-1):
        orange.append(apple[i+1] - apple[i])
orange = sorted(orange)

if count == 0:
    if len(orange) != 1:
        if n != 1:
            print(sum(orange[:-(n-1)]))
        else:
            print(sum(orange))
    else:
        print(orange[0])
else:
    print(0)

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