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: Runtime Error

Solution code:
big = 10**5 + 1
small = -1 * big

def streamline(n,m,x):
    if n >= m:
        return 0

    _x = sorted(x)
    diff = [[big,0] for _ in range(m-1)]

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

    _diff = sorted(diff,reverse=True)

    start = 0
    end = 0
    count = 0

    for i in range(n-1):
        end = _diff[i][1] + 1
        count += max(_x[start:end]) - min(_x[start:end])
        start = end
    else:
        count += max(_x[end:]) - min(_x[end:])

    return count
if __name__ == '__main__':
    n,m = map(int,raw_input().split())
    x = list(map(int,raw_input().split()))

    print streamline(n,m,x)

Submission status: Runtime Error

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

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

#print(diff)

if len(diff) == 0:
    print(0)
    exit()

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

print(sum(diff))

Submission status: Runtime Error

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

if N >= M:
    print(ans)
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: Runtime Error

Solution code:
from sys import sys
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[:-n]) if n < m else 0)

Submission status: Runtime Error

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)
print(result)

Submission status: Runtime Error

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()
result = 0
for i in range(n - m):
  result += y[i]
print(result)

Submission status: Runtime Error

Solution code:
a

Submission status: Runtime Error

Solution code:
import sys
N,M = map(int,input().split())
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: Runtime Error

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(n-1):
    dif.append(x[i+1]-x[i]-1)
  dif.sort(reverse=True)
  ans=x[n-1]-x[0]+1
  for j in range(n):
    ans-=dif[j]
  print(ans-n+1)

Submission status: Runtime Error

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

if M == 1:
  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: Runtime Error

Solution code:
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if n>=m:
  print(0)
elif m!=1:

  d=[[L[i+1]-L[i],i] for i in range(m-1)]
  d.sort()
  d.reverse()
  ans=L[m-1]-L[0]

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

else:
  print(0)

Submission status: Runtime Error

Solution code:

n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if m!=1:

  d=[[L[i+1]-L[i],i] for i in range(m-1)]
  d.sort()
  d.reverse()
  ans=L[m-1]-L[0]

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

else:
  print(0)

Submission status: Runtime Error

Solution code:

n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if m!=1:

  d=[[L[i+1]-L[i],i] for i in range(m-1)]
  d.sort()
  d.reverse()
  ans=L[m-1]-L[0]

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

else:
  print(0)

Submission status: Runtime Error

Solution code:

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

#d=np.array([L[i+1]-L[i] for i in range(m-1)])
d=[L[i+1]-L[i] for i in range(m-1)]
ans=L[m-1]-L[0]

for i in range(n-1):
  #a=np.argmax(d)
  a=d.index(max(d))
  d[a]=0
  ans-=(L[a+1]-L[a])
  




print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()

d=np.array([L[i+1]-L[i] for i in range(m-1)])

ans=L[m-1]-L[0]

for i in range(n-1):
  a=np.argmax(d)
  d[a]=0
  ans-=(L[a+1]-L[a])
  




print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()

d=np.array([L[i+1]-L[i] for i in range(m-1)])

ans=L[m-1]-L[0]

for i in range(n-1):
  a=np.argmax(d)
  d[a]=0
  ans-=(L[a+1]-L[a])
  




print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()

d=np.array([L[i+1]-L[i] for i in range(m-1)])
ans=L[m-1]-L[0]

for i in range(n-1):
  a=argmax(d)
  ans-(L[a+1]-L[a])
  d[a]=0
  




print(ans)

Submission status: Runtime Error

Solution code:
import sys
N, M = map(int, input().split())
X = sorted(list(map(int,input().split())))
A = []
if M == 1:
    print(0)
    sys.exit()
for i in range(M - 1):
    dif = X[i + 1] - X[i]
    A.append(dif)
for i in range(N - 1):
    A.remove(max(A))
print(sum(A))

Submission status: Runtime Error

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

for i in range(m-1):
    dif.append(abs(a[i]-a[i+1]))

dif = sorted(dif, reverse=True)


for i in range(n-1):
    dif.remove(dif[0])

print(sum(dif))

Submission status: Runtime Error

Solution code:
def solve():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    x.sort()
    diff = [x[i + 1] - x[i] for i in range(m)]
    diff.sort(reverse=True)
    return sum(diff[n - 1:])


if __name__ == "__main__":
    print(solve())

Submission status: Runtime Error

Solution code:
def solve():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    diff = sorted([x[i + 1] - x[i] for i in range(m)], reverse=True)
    return sum(diff[n - 1:])


if __name__ == "__main__":
    print(solve())

Submission status: Runtime Error

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


if __name__ == "__main__":
    print(solve())

Submission status: Runtime Error

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

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

X.sort()

buff = []

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


