Problem:


Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
if n>=m:
    lists=list(map(int,input().split()))
    print(0)
elif n<m:
        lists=list(map(int,input().split()))
        lists=sorted(lists)
        dislist=[0, 0]
        for i in range(m-1):
            dislist.append(lists[i+1]-lists[i])
        dislist=sorted(dislist)
        if n!=1:
            print(sum(dislist[:-(n-1)]))
        else:
            print(sum(dislist))

Submission status: Accepted

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

locs.sort()
 
difLocs = []
first = locs[0]
for bot,top in zip(locs, locs[1:]):
  if top == first:
    break
  difLocs.append(top-bot)
 
difLocs.sort()
if (m-n) < 0:
  print(0)
else:
  newLocs = difLocs[0:m-n]
  print(sum(newLocs))

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
locs = [int(x) for x in input().split()]
locs.sort()
 
difLocs = []
first = locs[0]
for bot,top in zip(locs, locs[1:]):
  if top == first:
    break
  difLocs.append(top-bot)
 
difLocs.sort()
if (m-n) < 0:
  print(0)
else:
  newLocs = difLocs[0:m-n]
  print(sum(newLocs))

Submission status: Accepted

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

    # 目的地よりコマが多いなら操作は不要
    if N >= M:
        print(0)
        return

    Xs.sort()
    distance_list = []
    for i in range(M):
        if i == M-1:
            break
        distance = abs(Xs[i] - Xs[i+1])
        distance_list.append(distance)
    distance_list.sort(reverse=True)

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

    print(sum(distance_list))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

gap.sort()
print(sum(gap if N == 1 else gap[:-1 * (N - 1)]))

Submission status: Accepted

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

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    Diff = []
    for i in range(len(Data)-1):
        Diff.append(Data[i+1]-Data[i])
    Diff.sort()
    ans = 0
    for i in range((M-1)-(N-1)):
        ans += Diff[i]
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

dis_X = sorted(dis_X, reverse=True)
ans = 0
for i in range(N-1, len(dis_X)):
    ans += dis_X[i]

print(ans)

Submission status: Accepted

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums2.sort()
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:len(nums3)-n+1]
if(n>=m):
  print(0)
else:
  print(sum(nums3))

Submission status: Accepted

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

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

diff.sort()

indexes = [x[1] for x in diff[-1:-min(n,m):-1]]

final_index = [0]
for i in indexes:
	final_index.append(i)
	final_index.append(i-1)

final_index.append(len(coords)-1)
final_index.sort()

total = 0
for i in range(1,len(final_index),2):
	total += coords[final_index[i]]-coords[final_index[i-1]]

print(total)

Submission status: Accepted

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

for i in range(M - 1):
	Y.append(X[i + 1] - X[i])
Y = sorted(Y, reverse = True)

print(max(X) - min(X) - sum(Y[0 : N - 1]))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = sorted(list(map(int, input().split())))
    if len(X) <= N:
        print(0)
        exit()

    diff = sorted([X[idx + 1] - x for idx, x in enumerate(X[:-1])], reverse=True)
    print(sum(diff[N - 1:]))

Submission status: Accepted

Solution code:
import sys
n,m=map(int,input().split())
if m<=n:
  print(0)
  sys.exit()
x=sorted(list(map(int,input().split())))
l=[0]*(m-1)
for i in range(m-1):
  l[i]=x[i+1]-x[i]
l.sort()
s=0
for i in range(n-1):
  s+=l[m-2-i]
print(x[m-1]-x[0]-s)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
maxdi = 0
if M-N <= 0:
    print(0)
    exit()
dis = []
for i in range(M-1):
    dis.append(X[i+1]-X[i])
alldi = max(X)-min(X)
dis.sort(reverse=True)
partdi = sum(dis[:N-1])
print(alldi-partdi)

Submission status: Accepted

Solution code:
# ABC117C - Streamline
def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    dist = [j - i for i, j in zip(X, X[1:])]
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC117C - Streamline
def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    dist = []
    for i, j in zip(X, X[1:]):
        dist.append(j - i)
    dist.sort(reverse=1)
    ans = sum(dist[N - 1 :])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

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

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

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

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

Submission status: Accepted

Solution code:
import heapq
N , M = map(int,input().split())
X = list(map(int,input().split()))
X.sort(reverse=True)
diff = []
heapq.heapify(diff)
for i in range(len(X) - 1):
    heapq.heappush(diff , -1 * (X[i] - X[i + 1]))
for _ in range(N - 1):
    if not diff:
        print("0")
        exit()
    else:
        heapq.heappop(diff)

