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(input().split())
MAP = list(sorted([int(X[i]) for i in range(M)]))

if M <= N:
    print(0)
else:
    MAP_diff = list(sorted([MAP[i+1] - MAP[i] for i in range(M-1)]))
    print(sum(MAP_diff[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

arr=np.sort(np.array(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis)

for i in range(n-1):
    if len(lis)>0:
        lis.pop()

print(sum(lis))

Submission status: Accepted

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

arr=np.array(sorted(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis)

for i in range(n-1):
    if len(lis)>0:
        lis.pop()

print(sum(lis))

Submission status: Accepted

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

arr=np.array(sorted(x))
dif=np.diff(arr)

lis=dif.tolist()
lis=sorted(lis,reverse=True)

for i in range(n-1):
    if len(lis)>0:
        lis.pop(0)

print(sum(lis))

Submission status: Accepted

Solution code:
import fractions
import sys
from functools import reduce
import math

input = sys.stdin.readline

def main():
    n, m = input_list()
    if n >= m:
        print(0)
        exit(0)
    x = list(sorted(input_list()))
    diffs = []
    for i in range(1, m):
        a = abs(x[i] - x[i-1])
        diffs.append((i, a))
    dd = list(sorted(diffs, key=lambda v: v[1], reverse=True))
    ans = 0
    for i, v in enumerate(dd):
        if i < n-1:
            continue
        ans += v[1]
    print(ans)
    # args = [d[0] for d in dd[:n-1]]
    # r = divide_list(x, args)
    # ans = 0
    # for xx in r:
    #     ans += max(xx) - min(xx)
    # print(ans)


def divide_list(l, args):
    result = []
    pre = 0
    for i, a in enumerate(args):
        if i != 0:
            pre = args[i-1]
        result.append(l[pre:a])
        if i == len(args) - 1:
            result.append(l[a:])
    return result

def divide_two(n):
    c = 0
    while True:
        if c >= 2:
            break
        if n % 2 != 0:
            break
        n //= 2
        c += 1
    return c

def get_camulative(l):
    import itertools
    # 累積和
    return [0] + list(itertools.accumulate(l))

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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


def input_list_str():
    return list(map(str, input().split()))


def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


if __name__ == "__main__":
    main()

Submission status: Accepted

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]) if m - n > 0 else 0)

Submission status: Accepted

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 n < m else 0)

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()
    diffs = []
    for i in range(m - 1):
        diffs.append((x[i+1] - x[i], i))
    diffs.sort(reverse=True)
    diffs = diffs[:n-1]

    idxs = sorted([0] + [diff[1] + 1 for diff in diffs], reverse=True)

    ans = 0
    pos = -1
    for i in range(m):
        if idxs and i == idxs[-1]:
            pos = x[idxs.pop()]
        else:
            ans += x[i] - pos
            pos = x[i]
    print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=sorted(list(map(int,input().split())))
if n>=m:
    print(0)
    exit()
s=l[-1]-l[0]
l=sorted([a-b for a,b in zip(l[1:],l)], reverse=True)
print(s-sum(l[:n-1]))

Submission status: Accepted

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

n, m = map(int, input().strip().split())
xs = sorted(list(map(int, input().strip().split())))
if n >= m:
    print(0)
elif n == 1:
    print(xs[-1] - xs[0])
else:
    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]])

    total = 0
    for i in range(len(divides) + 1):
        if i == 0:
            interval = xs[divides[i]] - xs[0]
        elif i < len(divides):
            interval = xs[divides[i]] - xs[divides[i - 1] + 1]
        else:
            interval = xs[-1] - xs[divides[i - 1] + 1]

        total += interval

    print(total)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
index = []
ans = 0
s = 0
for i in range(len(X)-1):
    index.append([i,X[i+1]-X[i]])
index.sort(key=lambda x: x[1],reverse=True)
if N<M:
    for i in range(N-1,len(index)):
        ans += index[i][1]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

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

x.sort()

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

diff.sort()


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

Submission status: Accepted

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

Submission status: Accepted

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)

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

Submission status: Accepted

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 = sorted(dist, reverse= True)

  ans = sum(dist[N-1:])

print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort(key=int)
p=[0]*M
for i in range(1,M): p[i]=X[i]-X[i-1]
p.sort(key=int)
for i in range(min(N-1,M)): p.pop(-1)
print(sum(p))

Submission status: Accepted

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)]))

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

y=[0]*(m-1)
for i in range(m-1):
    y[i]=x[i+1]-x[i]
if n==1:
    print(sum(y))
    exit()
y.sort(reverse=True)
for i in range(n-1):
    y[i]=0
print(sum(y))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)


N, M = map(int,input().split())
Xm = list(map(int,input().split()))
 
