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:
try:
    N, M = map(int, input().split())
    Xi = [int(x) for x in input().split()]

    if N < 1 or N > 10**5:
        exit()
    if M < 1 or M > 10**5:
        exit()
    if M != len(Xi):
        exit()
    for i in Xi:
        if i < -10**5 or i > 10**5:
            exit()
    Xi = sorted(Xi)

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

    a = sorted(a, reverse=True)
    s = 0
    # print(a)
    for i in range(N-1):
        # print(a[0])
        a.pop(0)

    for i in a:
        s += abs(i)
    print(s)
except ValueError:
    exit()
except IndexError:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.sort()

c = []
if len(b) != 1:
  for i in range(len(b) - 1):
    c.append(b[i+1] - b[i])

if len(b) == 1:
  print(0)
  
    
else:  
  c.sort()
  for j in range(min(a[0] - 1,a[1])):
    c[len(c)- 1 - j] = 0
  print(sum(c))

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 = X[-1] - X[0]
    d = [X[i + 1] - X[i] for i in range(M - 1)]
    d.sort(reverse=True)
    result = l - sum(d[:N - 1])
    print(result)

Submission status: Accepted

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


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

n, m = [int(_) for _ in input().split()]
x = np.array(sorted((int(_) for _ in input().split())))
d = np.sort(x[1:] - x[:-1])[:-n:-1]

print(x[-1] - x[0] - np.sum(d))

Submission status: Accepted

Solution code:
nandm = input().split()
numlist = input().split()
N = int(nandm[0])
M = int(nandm[1])
if N >= M:
    print(0)
else:
    intlist = [int(num) for num in numlist]
    intlist.sort()
    subtract = [intlist[i + 1] - intlist[i] for i in range(len(intlist) - 1)]
    subtract.sort()
    print(sum(subtract[:(M - N)]))

Submission status: Accepted

Solution code:
def solve(n, m, x):
    if n >= m:
        return 0

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

    return x[-1] - x[0] - sum(y[:n-1])

def main():
    n, m = map(int, input().split())
    x = [int(s) for s in input().split()]
    x.sort()

    print(solve(n, m, x))

main()

Submission status: Accepted

Solution code:
import numpy as np
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X = np.sort(X)