print(-1 * sum(diff))

Submission status: Accepted

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

if n >= m:
  print(0)
  sys.exit()
  
x = sorted(list(map(int,input().split())))
print(sum(sorted([x[i+1]-x[i] for i in range(m-1)],reverse = True)[n-1:]))

Submission status: Accepted

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

if n >= m:
  print(0)
  sys.exit()
  
x = list(map(int,input().split()))
x.sort()
between = [x[i+1]-x[i] for i in range(len(x)-1)]
between.sort(reverse = True)
print(sum(between[n-1:]))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(Xd[:max(M-N, 0)]))

Submission status: Accepted

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

Submission status: Accepted

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

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

x.sort()
y = x[1::]
dlt = [i-j for (i,j) in zip(y,x)]
dlt.sort()
if n==1:
  print(sum(dlt))
else:
  print(sum(dlt[0:-1*n+1]))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
li = list(map(int,input().split()))
li2 = []
li.sort()
for i in range(m-1):
    li2.append(abs(li[i]-li[i+1]))
li2.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readlines()
N, M = map(int, input[0].strip().split(' '))
Xs = list(map(int, input[1].strip().split(' ')))
s = sorted(Xs)
gaps = [s[i+1]-s[i] for i in range(len(s)-1)]
sgaps = sorted(gaps)
if N>=M:
    print(0)
