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=list(map(int,input().split()))
if n>=m:
  print(0)
else:
  x.sort()
  delta=[x[i+1]-x[i] for i in range(len(x)-1)]
  delta.sort()
  summation=0
  for i in range(len(delta)-n+1):
    summation+=delta[i]
  print(summation)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

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

X.sort()

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

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

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
X = np.sort(list(map(int, input().split())))
D = np.sort(X[1:] - X[:-1])[::-1]
print(np.sum(D[N-1:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X.sort()

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

k = 0
if M-N > 0:
    k = M-N

print(sum(d[0:k]))

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)
    diff = []
    pre = X[0]
    for i in range(1, M):
        diff.append(X[i] - pre)
        pre = X[i]
    diff = sorted(diff)
    print(sum(diff[:M - N]))

Submission status: Accepted

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

if N >= M:
    print(0)
else:
    for i in range(M-1):
        XS[i] = X[i+1] - X[i]
    XS.sort( key = None, reverse = True)
    ans = X[-1] - X[0]
    ans -= sum( XS[:N-1])
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
x2=[]
#print(x)
for i in range(M-1):
    x2.append(x[i+1]-x[i])
x2.sort()
s=0
#print(x2)
for i in range(len(x2)-N+1):
    s+=x2[i]
print(s)

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
xlist = [int(x) for x in input().split()]
if len(xlist) == 1:
    print(0)
else:
    xlist.sort()
    difflist = []
    for i in range(len(xlist)-1):
        difflist.append(abs(xlist[i] - xlist[i + 1]))
    difflist.sort(reverse=True)
    if difflist[n - 1:] != []:
        print(sum(difflist[n - 1:]))
    else:
        print(0)

Submission status: Accepted

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

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

result = 0
for i, yi in enumerate(Y):
    if i >= N - 1:
        result += yi

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, raw_input().split())
X = map(int, raw_input().split())
X.sort()
Y = [X[i+1]-X[i] for i in range(M-1)]
Y.sort()
print sum(Y[:max(0,M-N)])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans=sum(Y)
for i in range(min(N-1,M-1)):
    ans-=Y[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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()
if n >= m:
  print(0)
else:
  print(sum(a[:m-n]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
if N >= M:
    print(0)
    exit(0)

X = list(map(int, input().split()))
X = sorted(X)
X = [x-X[0] for x in X]
diff = [x-y for x,y in zip(X[1:], X)]
# print(X)
# print(diff)
diff = sorted(diff)
sum  = sum(diff[:M-1-(N-1)])
# print("diff: ", diff,"M: ", M, ", N-1: ",  N-1)

print(sum)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split(" "))
    X = sorted(map(int, input().split(" ")))
    if N >= M:
        print(0)
        return
    subX = sorted([X[i+1] - X[i] for i in range(M-1)])
    if N == 1:
        print(sum(subX))
        return
    print(sum(subX[:-(N-1)]))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

diff = [X[i] - X[i - 1] for i in range(1, M)]
diff.sort()
ret = 0
for i in range(len(diff) - N + 1):
    ret += diff[i]
print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys


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

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

    return sum(diffs[:M - N])


N, M = map(int, input().split())
Xs = sorted(list(map(int, input().split())))
print(solve(N, M, Xs))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
a = sorted(X)
b = sorted([x - y for x, y in zip(a[1:], a)])
ans = sum(b[:(-N+1)]) if N >= 2 else sum(b)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

input = sys.stdin.readline
n, m = map(int, input().split())
x_list = list(map(int, input().split()))
x_list.sort()
dis_list = []
start = x_list[0]
for i in range(1, m):
    dis_list.append(abs(start-x_list[i]))
    start = x_list[i]
dis_list.sort(reverse=True)
ans = sum(dis_list[n-1:])
print(ans)

Submission status: Accepted

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

x=sorted(x)

diff = [0]*(m-1)
for i in range(0,m-1):
  diff[i]=x[i+1]-x[i]
#print(diff)
diff = sorted(diff,reverse=True)
#print(diff)

ans =sum(diff)
for i in range(min(n-1,m-1)):
  ans = ans - diff[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math as mt
import itertools as it
import functools as ft
import random as rnd

stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), input().split(sep)))
joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args)

N, M = stdin()
X = stdin()
X.sort()
dif = [0] * (len(X) - 1)
for i in range(len(X) - 1):
    dif[i] = X[i + 1] - X[i]