buff.sort()
buff = buff[::-1]

for i in range(min(N-1, len(buff))):
    # print(buff.pop(0))

print(sum(buff))

Submission status: Runtime Error

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

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

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

interval.sort(reverse = True)

answer = x[m-1] - x[0]
for i in range(n-1):
    answer -= interval[i]

print(answer)

Submission status: Runtime Error

Solution code:

import math
n, m = map(int, input().split())
s = [int(i) for i in input().split()]
s.sort()
a = []

for i in range(m-1):
    a.append(s[i]-s[i+1])
    
for i in range(math.ceil(n/2)):
    del a[a.index(min(a))]
 
print(abs(sum(a)))

Submission status: Runtime Error

Solution code:
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

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

list.sort(a, reverse=True)
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
        break
    count -= a[i]

print(count)

Submission status: Runtime Error

Solution code:
Import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

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

list.sort(a, reverse=True)
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
        break
    count -= a[i]

print(count)

Submission status: Runtime Error

Solution code:
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

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

list.sort(a, reverse=True)
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
	    break
    count -= a[i]

print(count)

Submission status: Runtime Error

Solution code:
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

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

list.sort(a, reverse=True)
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    count -= a[i]

print(count)

Submission status: Runtime Error

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

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

ans = sum(xx)
print(ans)

Submission status: Runtime Error

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

if N >= M:
  print(0)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  #print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  #print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  #print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  #print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Runtime Error

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

if N >= M:
  print(0)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
if n >= m:
  print(0)
else:
  x = sorted(list(map(int, input().split())))

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

  y_sorted = sorted(y, reverse=True)

  ans = 0
  for _y in y[n:]:
    ans += _y
  print(ans)

Submission status: Runtime Error

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

if N > M:
  print(0)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  #print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  #print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  #print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  #print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Runtime Error

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

if N > M:
  print(0)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Runtime Error

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

if N > M:
  print(0)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  DX.append(0)
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  #print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  #print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  #print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  #print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Runtime Error

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()
count = 0
for i in range(m - n):
    count += y[i]
print(count)

Submission status: Runtime Error

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

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

xdelta.sort()

if M > N:
	if N == 1:
		print(sum(xdelta))
	else:
		for i in range(M-1):
			xdelta.pop(-1)
		print(sum(xdelta))
else:
	print(0)

Submission status: Runtime Error

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

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

xdelta.sort()
#print(xdelta)

if M == 1:
	print(0)
elif N == 1:
	print(sum(xdelta))
elif M <= N:
	print(0)
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

xdelta.sort()
#print(xdelta)

if M == 1:
	print(0)
elif N == 1:
	print(sum(xdelta))
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

xdelta.sort()
#print(xdelta)

if xdelta == [] or N == 1:
	print(int(sum(map(abs,X))))
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

xdelta.sort()
#print(xdelta)

if xdelta == []:
	print(int(sum(map(abs,X))))
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

if xdelta == []:
	print(int(sum(map(abs,X))))
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

xdelta.sort()

if xdelta == []:
	print(int(sum(map(abs,X))))
else:
	for i in range(M-1):
		xdelta.pop(-1)
	print(sum(xdelta))

Submission status: Runtime Error

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

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

xdelta.sort()

for i in range(M-1):
	xdelta.pop(-1)
	
print(sum(xdelta))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if len(X) >1:
    l = [X[i+1]-X[i] for i in range(M-1)]
else:
    print(0)
    exit()
ll = sorted(l,reverse=True)
for i in range(N-1):
    ma = l.index(ll[i])
    l[ma] = 0
print(sum(l))

Submission status: Runtime Error

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

x = list(map(int, input().split()))
x = sorted(x)
diff = [x[i+1] - x[i] for i in range(0, m-1)]

sort_diff = sorted(diff, reverse=True)

sum = 0

if len(sort_diff) == 0:
    print(0)
    exit()

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

for i in range(len(sort_diff)):
    sum += sort_diff[i]
print(sum)

Submission status: Runtime Error

Solution code:

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

x = list(map(int, input().split()))
x = sorted(x)
diff = [x[i+1] - x[i] for i in range(0, m-1)]

sort_diff = sorted(diff, reverse=True)

sum = 0
for i in range(n-1):
    sort_diff.pop(0)

for i in range(len(sort_diff)):
    sum += sort_diff[i]
print(sum)

Submission status: Runtime Error

Solution code:
import itertools

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

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

x = sorted(x)
sum = 0
min = 1000000
flag = False
if m == 1:
    flag = True
    min = 0

l = list(range(1, m))

