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=map(int,input().split())
x=sorted(list(map(int,input().split())))
b=[]
for i in range(m-1):
  b.append(x[i+1]-x[i])
b=sorted(b)
print(sum(b[:-n+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


main()

Submission status: Wrong Answer

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

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


main()

Submission status: Wrong Answer

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

X = [j for j in map(int, input().split())]
X.sort()
s = max(X)-min(X)
d = [X[i+1]-X[i] for i in range(M-1)]
d.sort()

# print(s)
# print(X)
# print(d)
# print(d[-N+1:])
print(s-sum(d[-N+1:]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    xs = list(map(int, input().split()))
    xs = sorted(xs)
    diff_xs = sorted([xs[i+1] - xs[i] for i in range(m-1)])
    if len(diff_xs) == 1:
        ans = diff_xs[0]
    else:
        ans = sum(diff_xs[:-(n-1)])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff = set(diff[:N-1])


if N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    X = sorted(map(int, input().split()))
    D = [0]*(M-1)
    for i in range(M-1):
        D[i] = X[i + 1] - X[i]
    D.sort()
    ans = sum(D[:M-N])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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()
print(sum(Y[:M-N]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, m, *x = map(int, open(0).read().split())
x.sort()
a = []
for i in range(m-1):
    a += [x[i+1]-x[i]]
a.sort()
ans = sum(a[:m-n])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
n, m = map(int, input().split())
if n >= m:
    print(0)
    sys.exit()
x = [int(i) for i in input().split()]
x = np.array(sorted(x))
diff = np.abs(np.diff(x))
diff = sorted(diff)
print(sum(diff[:-n + 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
X = sorted(list(map(int, input().split())))
A = []
for a, b in zip(X, X[1:]):
	A.append(b - a)
A.sort()
print(sum(A[:m-n]))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
X = sorted(list(map(int, input().split())))
A = []
for a, b in zip(X, X[1:]):
	A.append(b - a - 1)
A.sort(reverse = True)
print(X[-1] - X[0] + 1 - sum(A[:n-1]) - n if m != 1 else 0)

Submission status: Wrong Answer

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

for i in range(len(X)-1):
    index.append([i,X[i+1]-X[i]])
index.sort(key=lambda x: x[1],reverse=True)
j = 0
ans = 0
if N<M:
    for i in range(N-1):
        ans += abs(X[index[i][0]]-X[j])
        j = index[i][0]+1
    ans += X[-1]-X[j]

print(ans)

Submission status: Wrong Answer

Solution code:
# 隣との距離が大きいところで担当区間を区切るのがお得。
# 各担当区間では行ったり来たりせずに端から端まで動くのがお得。

n, m = map(int, input().strip().split())
xs = sorted(list(map(int, input().strip().split())))

intervals = sorted([(xs[i + 1] - xs[i], i) for i in range(m - 1)], reverse=True)
divides = sorted([ind for _, ind in intervals[:n - 1]])

if n >= m:
    print(0)
else:
    length_of_intervals = []
    for i in range(len(divides) + 1):
        current = divides[i] if i < len(divides) else m
        prev = divides[i - 1] if i > 0 else 0
        length_of_intervals.append(current - prev) # 最初の1個は配置時点で到達している

    ans = sum(length_of_intervals)

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

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

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

Submission status: Wrong Answer

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

ans = 0

if N >= M:
  pass
else:
  X.sort()
  dist = []
  
  for i in range(1,M):
    dist.append(abs(X[i]-X[i-1]))
  dist.sort()
  ans = sum(dist[:-N+1])

print(ans)

Submission status: Wrong Answer

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

lx = sorted(list(map(int, input().split())))

ld = sorted([lx[i+1]-lx[i] for i in range(m-1)])
if n == 1:
    print(sum(ld))
else:
    print(sum(ld[:-(n-1)]))
print(lx, ld)

Submission status: Wrong Answer

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

lx = sorted(list(map(int, input().split())))

ld = sorted([lx[i+1]-lx[i] for i in range(m-1)])
print(sum(ld[:-(n-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
X = sorted(map(int, input().split()))
diff = []
for x, y in zip(X, X[1:]):
    diff.append(y-x)
diff.sort()
print(sum(diff[:m-n]))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#import numpy as np
#from decimal import *

N, M = MAP()
X = LIST()
X.sort()
#print(X)
Y = [X[i]-X[i-1] for i in range(1, M)]
Y.sort()
#print(Y)
print(sum(Y[:M-N]))

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(m-1)]
l.sort()
ans = sum(l[:m-1-(n-1)])
print(ans)

Submission status: Wrong Answer

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

D = [abs(X[i] - X[i+1]) for i in range(M-1)]
D.sort(reverse=True)

print(sum(D[N:]))

Submission status: Wrong Answer

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

D = [abs(X[i] - X[i+1]) for i in range(M-1)]
D.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
X=sorted(set(list(map(int,input().split()))))
Y=[]
for i in range(M-1):
    y=X[i+1]-X[i]
    Y.append(y)
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())))
Y=[]
for i in range(M-1):
    y=X[i+1]-X[i]
    Y.append(y)
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())))

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

Submission status: Wrong Answer

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

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

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

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

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:
    d = [0]*(m-1)
    for i in range(m-1):
        d[i] = x[i+1] - x[i]
    d.sort(reverse=True)
    if m%2 == 0:
        e = m-n-1
    else:
        e = m-n-2
    print(sum(d) - sum(d[0:e]))

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:
    d = [0]*(m-1)
    for i in range(m-1):
        d[i] = x[i+1] - x[i]
    d.sort(reverse=True)
    if m-n <= 2:
        print(sum(d) - d[0])
    else:
        print(sum(d) - sum(d[0:m-n-2]))

Submission status: Wrong Answer

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

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

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

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:
    d = [0]*(m-1)
    for i in range(m-1):
        d[i] = x[i+1] - x[i]
    d.sort(reverse=True)
    print(sum(d) - sum(d[0:m-n-2]))

Submission status: Wrong Answer

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

X.sort()
ans = 0
starts = []*(N-1)
dist = []
starts.append(0)

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

for i in range(N-1):
    high = 0
    elem = 0
    for j in range(len(dist)):
        if high < dist[j]:
            high = dist[j]
            dist[j] = 0
            elem = j
    starts.append(elem+1)
starts.append(len(X))

for i in range(N):
    root = X[starts[i]:starts[i+1]]
    for j in range(len(root)-1):
        while root[j] != root[j+1]:
            root[j] += 1
            ans += 1
print(ans)

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()
#print(A)
#print(B)   

if N >=M:
  ans = 0
else:
  ans = sum(B[:-(N-1)])
print(ans)
           
#dp[i][j]: i個目までにj

Submission status: Wrong Answer

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

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

sa = []

for i in range(M-1):
  a = List[i+1] - List[i]
  sa.append(a)
  
sa.sort()
l = len(sa)

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

Submission status: Wrong Answer

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

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

sa = []

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

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

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,m=map(int, input().split())
    l=list(map(int,input().split()))
    l.sort()
    if n==1 and m>=2:
        print(l[-1]-l[0])
    elif n==1 and m==1:
        print(0)
    else:
        aida=[]
        for i in range(m-1):
            aida.append(l[i+1]-l[i])
        aida.sort()
        print(sum(aida[:m-n]))
resolve()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,m=map(int, input().split())
    l=list(map(int,input().split()))
    l.sort()
    if n==1 and m>=3:
        print(min(l[-2]-l[0],l[-1]-l[1]))
    elif n==1 and m==2:
        print(l[1]-l[0])
    elif n==1 and m==1:
        print(0)
    else:
        aida=[]
        for i in range(m-1):
            aida.append(l[i+1]-l[i])
        aida.sort()
        print(sum(aida[:m-n]))
resolve()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,m=map(int, input().split())
    l=list(map(int,input().split()))
    l.sort()
    aida=[]
    for i in range(m-1):
        aida.append(l[i+1]-l[i])
    aida.sort()
    print(sum(aida[:m-n]))
resolve()

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]
Y = sorted(Y)
print(sum(Y[:-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(M-1)]
L.sort()
print(sum(L[:(M-1)-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X.sort()
K = sorted(
    [
        abs(X[i] - X[i - 1])
        for i in range(1, M)
    ],
    reverse=True
)

print(K)

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

Submission status: Wrong Answer

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

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

print(K)

print(sum(K[:M - (N - 1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
# from collections import deque
# import bisect
# import math
# import itertools
# from queue import deque
# from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7


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


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N, M = inp_list()
X = inp_list()
X.sort()
# print(X)

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

d = [X[i + 1] - X[i] for i in range(M - 1)]
# for i in range(M - 1): d.append(X[i + 1] - X[i])
# print(d)
d.sort()
ans = sum(d[: - (N - 1)])
# ans = 0
# for i in range(M - 1 -  (N - 1)):
    # ans += d[i]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
# from collections import deque
# import bisect
# import math
# import itertools
# from queue import deque
# from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7


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


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N, M = inp_list()
X = inp_list()
X.sort()
# print(X)

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

d = []
for i in range(M - 1): d.append(X[i + 1] - X[i])
# print(d)
d.sort()
ans = sum(d[: - (N - 1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
# from collections import deque
# import bisect
# import math
# import itertools
# from queue import deque
# from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7


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


def inp_list(): return list(map(int, sys.stdin.readline().split()))


N, M = inp_list()
X = inp_list()
X.sort()
# print(X)

d = []
for i in range(M - 1): d.append(X[i + 1] - X[i])
# print(d)
d.sort()
ans = sum(d[: - (N - 1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	n, m = tin()
	al = lin()
	#s = input()
	al.sort()
	diff_al = [vj-vi for vi, vj in zip(al, al[1:])]
	#pa(al)
	#pa(diff_al)
	diff_al.sort()
	#pa(diff_al)
	ret = sum(diff_al[:-(n-1)])
	print(ret)
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

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

dX = [X[i+1]-X[i] for i in range(M-1)]
dX.sort()
p = N - 1
print(sum(dX[:-p]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

l = len(res)
k = n - 1

print(sum(res[:l - k]))

Submission status: Wrong Answer

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

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

k = n - 1
print(sum(res[:-k]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(' '))
if N >= M:
    print(0)
else:
    points = sorted(tuple(map(int, input().split(' '))))
    distances = sorted([abs(p1 - p2) for p1, p2 in zip(points, points[1:])])
    print(sum(distances[:N + 1]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(' '))
if N >= M:
    print(0)
else:
    points = sorted(tuple(map(int, input().split(' '))))
    distances = sorted([abs(p1 - p2) for p1, p2 in zip(points, points[1:])])
    print(sum(distances[:-N + 1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# sys.stdin.readline()
import numpy as np

import math

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

Submission status: Wrong Answer

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

if (M <= N):
    print(0)
else:
    Xs = list(map(int, input().split()))
    Xs.sort()
    W = Xs[-1] - Xs[0]
    Ds = [Xs[i+1]-Xs[i] for i in range(M-1)]
    Ds.sort(reverse=True)

    gaps = 0
    print(Ds)
    for i in range(N-1):
        gaps += Ds[i]

    print(W-gaps)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
xxx = list(map(int, input().split()))
 
#どうでもよいとき
if n >= m:
    print(0)
    exit()
xxx.sort()
ddd = [b - a for a, b in zip(xxx, xxx[1:])]
ddd.sort()#M-1個の距離
print(1)

Submission status: Wrong Answer

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

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

T.sort()
diff = []
for i in range(1,m):
    diff.append(T[i]-T[i-1])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if M == 1:
    print(0)
else:
    l.sort()
    l_dist =[]

    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    ans = sum(l_dist[:len(l_dist) - (N-1)])

    print(ans)

Submission status: Wrong Answer

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

l.sort()

l_dist =[]

for i in range(1, M):
    l_dist.append(abs(l[i]-l[i-1]))

print(sum(sorted(l_dist)[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import itertools
import math

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

x.sort()

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

dis.sort(reverse=True)
for i in range(len(dis)):
    dis[i] = 0

print(sum(dis))

Submission status: Wrong Answer

Solution code:
import itertools
import math

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

x.sort()
print(x)

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

dis.sort(reverse=True)
for i in range(len(dis)):
    dis[i] = 0

print(sum(dis))

Submission status: Wrong Answer

Solution code:
l = [int(x) for x in  input().split(" ")]
N,M = l[0],l[1]
l = [int(x) for x in input().split(' ')]
l2 = []
l.sort()
pre = l[0]
for i in range(1,len(l)):
  l2.append(l[i]-pre)
  pre = l[i]
l2.sort()
sum_ = 0
if M<= N:
  print(0)
for i in range(M-N):
  sum_+=l2[i]
print(sum_)

Submission status: Wrong Answer

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


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

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

distances = sorted(X[i+1]-X[i] for i in range(m-1))
print(sum(distances[:min(-n+1, -1)]))

Submission status: Wrong Answer

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


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

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

distances = sorted(X[i+1]-X[i] for i in range(m-1))
print(distances)
print(sum(distances[:min(-n+1, -1)]))

Submission status: Wrong Answer

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


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

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

distances = sorted(X[i+1]-X[i] for i in range(m-1))
print(sum(distances[:-n+1]))

Submission status: Wrong Answer

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


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

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

distances = sorted(X[i+1]-X[i] for i in range(m-1))

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

Submission status: Wrong Answer

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

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

distances = sorted((X[i+1]-X[i], i, i+1) for i in range(m-1))

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

ans = 0
distances = distances[-n+1:]
distances.sort(key=lambda x: x[1])

start = 0
for _, l, r in distances:
    ans += X[l] - X[start]
    start = r

if start < r:
    ans += X[-1] - X[start]

print(ans)

Submission status: Wrong Answer

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

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

distances = sorted((X[i+1]-X[i], i, i+1) for i in range(m-1))

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

ans = 0
distances = distances[-n+1:]
distances.sort(key=lambda x: x[1])

start = 0
for _, l, r in distances:
    ans += X[l] - X[start]
    start = r

ans += X[-1] - X[start]

print(ans)

Submission status: Wrong Answer

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

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

distances = sorted((X[i+1]-X[i], i, i+1) for i in range(m-1))

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

ans = 0
distances = distances[-n+1:]
distances.sort(key=lambda x: x[1])

start = 0
for _, l, r in distances:
    ans = X[l] - X[start]
    start = r
print(ans)