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



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
Xs = list(map(int,input().split()))
Xs.sort()
X_dis = []
for i in range(M-1):
  X_dis.append(Xs[i+1]-Xs[i])
X_dis.sort(reverse = True)
dis = sum(X_dis[:N-1])
ans = Xs[-1]-Xs[0]-dis
print(ans)

Submission status: Accepted

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


if n >= m:
    # 全ての地点にコマを置ける
    print(0)
else:
    x.sort()
    # 隣り合う地点間の距離を求める
    # ex. 地点:[-100,0,1,5]
    # ans. diff:[100,1,4]
    diff = [x[i + 1] - x[i] for i in range(m - 1)]
    diff.sort()
    # そのなかで削減可能なのは、高々n-1個分
    print(sum(diff[0:m - n]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
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)
print(sum(y[n-1:]))

Submission status: Accepted

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print(0)
    elif N < M: 
        X.sort()
        diff = [0 for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = X[i+1] - X[i]
        diff.sort(reverse=True)
        
        ans = X[-1] - X[0]
        for i in range(N-1):
            ans -= diff[i]
        print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    if N >= M: 
        print(0)
    elif N < M: 
        X.sort()
        diff = [[0,0] for _ in range(M-1)]
        for i in range(M-1):
            diff[i] = [X[i+1] - X[i],i]
        diff.sort(reverse=True)
        diff = diff[:N-1]
        diff.sort(key=lambda p:p[1])

        ans = 0
        cnt = 0
        for i in range(N-1):
            ans += X[diff[i][1]] - X[cnt]
            cnt = diff[i][1] + 1
        ans += X[-1] - X[cnt]
        print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
d = []
if N > M:
    N = M
for i in range(M - 1):
    d.append(abs(X[i + 1] - X[i]))
d.sort()
print(sum(d[:M - N]))

Submission status: Accepted

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

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

Submission status: Accepted

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

X=list(map(int,input().split()))
X.sort()
di=[]

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

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    del_X = [0]*M
    for i in range(1, M):
        del_X[i] = abs(X[i] - X[i-1])
    
    del_X.sort()
    
    if N == 1:
        pass
    
    for n in range(N-1):
        if len(del_X) > 0:
            del_X.pop()
    
    print(sum(del_X))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,m=map(int,input().split())
if n>=m:
    print(0)
    exit()
x = list(map(int,input().split()))
ans=0
x.sort()
data=[(float('inf'),0)]*m
for i in range(1,m):
    data[i]=(x[i]-x[i-1],i)
data2 = sorted(data ,reverse=True)
data3 = sorted([data2[i][1] for i in range(n)]+[len(x)])
for i in range(1,n+1):
    ans += x[data3[i]-1] - x[data3[i-1]]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N >= M:
  print(0)
  exit()
  
X = list(map(int,input().split()))
X.sort()

dif = {}
for i in range(M-1):
  dif[i] = X[i+1]-X[i]
  
sorted_dif = sorted(dif.items(), key=lambda x:x[1], reverse=True)
difs = 0
for i in range(N-1):
  difs += sorted_dif[i][1]

print(X[-1]-X[0]-difs)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

N, M = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
d_list = [abs(a - b) for a, b in zip(x_list[:-1], x_list[1:])]
d_list = sorted(d_list, reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n,m = inpl()
x = inpl()
x.sort()
dd = []
for i in range(m-1):
    dd.append(abs(x[i] - x[i+1]))
dd.sort()
s = 0
for i in range(m-n):
    s += dd[i]
print(s)

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
X = list(map(int, input().split()))
X_sort = np.array(sorted(X))[::-1]
X_diff = X_sort[:M-1] - X_sort[1:]

X_diff_sort = sorted(X_diff)[::-1]

print(max(sum(X_diff_sort[N-1:]), 0))

Submission status: Accepted

Solution code:
N,M = [int(x) for x in input().split()]
if N>=M:
  print(0)
  exit()
X = [int(x) for x in input().split()]
X.sort()
L = []
for i in range(M-1):
  L.append(X[i+1]-X[i])
L.sort()
ans = sum(L[:M-N])
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x=list(map(int, input().split()))
x.sort()
r=[0]*(len(x)-1)
for i in range(len(x)-1):
    r[i] = x[i+1]-x[i]
print(sum(sorted(r, reverse=True)[n-1:]))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
L = sorted(LI())


total = L[-1] - L[0]
diff_list = sorted([L[i+1] - L[i] for i in range(m-1)], reverse=True)
if n >= m:
    print(0)
else:
    print(total - sum(diff_list[:n-1]))

Submission status: Accepted

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

diff = sorted([X[i + 1] - X[i] for i in range(M - 1)])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
if M == 1 or N >= M: print(0); exit()

X = sorted(list(map(int, input().split())))
if N == 1: print(max(X) - min(X)); exit()

dif = list(sorted([[X[i+1] - X[i], i] for i in range(M-1)], reverse=True))[:N-1]
col = [x[1] for x in dif]
if col[0]:
	l = col[0]
	s = X[l] - X[0]
else:
	l = 0
	s = 0

col.append(M-1)

for i in col:
	s += X[i] - X[l]
	l = i+1
print(s)

Submission status: Accepted

Solution code:
#xi以外の場所にコマを置く理由がない
#コマを置くというのは担当区間の分割
#xi x(i + 1)の間隔を無視するということ
#距離がでかいところから分割してやれば良い
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
l = [X[i + 1] - X[i] for i in range(M - 1)]
l.sort()
for _ in range(N - 1):
  if len(l):
    l.pop()
print(sum(l))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())));#print(x)

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