dif.sort()
ans = X[-1] - X[0] - sum(dif[-1:-N:-1])
print(0 if ans <= 0 else ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
Y = []
for a,b in zip(X,X[1:]):
    Y.append(b-a)
Y.sort()
print(sum(Y[:max(0,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)
    exit()
diff = []
for i in range(m-1):
    diff.append(X[i+1] - X[i])
diff.sort()
print(sum(diff[:(m-n)]))

Submission status: Accepted

Solution code:

def B():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    cnt = 0
    for i in range(N - 1):
        cnt += L[i]
    if L[-1] < cnt:
        print("Yes")
    else:
        print("No")
#B()

def C():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    tmp = []
    for i in range(1, len(X)):
        tmp.append(X[i] - X[i - 1])
    X.sort()
    ans = X[-1] - X[0]
    #ans -= N
    tmp.sort()
    tmp = tmp[::-1]
    if N - 1 >= len(tmp):
        print(0)
        return 0
    for i in range(N - 1):
        ans -= tmp[i]
    print(ans)
C()

Submission status: Accepted

Solution code:
# coding: utf-8
import heapq as hq
n,m=map(int,input().split())
x=sorted(list(map(int,input().split())))
a=[]
ans=x[-1]-x[0]
if n>=m:
    print(0)
    exit()
for i in range(m-1):
    hq.heappush(a,x[i]-x[i+1])
for i in range(n-1):
    ans+=hq.heappop(a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Y=sorted(Y,key=lambda y: y[0])
ans=0
for i in range(M-N):
    ans+=Y[i][0]
    
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()
    y = []
    for i in range(m-1):
        y.append(abs(x[i]-x[i+1]))
    y.sort()
    sa = m - n
    res = 0
    for i in range(sa):
        res += y[i]
    print(res)

Submission status: Accepted

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(a[i+1]-a[i])
b.sort(reverse=True)
ans = 0
for i in range(n-1,len(b)):
    ans += b[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

goal = sorted(goal)
sabun = [goal[_+1]-goal[_] for _ in range(M-1)]
sabun = sorted(sabun)

if M-N <= 0:
  print(0)
else:
  k = M-N
  print(sum(sabun[:k]))

Submission status: Accepted

Solution code:
def read():
    return int(input())


def readlist():
    return list(map(int, input().split()))


def readmap():
    return map(int, input().split())


N, M = readmap()
X = readlist()

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

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

print(ans)

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()
nums = []
for i in range(1, m):
    nums.append(X[i] - X[i-1])
nums.sort()
for i in range(n-1):
    if len(nums) == 0:
        break
    nums.pop()
print(sum(nums))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def main():
    n, m = get_int_list()
    xs = get_int_list()

    if n >= m:
        print(0)
        return
    xs.sort()
    diffs = [a - b for a, b in zip(xs[1:], xs)]
    diffs.sort()
    print(sum(diffs[:m-n]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

xs = sorted(X)
ds = [xs[i + 1] - xs[i] for i in range(M - 1)]
ds = sorted(ds)

result = sum(ds[:max(M - N, 0)])

print(result)

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    lst.sort()
    diff_lst = []
    for i in range(len(lst)-1):
        diff = lst[i+1] - lst[i]
        diff_lst.append(diff)
    diff_lst.sort(key = lambda x: -x)
    print(sum(diff_lst[n-1:]))

Submission status: Accepted

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

Submission status: Accepted

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

hq = []
for i in range(m-1):
    heapq.heappush(hq, x[i+1] - x[i])


move = 0

for i in range(m-n):
    move += heapq.heappop(hq)

print(move)

Submission status: Accepted

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

x.sort()

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

distance = [0]*(m-1)
for i in range(m-1):
    distance[i] = x[i+1]-x[i]

distance.sort()

ans = sum(distance[:(m-n)])
print(ans)

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
#template input
import bisect

def IT(): #整数1個
    return int(input())
def IL(): #整数複数
    return [int(_) for _ in input().split()]
def IS(): #文字列複数
    return [_ for _ in input().split()]
def ILS(n): #整数1つ　縦にズラーってなってるやつ
    return [int(input()) for _ in range(n)]
def SLS(n): #文字列　縦にズラーってなってるやつ
    return [input() for _ in range(n)]
def ILSS(n): #整数複数　縦にズラーってなってるやつ
    return [[int(_) for _ in input().split()] for j in range(n)]

#template technique
def bit_full_search(ss):
    n = len(ss)
    for i in range(1 << n):
        s = ""
        for j in range(n + 1):
            if ((1 & i >> j) == 1):
                s += ss[j]
        print(s)
def bit_full_search2(A):
    #https://blog.rossywhite.com/2018/08/06/bit-search/
    value = []
    for i in range(1 << len(A)):
        output = []

        for j in range(len(A)):
            if ((i >> j) & 1) == 1:
                #output.append(A[j])
                output.append(A[j])
        value.append([format(i, 'b').zfill(16), sum(output)])

    value.sort(key=lambda x:x[1])
    bin = [value[k][0] for k in range(len(value))]
    val = [value[k][1] for k in range(len(value))]
    return bin, val
def matching_bisect(list1_val, list2_val, n):
    #2分探索
    j = 0
    for i in range(len(list1_val)):
        j = bisect.bisect_left(list2_val, n - list1_val[i])
        if j < len(list2_val):
            if list1_val[i] + list2_val[j] == n:
                #ans = list2_bin[j] + list1_bin[i]
                break
    return j

"""ここからメインコード"""
def main():
    n, m = IL()
    x = IL()
    x.sort()

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

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


main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
x = list(li())
if n >= m:
    print(0)

else:
    diffs = []
    
    x.sort()
    
    for idx in range(m-1):
        diffs.append(x[idx+1]-x[idx])
        
    diffs.sort()
    
    for _ in range(n-1):
        diffs.pop()
        
    print(sum(diffs))

Submission status: Accepted

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

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

y.sort()
print(sum(y[:max(m-n,0)]))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=sorted([int(i) for i in input().split()])
ans=X[-1]-X[0]
Y=[0 for i in range(M-1)]
for i in range(M-1):
    Y[i]=X[i+1]-X[i]
Y.sort(reverse=True)
for i in range(min(N-1,M-1)):
    ans-=Y[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)
    exit()
x.sort()
dist =[]
for i in range(1,m):
    dis = x[i]-x[i-1]
    dist.append(dis)
dist.sort()
#print(dist)
ans = 0
for i in range(m-n):
    ans += dist[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)
    exit()

x = sorted(x)

d = []

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

d = sorted(d)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def  inpl(): return list(map(int, input().split()))
N, M = inpl()
X = sorted(inpl())
D = sorted([X[i+1] - X[i] for i in range(M-1)])
if N == 1:
    print(sum(D))
else:
    print(sum(D[:-N+1]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
are = [0]*(m-1)
for i in range(m-1):
        are[i]= a[i+1] -a[i]
are.sort()
for i in range(n-1):
        if are:
                are.pop()
print(sum(are))

Submission status: Accepted

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

ans = 0
if N < M:
    L = []

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

    L.sort()
    for i in range(M-N):
        ans += L[i]
print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
xs = na()
xs.sort()

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

ds.sort()

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

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

if n >= m:print(0)
else:
    dist = [abs(x[i+1] - x[i]) for i in range(M-1)]
    dist = sorted(dist)
    print(sum(dif[:-(n-1)]))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if m==1:
  print(0)
else:
  num=n-1
  sl=sorted(l)

  dsl=[abs(sl[i+1]-sl[i]) for i in range(m-1)]

  for i in range(num):
    ml=max(dsl)
    dsl.remove(ml)
  
  print(sum(dsl))

Submission status: Runtime Error

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

num=n-1
sl=sorted(l)

dsl=[sl[i+1]-sl[i] for i in range(m-1)]
priny(dsl)

Submission status: Runtime Error

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

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

else:
    li_X.sort()

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

    li_X_delta_sorted_rev = sorted(li_X_delta, reverse=True)
    path_ignorable = []
    cnt = 0

    for i in range(n-1):
        path_ignorable.append(li_X_delta.index(li_X_delta_sorted_rev[cnt]))
        cnt += 1
    # ignore path from li_X[i] to li_X[i+1] for i in path_ignorable
    
    ans = 0
    for i in range(len(li_X_delta)):
        if(i in path_ignorable):
            continue
        ans += li_X_delta[i]
    print(ans)

Submission status: Runtime Error

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

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

else:
    li_X.sort()

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

    li_X_delta_sorted_rev = sorted(li_X_delta, reverse=True)
    path_ignorable = []
    cnt = 0

    for i in range(n-1):
        path_ignorable.append(li_X_delta.index(li_X_delta_sorted_rev[cnt]))
        cnt += 1
    # ignore path from li_X[i] to li_X[i+1] for i in path_ignorable
    
    ans = 0
    """
    for i in path_ignorable:
        print("from " + str(i) + " to " + str(i+1))
    """
    for i in range(len(li_X_delta)):
        if(i in path_ignorable):
            continue
        ans += li_X_delta[i]
    print(ans)