if N >= M:
  print(0)
  sys.exit()
  
Xm.sort()
ans = Xm[M-1]-Xm[0]
 
Li = [Xm[i+1]-Xm[i] for i in range(M-1)]
Li.sort(reverse=True)
ans -= sum(Li[0:N-1])
print(ans)

Submission status: Accepted

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

if N >= M:
  print(0)
  sys.exit()
  
Xm.sort()
ans = Xm[M-1]-Xm[0]

Li = [Xm[i+1]-Xm[i] for i in range(M-1)]
Li.sort(reverse=True)
ans -= sum(Li[0:N-1])
print(ans)

Submission status: Accepted

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


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

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

l = sorted(l)[::-1]

del l[0: n - 1]

print(sum(l))

Submission status: Accepted

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

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

l = sorted(l)[::-1]

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

Submission status: Accepted

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


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

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

l = sorted(l)[::-1]

del l[0: n - 1]

print(sum(l))

Submission status: Accepted

Solution code:
import sys

N, M = map(int, input().split())
X = list(map(int, sys.stdin.readline().rsplit()))

X.sort()
tmp = [0] * (M - 1)
for i in range(M - 1):
    tmp[i] = X[i + 1] - X[i]
tmp.sort()

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

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
x=list(map(int, input().split()))
x.sort()
tmp=[0]*(m-1)
for i in range(m-1):
    tmp[i]=x[i+1]-x[i]#隣り合う座標の距離
tmp.sort()
if n>=m:
    print(0)
else:
    print(sum(tmp[0:m-n]))
    #隣り合う距離が小さいものを（座標数ー駒数）分選ぶ
    #つまり、移動しないと辿り着けない座標を取ってくる。

Submission status: Accepted

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)[: 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)
    exit(0)

if n==1:
    print(max(x) - min(x))
    exit(0)

x= sorted(x)

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

dist = sorted(dist,reverse=True)

total=max(x)-min(x)
for j in range(n-1):
    total -=dist[j]


print(total)

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
x = list(map(int,input().split()))
if n>=m:
    print(0)
else:
    b = sorted(x)
    d = list(np.diff(b))
    d.sort(reverse=True)
    sum = sum(d[:n-1])
    print(b[m-1]-b[0]-sum)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Xlist = sorted(list(map(int, input().split())))
sbn = [Xlist[i+1]-Xlist[i] for i in range(M-1)]
sbn.sort()
# print(sbn)
ans = 0
if N < M:
    for i in range(M-N):
        ans += sbn[i]
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Xs = sorted(list(map(int, input().split())))
diffs = sorted([Xs[i+1] - Xs[i] for i in range(len(Xs)-1)])
if N >= M:
  ans = 0
else:
  ans = sum(diffs[:M-N])

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(reverse=True)
print(x[-1] - x[0] - sum(d[:n - 1]))

Submission status: Accepted

Solution code:
def main():
    import numpy as np
    n, m = map(int, input().split())
    inlis = list(map(int, input().split()))
    if n >= m:
        print(0)
    else:
        inlis.sort()
        arr = np.array(inlis)
        #print(arr)
        diff = np.diff(arr)
        diff = list(diff)
        #print(diff)
        diff.sort()
        #print(diff)
        for _ in range(n-1):
            diff.pop()
        print(sum(diff))



if __name__ == "__main__":
    main()

Submission status: Accepted

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

X.sort()
ans = X[-1] - X[0]
D = []

for i in range(M-1):
  D += [X[i+1] - X[i]]
D.sort(reverse=True) 
if N >= M:
  print(0)
  exit()

for i in range(N-1):
  ans -= D[i]
print(ans)

Submission status: Accepted

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]) if 0 <= m-n else 0)

Submission status: Accepted

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

if M <= N:
    print(0)
else:
    for i in range(1, M):
        diff.append(X[i] - X[i-1])   
    diff.sort()
    print(sum(diff[0:M-N]))

Submission status: Accepted

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]) if M > N else 0)

Submission status: Accepted

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)
if N != 1:
	del Y[-(N-1):]
#print(Y)

print(sum(Y))

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    *A, = map(int, input().split())

    A.sort()
    tot = A[-1] - A[0]

    B = [a1 - a0 for a0, a1 in zip(A, A[1:])]
    B.sort()

    for _ in range(N - 1):
        if not B: break
        tot -= B.pop()
        if tot is 0: break

    print(tot)


if __name__ == '__main__':
    main()

Submission status: Accepted

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: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,m=map(int,readline().split())
    x=list(map(int,readline().split()))
    x.sort()
    d=[0]*(m-1)
    for i in range(1,m):
        d[i-1]=x[i]-x[i-1]

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

