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:
from heapq import heappush, heappop


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

X.sort()

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

q1 = []

for i in range(M - 1):
    sec = X[i + 1] - X[i]
    heappush(q1, -sec)

ans = 0
for _ in range(N - 1):
    ans -= heappop(q1)

print(X[-1] - X[0] - ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop


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

X.sort()

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

q1 = []

for i in range(M - 1):
    sec = X[i + 1] - X[i]
    heappush(q1, (-sec, i + 1))

q2 = []
for n in range(N - 1):
    sec, idx = heappop(q1)
    heappush(q2, idx)

heappush(q2, M)
cur = 0
ans = 0
for n in range(N):
    idx = heappop(q2)
    ans += X[idx - 1] - X[cur]
    cur = idx

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

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

print(ans)

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

ans = 0
if N < M:
    Xm = sorted(Xm)
    Ym = [0] * (M-1)

    for i in range(1, M):
        Ym[i-1] = abs(Xm[i] - Xm[i-1])

    Ym = sorted(Ym, reverse=True)
    for i in range(N-1):
        Ym[i] = 0

    ans = sum(Ym)

print(ans)

Submission status: Accepted

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

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()
nas = lambda: stdin.readline().split()

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

x.sort()
d = [x[i] - x[i - 1] for i in range(1, m)]
d.sort()
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()))
if N >= M:
    print(0)
    exit()
X.sort()
leng = []
for i in range(1, M):
    leng.append(X[i] - X[i-1])
ans = sorted(leng, reverse = True)
print(sum(ans[N - 1:]))

Submission status: Accepted

Solution code:
N, M = [int(n) for n in input().split()]
X = sorted([int(n) for n in input().split()])
if N>=M:
    print(0)
else:
    dx = []
    for j in range(M-1):
        dx.append(X[j+1]-X[j])
    ans = sorted(dx, reverse=True)
    print(sum(ans[N-1:]))

Submission status: Accepted

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

if M <= N:
  print(0)
  exit(0)
  
X.sort()
intervals = []
for i in range(M-1):
  intervals.append(X[i+1] - X[i])
  
intervals.sort()
R = max(X) - min(X)
for _ in range(N-1):
  R -= intervals.pop()
  