else:
    print(sum(sgaps[:(M-N)]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import heapq

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

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

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

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

heapq.heapify(l)

for _ in range(N-1):
    heapq.heappop(l)

print(-sum(l))

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    for _ in range(n-1): dis.pop(-1)
    print(sum(dis))

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    ans = dis[::-1]
    for _ in range(n-1): ans.pop(0)
    print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x.sort()

if n>=m:
    print(0)
    exit()
sa=[]
for i in range(m-1):
    sa.append(x[i+1]-x[i])
sa.sort(reverse=True)
ans=x[-1]-x[0]
for i in range(n-1):
    ans-=sa[i]
print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter

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

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

# 目的地を小さい順に確認し、目的地間の区間の長さが長い所の両端に駒を置く
# 最小手数は全区間の長さから駒が移動しない区間の長さの和を除いたもの

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

# 大きい方からN-1個の区間はコマで移動しない
dist_between_destination.sort(reverse = True)

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

Submission status: Accepted

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
X = [int(x) for x in stdin.readline().rstrip().split()]
X.sort()
if N >= M:
    print('0')
else:
    r = []
    for i in range(M - 1):
        r.append(X[i + 1]-X[i])
    r.sort()
    if N != 1:
        print(sum(r[:-N+1]))
    else:
        print(sum(r))

Submission status: Accepted

Solution code:
import sys
N,M=map(int,input().split())
L=[i for i in map(int,input().split())]
D=[]
if N >= M or M==1 :
    print(0)
    sys.exit()

L.sort()
for i in range(0,M-1) :
    D.append(abs(L[i]-L[i+1]))
D.sort(reverse=True)
print(sum(D[N-1:]))

Submission status: Accepted

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

dis=[]

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

for i in range(m-1):
    dis.append(x[i+1]-x[i])
# x_i+1 と x_iの距離の最小値を取っていく
# distanceをソートして、小さいものからm-n-1個得る
dis.sort()

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
minX = X[0]
maxX = X[-1]
ans = maxX-minX
Y = [X[i+1] -X[i] for i in range(M-1)]
Y.sort()

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    if n >= m:
        print(0)
        exit()
    minimum = x[0]
    maximum = x[-1]
    if n == 1:
        print(maximum-minimum)
        exit()
    arr = []
    for i in range(len(x)-1):
        arr.append(abs(x[i+1]-x[i]))

    arr.sort()

    print(maximum-minimum-sum(arr[-(n-1):]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    # ABC117c
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    if(m == 1):
        print(0)
        exit(0)
    if(n >= m):
        print(0)
        exit(0)
    x.sort()
    sa = [0]
    for i in range(1, m):
        sa.append(x[i]-x[i-1])
    # print(x)
    # print(sa)
    # print('aa')
    sa.sort()
    # or _ in range(min(n-1, len(sa)-1)):
    #    sa = sa[:-1]
    # print(sa)
    print(sum(sa[:len(sa)-min(n-1, len(sa)-1)]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
trg = list(map(int,input().split()))
trg.sort()
dist = []
if m ==1:
    print(0)
    exit()
for i in range(1,m):
    dist.append(trg[i] - trg[i-1])
dist.sort()

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

Submission status: Accepted

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

Y.sort(reverse=False)

if M - N <= 0:
    
    print(sum(Y[:0]))

else:

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

Submission status: Accepted

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

Submission status: Accepted

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

s = sorted(X)
dist = [s[n]-s[n-1] for n in range(1,len(s))]
dist = sorted(dist)[:M-N]
if N >= M:
  print(0)
else:
  res = 0
  for n in range(len(dist)):
    res += dist[n]
  print(res)

Submission status: Accepted

Solution code:

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

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

a.sort()

list = [i for i in range(len(a) - 1)]

for nn,value in enumerate(a):
    if nn == 0:
        diff = value
    else:
        list[nn-1] = value - diff
        diff = value

list.sort(reverse=True)
count = min(n-1, len(list))
for dl in range(count):
    list[dl] = 0

print(sum(list))

Submission status: Accepted

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    # N = int(input())
    N,M = map(int, input().split())
    XS = list(map(int, input().split()))
    XS.sort()
    intervals = [0]*(M-1)
    for i in range(M-1):
        intervals[i] = XS[i+1]-XS[i]
    intervals.sort(reverse=True)
    print(XS[M-1]-XS[0]-sum(intervals[:min(M-1,N-1)]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
X = np.array(sorted(map(int, input().split())))

distances = sorted(X[1:] - X[:-1], reverse=True)
print(sum(distances[N-1:]))

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
X = np.array(sorted(map(int, input().split())))

distances = sorted(X[1:] - X[:-1])
print(sum(distances) if N == 1 else sum(distances[:-N+1]) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=map(int,input().split())
if n>=m:
    print(0)
else:
    y=[int(i) for i in x]
    y.sort()
    z=[]
    for h  in range(1,len(y)):
        z.append(y[h]-y[h-1])
    z.sort()
    w=z[:m-n]
    print(sum(w))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

distance = []

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

distance.sort()
if n >= m:
    print(0)
else:
    for i in range(n - 1):
        distance.pop()

    print(sum(distance))

Submission status: Accepted

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

t=0
if n<m:
  x.sort()
  s=list()
  for i in range(m-1):
    s.append(x[i+1]-x[i])
  s.sort()

  for i in range(m-n):
    t += s[i]

print(t)

Submission status: Accepted

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

if n >= m:
  print(0)
elif n == 1:
  Diffs.sort()
  print(sum(Diffs))
else:
  Diffs.sort()
  print(sum(Diffs[:-(n-1)]))

Submission status: Accepted

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

li=[]


for i in range(m-1):
  li.append(x[i+1]-x[i])
li.sort(reverse=True)

print(max(x)-min(x)-sum(li[:(n-1)]))

Submission status: Accepted

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

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

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    X.sort()
    spot = [0]*M
    diff = [[0, 0]]*(M-1)
    for i in range(M-1):
        diff[i] = [X[i+1]-X[i], i]
    diff.sort(key=lambda x: x[0])
    if N == 1:
        print(X[-1]-X[0])

    else:
        ans = X[-1] - X[0]
        for i in range(N-1):
            tmp = diff.pop()
            ans -= tmp[0]
        print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
X=sorted(map(int,input().split()))
if n>=m:
    r=0
else:
    L=[]
    for i in range(m-1):
        l=X[i+1]-X[i]
        L.append(l)
    L.sort(reverse=True)
    r=X[-1]-X[0]
    for j in range(n-1):
        r-=L[j]
print(r)

Submission status: Accepted

Solution code:
import sys
import os
import heapq
N,M = map(int,input().split())

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

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

Q = []
for i in range(M-1):
    heapq.heappush(Q,(-(X[i+1]-X[i]),i))

sum = 0
for i in range(1,N):
    sum = sum + (-heapq.heappop(Q)[0])
print(X[M-1]-X[0]-sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline

    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    x.sort()
    ls = [0, m - 1]
    d = []
    for i in range(1, len(x)):
        d.append([abs(x[i] - x[i-1]), i, i - 1])
    d.sort(reverse=True)
    for i in range(min(n-1, len(d))):
        ls.append(d[i][1])
        ls.append(d[i][2])
    ls.sort()
    t = 0
    for i in range(1, len(ls), 2):
        t += (abs(x[ls[i]] - x[ls[i-1]]))
    print(t)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

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

Submission status: Accepted

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

if M == 1:
  print (0)
  sys.exit()
Y = []
for i in range(M-1):
  Y.append(X[i+1]-X[i])
Y = sorted(Y)
for i in range(N-1):
  if len(Y) == 0:
    print (0)
    sys.exit()
  Y.pop()
print (sum(Y))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X.sort()
dist = [0]*(M-1)

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

dist.sort()
ans = 0
if N < M:
    for i in range((M-1)-(N-1)):
        ans += dist[i]

print(ans)

Submission status: Accepted

Solution code:
import heapq

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

X.sort()
dist = [0]*(M-1)

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

heapq.heapify(dist)
for i in range(N-1):
    if len(dist) == 0:
        break
    heapq.heappop(dist)

ans = 0
if len(dist) != 0:
    ans = -sum(dist)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    if n>=m:
        print(0)
        return
    X=list(map(int,input().split()))
    X.sort()
    D=[0]*(m-1)
    for i in range(m-1):
        D[i]=(i,X[i+1]-X[i])
    from operator import itemgetter
    D.sort(key=itemgetter(1),reverse=1)
    ans=0
    ppos=0
    for i in range(n-1):
        pos=D[i][0]
        ans+=X[pos]-X[ppos]
        ppos=pos+1
    print(ans+X[-1]-X[ppos])
resolve()

Submission status: Accepted

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

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

l.sort()
d = [0]*(m-1)
for i in range(m-1):
  d[i] = l[i+1] - l[i]

d.sort()

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

Submission status: Accepted

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

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

l.sort()
d = [0]*(m-1)
for i in range(m-1):
  d[i] = l[i+1] - l[i]

d.sort()

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

Submission status: Accepted

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

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

x.sort()

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

d.sort()

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

Submission status: Accepted

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

koma_dist = []

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

if n<m:
    print(sum(sorted(koma_dist)[:m-n]))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C - Streamline(ABC117)
N,M=map(int, input().split())
X=list(map(int, input().split()))
X.sort(reverse=True)
ans=X[0]-X[-1]
A=[]
if N<M:
    for i in range(M-1):
        A.append(X[i]-X[i+1])
    A.sort(reverse=True)
    ans-=sum(A[0:N-1])
else:
    ans=0
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
array = list(map(int, input().split()))
array.sort()
diff = [0] * (M-1)
for i in range(M-1):
    diff[i] = abs(array[i+1] - array[i])
diff.sort(reverse=True)
for i in range(N-1):
    if len(diff) == 0:
        break
    diff.pop(0)
print(sum(diff))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(sum(diff[:max(0, m-n)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
##############################

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

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

X.sort()

# 各コマの距離を求める
cost = [0] * (M-1)
for i in range(1, M):
    cost[i-1] = X[i] - X[i-1]

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

print(sum(cost))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if n >= m:
        print(0)
    else:
        x.sort()
        y = []
        for i, v in enumerate(x):
            if i != 0:
                y.append([abs(v - x[i-1]), i, i-1])
        y = sorted(y, key=lambda x: x[0], reverse=True)
        cnt = 0
        z = [0, m-1]
        for i in range(n-1):
            z.append(y[i][1])
            z.append(y[i][2])
        z.sort()
        for i in range(1, len(z), 2):
            cnt += abs((x[z[i]] - x[z[i-1]]))
        print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

if n>=m:
  print(0)
else:
  X.sort()
  dis=[X[i]-X[i-1] for i in range(1,m)]
  dis.sort()
  print(sum(dis[:m-n]))

Submission status: Accepted

Solution code:
import numpy as np

n,m=map(int,input().split())
x=[int(i) for i in input().split()]
if n>=m:
    print(0)
else:
    x.sort()

    dif=np.diff(x)

    dif.sort()

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

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    zan=m-n
    y = []
    res = 0
    for i in range(1,m):
        y.append(x[i]-x[i-1])
    
    y.sort()

    for i in range(0,zan):
        res +=y[i]
    
    print(res)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
x=sorted(list(map(int,input().split())))
y=[]
for i in range(M-1):
   y.append(x[i+1]-x[i])
y=sorted(y)
z=0
for i in range(M-N):
  z+=y[i]
print(z)

Submission status: Accepted

Solution code:
# print('input >>')
N, M = map(int,(input().split()))
X = list(map(int,(input().split())))
X.sort()
Xdiff = [X[i + 1] - X[i] for i in range(len(X) - 1)]
Xdiff.sort(reverse=True)
ans = (X[-1]-X[0]) - sum(Xdiff[:N-1])

# print('----------output----------')
print(ans)

Submission status: Accepted

Solution code:
# ABC117 C - Streamline
n,m = map(int,input().split())
x = list(map(int,input().split()))
l = []

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

ans = x[m-1] - x[0]
l.sort(reverse=True)

for i in range(min(n-1,m-1)):
    ans -= l[i]
print(max(ans,0))