if __name__=="__main__":
    main()

Submission status: Accepted

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)]
if n == 1:
  print(sum(l))
else:
  print(sum(L))

Submission status: Accepted

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[::-1]
ans = sum(l[max(0,n-1):])
print(ans)

Submission status: Accepted

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

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

diff_list.sort(reverse=True)
print(sum(diff_list[N-1:]))

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=X[i+1]-X[i]
    Y.append(y)
y=sorted(Y)[::-1]
print(sum(y[N-1:]))

Submission status: Accepted

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

DIFFS.sort()
for i in range(N - 1):
    if DIFFS != []:
        DIFFS.pop(-1)

print(sum(DIFFS))

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

l = []

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

for i in range(n-1):
  l[i] = 0
  
print(sum(l))

Submission status: Accepted

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

Submission status: Accepted

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

if n >= m:
  print(0)
  quit()
  
x.sort()
y = [x[i]-x[i-1] for i in range(1, m)]
y.sort(reverse = True)
ans = sum(y)

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

Submission status: Accepted

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

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

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

d.sort()

for i in range(N-1):
    if len(d) > 0:
        d.pop(-1)

print(sum(d))

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
X = list(map(int, input().split()))
if n >= m:
    print(0)
    exit()
X.sort()
dX = [X[i + 1] - X[i] for i in range(m - 1)]
dX.sort()
print(sum(dX[: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:
    d = [0]*(m-1)
    for i in range(m-1):
        d[i] = x[i+1] - x[i]
    d.sort()
    print(sum(d[0:m-n]))

Submission status: Accepted

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

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

x = sorted(x)

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

r = sorted(r,reverse = True)
print(sum(r[n-1:]))

Submission status: Accepted

Solution code:

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

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

x.sort()
cnt = 0
l = [0] * (len(x)-1)

if n < m :
  for i in range(len(x)-1):
    l[i] = x[i+1] - x[i] 
  
  l.sort()
  cnt = l[0:m-n]
  
  print(sum(cnt))
else :
  print(cnt)

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    x = sorted([x[i] - x[i - 1] for i in range(1, m)])
    print(sum(x[: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:
    ans = x[m-1]-x[0]
    x = sorted([x[i] - x[i - 1] for i in range(1, m)])
    print(ans - sum(x[m-n:]))

Submission status: Accepted

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]
if(N<M):
    print(sum(diff))
else:
    print("0")

Submission status: Accepted

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

Submission status: Accepted

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

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)

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

print(sum(sorted(L)[:(M-N)]))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    X = sorted(list(map(int, input().split())))

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

    dist = sorted(dist, reverse=True)
    res = 0
    for i in range(n - 1, len(dist)):
        res += dist[i]

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
if M>N:
  X.sort()
  D = []
  for i in range(M-1):
    d = X[i+1]-X[i]
    D.append(d)
  
  D.sort()
  D = D[:(M-N)]
  ans = sum(D)
  
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(reverse=True)
# print(d)

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

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(abs(A[i+1]-A[i]))
B.sort()
#print(A)
#print(B)   

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

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(reverse=True)

print((x[-1] - x[0]) - sum(d[:n-1]))

Submission status: Accepted

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)

if N >= M:
  print(0)
else:
  print(sum(sa[:l-N+1]))

Submission status: Accepted

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

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

Submission status: Accepted

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

X.sort()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = X[i+1]-X[i]

diff.sort()
if N>=M:
    ans = 0
else:
    ans = sum(diff[:M-N])
print(ans)

Submission status: Accepted

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>=m:
        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: Accepted

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

if N>=M:
    out=0
else:
    aida=[0]*(M-1)
    for i in range(M-1):
        aida[i] = X[i+1] - X[i]
    #print(aida)
    aida = sorted(aida, reverse=True)

    out = X[-1] - X[0]
    for j in range(N-1):
        out-=aida[j]

print(out)

Submission status: Accepted

Solution code:
n,m,*x = map(int,open(0).read().split())
x.sort()
if n >= m:
  print(0)
  exit()
a = sorted([x[i+1]-x[i] for i in range(m-1)],reverse=True)
ans = x[-1]-x[0]
for i in range(n-1):
  ans -= a[i]
print(ans)

Submission status: Accepted

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)
if N == 1:
  print(sum(Y))
else:
  print(sum(Y[:-N+1]))

Submission status: Accepted

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()
print(sum(ddd[:m - n]))

Submission status: Accepted

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()
N = M if N > M else N
print(sum(L[:(M-1)-(N-1)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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(sum(K[N - 1:]))

Submission status: Accepted

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(x[i+1]-x[i])
r.sort(reverse=True)
print(sum(r[n-1:]))

Submission status: Accepted

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