print(R)

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()
    sa=[X[i+1]-X[i] for i in range(M-1)]
    sa.sort()
    res=0
    for i in range(1,N):
        res+=sa[-i]
    print(sum(sa)-res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

    X = sorted(X)

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

    kukan = sorted(kukan, reverse=True)

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

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

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
li = [0]*(M-1)
X.sort()
for i in range(M-1):
    li[i] = abs(X[i+1]-X[i])
li.sort()
print(sum(li[: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(0)
dist = []
for i in range(m-1):
    dist.append(abs(x[i]-x[i+1]))
dist.sort(reverse = True)
ans = 0
for i in range(n-1):
    ans += dist[i]
print(x[-1]-x[0]-ans)

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:
    ans=0
else:
    for i in range(m-1):
        l[i]=x[i+1]-x[i]

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, M = LI()
    X = LI()
    X = sorted(X)
    diff = []
    for i in range(M-1):
        d = X[i+1] - X[i]
        diff.append(d)
    diff = sorted(diff)
    while N - 1> 0 and len(diff) > 0:
        diff.pop()
        N -= 1
    ans = sum(diff)
    print(ans)
    '''
    X = sorted(X)
    min_dist = [0] * M
    if N == 1:
        return X[-1] - X[0]
    print(X)
    for i in range(1, M-1):
        min_dist[i] = min(abs(X[i] - X[i-1]), abs(X[i+1] - X[i]))
        print(min_dist)
    min_dist = sorted(min_dist)[::-1]
    print(min_dist)
    for i in range(N-2):
        if len(min_dist) > 0:
            min_dist.pop(-1)
    print(sum(min_dist))
    '''
main()

Submission status: Accepted

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

sm = x[-1] - x[0]
acc = 0
if n >= m:
    print(0)
    exit()
print(sm - sum(diff[:n-1]))

Submission status: Accepted

Solution code:
import sys

n, m, *x = map(int, sys.stdin.read().split())

def main():
    x.sort()
    s = x[-1] - x[0]
    d = [0] + sorted([x[i+1] - x[i] for i in range(m-1)], reverse=True)
    for i in range(m-1):
        d[i+1] += d[i]
    
    return s - d[min(n, m)-1]

if __name__ == '__main__':
    ans = main()
    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()
    dis = []
    for i in range(m-1):
        dis.append(x[i+1]-x[i])
    dis.sort()

    ans = 0
    for i in range(m-n):
        ans += dis[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()
    diff = [X[i] - X[i-1] for i in range(1,M)]
    diff.sort()
    print(sum(diff[:M-N]))

Submission status: Accepted

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

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

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

intervals.sort()
result=x[len(x)-1]-x[0]
for i in range(n-1):
    result-=intervals.pop()
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

    i = 0
    while Y and i < N - 1:
        i += 1
        Y.pop()
    print(sum(Y))










if __name__ == '__main__':
    main()

Submission status: Accepted

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

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [xn - x for xn, x in zip(X[1:], X[:-1])] #[X[i + 1] - X[i] for i in range(len(X) - 1)]
    # for _ in range(N - 1):
    #     x_diff.remove(max(x_diff))
    x_diff.sort(reverse=True)
    ans = sum(x_diff[N-1:])

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys

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

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

x_diff = np.diff(x)
x_diff_sort = sorted(x_diff)[::-1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
B=sorted(A)
if N>=M:
  print(0)
else:
  a=M-N
  C=[0]*(M-1)
  for i in range(M-1):
    C[i]=B[i+1]-B[i]
  D=sorted(C)
  ans=sum(D[:a])
  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()
a=[0]
for i in range(m-1):
   a.append(x[i+1]-x[i])
a.sort(reverse=True)
for i in range(n-1):
    a[i]=0
print(sum(a))

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.append(x[i+1] - x[i])
y.sort()
if n >= m:
    print(0)
else:
    ans = 0
    for i in range(m - n):
        ans += y[i]
    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    X=list(map(int,input().split()))
    X.sort()
    dif=[0]*(M-1)
    for i in range(M-1):
        dif[i]=X[i+1]-X[i]
    dif.sort(reverse=True)
    ans=sum(dif[N-1:])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M, *X = map(int, open(0).read().split())
X = sorted(X)
if N >= M:
  print(0)
  exit(0)
D = []
for i in range(M-1):
  D.append(abs(X[i] - X[i+1]))
D = list(reversed(sorted(D)))
print(X[M-1] - X[0] - sum(D[: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()])
L=sum(sorted(list(map(lambda x: x[1]-x[0], zip(X,X[1:]))))[:M-N])
print(0) if N>=M else print(L)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import sub
N, M = map(int, input().split())
X = sorted(map(int, input().split()))
print(sum(sorted(map(sub, X[1:], X), reverse=True)[N-1:]))

Submission status: Accepted

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

a.sort()
a.reverse()

ans=sum(a)-sum(a[0:n-1])
print(ans)

Submission status: Accepted

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

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

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

print(sum(D))

Submission status: Accepted

Solution code:
# coding: utf-8

(koma_count, target_count) = [int(i) for i in input().rstrip().split(" ")]
target = sorted([int(i) for i in input().rstrip().split(" ")])
area = sorted([target[j] - target[j-1] for j in range(1, target_count)])
must = max(0, target_count-koma_count)

ans = 0
if must > 0:
    ans = sum(area[0:must])
print(ans)

Submission status: Accepted

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

l = sorted(list(map(int, input().split())))
m = sorted([l[i+1] - l[i] for i in range(m-1)])

for i in range(n-1):
    m.pop()

print(sum(m))

Submission status: Accepted

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

X.sort()
Xx = sorted( [ X[i+1]-X[i] for i in range(m-1) ], reverse=True )
print(sum(Xx[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()
diff = [x[i] - x[i - 1] for i in range(1, m)]
diff.sort()
i = 0
while diff and i < n - 1:
    diff.pop()
    i += 1
print(sum(diff))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


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

X.sort()
diffs = np.abs(np.diff(X))
diffs.sort()
if N >= M:
    print(0)
else:
    ans = diffs.cumsum()[M - N - 1]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

length = X[m-1] - X[0]
L = []
for i in range(m-1):
	L.append(X[i+1] - X[i])
L.sort()

if n >= m:
	print(0)
else:
	print(length - sum(L[m-1-(n-1):m-1]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x.sort()

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

num.sort()
if n > m:
  print(0)
else:
  print(sum(num[:m-n]))

Submission status: Accepted

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

Submission status: Accepted

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

if m <= n:
    ans = 0
else:
    x.sort()
    dx = [x[i]-x[i-1]-1 for i in range(1,m)]
    dx.sort(reverse=True)
    ans = x[m-1] - (x[0] - 1) - n - sum(dx[:n-1])

print(ans)

Submission status: Accepted

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

x.sort()
dx = [x[i]-x[i-1]-1 for i in range(1,m) if x[i]-x[i-1]-1>0]
dx.sort(reverse=True)

a = 0
if n-1 <= len(dx):
    for i in range(n-1):
        a += dx[i]
else:
    a = sum(dx)

if m <= n:
    ans = 0
else:
    ans = x[m-1] - (x[0] - 1) - n - a
print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

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

Submission status: Accepted

Solution code:
import heapq

def solve():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X_ = sorted(X)
    ret = X_[-1] - X_[0]
    diff = [X_[i]-X_[i+1] for i in range(M-1)]
    heapq.heapify(diff)

    while N > 1 and diff:
        ret += heapq.heappop(diff)
        N -= 1
        
    print(ret)
    
solve()

Submission status: Accepted

Solution code:
def solve():
    N, M, *X = map(int, open(0).read().split())
    
    X.sort()
    dx = [0 for _ in range(M - 1)]
    for i in range(1, M):
        dx[i - 1] = X[i] - X[i - 1]
    
    dx.sort()
    ans = 0
    for i in range(M - N):
        ans += dx[i]
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

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

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

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))

if N >= M:
  print("0")
else:
  queue = []
  for i in range(M):
    if i + 1 < M:
      heappush(queue, -abs(X[i+1] - X[i]))

  for i in range(N-1):
    heappop(queue)

  print(-sum(queue))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

nm=input().split(" ")
N=int(nm[0])
M=int(nm[1])
xi=input().split(" ")

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

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

#dXから上位N-1個を取り除いたあとの総和が多分答え
ans=0
for i in range(M-N):
  ans+=dX[i]
print(ans)

Submission status: Accepted

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


def solve(N: int, M: int, X: "List[int]"):
    x = sorted(X)
    x_dif = []
    for i in range(M-1):
        x_dif.append(x[i+1]-x[i])
    x_dif.sort(reverse=True)
    print(sum(x_dif[N-1:]))


    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    X = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, X)

if __name__ == '__main__':
    main()

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[i] for i in range(M - N)))

Submission status: Accepted

Solution code:
from operator import itemgetter
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],i])
lis.sort(reverse=True)
if n>=m:
  print(0)
else:
  if n==1:
    print(max(x)-min(x))
  else:
    cnt=x[lis[0][1]]-x[0]+x[m-1]-x[lis[n-2][1]+1]
    for i in range(1,n-1):
      cnt+=x[lis[i][1]]-x[lis[i-1][1]+1]
    print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(sum(sec))

Submission status: Accepted

Solution code:
def main():
    N, M, *X = map(int, open(0).read().split())
    X = sorted(set(X))
    D = [j - i for i, j in zip(X, X[1:])]
    D.sort()
    print(0 if M <= N else sum(D[:M - N]))
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, M, *X = map(int, open(0).read().split())
    X.sort()
    D = [j - i for i, j in zip(X, X[1:])]
    D.sort()
    print(0 if M <= N else sum(D[:M - N]))
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N, M = map(int, input().split())
    *X, = map(int, stdin.readline().split())
    X.sort()
    D = [X[i + 1] - X[i] for i in range(M - 1)]
    D.sort()
    print(0 if M <= N else sum(D[: M - N]))
    return


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

    print(ans)
    return

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

if __name__ == '__main__':
    examC()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

if N < M:
    Y = sorted(X)
    sections = sorted([Y[i] - Y[i - 1] for i in range(1, M)])
    for i in range(M - N):
        ans += sections[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()
  A = [X[i+1]-X[i] for i in range(M-1)]
  A.sort()
  print(sum(A[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n,m = map(int,input().split())
X = sorted(list(map(int,input().split())))
diffs = deque()
if m <= n:
  print(0)
else:
  for i in range(m - 1):
    diffs.append(X[i + 1] - X[i])
  diffs = deque(sorted(diffs,reverse = True))
  for i in range(n - 1):
    diffs.popleft()
  print(sum(diffs))

Submission status: Accepted

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

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

suma = 0
if N >= M:
    print(0)
else:
    for i in range(M-N):
        suma += diff[i]
    print(suma)

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()
n = min(M-1,N-1)
for i in range(n):
    d.pop()
ans = sum(d)
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()
z=[]
for i in range(1,m):
    z.append(x[i]-x[i-1])
z.sort()
if n==1:
    print(x[m-1]-x[0])
else:
    print(x[m-1]-x[0]-sum(z[-(n-1):]))

Submission status: Accepted

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

Submission status: Accepted

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