diff = np.diff(np.sort(X), n=1)
print(np.sum(np.sort(diff)[::-1][N-1:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

class Solver(object):
    def __init__(self):
        self._input()
        self.heap = []

    def solve(self):
        if self.n >= self.m:
            print(0)
            return

        self.x_s.sort()
        for i in range(len(self.x_s) - 1):
            heapq.heappush(self.heap, -abs(self.x_s[i+1] - self.x_s[i]))

        for i in range(self.n - 1):
            heapq.heappop(self.heap)

        print(-sum(self.heap))

    def _input(self):
        self.n, self.m = list(map(int, input().split(" ")))
        self.x_s = list(map(int, input().split(" ")))


if __name__ == "__main__":
    Solver().solve()

Submission status: Accepted

Solution code:
import numpy as np
N,M=map(int,input().split())
Xm = list(map(int,input().split()))
if N>=M:
  print(0)
else:
  Xm.sort()
  list1 = np.array(Xm[0:M-1])
  list2 = np.array(Xm[1:M])
  diff = (list2-list1).tolist()
  if N==1:
    print(sum(diff))
  else:
    diff.sort()
    while (N>1):
      N=N-1
      diff.pop()
    print(sum(diff))

Submission status: Accepted

Solution code:
import sys

def main(N, M, Xs):
    Xs.sort()

    if N >= M:
        return 0

    assert M == len(Xs)

    diffs = []
    # find max gaps between visiting points
    for i in range(1, M):
        # index after gap, gap length
        diffs.append((i, Xs[i] - Xs[i-1]))
    diffs.sort(key=lambda x: x[1], reverse=True)

    # index of the first places
    positions = [0] + [diff[0] for diff in diffs[:N-1]]

    # moves = sum of (end point - start point)
    moves = [ Xs[-1] - Xs[positions[-1]] ] # the last
    for i in range(1, N):
        moves.append(Xs[positions[i]-1] - Xs[positions[i-1]])

    return sum(moves)

line = sys.stdin.readline()
N,M = [int(x) for x in line.strip().split(' ')]

line = sys.stdin.readline()
Xs = [int(x) for x in line.strip().split(' ')]

print(main(N, M, Xs))

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(reverse=True)
print(sum(Y[N-1:]))

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.sort()
if N >= M:
    print(0)
else:
    print(sum(sa[:M - N]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
b=[0]*(m-1)
for i in range(0,m-1):
  b[i]=a[i+1]-a[i]
c=sorted(b)
s=a[m-1]-a[0]
if m>n:
  for i in range(0,n-1):
  	s=s-c[m-i-2]
else:
  s=0
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:
    X.sort()
    D = []
    for i in range(M-1):
        D.append(X[i+1]-X[i])
    D.sort()
    print(sum(D[0:M-N]))

Submission status: Accepted

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

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

Submission status: Accepted

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

if N>=M:
    print(0)
else:
    lst_diff = []
    for i in range(M-1):
        diff = abs(lst_X[i] - lst_X[i+1])
        lst_diff.append(diff)
    lst_diff.sort(reverse=True)
    print(sum(lst_diff[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()
  dx = []
  for i in range(m-1):
    dx.append(abs(x[i]-x[i+1]))
  dx.sort()
  print(sum(dx[:m-n]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N,M=list(map(int,input().split()))
X=np.sort(np.array(list(map(int,input().split()))))

if N>=M:
    print(0)
else:
    L=np.sort(np.diff(X))
    ans=X[-1]-X[0]
    for i in range(1,N):
        ans-=L[-i]
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m = map(int,input().split())
xlist = list(map(int,input().split()))

xlist.sort()
ylist=[]

for i in range(1,m):
    ylist.append(xlist[i]-xlist[i-1])

ylist.sort()
a=0
for i in range(0,m-n):
    a+=ylist[i]
print(a)

Submission status: Accepted

Solution code:
N, M = map(int, input().split( ))
datas = list(map(int, input().split( )))
datas.sort()
Ms = M - 1

dest = list()
K = 0
i = 0
for i in range(Ms):
  K = datas[i+1] - datas[i]
  dest.append(K)

dest.sort()
L = 0
MN = M - N
for j in range(MN):
  L = L + dest[j]

print(L)

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:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:m-n]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:m-n]))
else:
    print(0)

Submission status: Accepted

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

X.sort()

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

P = P[::-1]

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n < m:
    x_distance = [x2 - x1 for x1, x2 in zip(x, x[1:])]
    x_distance.sort()
    print(sum(x_distance[:(m-n)]))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

if n < m:
    x_distance = [x[i + 1] - x[i] for i in range(m - 1)]
    x_distance.sort()
    ans = 0
    for i in range (m - n):
        ans += x_distance[i]
    print(ans)
else:
    print(0)

Submission status: Accepted

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

if n < m:
    x_distance = [abs(x[i + 1] - x[i]) for i in range(m - 1)]
    x_distance.sort()
    ans = 0
    for i in range (m - n):
        ans += x_distance[i]
    print(ans)
else:
    print(0)

Submission status: Accepted

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

#XX = [0 for _ in range(M+1)]
XX = []
for i in range(M):
    if i == 0:
#        XX[i] = 0
        continue
    else:
        XX.append((abs(X[i] - X[i-1]),i-1))

XX_sort = sorted(XX,reverse=True)
#XX_sort_1 = XX_sort[:N-1]
XX_sort_2 = XX_sort[N-1:]

if N >= M:
    ans = 0
elif N == 1:
    ans = abs(X[-1] - X[0])
else:
    ans = 0
    for k,v in XX_sort_2:
        ans += k

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
buf=input().split()
a=[None for i in range(m)]
b=[None for i in range(m-1)]
for i in range(m):
    a[i]=int(buf[i])
a.sort()
if n==1:
    print(a[-1]-a[0])
else:
    for i in range(1,len(a)):
        b[i-1]=a[i]-a[i-1]
    b.sort()
    print(sum(b[:-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 = sorted(x)
    x = [x[i+1]-x[i] for i in range(len(x)-1)]

    x = sorted(x, reverse=True)
    x = x[n-1:]
    ans = 0
    for i in x:
        ans+=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 = sorted(x)
    xx = [x[i+1]-x[i] for i in range(len(x)-1)]

    xx = sorted(xx, reverse=True)
    xx = xx[n-1:]
    ans = 0
    for i in xx:
        ans+=i
    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()
D = []
for i in range(M-1):
    D.append(X[i+1]-X[i])

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq

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

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

XM.sort()
diffs = [XM[i + 1] - XM[i] for i in range(M - 1)]
nmin_diffs = heapq.nlargest(N - 1, diffs)
nmin_diffs_ind = [diffs.index(nmin_diffs[i]) for i in range(N - 1)]
nmin_diffs_ind.sort()
nmin_diffs_ind.append(len(XM) - 1)

dist = XM[nmin_diffs_ind[0]] - XM[0]
for i in range(N - 1):
    dist += XM[nmin_diffs_ind[i + 1]] - XM[nmin_diffs_ind[i] + 1]

print(dist)

Submission status: Accepted

Solution code:
import sys
import numpy as np

def main(inp):
    N,M=map(int,next(inp).split())
    X=np.array(list(map(int,next(inp).split())))
    X=np.sort(X)
        
#     N=inp[0]
#     M=inp[1]    
#     X=np.array(inp[2])
    X=np.sort(X)    
#    print("N",N)
#    print("M",M)
#    print("X",X)
    
    if(N>M): return 0
    if(N==1):
        ans=X[len(X)-1]-X[0]
        return ans
    
    delta=np.empty([M-1],dtype=int)
    
    for i in range(M-1):
        delta[i]=X[i+1]-X[i]
    
#    print(X)
#    print("delta",delta)
    
    
    #delta=np.split(delta,[1,2])
    #print(delta)
    
    splpoint=np.argpartition(np.array(delta),-N+1)[-N+1:]
#     splpoint=np.sort(splpoint)
    
    delta=np.delete(delta,splpoint)
#    print(splpoint)
#    print(delta)    
    
    ans=sum(delta)
        
    return ans

print(main(sys.stdin))

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()
    dis = []
    for i in range(m - 1):
        dis.append(X[i + 1] - X[i])
    dis.sort(reverse = True)
    div = 0
    total = X[m - 1] - X[0]

    for i in range(n - 1):
        div += dis[i]
    print(total - div)

Submission status: Accepted

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

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

diff_sum = 0

for i in range(len(diff) - (n - 1)):
    diff_sum += diff[i]

print(diff_sum)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())
    X = sorted([int(i) for i in input().split()])
    diff = [None for i in range(M-1)]

    for i in range(len(diff)):
        diff[i] = X[i+1] - X[i]

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

Submission status: Accepted

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

diff = []

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

diff = sorted(diff)

diff_sum = 0
diff_len = m - 1

for i in range(diff_len - (n - 1)):
    diff_sum += diff[i]

print(diff_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
xs = list(map(int,input().split()))
ls=[]
before=1000000
for x in sorted(xs):
    if before != 1000000:
        ls.append(abs(x-before))
    before = x
ls2 = sorted(ls,reverse=True)
print(sum(ls2[n-1:]))

Submission status: Accepted

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

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

    x = sorted(list(map(int, input().split())))
    y = sorted([x[i]-x[i-1] for i in range(m-1,0,-1)])[::-1]
    s = sum(y)

    print(s-sum(y[:n-1]))
    
if __name__=="__main__":
    main()

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

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-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 pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    x = sorted(LI())
    t = sorted([x[i+1]-x[i] for i in range(m-1)])
    r = 0
    for i in range(m-n):
        r += t[i]

    return r



print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import IPython

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()
X_list = ds_in()
if N >= M:
    print(0)
    exit()

X_list.sort()
diff = [X_list[i+1] - X_list[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()
Xd = [0] * (M-1)
for i in range(M-1):
  Xd[i] = X[i+1] - X[i]

Xd.sort()

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

Submission status: Accepted

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

# print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
if n-1>0:
    print(sum(diff_list[:-(n-1)]))
else:
    print(sum(diff_list))    

#%%


#%%

Submission status: Accepted

Solution code:
x,y=map(int,input().split())
if x>=y:
  print(0)
  exit()
a=list(map(int,input().split()))
a.sort()
l=a[0]
m=[]
for i in a[1:]:
  m.append(i-l)
  l=i
m.sort(reverse=True)
print(sum(m[x-1:]))

Submission status: Accepted

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

Submission status: Accepted

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

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

d.sort()
print(sum(d[:-(n-1)])) if n-1 > 0 else print(sum(d))

Submission status: Accepted

Solution code:
c = list(map(int,input().split()))
N,M = c[0],c[1]
X = list(map(int,input().split()))
if N>=M:
  print(0,end="")
else:
  X.sort()
  differences = [X[i+1]-X[i] for i in range(len(X)-1)]
  differences.sort()
  for i in range(N-1):
    differences.pop()
  print(sum(differences),end="")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if N < M:
    mar = [abs(X[i+1] - X[i]) for i in range(M-1)]
    mar.sort()
    print(sum([mar[i] for i in range(M-N)]))
else:
    print(0)

Submission status: Accepted

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

    if N >= M:
        return 0

    x = [j-i for i, j in zip(t, t[1:])]
    x.sort(reverse=True)
    return t[-1] - t[0] - sum(x[:N-1])


print(main())

Submission status: Accepted

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

    if N >= M:
        return 0

    x = [j-i for i, j in zip(t, t[1:])]
    x.sort(reverse=True)
    return (t[-1] - t[0]) - sum([x[i] for i in range(N - 1)])


print(main())

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n, m = getNM()
point = getList()
point.sort()
diff = []
for i,j in zip(point, point[1:]):
    diff.append(abs(i-j))

diff.sort()
if n != 1:
    diff = diff[:-n+1]
print(sum(diff))

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    t = list(map(int, input().split()))
    if n >= m:
        return 0
    t.sort()
    x = [[t[i], t[i+1] - t[i]] for i in range(m-1)]
    x.append([t[m-1], 0])
    x.sort(key=lambda a: a[1], reverse=True)
    sum_dis = t[m-1] - t[0]
    skip_dis = sum([x[i][1] for i in range(n - 1)])
    return sum_dis - skip_dis


print(main())

Submission status: Accepted

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

Submission status: Accepted

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

def main():
  n, m = map(int, input().split())
  xs   = list(map(int, input().split()))
  xs.sort()
  dxs = [abs(xs[i]-xs[i+1]) for i in range(len(xs)-1)]

  if n >= m:
    print(0)

  else:
    dxs.sort(reverse=True)
    del dxs[:(n-1)]
    print(sum(dxs))


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

Submission status: Accepted

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

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

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(len(X)-1)]
diff.sort()
diff.reverse()

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

Submission status: Accepted

Solution code:
import numpy as np

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

p = np.sort(np.array(x))
dif = np.sort(p[1:] - p[:-1])
if n > 1:
    print(np.sum(dif[: 1 - n]))
else:
    print(np.sum(dif))

Submission status: Accepted

Solution code:
#117-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()

n,m=IL()
xl=IL()
if n>=m:
    print(0)
else:
    dif=[]
    xl.sort()
    for i in range(m-1):
        dif.append(xl[i+1]-xl[i])
    dif.sort()
    print(sum(dif[:m-n]))

Submission status: Accepted

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

x_sort = sorted(x_arr)

if n >= m or m==1:
    print(0)
else:
    x_sub = [x_sort[i+1]-x_sort[i] for i in range(m-1)]
    x_sub = sorted(x_sub)
    print(sum(x_sub[:(m-1)-(n-1)]))

Submission status: Accepted

Solution code:
I=lambda:map(int,input().split());n,m=I();x=sorted(I());print(sum(sorted(j-i for i,j in zip(x,x[1:]))[:max(0,m-n)]))

Submission status: Accepted

Solution code:
n,m,*x=map(int,open(0).read().split());x.sort();print(sum(sorted(j-i for i,j in zip(x,x[1:]))[:max(0,m-n)]))

Submission status: Accepted

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

XList = sorted(XList)
deltaList = [0]

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


for i in range(N-1) :
    deltaList[i] = 0
    if i == len(deltaList)-1 :
        break
print(sum(deltaList))

Submission status: Accepted

Solution code:
# できなかった
# nコのコマでmコの地点を訪れる
# 指定された地点のみをスタート地点と考えて問題ない
# 右にのみ進むと考えて問題ない
# nコの範囲に分け、その合計幅の最小値を求める
# ※訪れていないN-1コの開区間を最大化すれば良い！！！
import math
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
if n>=m:
    print(0)
else:
    dimX=[x[i]-x[i-1]for i in range(1,m)]
    # print(x,dimX)
    dimX.sort(key=lambda x:-x)
    ans=x[-1]-x[0]
    for i in range(n-1):
        ans-=dimX[i]
    print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
if n==1:
    print(max(x)-min(x))
elif m==1 or n>=m:
    print(0)
else:
    x.sort()
    goukei=0
    l=[0]
    for i in range(m-1):
        goukei+=x[i+1]-x[i]
        l.append(x[i+1]-x[i])
    i=0
    l.sort()
    for i in range(n-1):
        goukei-=l[-1-i]
    print(goukei)

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 = [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 = list(map(int, input().split()))
X_list.sort()
err_list = [X_list[i+1] - X_list[i] for i in range(len(X_list)-1)]
err_list.sort()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

input1 = input().split()
N = int(input1[0])
M = int(input1[1])
zahyo = list(map(int,input().split()))

if M == 1 or N >= M:
    print(0)
else:
    zahyo.sort()

    sa = [0]*(M-1)
    for i in range(M-1):
        sa[i] = zahyo[i+1] - zahyo[i]

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

Submission status: Accepted

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

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

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

x_list = sorted(x)

distance_list = []

for i in range(0, M-1):
    distance = x_list[i+1] - x_list[i]
    distance_list.append(distance)

result = sum(sorted(distance_list)[::-1][N-1:])
print(result)

Submission status: Accepted

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

margin = []
prev = x_list[0]
for i in range(1, M):
    margin.append(x_list[i] - prev)
    prev = x_list[i]

margin.sort(reverse=True)
print(x_list[-1] - x_list[0] - sum(margin[:N-1]))

Submission status: Accepted

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

sorted_place_list = sorted(place_list)

dis_list = []

if n >= m :
    ans = 0 
else:
    for i,place in enumerate(sorted_place_list[:m-1]):
        dis = abs(sorted_place_list[i+1]-place)
        dis_list.append(dis)

    sorted_dis_list = sorted(dis_list)
    ans = sum(sorted_dis_list[:m-n])

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split(" "))
if n>=m:
  print(0)
  exit()
s=list(map(int,input().split(" ")))
s.sort()
t=[]
for i in range(1,m):
  t.append(s[i]-s[i-1])
t.sort()
for j in range(n-1):
  t.pop() 
print(sum(t))

Submission status: Accepted

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

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

#print(X)
X.sort()
#print(X)

L = []
for i in range(m-1):
  L.append(X[i+1] - X[i])
#print(L)
L.sort(reverse=True)
#print(L)
print((X[m-1]-X[0]) - (sum(L[0:n-1])))

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

if(N >= M):
    print(0)
else:
    L = []
    for i in range(1, M):
        L.append(X[i] - X[i-1])
    L.sort()
    res = X[-1] - X[0]
    for _ in range(N-1):
        res -= L.pop()
    print(res)

Submission status: Accepted

Solution code:
import copy
import itertools

def funcF(p):
    return p

def funcG(p):
    return funcF(p) + p*p

N, M = (int(i) for i in input().split())
X = list(int(i) for i in input().split())
X.sort()

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

# X[0] ... X[M-1] をN個のグループに分ける
# X[i]とX[i+1]の間でグループを分ける場合Cx=iとする（i = 0 .. M-2の個数(N-1)個のCombinationで求まる）

# minS=max(X) - min(X)
# for C in list(itertools.combinations(range(M-1), N-1)):
#     tmpS = 0
#     if C[0] > 0:
#         tmpS += X[C[0]] - X[0]
#     for i in range(N-2):
#         if C[i+1] > C[i]+1:
#             tmpS += X[C[i+1]] - X[C[i]+1]
#     tmpS += X[M-1] - X[C[N-2]+1]
#     if tmpS < minS:
#         minS = tmpS

L=[]
for i in range(M-1):
    L.append(X[i+1] - X[i])
L.sort(reverse=True)
tmpL=0
for i in L[:N-1]:
    tmpL += i

print("{}".format(X[M-1]-X[0]-tmpL))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = list(sorted(x))
d = [x1 - x0 for x1, x0 in zip(x[1:], x)]
d = list(sorted(d))

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