for i in itertools.combinations(l, n-1): # Change the conbination mCn
    if flag == True:
        break
    sum = 0
    for j in range(0, n):
        if j == 0:
            sum += x[i[0]-1] - x[0]
        elif j == n-1:
            sum += x[-1] - x[i[-1]]
        else:
            sum += x[i[j]-1] - x[i[j-1]]
    if sum < min:
        min = sum

print(min)

Submission status: Runtime Error

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

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

dist.sort()

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

print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Y.sort()

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

Submission status: Runtime Error

Solution code:
N=int(input())
M=int(input())

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

Y=list()
before=X[0]
for i in X:
  diff = i - before
  if diff == 0:
    continue
  Y.append(diff)
  before = i

Y=sorted(Y)

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

Submission status: Runtime Error

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

for i in range(M-1):
  subX.append(X[i+1]-X[i])
  
subX = sorted(subX)  
ans = sum(subX)

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

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#ABC116C
import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n=hoge[0]
m=hoge[1]

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

hoge.sort()
#print(hoge)
x=[]
sum=0
for i in range(0,m-1):
	x.append(hoge[i+1]-hoge[i])
	sum+=hoge[i+1]-hoge[i]
x.sort(reverse=True)

#print(x)

sc=0
for i in range(0,n-1):
	sc+=x[i]

print(sum-sc)

Submission status: Runtime Error

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

if n==1:
	res=0
else:
	y=sorted(x)
	z=[0]*(n-1)
	for i in range(n-1):
		z[i]=y[i+1]-y[i]
	v=sorted(z,reverse=True)
	w=0
	if k>1:
		for i in range(k-1):
			w+=v[i]
		res=y[n-1]-y[0]-w
	else:
		res=y[n-1]-y[0] 

print(res)

Submission status: Runtime Error

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

if n==1:
	res=0
else:
	y=sorted(x)
	z=[0]*(n-1)
	for i in range(n-1):
		z[i]=y[i+1]-y[i]
	v=sorted(z,reverse=True)
	w=0
	for i in range(k-1):
		w+=v[i]
	res=y[n-1]-y[0]-w

print(res)

Submission status: Runtime Error

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

if N >= M:
  print(0)
else:
  X.sort()

  diffs = [0]*(M-1)

  for i in range(M-1):
    diffs[i] = abs(x[i+1] - X[i])

  diffs.sort(key= lambda x: -x[0])
  
  print(sum(diffs[N-1:]))

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ls=[]
for i in range(b-1):
  ls.append(lis[i+1]-lis[i])