Submission status: Accepted

Solution code:
def resolve():
    n, m = list(map(int, input().split()))
    xxx = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
    else:
        lll = sorted([xxx[i+1]-xxx[i] for i in range(m-1)])[::-1]
        print(xxx[m-1]-xxx[0] - sum(lll[:n-1]))
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import math

def main():
    N,M = map(int, input().split())
    X = sorted(list(map(int, input().split())))
    D = sorted([X[i+1]-X[i] for i in range(M-1)], reverse=True)
    print(sum(D[N-1:]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import math

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append(X[i+1] - X[i])
    D.sort(reverse=True)
    D = D[N-1:]
    n = 0
    for d in D:
        n += d
    print(n)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Xs = list(map(int, input().split()))
Xs.sort()
#print(Xs)
if N >= M:
  print(0)
  exit()
Ds = []
for i in range(1, M):
  D = Xs[i]-Xs[i-1]
  Ds.append(D)
Ds.sort()
#print(Ds)
r = sum(Ds[:M-N])
print(r)

Submission status: Accepted

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

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

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    X.sort()
    dX = [(i, X[i + 1] - X[i]) for i in range(M - 1)]
    dX.sort(key=lambda x: x[1], reverse=True)
    print(sum(d[1] for d in dX[N - 1:]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
xs = sorted(set(map(int, input().split())))
ds = sorted([y - x for x, y in zip(xs, xs[1:])])
ds = ds[:max(0, len(ds) - n + 1)]
if len(ds) > 0:
    print(sum(ds))
else:
    print(0)

Submission status: Accepted

Solution code:
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()

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
if m <= n:
  print(0)
else:
  Dif_X = []
  for i in range(m-1):
    Dif_X += [X[i+1]-X[i]]
  Sum_Dif_X = sum(Dif_X)  
  Sorted_Dif_X = sorted(Dif_X, reverse = True)
  count = 0  
  while count < n-1:
    Sum_Dif_X -= Sorted_Dif_X[count]
    count += 1
  print(Sum_Dif_X)

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    dX = [(i, X[i + 1] - X[i]) for i in range(M - 1)]
    dX.sort(key=lambda x: x[1], reverse=True)
    print(sum(d[1] for d in dX[N - 1:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())

c = list(map(int,input().split()))
c.sort()

d = []
for i in range(b-1):
    d.append(c[i+1] - c[i])

d.sort()

#print(c,d)

for k in range(a - 1):
    if d == []:
        print(0)
        exit()
    d.pop()

print(sum(d))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
xl=list(map(int,input().split()))
if n>=m:
    print(0)
    exit()
xl=sorted(xl)
dl=[]
for i in range(m-1):
    dl.append(abs(xl[i]-xl[i+1]))
dl=sorted(dl)
print(sum(dl[:m-n]))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
dif = []
x.sort()

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

ans = sum(dif[n-1:])
print(ans)

Submission status: Accepted

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

dis = []
for i in range(M-1):
    dis.append(abs(X[i]-X[i+1]))
dis1 = sorted(dis)

ans = 0
if N >= M:
    print(0)
else:
    for j in range(M-N):
        ans += dis1[j]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
x = sorted(X)
i = 0
diff = []
while i + 1 < len(x):
    diff.append(x[i] - x[i + 1])
    i += 1
d = sorted(diff)[N - 1:]
print(abs(sum((d))))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
t = sorted(X, reverse=True)
h = [0] * M
for i in range(1, M):
    h[i] = abs(t[i - 1] - t[i])

tmp = sorted(h, reverse=True)
print(sum(tmp[N - 1:]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC117 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
x = LI()
x.sort()
if n >= m:
    print(0)
    quit()
y = [abs(x[i] - x[i+1]) for i in range(m-1)]
y.sort()
ans = sum(y[:m-n])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

gap.sort()

ans = sum(gap)
for i in range(N-1):
    ans -= gap[-1-i]

print(ans)

Submission status: Accepted

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

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

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

gap.sort()
gap.reverse()
t = sum(gap)
for i in range(N-1):
    t -= gap[i]

print(t)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
d = []
for i, j in enumerate(x):
    if x[-1] != j:
        d.append(x[i + 1] - x[i])
d.sort()
r = 0
for k in range(m - n):
    r += d[k]
print(r)

Submission status: Accepted

Solution code:
import itertools

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

Submission status: Accepted

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

X.sort()
A = list(y-x for x, y in zip(X, X[1:]))
A.sort(reverse=True)

print(sum(iter(A[N-1:])))

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
x.sort()
x_diff = []
for i in range(m - 1):
  x_diff.append(x[i + 1] - x[i])
x_diff.sort()
move = 0
for i in range(m - n):
  move += x_diff[i]
print(move)

Submission status: Accepted

Solution code:
n,m = [int(i) for i in input().split()]

l = sorted([int(i) for i in input().split()])

l2 = sorted([l[i] - l[i-1] for i in range(1,m)],reverse = True)

if len(l2) < n:
    print(0)
else:
    print(sum(l2[n-1:]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X = sorted(X)
Y = [X[i+1]-X[i] for i in range(M-1)] #差のリスト
Y = sorted(Y)
if N >= M:
  print(0)
else:
  print(sum(Y[:(M-N)]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    N,M = map(int, input().split())
    X = list(map(int, input().split()))
    X = sorted(X)
    X = X[::-1]
    Y = []
    for i in range(M-1):
        Y.append(X[i]-X[i+1])
    Y = sorted(Y)
    Y = Y[::-1]
    
    Ans = X[0] - X[-1]
    for i in range(min(N-1,M-1)):
        Ans -= Y[i]
    print(Ans)
    
    
    
main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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


print(sum(d[:M-N]))

Submission status: Accepted

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

    sa = []
    for i in range(M-1):
        sa.append(x[i+1] - x[i])
    sa = sorted(sa, reverse=True)
    # print(sa)
    if N >= M:
        print(0)
    else:
        print(sum(sa[N-1:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

data.sort()

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

disdata.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

x.sort()
i = 0
a = [0]*(m-1)
for i in range(m-1):
    a[i] = x[i+1]-x[i]
a.sort()
if n == 1:
    print(sum(a))
else:
    print(sum(a[:-(n-1)]))

Submission status: Accepted

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

    if N >= M:
        print(0)
        exit()
    sort_X = sorted(X)
    diff = []
    for i in range(M-1):
        diff.append(sort_X[i+1]-sort_X[i])

    sort_diff = sorted(diff)

    ans = 0
    for i in range(len(sort_diff) - (N - 1)):
        ans += sort_diff[i]

    print(ans)
    

main()

Submission status: Accepted

Solution code:

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

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

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

#print(diff)

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

print(sum(diff))

Submission status: Accepted

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

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

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

sa.sort(reverse=True)

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

Submission status: Accepted

Solution code:
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, reverse=True)
print(sum(s[N-1:]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
d = [0] * (M - 1)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
d = [0] * (M - 1)
ans = 0

if N >= M:
    print(ans)
else:
    for i in range(M - 1):
        d[i] = abs(X[i + 1] - X[i])
    d.sort(reverse=True)
    b = d[:N - 1]
    for i in range(M - 1):
        if d[i] in b:
            b.remove(d[i])
        else:
            ans += d[i]
    print(ans)

Submission status: Accepted

Solution code:
#C-Streamline
n,m=map(int,input().split())
x=list(map(int,input().split()))
x=sorted(x)
s=[]
for i in range(m-1):
    s.append(x[i+1]-x[i])
s=sorted(s)
k=0
for i in range(m-n):
    k+=s[i]
print(k)

Submission status: Accepted

Solution code:
from sys import stdin
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m - n]) if n < m else 0)

Submission status: Accepted

Solution code:
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(sum(y[:m-n]) if n < m else 0)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
X = sorted(LIST())

if N >= M:
	print(0)
else:
	L = [0] * (M-1)
	for i in range(M-1):
		L[i] = X[i+1]-X[i]
	L.sort()
	print(sum(L[:(M-N)]))

Submission status: Accepted

Solution code:
N,M= [int(s) for s in input().split()]
lst = [int(s) for s in input().split()]
lst.sort()
gap=[]
for i in range(1,M):
  gap.append(lst[i]-lst[i-1])
gap.sort()
ans=0
for i in range(M-N):
  ans+=gap[i]
print(ans)

Submission status: Accepted

Solution code:
import math;

def swap(a,b):
    return (b,a)

def gcd(a,b):
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def main():
    n,m = map(int,input().split())
    X = [x for x in map(int,input().split())]
    if n>=m:
        print(0)
        return
    X.sort()
    X_dif = []
    for i in range(1,m):
        X_dif.append([abs(X[i]-X[i-1]),i])
    ans = 0
    X_dif.sort()
    X_dif.reverse()
    index = [0]
    for i in range(n-1):
        index.append(X_dif[i][1])
    for i in range(1,n):
        ans += X[index[i]-1] - X[index[i-1]]
    ans += X[-1]-X[index[-1]]
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m, *x = map(int, open(0).read().split())
x.sort()
d = [i-j for i, j in zip(x[1:], x)]
d.sort()
print(sum(d[:m-n]) if n < m else 0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x_lis = list(map(int,input().split()))
x_lis.sort()
diff = []
for i in range(m-1):
	diff.append((x_lis[i+1]-x_lis[i]))
a_diff=sorted(diff, reverse=True)

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

Submission status: Accepted

Solution code:
import sys
N,M = map(int,input().split())
if N>M:
    print(0)
    sys.exit()
Xs = sorted(list(map(int,input().split())))
ranges = sorted([abs(Xs[i]-Xs[i-1]) for i in range(1,len(Xs))],reverse=True)
ans = Xs[M-1]-Xs[0]
for n in range(N-1):
    ans-=ranges[n]
print(ans)

Submission status: Accepted

Solution code:
import sys
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
dif=[]
if m<=n:
  print(0)
  sys.exit()
else:
  for i in range(m-1):
    dif.append(x[i+1]-x[i])
  dif.sort(reverse=True)
  ans=x[m-1]-x[0]
  for j in range(n-1):
    ans-=dif[j]
  print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = [int(x) for x in input().split()]
X.sort()
length = [x - X[i] for i,x in enumerate(X[1:])]
length.sort(reverse=True)
print(sum(length[N-1:]))

Submission status: Accepted

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

    if N >= M: return 0
    
    L = []
    for i in range(M-1):
        L.append(X[i+1] - X[i])
    L = sorted(L)
    return sum(L[:M-N])

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

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

X.sort(reverse=True)

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

diff.sort()

for _ in range(N-1):
    diff.pop()

print(sum(diff))

Submission status: Accepted

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

dis.sort()
if N >= M:
  print(0)
else:
  ans = 0
  for i in range(M - N):
    ans += dis[i]
  print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split());
xList =list(map(int, input().split()))
xList.sort();

if N >= M:
  print(0);
else:
  diff = xList[M - 1] - xList[1];

  diffList = [];
  prevX = xList[0];
  for i in range(1, M):
      x = xList[i];
      diffList.append(x - prevX);
      prevX = x;
  diffList.sort();

  for i in range(N - 1):
      diffList.pop();

  length = len(diffList);
  result = 0;
  for i in range(length):
      result += diffList[i];

  print(result);

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
Dis = [0]*(m-1)

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

for i in range(m-1):
    diff = abs(X[i]-X[i+1])
    Dis[i] = diff

Dis.sort()

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

print(sum(Dis))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [int(v) for v in input().split()]
if N >= M:
    print(0)
else:
    Xs = list(sorted(X))
    Xd = [Xs[i + 1] - Xs[i] for i in range(M - 1)]
    Xds = list(sorted(Xd)) #区間の数はM-1になってるから、これから大きい順に落としてN個の区間にする
    if N > 1:
        Xdsl = Xds[:-(N - 1)]
    else:
        Xdsl = Xds

    print(sum(Xdsl))

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,M = II()
X = III()

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

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

d.sort(reverse=True)
print(X[M-1]-X[0]-sum(d[:N-1]))