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: Wrong Answer

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

sortedX = sorted(X)
diffX = list(np.roll(sortedX, -1) - sortedX)

#if N >= M:
#	print('0')
#else:
#	print(sum(sorted(diffX[:-1])[:-(N-1)]))
print(sum(sorted(diffX[:-1])[:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def distance_to_right(p, point_list):
    if p == len(point_list) - 1:
        return None
    else:
        return point_list[p+1] - point_list[p]


# main

num_koma, num_to_visit = [int(element) for element in input().split(' ')]
points = [int(element) for element in input().split(' ')]

points.sort()

costs = [distance_to_right(p, points) for p in range(0, num_to_visit-1)]
costs.sort()

print(sum(costs[:-(num_koma-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sortedX = sorted(X)
diffX = list(np.roll(sortedX, -1) - sortedX)

if N >= M:
	print('0')
else:
	print(sum(sorted(diffX[:-1])[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
from statistics import median
N_M = input().split()
N = int(N_M[0])
M = int(N_M[1])
Xs = input().split()
for i in range(M):
    Xs[i] = int(Xs[i])
X_min = min(Xs)
X_max = max(Xs)
X_median = median(Xs)
answer = min(X_max - X_median, X_median - X_min)
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N == 1:
    ans = max(X) - min(X)
    print(ans)
    exit()

if M == 1:
    ans = 0
    print(ans)
    exit()

Xs = sorted(X)

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

diffs = sorted(diff)
print(sum(diffs[:N - (M - 2)]))

Submission status: Wrong Answer

Solution code:
import itertools
n,m = map(int,input().split())
zahyou_list = [i for i in map(int,input().split())]
zahyou_list.sort()
#print(zahyou_list)
kaisa_list = []
#print(sum(kaisa_list))

for i in range(len(zahyou_list)-1):
    kaisa_list.append(zahyou_list[i+1] - zahyou_list[i])
mini = sum(kaisa_list)
kaisa_list.sort()

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

Submission status: Wrong Answer

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

ans = sum(sorted(sub)[:m-n+1])
if n>= m:
    ans = 0
print(ans)
# print("slip list is ", skip_list)
# print("sub List is ", sub)

Submission status: Wrong Answer

Solution code:
data = input().split()  
n = int(data[0])
m = int(data[1])
x = [int(s) for s in input().split()]

x.sort()

y=[]
tmp=0
for i in x:
    y.append(i-tmp)
    tmp=i

y.sort()
y.reverse()

res=x[-1]-x[0]

if(len(y) < n):
    n = len(y)


for i in range(n-1):
    res-=y[i]

print(res)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
minus = (N//2) + (N%2)
D = [X[i+1] - X[i] for i in range(M-1)]
D.sort()
ans = 0
for i in range(M-1-minus):
    ans += D[i]
print(ans)

Submission status: Wrong Answer

Solution code:
nm=[int(x) for x in input().split()]
x=[int(y) for y in input().split()]
margin=[]
sort_x=sorted(x)
for p in range(nm[1]-1):
    margin.append(sort_x[p+1]-sort_x[p])
sort_mar=sorted(margin)
print(sum(sort_mar[:nm[1]-nm[0]]))

Submission status: Wrong Answer

Solution code:
import itertools
n,m = map(int,input().split())
zahyou_list = [i for i in map(int,input().split())]
zahyou_list.sort()
#print(zahyou_list)
kaisa_list = []
#print(sum(kaisa_list))

for i in range(len(zahyou_list)-1):
    kaisa_list.append(zahyou_list[i+1] - zahyou_list[i])
mini = sum(kaisa_list)
kaisa_list.sort()
print(kaisa_list)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
xlist = [int(_) for _ in input().split()]
xlist.sort()

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

print(sum(nlist[0:-n+1]))

Submission status: Wrong Answer

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


N, M = [int(elem) for elem in fin().split()]
X_list = sorted([int(elem) for elem in fin().split()])
min_X = X_list[0]
max_X = X_list[-1]
exists = [False] * (max_X - min_X + 1)
for X in X_list:
    X -= min_X
    exists[X] = True

distances = []
last_idx = 0
for i, flag in enumerate(exists[1:], start=1):
    if flag:
        distances.append(i - last_idx - 1)
        last_idx = i

distances.sort(reverse=True)
# print(distances)
# print(len(exists))
minimum_moves = max(0, len(exists) - N) - sum(distances[:N - 1])
print(minimum_moves)

Submission status: Wrong Answer

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

x_list.sort()
ans = 0

margin = [0] * (M - 1)
f = x_list[0]
for i in range(1, M):
    margin[i - 1] = x_list[i] - f
    f = x_list[i]
margin = sorted(enumerate(margin), key=lambda x: -x[1])

cnt = [0]
for i, v in margin:
    cnt.append(i + 1)
    if len(cnt) == N:
        break

start = 0
prev = x_list[0]
for i, c in enumerate(cnt[1:]):

    for j in range(start, c):
        ans += abs(x_list[j] - prev)
        prev = x_list[j]
    start = c
    prev = x_list[c]

last = cnt[-1]
for i in range(last, M):
    if i == last:
        prev = x_list[i]
    else:
        ans += abs(x_list[i] - prev)
        prev = x_list[i]

print(ans)

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]
xs = sorted([int(i) for i in input().split()])
li = sorted([xs[i+1] - xs[i] for i in range(m-1)])
if len(li) == 1 and n == 1:
    print(li[0])
else:
    print(sum(li[:-n+1]))

Submission status: Wrong Answer

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

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

Y.sort(reverse=True)
print('Y',Y)

res = X[-1] - X[0]
print('res', res)
for i in Y[:N-1]:
	res -= i
print('res',res)

print(res)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[]
for i in range(m-1):
  B.append(abs(A[i+1]-A[i]))
B.sort()
if n==1:
  sum(B)
else:
  print(sum(B[:-n+1:]))

Submission status: Wrong Answer

Solution code:
n, m = list(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()


'''
for i in range(n-1):
    y.remove(max(y))
'''
w = sum(y[:m-n])
print(w)

Submission status: Wrong Answer

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

if n>=m:
    print(0)
    
else:
    li = sorted(list(map(int,input().split())),reverse=True)

    long=max(li)-min(li)
    dis=[li[i]-li[i+1] for i in range(m-1)]
    dis_sort=sorted(dis,reverse=True)
    print(sum(dis_sort[n:]))

Submission status: Wrong Answer

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

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

x.sort()

z = []
for i in range(len(x) - 1):
    z.append(abs(x[i] - x[i + 1]))

z.sort()
for _ in range(n - 1):
    if len(z) > 1:
        z.pop()

print(sum(z))

Submission status: Wrong Answer

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(tmp != []):
  print(sum(tmp[:-(N-1)]))
else:
  print(0)

Submission status: Wrong Answer

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

x.sort()
y = [abs(x2 - x1) for x1, x2 in zip(x[1:], x[:-1])]
y.sort()

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

Submission status: Wrong Answer

Solution code:
import math

def solve(N,M,Xs):
    if N >= M:
        return 0

    Ds = [abs(Xs[i]-Xs[i+1]) for i in range(len(Xs)-1)] # Ds[j] := distance between j and j+1

    Ds.sort()
    #print(Ds)
    #print(math.ceil(N/2))

    return sum(Ds[0:len(Ds)-(math.ceil(N/2))])

def main():
    N, M = map(int, input().split())
    Xs = list(map(int, input().split()))
    Xs.sort()
    print(solve(N,M,Xs))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

X = sorted(X)

if len(X) <= N:
	print(0)

else:
	l = []
	for n in range(len(X)):
		if n != 0:
			l.append(abs(X[n - 1] - X[n]))

	ans = 0
	l = sorted(l, reverse=False)
	print(l)
	for i in range(M - N):
		ans += l[0]
		l.pop(0)

	print(ans)

Submission status: Wrong Answer

Solution code:
from statistics import median
N_M = input().split()
N = int(N_M[0])
M = int(N_M[1])
Xs = input().split()
for i in range(M):
    Xs[i] = int(Xs[i])
X_min = min(Xs)
X_max = max(Xs)
X_median = median(Xs)
answer = min(X_max - X_median, X_median - X_min) + 1
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(tmp != []):
  print(sum(tmp) - sum(tmp[-(N-1):]))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]
xs = sorted([int(i) for i in input().split()])
li = sorted([xs[i+1] - xs[i] for i in range(m-1)])
if len(li) == 1:
    print(sum(li))
else:
    print(sum(li[:-n+1]))

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=a.split()
n=int(a[0])
m=b[1]
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
c=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n-1):
    del ll[-1]
print(sum(ll))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A=list(map(int,input().split()))
A.sort()
B=[]
for i in range(m-1):
  B.append(abs(A[i+1]-A[i]))
B.sort()
print(sum(B[:-n+1:]))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter


def r(n, m, xl):
    if len(xl) == 1:
        return 0
    xl.sort()
    min_x = abs(min(xl))
    xl = [x + min_x for x in xl]
    xm = [xl[i+1] - xl[i] for i in range(len(xl)-1)]
    xm.sort()
    return sum(xm[:n+1])


def main():
    input = sys.stdin.readline
    n, m = list(map(int, input().split()))
    xl = list(map(int, input().split()))

    print(r(n, m, xl))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

distX = sorted(X)
# print(distX)
distX = [abs(x-y) for x,y in zip(distX[:-1],distX[1:])]
# print(distX)
sorDX = sorted(distX)

a = N-1
print(sum(sorDX[:-a]))

Submission status: Wrong Answer

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

D = [X[i+1]-X[i] for i in range(len(X)-1)]
#print(D)
D.sort()
print(sum(D[:-(N-1)]))

Submission status: Wrong Answer

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

tmp = sorted([abs(X[i+1] - X[i]) for i in range(M-1)])
if(tmp != []):
  print(sum(tmp) - sum(tmp[-(N-1):]))
else:
  print(0)

Submission status: Wrong Answer

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

diffs = [0 for _ in range(M)]
for m in range(1, M):
    diffs[m] = sorted_X[m] - sorted_X[m-1]
sorted_D = sorted(diffs)

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

Submission status: Wrong Answer

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

ans = sum(sorted(sub)[:m-n+1])
print(ans)
# print("slip list is ", skip_list)
# print("sub List is ", sub)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,Lsize = (int(i) for i in input().split())  
L = [int(i) for i in input().split()]
L.sort()



if Lsize > n:
    result = n - 1 
    for i in range(n+1, Lsize):
        result = result + (L[i] - L[i-1])
else:
    result = 0
        
print("{}".format(result))

Submission status: Wrong Answer

Solution code:
# 整数の入力
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()



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


    ans = 0
    for i in range(M-N):
        ans = ans+Y[i]
        

    print(ans)

Submission status: Wrong Answer

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

dist = []
for i in range(len(x)-1):
    dist.append([x[i+1]-x[i],i])
sorted_dist = sorted(dist,key=lambda x:x[0])
top_n = sorted(sorted_dist[-1*n+1:],key=lambda x:x[1])

total = 0
idx = 0
for _,i in top_n:
    total += abs(x[i] - x[idx])
    idx = i+1
total += abs(x[-1] - x[idx])
print(total)

Submission status: Wrong Answer

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

x_sort = sorted(x)

check = []
for i in range(len(x)-1):
    check.append(abs(x_sort[i+1] - x_sort[i]))
    
check_sorted = sorted(check)

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X_list = list(map(int, input().split()))
X_list.sort()
err_list = [X_list[i+1] - X_list[i] for i in range(len(X_list)-1)]
err_list.sort()

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

Submission status: Wrong Answer

Solution code:
n,m = list(map(int,input().split()))
x = list(map(int,input().split()))
x = sorted(x)
val = []
for i in range(m-1):
  val.append(x[i+1]-x[i])
if n == 1:
  print(sum(val))
else:
  m = sorted(val)[-n+1:]
  js = []
  for i in m:
    js.append(val.index(i))
  js = sorted(js)
  s = 0
  x1 = 0
  for i in range(len(js)):
    s += sum(val[x1:js[i]])
    x1 = js[i]+1
  s += sum(val[x1:])
  print(s)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

if N == 1:
    print(X[-1] - X[0])
else:
    C = list(combinations(list(range(1, len(X))), N - 1))
    ans = X[-1] - X[0]
    for i, ci in enumerate(C):
        tmp = X[ci[0] - 1] - X[0]
        for j in range(len(ci) - 1):
            tmp += X[ci[1] - 1] - X[ci[0]]
        tmp += X[-1] - X[ci[-1]]
        if tmp < ans:
            ans = tmp
    print(ans)

Submission status: Wrong Answer

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

li = sorted([xs[i+1] - xs[i] for i in range(m-1)])
# for i in range(m-1):
print(sum(li[:-n+1]))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter


def r(n, m, xl):
    xl.sort()
    xm = [xl[i+1] - xl[i] for i in range(len(xl)-1)]
    xm.sort()
    return sum(xm[:n+1])


def main():
    input = sys.stdin.readline
    n, m = list(map(int, input().split()))
    xl = list(map(int, input().split()))

    print(r(n, m, xl))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

B=[]
for i in range(m-1):
  B.append(abs(A[i+1]-A[i]))
B.sort()
print(sum(B[:-n+1:]))

Submission status: Wrong Answer

Solution code:
import sys
N,M = map(int, input().split())
X = list(map(int, input().split()))
# N,M = 2,5
# X = [1,2,10,12, 14]
X.sort()
if N>=M: 
    print(0)
    sys.exit()
dif = [X[i+1]-X[i] for i in range(M-1)]
dif = sorted(dif)[:-N+2]
print(sum(dif))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
nm=[int(x) for x in input().split()]
x=[int(y) for y in input().split()]
margin=[]
sort_x=sorted(x)
for p in range(nm[1]-1):
    margin.append(sort_x[p+1]-sort_x[p])
sort_mar=sorted(margin)
print(sum(margin)-sum(sort_mar[len(sort_mar)-nm[0]+1:]))

Submission status: Wrong Answer

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

dis = [(L[i+1] - L[i],i) for i in range(M-1)]
dis1 = [L[i+1]-L[i] for i in range(M-1)]
dis = sorted(dis,key=lambda x:(x[0],-x[1]))[::-1]
l = [0]
for i in range(N-1):
    try:l.append(dis[i][1]+1)
    except:pass
#print(l,dis1)
ans = 0
cnt = 0
for i in range(len(l)-1):
    cnt +=1
#    print(dis1[l[i]:l[i+1]-1],l[i],l[i+1])
    ans += sum(dis1[l[i]:l[i+1]-1])
ans += sum(dis1[l[cnt]:])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M =list(map(int, input().split()))
X = input().split()
for i in range(len(X)):
    X[i] = int(X[i])
X = np.array(X)
X.sort()

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

Y = np.array(y)
Y.sort()

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

Submission status: Wrong Answer

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

tmp = sorted([abs(X[i+1] - X[i]) for i in range(M-1)])
if(tmp != []):
  print(sum(tmp) - sum(tmp[N+1:]))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

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

x.sort()
x2 = [0] + x[:-1]

xn = np.array(x)
x2n = np.array(x2)

diff = (xn - x2n)[1:]
diff.sort()

ds = diff[:-(n-1)]

diffsum = np.sum(ds)

print(diffsum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X.sort()
difX = [abs(i-j) for i,j in zip(X[1:],X[:-1])]
difX.sort()

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

Submission status: Wrong Answer

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

X = list(set(X))
X.sort()
M = len(X)
L = [X[i+1] - X[i] for i in range(len(X)-1)]
L.sort()

if M <= N:
    print(0)
else:
    print(abs(sum(L[:-(N-1)])))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
if m==1:
  print(0)
  exit()
l.sort()
l2=[]
x0=l[0]
l.remove(x0)
for x in l:
  l2.append(x-x0)
  x0=x
l2.sort()
print(sum(l2[:-(n-1)]))

Submission status: Wrong Answer

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

X = list(set(X))
X.sort()
M = len(X)
L = [X[i+1] - X[i] for i in range(len(X)-1)]
L.sort()

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

Submission status: Wrong Answer

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

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

diff.sort()
print(sum(diff[:N+1]))

Submission status: Wrong Answer

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

if m == 1:
  print(0)
elif m == 2 and n == 1:
  print(y[0])
elif m == 2 and n > 1:
  print(0)
else:
  for i in range(m - 3):
    total += y[i]
  print(total)

Submission status: Wrong Answer

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

X.sort()

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

L.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for k in range(1,M):
    D[k] = X[k]-X[k-1]

print(sum(sorted(D)[:M-N+1]))

Submission status: Wrong Answer

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

# print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%
if m <= n:
    print(0)



#%%

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


#%%
diff_list.sort()


#%%
print(sum(diff_list[:-(n-1)]))

#%%


#%%

Submission status: Wrong Answer

Solution code:
n, m=map(int, input().split())
x=list(map(int, input().split()))
x=sorted(x)
y=[0]*(m-1)

for i in range(m-1):
    y[i]=x[i+1] - x[i]
z=sorted(y)

print(sum(z[0:m-n]))

Submission status: Wrong Answer

Solution code:
N,M=[int(_) for _ in input().split()]
aX=sorted(int(_) for _ in input().split())
aD =sorted(aX[i]-aX[i-1] for i in range(1,M))
print(sum(aD[:M - N ]))

Submission status: Wrong Answer

Solution code:
from sys import stdin

nm = list(map(lambda x: int(x), stdin.readline().rstrip().split(" ")))
x = list(map(lambda x: int(x), stdin.readline().rstrip().split(" ")))

x.sort()
distance = []
for i in range(1, len(x)):
    distance.append(x[i] - x[i-1])

if len(distance) <= nm[0] - 1:
    print(0)
else:
    distance.sort()
    print(sum(distance[:(nm[0]-1)*-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
N,M = map(int, input().split())
X = list(map(int, input().split()))
# N,M = 2,5
# X = [1,2,10,12, 14]
X.sort()
if N>=M: 
    print(0)
    sys.exit()
dif = [X[i+1]-X[i] for i in range(M-1)]
dif = sorted(dif)[:-N+1]
print(sum(dif))

Submission status: Wrong Answer

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

X.sort()
X = list(set(X))
M = len(X)
L = [X[i+1] - X[i] for i in range(len(X)-1)]

L.sort()

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

Submission status: Wrong Answer

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

X = sorted(X)

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

d = sorted(d)

print(sum(d[:len(d)-N+1]))

Submission status: Wrong Answer

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

# print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%
if m == n:
    print(0)



#%%

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


#%%
diff_list.sort()


#%%
print(sum(diff_list[:-(n-1)]))

#%%


#%%

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    xs = sorted(map(int, input().split()))
    dists = sorted(xs[i] - xs[i - 1] for i in range(1, len(xs)))
    print(sum(dists[: m - n]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m =(int(i) for i in input().split())
x = list(int(i) for i in input().split())
x.sort()
sa = []
ans = 0
if m == 1 or m == 2:
  print('0')
  exit()
for i in range(m-1):
  sa.append(x[i+1]-x[i])
#print(sa)
if max(sa) in [sa[0],sa[m-2]]:
 tmp1 =max(sa)
 sa.remove(tmp1)
 ans = sum(sa)-max(sa[0],sa[m-3])
else: 
 ans = sum(sa)-max(sa)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=[int(_) for _ in input().split()]
aX=sorted(int(_) for _ in input().split())
aD =sorted(aX[i]-aX[i-1] for i in range(1,M))
print(sum(aD[:N+1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
s = [0] * M
for i in range(M - 1):
    s[i] = X[i + 1] - X[i]
s.sort()
print(sum(s[:-N + 1]))

Submission status: Wrong Answer

Solution code:
import sys,collections,math;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n,m = Is()
Xs = sorted(Is())
dist = []
for i in range(m-1):
    dist.append(Xs[i+1]-Xs[i])
print(sum(sorted(dist)[:m-n]))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%
if m == n:
    print(0)



#%%

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


#%%
diff_list.sort()


#%%
print(sum(diff_list[:-(n-1)]))

#%%


#%%

Submission status: Wrong Answer

Solution code:
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)])
total = 0

for i in range(m - 3):
  total += y[i]

print(total)

Submission status: Wrong Answer

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

if M <= N:
    print(0)
else:
    X.sort()
    dX = np.diff(X)
    dX.sort()
    print(sum(dX[:N+1]))

Submission status: Wrong Answer

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

for i in range(m - 1):
    A.append(X[i + 1] - X[i])
    
A.sort()
print(sum(A[:-(n - 1)]))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import numpy as np
N, M = map(int,input().split())
X = list(map(int,input().split()))
X = sorted(X)
d = np.diff(X)
d = sorted(d)
print (sum(d[:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

SA.sort()
print(sum(SA[:-(N-1)]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

X.sort()

L = [X[i+1] - X[i] for i in range(len(X)-1)]

L.sort()

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

Submission status: Wrong Answer

Solution code:
# C

def main_c(X_list, N, M):
    
    X_list_s = sorted(X_list)
    D_list = [X_list_s[i+1] - X_list_s[i] for i in range(M-1)]
    D_list_s = sorted(D_list)
    
    res = sum(D_list_s[:(M - N)])
    
    return res

if __name__ == "__main__":
    N, M = map(int, input().split())
    X_list = list(map(int, input().split()))
    res = main_c(X_list, N, M)
    print(res)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
array = sorted([int(x) for x in input().split()])
tmp = array[0]
tmp_array = []
for i in range(len(array)):
    tmp_array.append(array[i]-tmp)
    tmp = array[i]
tmp_array = sorted(tmp_array)


print(sum(tmp_array[:n+2]))

Submission status: Wrong Answer

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