ls.sort()
print(sum(ls[n-1:])

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop, heappush, nsmallest
n, m = map(int, raw_input().split())
X = map(int, raw_input().split())
X.sort()
heap = []
for i in range(1, m):
    diff = X[i] - X[i-1]
    if len(heap) >= n - 1 and nsmallest(1, heap)[0][0] < diff:
        heappop(heap)
        heappush(heap, (diff, X[i-1], X[i]))
    elif len(heap) < n - 1:
        heappush(heap, (diff, X[i-1], X[i]))
    else:
        continue
heap.sort(key=lambda X : X[1])
cp = X[0]
move = 0
for j in range(len(heap)):
    diff, left, right = heap[j]
    move += left - cp
    cp = right
move += (X[-1] - cp)
print move

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
if m =< n:
    print(0)
    quit()

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

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

dist_l.sort()
print(sum(dist_l[:-(n-1)]))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
dif = []
pos = []
ans = 0

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

    dif.sort(reverse = True)

    for i in range(n - 1):
        pos.append(dif[i][1])

    pos.sort()

    ans += x[pos[0]] - x[0]
    if n > 1:
        for i in range(n - 2):
            ans += x[pos[i + 1]] - x[pos[i] + 1]
    ans += x[m - 1] - x[pos[-1] + 1]

    print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
dif = []
pos = []
ans = 0

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

    dif.sort(reverse = True)

    for i in range(n - 1):
        pos.append(dif[i][1])

    pos.sort()

    ans += x[pos[0]] - x[0]
    for i in range(n - 2):
        ans += x[pos[i + 1]] - x[pos[i] + 1]
    ans += x[m - 1] - x[pos[-1] + 1]

    print(ans)

Submission status: Runtime Error

Solution code:
import sys

n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
dif = []
pos = []
ans = 0

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

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

dif.sort(reverse = True)

for i in range(n - 1):
    pos.append(dif[i][1])

pos.sort()

ans += x[pos[0]] - x[0]
for i in range(n - 2):
    ans += x[pos[i + 1]] - x[pos[i] + 1]
ans += x[m - 1] - x[pos[-1] + 1]

print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
zahyou = list(map(int,input().split()))
zahyou.sort()
difference = []
ans = 0

if M == 1:
    ans = 0
else:
    for i in range (M-1):
        difference.append(zahyou[i+1] - zahyou[i])
    difference.append(0)
    difference.sort()
    difference.reverse()
    length = zahyou[-1] - zahyou[0]
    ans = int(length)
    for i in range (N-1):
        ans -= difference[i]

print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
zahyou = list(map(int,input().split()))
zahyou.sort()
difference = []
ans = 0

if M == 1:
    ans = 0
else:
    for i in range (M-1):
        difference.append(zahyou[i+1] - zahyou[i])
    difference.append(0)
    difference.sort()
    difference.reverse()
    length = zahyou[-1] - zahyou[0]
    ans = int(length)
    for i in range (N):
        ans -= difference[i-1]

print(ans)

Submission status: Runtime Error

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

x.sort()
dist = [0] * (m-1)
for i in range(m-1):
    dist[i] = x[i+1] - x[i]
dist.sort(reverse=True)

length = x[m-1]-x[0]
for i in range(n-1):
    length -= dist[i]

print(length)

Submission status: Runtime Error

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

x.sort()
print(x)

dist = [0] * (m-1)
for i in range(m-1):
    dist[i] = x[i+1] - x[i]
dist.sort(reverse=True)

length = x[m-1]-x[0]
for i in range(n-1):
    length -= dist[i]

print(length)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
diff = []
if M <= N:
    print(0)

for i in range(1, m):
    diff.append(x[i] - x[i - 1])
diff.sort()
print(sum(diff[:m-n]))

Submission status: Runtime Error

Solution code:
import sys

diff = deque()

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

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

X.sort()

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

diff.sort()

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

print(sum(diff))

Submission status: Runtime Error

Solution code:
import sys

diff = deque()

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

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

X.sort()

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

diff.sort()

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

print(sum(diff))

Submission status: Runtime Error

Solution code:
# メタデータ入力
n, m = map(int,input().split())

# 座標入力
pos = list(map(int,input().split()))
# ソート
pos.sort()

# 差分計算
distances = []
for i, x in enumerate(pos):
  if i == 0:
    continue
  distances.append(abs(pos[i] - pos[i-1]))

distances.sort()
for i in range(n-1):
	distances.pop()
print(sum(distances))

Submission status: Runtime Error

Solution code:
from collections import deque
N, M = map(int, input().split())
X = deque(map(int, input().split()))
if N >= M:
	print(0)
else:
	X.sort()
	x = 0
	y = deque([0] * (N - 1))
	for i in range(M - 1):
		a = X[i+1] - X[i]
		b = min(y)
		if a > b:
			y.remove(b)
			y.append(a)
			x += b
		else:
			x += a
	print(x)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
	print(0)
else:
	X.sort()
	x = 0
	y = [0] * (N - 1)
	for i in range(M - 1):
		a = X[i+1] - X[i]
		b = min(y)
		if a > b:
			y.remove(b)
			y.append(a)
			x += b
		else:
			x += a
	print(x)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
x = 0
y = [0] * (N - 1)
for i in range(M - 1):
	a = X[i+1] - X[i]
	b = min(y)
	if a > b:
		y.remove(b)
		y.append(a)
		x += b
	else:
		x += a
print(x)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

x_diff = [x[i+1] - x[i] for i in range(len(x)-1)]
for _ in range(n-1):
    del x_diff[x_diff.index(max(x_diff))]
    
print(sum(x_diff))

Submission status: Runtime Error

Solution code:
import numpy as np
import math as m

# 初期値を入力

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

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

# 関数定義

def Merge(A, p, q, r):
    # 配列Aのpからr番目の部分配列をソートすることを目的とする関数．ここで，pからqの部分配列と，q+1からrの部分配列はそれぞれソートされているものとする．
    n_1 = q - p + 1
    n_2 = r - q
    B = A[p : q + 1]
    B = np.append(B, 100000)
    C = A[q + 1 : r + 1]
    C = np.append(C, 100000)
    i = 0
    j = 0
    k = p

    while i <= n_1 - 1 or j <= n_2 - 1:
        if B[i] <= C[j]:
            A[k] = B[i]
            i += 1
            k += 1
        else:
            A[k] = C[j]
            j += 1
            k += 1

    return A

def Merge_Sort(A, p, r):
    if p < r:
        q = m.floor( (p + r)/2 )
        A = Merge_Sort(A, p, q)
        A = Merge_Sort(A, q + 1, r)
        A = Merge(A, p, q, r)

    return A

# メインプログラム

X = np.array(X)

p = 0
r = len(X) - 1

Sorted_X = Merge_Sort(X, p, r)

Distance = []

for i in range(r):
    Distance.append(X[i+1] - X[i])

Distance = np.array(Distance)

Sorted_Distance = Merge_Sort(Distance, p, len(Distance)-1)

print(sum(Sorted_Distance[0:M-N-1]))