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:
n, m, *X = map(int, open(0).read().split())
X.sort()
Y = [0] * m
for i in range(m-1):
    Y[i] = X[i+1] - X[i]
Y.sort()
print(sum(Y[:m-n+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

move = sorted([x[i] - x[i - 1] for i in range(1, m)])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def calculate(n,m,arr):

    if n >= m:
        print(0)
        return

    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:(m-1-n-1)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Submission status: Wrong Answer

Solution code:
def calculate(n,m,arr):

    if n >= n:
        print(0)
        return

    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:(m-n)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Submission status: Wrong Answer

Solution code:
def calculate(n,m,arr):
    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:(m-n)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Submission status: Wrong Answer

Solution code:
def calculate(n,m,arr):
    arr.sort()
    drr = list([])
    for index in range(1,len(arr)):
        drr.append(arr[index] - arr[index-1])
    drr.sort()
    drr = drr[:m-1-(n-1)]
    print(sum(drr))



S = input().split(" ")
n = int(S[0])
m = int(S[1])

S = input().split(" ")

arr = list([int(s) for s in S])


calculate(n,m,arr)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import sys

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

Submission status: Wrong Answer

Solution code:
import itertools
import math
import sys

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

Submission status: Wrong Answer

Solution code:
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""
入力Pを何個かの塊にわかる
塊の中では1個の駒による操作が行われる、
1回にとなりの1ますに歯科移動できない
すなわちそのなかでの最大と最小の差の絶対値

"""
#solution:
#潜 影 蛇 手 ! !
N,M=pin()
P=sorted(list(pin()))#大小関係を整理する
print("*",P)
PP=[abs(P[j+1]-P[j]) for j in range(M-1)]
print(PP)
Q=sorted(PP)
print(Q)
if N>=M:#この場合必ず1個の拠点に1個以上のコマが置けるのでこのままでいい
    print(0)
else:
    for i in range(N-1):
        Q.pop()
    print(sum(Q))

Submission status: Wrong Answer

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

ans = 0

if not l:
    pass
else:
    ans = sum(l[:n+1])

print(ans)

Submission status: Wrong Answer

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

del x[m:]
x.sort()
d = []

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

d.sort()

print(sum(d[:(m-n)]))

Submission status: Wrong Answer

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

del x[m:]
x.sort()
d = []

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

d.sort()
del d[:(m-n)]

print(sum(d))

Submission status: Wrong Answer

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

del x[m:]
x.sort()
d = []

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

d.sort()
del d[-(n-1):]

print(sum(d))

Submission status: Wrong Answer

Solution code:
N_koma, M_visit = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
diff = []

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

diff.sort()

if (N_koma >= M_visit):
    print(0)
    exit()

answer = sum(diff[: -(N_koma - 1)])
print(answer)

Submission status: Wrong Answer

Solution code:
N_koma, M_visit = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
diff = []

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

diff.sort()

if (N_koma >= M_visit):
    print(0)
    exit()

answer = sum(diff[: -(N_koma - 1)])
print(answer)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
l = sorted(list(map(int,input().split())))
sub = [0]*(m-1)
for i in range(m-1):
  sub[i] = l[i+1] - l[i]
sub.sort()
a = 0
a = sum(sub[0:m-n])
print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
f = lambda:map(int,input().split())
n,m = f()
l = sorted(f())
m = sorted(l[i]-l[i-1] for i in range(1,m))
print(sum(m[:-n+1]))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
X = sorted(map(int,input().split()))
D = []

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

print(sum(sorted(D)[:len(D)-n+1]))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
X = sorted(map(int,input().split()))
D = []

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

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
M,N=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
l=[None]*(N-1)
for i in range(N-1):
    l[i]=X[i+1]-X[i]
l.sort()
print(sum(l[:N-M]))

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)
    exit()

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

l=sorted(l)
print(sum(l)-sum(l[-(n-1):]))

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)
    exit()

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

l=sorted(l)
print(sum(l)-sum(l[:-(n-1)]))

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)
    exit()

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

l=sorted(l)
print(sum(l[:-(n-1)]))

Submission status: Wrong Answer

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

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

l=sorted(l)
print(sum(l[:-(n-1)]))

Submission status: Wrong Answer

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

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

X = sorted(X)
print(X)
d = [X[i + 1] - X[i] for i in range(len(X) - 1)]
d = sorted(d)
print('d', d)
if N == 1:
    print(sum(d))
else:
    print(sum(d[:-N + 1]))

Submission status: Wrong Answer

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

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

X = sorted(X)
# print(X)
d = [X[i + 1] - X[i] for i in range(len(X) - 1)]
d = sorted(d)
# print('d', d)
print(sum(d[:-N + 1]))

Submission status: Wrong Answer

Solution code:
def solve(n, m, X):
    X.sort()
    distances = sorted(X[i] - X[i - 1] for i in range(1, m))
    return sum(distances[:m - n])

_n, _m = map(int, input().split())
_X = list(map(int, input().split()))
print(solve(_n, _m, _X))

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:
  d=[]
  for i in range(m-1):
    d.append(x[i+1]-x[i])
  d=sorted(d)
  print(sum(d[:n+1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
# 差分の大きい値（移動距離が長い）は駒の数-1でショートカットできる
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)

ans = 0
if K-N >= 0:
  ans = sum(X[:-(N-1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
# 差分の大きい値（移動距離が長い）は駒の数-1でショートカットできる
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)

ans = 0
if N<K:
  ans = sum(X[:-(N-1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
# 差分の大きい値（移動距離が長い）は駒の数-1でショートカットできる
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
X = np.sort(X)
X = np.diff(X)
X = np.sort(X)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_range = []
cut = [0]
count = 0
for i in range(m-1):
    x_range.append(abs(x[i]-x[i+1]))
h = heapq.nlargest(n-1,x_range)


for i in h:
    cut.append(x_range.index(i)+1)
cut.append(len(x))
print(1)
'''
for i in range(len(cut)-1):
    sum_range = x[cut[i]:cut[i+1]]
    count += abs(sum_range[0]-sum_range[len(sum_range)-1])
print(count)
'''

Submission status: Wrong Answer

Solution code:
import heapq
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_range = []
cut = [0]
count = 0
for i in range(m-1):
    x_range.append(abs(x[i]-x[i+1]))
h = heapq.nlargest(n-1,x_range)
print(1)
'''
for i in h:
    cut.append(x_range.index(i)+1)
cut.append(len(x))
for i in range(len(cut)-1):
    sum_range = x[cut[i]:cut[i+1]]
    count += abs(sum_range[0]-sum_range[len(sum_range)-1])
print(count)
'''

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# わからない

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, m = na()
x = na()

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

Submission status: Wrong Answer

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, m = na()
x = na()

d = [x[i] - x[i - 1] for i in range(1, m)]

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
x = sorted(set(map(int, input().split())))
t = []
for i, j in zip(x, x[1:]):
    t += [i - j]
t = sorted(t, reverse=True)
print(sum(t[n-1:]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

    X = sorted(X)

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

    kukan = sorted(kukan, reverse=True)

    jogai = 0
    for j in range(N-1):
        jogai += kukan[j]

    print(X[M-1] - X[0] - jogai)

    print(kukan)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

li.sort()
print(sum(li[:M-(N//2)-N%2-1]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

diffs.sort()

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

Submission status: Wrong Answer

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


def main():
    N, M = [int(x) for x in input().split()]
    X = [int(x) for x in input().split()]
    X.sort()
    Y = []
    for i in range(M - 1):
        Y.append(X[i + 1] - X[i])
    Y.sort()

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










if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    N, M = [int(x) for x in input().split()]
    X = [int(x) for x in input().split()]
    X.sort()
    Y = []
    for i in range(M - 1):
        Y.append(X[i + 1] - X[i])
    Y.sort()

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









if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

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

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

x_diff = np.diff(x)
x_diff_sort = sorted(x_diff)

ans = sum(x_diff_sort[: 1 - n])
# print(x)
# print(np.diff(x), x_diff_sort, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

x_diff = np.diff(x)
x_diff_sort = sorted(x_diff)

ans = sum(x_diff_sort[: 1 - n])
# print(x)
# print(np.diff(x), x_diff_sort, ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

N,M= [int(i) for i in input().split()]
X=sorted([int(i) for i in input().split()])
width=abs(X[-1]-X[0])
X=np.array(X)

W=math.ceil(width/N)
m=X[0]
M=X[-1]
ans=0

if N>=M:
    print(0)
else:
    for i in range(1,N+1):
        x=X[X<m+W*i]
        X=X[X>=m+W*i]
        try:
            ans+=abs(max(list(x))-min(list(x)))
        except:
            continue
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,M= [int(i) for i in input().split()]
X=sorted([int(i) for i in input().split()])
width=abs(X[-1]-X[0])
X=np.array(X)

W=width//N
m=X[0]
M=X[-1]
ans=0
count=1

if N>=M:
    print(0)
else:
    while M>=m+W*count:
        x=X[X<m+W*count]
        X=X[X>=m+W*count]
        count+=1
        try:
            ans+=abs(max(list(x))-min(list(x)))
        except:
            continue
    print(ans)

Submission status: Wrong Answer

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

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

D = D[:-1 * (N - 1)]

print(sum(D))

Submission status: Wrong Answer

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

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

print(X)
X.sort()
X = np.diff(X)
X.sort()
if n >= m:
    print(0)
else:
    print(X[:m-n].sum())

Submission status: Wrong Answer

Solution code:
def p_c():
    n, m = map(int, input().split())
    *X, = map(int, input().split())
    X.sort()
    L = [abs(X[i] - X[i + 1]) for i in range(m - 1)]
    L.sort()
    print(sum(L[:len(L) - n + 1]))
    
p_c()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
s = (abs(max(x) - min(x))) / n
a = x[0]
ans = 0
cnt = 1
for i in range(1, m):
    if x[i] + s >= x[m - 1] and 2 <= cnt <= n:
        s = (x[m - 1] - a) / (n - cnt)

    if x[i] >= a + s and cnt <= n:
        ans += x[i - 1] - a
        a = x[i]
        cnt += 1

    if cnt == n:
        ans += x[m - 1] - a
        break

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split()) 
X_lines = str(input())
X_lines = X_lines.split()
X = []

if N >= M:
    print(str(0))  
else:
    for i in range(len(X_lines)):
        X.append(int(X_lines[i]))

    X_arrays = X
    X_arrays.sort()      
    X_max = X_arrays[len(X_arrays)-1]
    X_min = X_arrays[0]
    X_kyokai = [X_min]
    if not N == 1:
        for i in range(N-1):
            X_kyokai.append((1/N)*((i+1)*X_max +(N-i-1)*X_min))
    X_kyokai.append(X_max)
    
    X_all_lines = [[]]
    if not len(X_kyokai)-2 == 0:
        for i in range(len(X_kyokai)-2):
            X_all_lines.append([])
    for i in range(len(X_kyokai)-1):
        for j in range(M):
            if X_kyokai[i] <= X_arrays[j] and X_arrays[j] < X_kyokai[i+1]:
                X_all_lines[i].append(X_arrays[j])
    X_all_lines[len(X_kyokai)-2].append(X_max) 
    
    sum_dif = 0
    count = 0
    for i in range(len(X_all_lines)):
        if len(X_all_lines[i]) == 0:
            count = count + 1
        else:
            i_max = X_all_lines[i][len(X_all_lines[i])-1]
            i_min = X_all_lines[i][0]
            dif = i_max - i_min
            sum_dif = sum_dif + dif
    
    diff_arrays = []
    
    for i in range(N):
        if len(X_all_lines[i]) >= 2:
            for j in range(len(X_all_lines[i])-1):
                diff_arrays.append(X_all_lines[i][j+1]-X_all_lines[i][j])
                
    diff_arrays.sort()
    diff_arrays.reverse()
    
    if count >= 1:
        for i in range(count):
            sum_dif = sum_dif - diff_arrays[i]
    
    print(str(sum_dif))

Submission status: Wrong Answer

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

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

ans = 0
for i in range(0, len(xs)):
  if m - i <= n:
    break
  ans += xs[i]
print(ans)

Submission status: Wrong Answer

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

X = sorted(list(map(int,input().split())))
difX = [[0,0]] + [[X[i+1]-X[i],i] for i in range(M-1)]
difX.sort(reverse=True)
idxs = [0] + [i+1 for difval, i in difX[:N-1]] + [M]
ans1 = 0
for i in range(len(idxs)-1):
  ans1 += X[idxs[i+1]-1] - X[idxs[i]] 


revdifX = [[X[i]-X[i-1],i] for i in range(M-1,0,-1)] + [[0,0]]
revdifX.sort(reverse=True)
revidxs = [0] + [i+1 for difval, i in difX[:N-1]] + [M-1]
ans2 = 0
for i in range(len(revidxs)-1,0,-1):
  ans2 += X[revidxs[i]] - X[revidxs[i-1]] 
print(min(ans1,ans2))

Submission status: Wrong Answer

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

X = sorted(list(map(int,input().split())))
difX = [[0,0]] + [[X[i+1]-X[i],i] for i in range(M-1)]
difX.sort(reverse=True)
idxs = [0] + [i+1 for difval, i in difX[:N-1]] + [M]

ans = 0
for i in range(len(idxs)-1):
  ans += X[idxs[i+1]-1] - X[idxs[i]] 
print(ans)

Submission status: Wrong Answer

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

x.sort()

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

num.sort()
print(sum(num[:m-n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools

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

X = sorted(X)

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

sub = sorted(sub)
print(sum(sub[:N+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())), reverse=True)
print(sum(Xlist[:M - N]) if N < M else 0)

Submission status: Wrong Answer

Solution code:
n, m = [int(el) for el in input().split(' ')]
X = [int(el) for el in input().split(' ')]
X = sorted(set(X))

if m <= n:
	print(0)
else:
	D = [0]*(m-1)
	for i in range(0, m-1):
		D[i] = X[i+1] - X[i]
	D = sorted(D)
	print(sum(D[:-(n-1)]))

Submission status: Wrong Answer

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

diff = []
for i in range(m-1):
    diff.append(abs(x[i]-x[i+1]))
p = 0 if len(diff) < n+1 else len(diff)-n+1
sec = sorted(diff)[:p]

print(sum(sec))

Submission status: Wrong Answer

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

if m <= n:
	print(0)
else:
	D = [0]*(m-1)
	for i in range(0, m-1):
		D[i] = X[i+1] - X[i]
	D = sorted(D)
	print(sum(D[:-(n-1)]))

Submission status: Wrong Answer

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

diff = []
for i in range(m-1):
    diff.append(abs(x[i]-x[i+1]))
sec = sorted(diff)[:(len(diff)-n+1)]
print(sum(sec))

Submission status: Wrong Answer

Solution code:
n, m, *x = map(int, open(0).read().split())
if n >= m:
  print(0)
  quit()
x = sorted(set(x))
lst = [j - i for i, j in zip(x, x[1:])]
lst.sort()
s = sum(lst[:-n + 1])
print(s)

Submission status: Wrong Answer

Solution code:
n, m, *x = map(int, open(0).read().split())
if n >= m:
  print(0)
  quit()
x.sort()
lst = [j - i for i, j in zip(x, x[1:])]
lst.sort()
s = sum(lst[:-n+1])
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def examA():
    T, X = LI()
    ans = T/X
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    ans = "Yes"
    if max(L)*2>=sum(L):
        ans = "No"
    print(ans)
    return

def examC():
    N, M = LI()
    X = LI(); X.sort()
    x = []
    for i in range(M-1):
        x.append(X[i+1]-X[i])
    x.sort()
    if N>=M:
        ans = 0
    else:
        ans = sum(x[:-N+1])
    print(ans)
    return

def examD():
    N, K = LI()
    A = LI()
    d = defaultdict(int)
    maxl = len(bin(K)) -2
    for a in A:
        for j in range(maxl):
            if a&1==1:
                d[j] +=1
            a >>=1
            if a==0:
                break
#    print(d)
    X = 0
    cur = []
    for i in range(maxl):
        c = (2**i)*(N-d[i]*2)
        cur.append([c,i])
    cur.sort()
    ans = sum(A)
#    print(maxl,X)
    ansC = 0
    for i in cur:
        if i[1]!=maxl:
            ansC +=max(i[0],0)
    ans +=ansC

    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

Solution code:
def examA():
    T, X = LI()
    ans = T/X
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    ans = "Yes"
    if max(L)*2>=sum(L):
        ans = "No"
    print(ans)
    return

def examC():
    N, M = LI()
    X = LI(); X.sort()
    x = []
    for i in range(M-1):
        x.append(X[i+1]-X[i])
    x.sort()
    ans = sum(x[:-N+1])
    print(ans)
    return

def examD():
    N, K = LI()
    A = LI()
    d = defaultdict(int)
    maxl = len(bin(K)) -2
    for a in A:
        for j in range(maxl):
            if a&1==1:
                d[j] +=1
            a >>=1
            if a==0:
                break
#    print(d)
    X = 0
    cur = []
    for i in range(maxl):
        c = (2**i)*(N-d[i]*2)
        cur.append([c,i])
    cur.sort()
    ans = sum(A)
#    print(maxl,X)
    ansC = 0
    for i in cur:
        if i[1]!=maxl:
            ansC +=max(i[0],0)
    ans +=ansC

    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()