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:
def c_streamline(N, M, X):
    if N >= M:
        return 0  # コマを各座標に置けば動かす必要がない

    X.sort()
    x_distance_from_left = [0] * M
    for i in range(1, M):
        x_distance_from_left[i] = X[i] - X[i - 1]
    x_distance_from_left.sort(reverse=True)
    # 隣接した2つの位置について、距離が大きなところは移動したくないので、
    # そこにはコマを置いて移動せずに済むようにする
    ans = (X[-1] - X[0]) - sum(x_distance_from_left[:N - 1])
    return ans

N, M = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_streamline(N, M, X))

Submission status: Accepted

Solution code:
# -*- Coding:utf-8 -*-
import sys

def split_with_func(string, func):
    return [func(x) for x in string.split()]

def get_line(func=None):
    string = sys.stdin.readline().rstrip()
    if func is not None:
        return split_with_func(string, func)
    else:
        return string

N, M = get_line(int)
data = sorted(list(set(get_line(int))))

M = len(data)
if M - N <= 0:
    print(0)
    exit()
ans = sum(sorted([data[i+1] - data[i] for i in range(M-1)])[:M-N])

print(ans)

Submission status: Accepted

Solution code:
fl = input()
sl = input()

koma_num, pos_num = [int (x) for x in fl.split(" ")]
poses = [int(x) for x in sl.split(" ")]
poses.sort()

if koma_num >= pos_num:
    print("0")
    exit()

dists = []
total = 0
for n in range(pos_num-1):
    dist = poses[n+1] - poses[n]
    dists.append(dist)
    total += dist

dists.sort()
dists.reverse()

if(len(dists) == 0):
    print("0")
    exit()

for n in range(koma_num-1):
    total -= dists[n]

print(total)

Submission status: Accepted

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

if n >= m:
    print(0)
    exit()
else:
    difference = []
    x.sort()
 
    for i in range(m-1):
        difference.append(x[i+1]-x[i])
 
    difference.sort()
 
    print(sum(difference[:m-n]))

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()))
 
if N >= M:
  print(0)
  exit(0)

X = sorted(X)
L = [X[i+1] - X[i] for i in range(M-1)]
L = sorted(L, reverse=True)
L_sum = sum(L)
ans = L_sum
for i in range(N-1):
  ans -= L[i]
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#C問題現状それっぽいやつ
N,M=[int(i) for i in input().split()]
Y=list(map(int,input().split()))
X=sorted(Y)
Sa=[]
ans=0
if N>=M:
    print(0)
else:
    for i in range(1,M):
        Sa.append(X[i]-X[i-1])
    Sa.sort()
    for i in Sa[:(M-N)]:
        ans=ans+i
    print(ans)

Submission status: Accepted

Solution code:
#C問題現状それっぽいやつ
N,M=[int(i) for i in input().split()]
Y=list(map(int,input().split()))
X=sorted(Y)
Sa=[]
ans=0
if N>=M:
    print(0)
else:
    for i in range(M-1):
        Sa.append(X[i+1]-X[i])
    Sa.sort()
    for i in Sa[:(M-N)]:
        ans=ans+i
    print(ans)

Submission status: Accepted

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


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

X.sort()

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

Y.sort()

ans = 0

for i in range(M - N):
    ans += Y[i]

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if N >= M:
        print(0)

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


if __name__ == "__main__":
    main()

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

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

Submission status: Accepted

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

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

X.sort()
Y=X[1:]-X[:-1]
Y.sort()

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

Submission status: Accepted

Solution code:
line = input().split(" ")
n = int(line[0])
m = int(line[1])
line = input().split(" ")
x = []
s = []

for i in line:
    x.append(int(i))

x.sort()
count = x[m-1] - x[0]

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

s.sort(reverse=True)
print(count - sum(s[0:(n - 1)]))

Submission status: Accepted

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

import numpy as np

def calc_distance(goals):
    dist_list = []
    for i in range(len(goals) - 1):
        dist_list.append(goals[i+1] - goals[i])
    return dist_list

points_num, goal_num = map(int, input().split())

goals = list(map(int, input().split()))
goals = sorted(goals)


dist_list = calc_distance(goals)
dist_list = sorted(dist_list)

if goal_num <= points_num:
    print(0)
else:
    print(sum(dist_list[:goal_num-points_num]))

Submission status: Accepted

Solution code:
import numpy as np
import math
nm = list(map(int,input().split()))
x = list(map(int,input().split()))
xs = sorted(x)
n=nm[0]
m=nm[1]

def dis(a):
    amax = np.argmax(a)
    amin = np.argmin(a)
    if len(a) == 1:
        return(0)
    else:
        return a[amax]-a[amin]

if n == 1:
    print(dis(xs))
elif m ==1:
    print(0)
else:
    sk = [0 for i in range(m-1)]
    for i in range(m-1):
        sk[i]=xs[i+1]-xs[i]
    sks = sorted(sk)
    sksr = list(reversed(sks))
    print(dis(xs)-sum(sksr[:(n-1)]))

Submission status: Accepted

Solution code:
# - coding : utf-8 -*-
 
n, m = list(map(int, input().split()))
xs_str = input().split()
xs = list(map(int, xs_str))
xs.sort()
 
ys = []
for x1, x2 in zip(xs[0:-1], xs[1:]):
    ys.append(abs(x2 - x1))
ys.sort()

for i in range(n - 1):
    if ys == []:
        break
    else:
        ys.pop()
print(sum(ys))

Submission status: Accepted

Solution code:
#coding: utf-8
N, M = map(int, input().split())
points = list(map(int, input().split()))
if N > M:
  print(0)
else:
  points.sort()
  koma = [points[i+1] - points[i] for i in range(M-1)]
  koma.sort()
  print(sum(koma[:M-N]))

Submission status: Accepted

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


def main() -> None:
    global Y
    for i in range(m - 1):
        Y += [X[i + 1] - X[i]]

    Y.sort()
    for i in range(n - 1):
        if len(Y) == 0:
            break
        Y.pop()

    if len(Y) == 0:
        print(0)
    else:
        print(sum(Y))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(ans)

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

    if n==1:
        print(x[-1]-x[0])
    else:

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

        xsa=sorted(xsa)



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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

distance_sorted = sorted(distance)
ans = 0
for i in range(0,M-N):
	ans += distance_sorted[i]

print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  ans = 0
  if N < M:
    g = [int(a) for a in input().split()]
    g = sorted(g)
    L = g[:-1]
    R = g[1:]
    sub = [b - a for a, b in zip(L, R)]
    sub = sorted(sub)
    ans = sum(sub[:(M-N)])
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
A = []
for a,b in zip(L, L[1:]) :
    A.append(b-a)
A = sorted(A)

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

Submission status: Accepted

Solution code:

def solve(N, M, Xs):
    Xs = sorted(Xs)
    diffs = sorted([Xs[i+1] - Xs[i] for i in range(M-1)], reverse=True)
    return Xs[-1] - Xs[0] - sum(diffs[:N-1])


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    Xs = list(map(int, input().split(" ")))
    print(solve(N, M, Xs))

Submission status: Accepted

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

Submission status: Accepted

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

XS = sorted(X)
XD = []

if N > M:
    print(0)

else:
    for i in range(0, len(XS)-1):
        XD.append(XS[i + 1] - XS[i])

    XD2 = sorted(XD)
    print(sum(XD2[:M-N]))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
if(len(X)<=N):
  print(0)
else:
  #print(N,M,X)
  sort_X=sorted(X)
  #print(sort_X)
  #print(sort_X[1:],sort_X[0:len(sort_X)-1])
  X_sa=[x - y for (x, y) in zip(sort_X[1:], sort_X[0:len(X)-1])]
  #print(X_sa)
  sort_X_sa=sorted(X_sa)
  #print(sort_X_sa)
  #print(sort_X_sa[:M-N])
  print(sum(sort_X_sa[:M-N]))

Submission status: Accepted

Solution code:
n = list(map(int, input().split())) 
i = list(map(int, input().split())) 
i.sort()
if(n[0] >= n[1]):
  print(0)
else:
	k=1
	kyori=list()
	while k < n[1]: 
	  sa=i[k]-i[k-1]
	  kyori.append(sa)
	  k=k+1
	kyori.sort()
	mm=1
	owari=n[1]-n[0]
	kyori=kyori[0:owari]
	idou=sum(kyori)
	print(idou)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split(" "))
points=list(map(int,input().split(" ")))
points.sort()
score=0
#score=points[-1]-points[0]
sas=[]

for i in range(0,m-1):
  sas.append(points[i+1]-points[i])
sas.sort()

#score-=sum(sas[-n+1:]) 
if n>=m:
  print(0)
  exit()
else:
  print(sum(sas[:m-n]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
N=int(s.split()[0])
M=int(s.split()[1])

a=input()
num=[]
n=a.split()
for i in(n):
  num.append(int(i))

num.sort()
sa=[]
sa_a=[]

for i in range(len(num)-1):
  n=num[i]-num[i+1]
  if(n<0): n*=-1
  sa.append(n)
  sa_a.append(n)
sa.sort()
sa.reverse()
m=0
for i in range(N-1,len(sa)):
  m+=sa[i]
print(m)

Submission status: Accepted

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

def m():
    a = [0] * M
    b = [0] * M
    for i in range(1, M):
        a[i] = abs(X[i] - X[i-1])
        b[i] = i

    if M <= N:
        return 0
    a.sort(reverse=True)
    for i in range(N-1):
        a[i] = 0

    return sum(a)


print(m())

Submission status: Accepted

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

def diffculc(st, ed):
    if ed < st:
        tmp = ed
        ed = st
        st = tmp
    if st < 0 and ed < 0:
        diff = abs(st - ed)
    elif -1 < st and ed < 0:
        diff = abs(ed - st)
    elif st < 0 and -1 < ed:
        diff = abs(st - ed)
    elif -1 < st and -1 < ed:
        diff = ed - st
    return diff

xList = [int(x) for x in input().split()]
sortedList = sorted(xList)

# 0番目と1番目の間のdiffはdiffcache[0]
diffcache = {}
diff = 0
for i, x in enumerate(sortedList):
    if i == len(sortedList) - 1:
        break
    diffcache[i] = diffculc(sortedList[i+1], x)

rangeset = []
counter = 0
for k, v in sorted(diffcache.items(), key=lambda x: -x[1]):
    if counter == N - 1:
        break
    diffcache.pop(k)
    counter = counter + 1

result = 0
for k,v in diffcache.items():
    result = result + v
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

# 座標数よりコマ数が多ければ初期位置で条件を満たす
if N >= M:
    print(0)
    sys.exit()

# ソートする
X.sort()

# 隣り合う数の差を計算する
diff = list()
for m in range(M-1):
    diff.append(X[m+1] - X[m])

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x_s=list(map(int,input().split()))
if n >= m:
    print(0)
    exit()
x_s.sort()
sa=[]

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

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

Submission status: Accepted

Solution code:
from sys import stdin



############################################
#from sys import stdin
import numpy as np

# read data for n sequences.
input = stdin.readline().split()
m = int(input[0])
n = int(input[1])
#n = int(stdin.readline().split()[1])
data = np.zeros(n)
d = stdin.readline().split()
for i,x in enumerate(d):
    data[i] = int(x)
data = sorted(data)

dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]

out = 0

# split in m data.
#print(data)
dif = np.zeros(len(data)-1)
for x in range(0, len(data)-1):
    dif[x] = data[x+1]-data[x]
out = sum(sorted(dif, reverse=True)[m-1:])

            

print(int(out))

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()
dif = []
for i in range(m - 1):
    dif.append((x[i + 1] - x[i], i + 1))
dif.sort(key = lambda x: x[0], reverse = True)
split = set()
for i in range(n - 1):
    split.add(dif[i][1])
ans = x[-1] - x[0]
for i in split:
    ans -= x[i] - x[i - 1]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
list_x = list(map(int, input().split()))
list_x.sort()
list_d = np.diff(list_x)
list_data = list_d.tolist()
if len(list_data) != 0:
    if N >= 2:
        list_data.sort()
        del list_data[-(N-1):]
        print(sum(list_data))
    if N == 1:
        print(sum(list_data))
else:
    print(0)

Submission status: Accepted

Solution code:
import copy

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

if N > M:
    ans = 0
else:
    ans = 0
    X.sort()
    diff = []
    for i in range(len(X) - 1):
        diff.append(X[i + 1] - X[i])
    diff_orig = copy.copy(diff)
    diff.sort()
    index = []
    for i in range(N - 1):
        index.append(diff_orig.index(diff.pop()))
    index.append(-1)
    index.sort()
    i = len(index) - 1
    index.append(len(X) - 1)
    while i >= 0:
        ans += X[index[i + 1]] - X[index[i] + 1]
        i -= 1
print(ans)

Submission status: Accepted

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

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        if N > M:
            N = M
        dx = sorted([abs(X[m] - X[m + 1]) for m in range(M - 1)])
        print(sum(dx[:M - N]))


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()
    dis = []
    for i in range(M-1):
        dis.append(X[i+1]-X[i])
    dis.sort()
    p = 0
    for j in range(M-N):
        p += dis[j]
    print(p)

Submission status: Accepted

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

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

L = sorted(L)
if N >= M:
    print(0)
elif N == 1:
    print(sum(L))
else:
    print(sum(L[:-N+1]))

Submission status: Accepted

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

if N >= M:
    print(0)
elif N == 1:
    print(abs(max(X) - min(X)))
else:
    X.sort()
    X_ = [((j - i), c) for c, (i, j) in enumerate(zip(X, X[1:]))]
    X_s = sorted(X_, reverse=True)
    koma_c = sorted([t[1] for t in X_s[:N-1]])

    if koma_c[-1] != M-1:
        koma_c.append(M-1)

    p = 0
    c = 0
    for i in koma_c:
        c += X[i] - X[p]
        p = i + 1

    print(c)

Submission status: Accepted

Solution code:
import numpy as np

a1 = list(int(i) for i in input().split())  
x = list(int(i) for i in input().split())  
x = np.sort(x)
n = a1[0]

delta = []
if len(x) <= n:
    print(0)
else:
    for i in range(0,len(x) - 1):
        delta.append(x[i + 1] - x[i])

    if n == 1:
        print(np.sum(delta))
    else:
        delta = np.sort(delta)
        f = delta.size - (n - 2)
        print(np.sum(delta[0:-(n-1):]))

Submission status: Accepted

Solution code:
from sys import stdin

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

    if N >= M:
        print(0)
        return

    X.sort()
    n1 = [(i + 1, X[i+1] - X[i]) for i in range(M - 1)]
    n1.sort(reverse=1, key=lambda x: x[1])
    n2 = n1[:N-1]
    n2.sort()

    ans, i = 0, 0
    for j, _ in n2:
        ans += X[i:j][-1] - X[i:j][0]
        i = j
    ans += X[i:][-1] - X[i:][0]
    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Accepted

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

d = []
X.sort()
p = 0
for x in X:
    d.append(x-p)
    p = x 

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

if(len(d)>0):
    print(sum(d))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

xm = np.array(xm)

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

Submission status: Accepted

Solution code:
# C
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def db(x): 
    global debug
    if debug: print(x)
debug = False
N,M = getIntList()
A = getIntList()
db((N,M))
db(A)
A.sort()
db(A)
b = zeros(M-1)
for i in range(M-1):
    b[i] = A[i+1] - A[i]
db(b)
b.sort()
b.reverse()
db(b)
sm = 0
for i in range(N-1,M-1):
    sm += b[i]
print(sm)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [int(x) for x in input().split()]
X.sort()
 
D = [0 for _ in range(M-1)]
for m in range(1, M):
    D[m-1] = X[m] - X[m-1]
D.sort()

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

Submission status: Accepted

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

# AtCoder Beginner Contest 117：C - Streamline


def main():
    N, M = [int(i) for i in raw_input().strip().split(' ')]
    X = [int(i) for i in raw_input().strip().split(' ')]

    if N >= M:
        print 0
        exit()

    sortx = sorted(X)
    dist = []
    for i in range(len(sortx) - 1):
        dist.append(sortx[i+1] - sortx[i])
    sortd = sorted(dist, reverse=True)
    print sum(sortd[N-1:])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  Y=Y[:N-1]
  Y.sort()
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)

Submission status: Accepted

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

a=sorted([(x[i+1]-x[i],i) for i in range(m-1)])[::-1]
s=0
v=0
for i,j in sorted(a[:n-1],key=lambda x:x[1]):
    v+=x[j]-x[s]
    s=j+1
if s<m:
    v+=x[-1]-x[s]
print(v)

Submission status: Accepted

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

sorted_diff = sorted(diff)
sorted_diff.reverse()

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

Submission status: Accepted

Solution code:
# 整数の入力
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()

# 距離の和が最小：範囲間の隙間が最大値だとよい。
# よって、ソートしたでかい順に引けばよい
X.sort()


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

    sa = X[M-1]-X[0]

    
    ans = sa
    
    for i in range(N-1):
        ans = ans-Y[M-i-2]
        

    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().strip().split(" ")]
if n >= m:
    result = 0
else:
    xs = sorted([int(i) for i in input().strip().split(" ")])
    ls = []
    for i in range(m-1):
        ls.append(xs[i+1] - xs[i])
    ls = sorted(ls)
    result = sum(ls[:m - n])

print(result)

Submission status: Accepted

Solution code:
N, M = input().split()
X = input().split()
deff = []
ans = 0

for i in range(len(X)):
    X[i] = int(X[i])

if int(N) >= int(M):
    print(0)
else:
    X.sort()

    for i in range(len(X)):
        if i == 0:
            pass
        else:
            deff.append(X[i] - X[i-1])

    deff.sort()

    for i in range(int(M) - int(N)):
        ans += deff[i]

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
X = sorted([int(_) for _ in input().split()])
D = sorted([X[i + 1] - X[i] for i in range(M - 1)])
print(X[-1] - X[0] - sum(D[max(M - N,0):]))

Submission status: Accepted

Solution code:
NM = list(map(int,input().split()))
N,M = NM[0], NM[1]
li = list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint as pp
N, M = map(int, input().split())
Xm = list(map(int, input().split()))

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

# center
Xm.sort()
subt = []
for i in range(M-1):
    subt.append(abs(Xm[i+1]-Xm[i]))

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().strip().split())
lis = map(int, raw_input().strip().split())
lis = sorted(lis)
lol = []
for i in range(1, m):
    lol.append(lis[i] - lis[i-1])
lol = sorted(lol)
ans = lis[-1] - lis[0]
idx = len(lol) - 1
if idx >= 0:
    for i in xrange(n-1):
        ans -= lol[idx]
        idx-=1
        if idx == -1:
            break
print ans

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

diff=np.diff(X_list, n=1)

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

sorted_diff=np.sort(diff)

cnt=np.sum(sorted_diff[:M-N])

print(cnt)

Submission status: Accepted

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

if(N>M):
    N=M
if(M==1):
    print(0)
    exit()
for i in range(0,len(x)-1):
    leng.append([x[i+1]-x[i],i])
leng=sorted(leng)

n=N
max=len(leng)-1
sets=list()
while n>1:
    n-=1
    sets.append(leng[max][1]+1)
    max-=1

for i in x:
    if(x.index(i) not in sets):
        sets.append(x.index(i))
        break
sets=sorted(sets) 
ans=0
for i in range(0,len(sets)-1):
    ans+=x[sets[i+1]-1]-x[sets[i]]

ans+=x[-1]-x[sets[-1]]

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
k=[]
for i in range(1,m):
    dif=l[i]-l[i-1]
    k.append(dif)
k.sort()
ans=0
for i in range(m-n):
    ans+=k[i]
print(ans)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().rstrip().split()))
X = list(map(int, input().rstrip().split()))
if N >= M:
    cnt = 0;
elif N == 1:
    cnt = max(X) - min(X)
else:
    X.sort()
    D = []
    for i in range(1, len(X)):
        D.append(X[i] - X[i - 1])
    D.sort()
    del D[-(N - 1):]
    cnt = sum(D)
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

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

l = [int(i) for i in input().split()]
l.sort()
if n == 1 or m == 1:
    print(l[-1]-l[0])
elif n > m:
    print(0)
else:
    d = [0 for i in range(m - 1)]
    for i in range(m-1):
        d[i] = l[i+1] - l[i]
    d = np.array(d)
    dl = d.argsort()[::-1]
    count = 0
    for i in range(n):
        if i == 0:
            count += l[dl[i]] - l[0]
            index = dl[i]
        elif i == n - 1:
            count += l[-1] - l[index+1]
        else:
            count += l[dl[i]] - l[index+1]
            index = dl[i]
    print(count)

Submission status: Accepted

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

    if (M < N):
        print(0)
        return
    X.sort()
    dx = []
    for i in range(M-1):
        dx.append(abs(X[i] - X[i+1]))
    dx.sort()
    
    res = 0
    for i in range(M-N):
        res += dx[i]
    print(res)

main()

Submission status: Accepted

Solution code:
nm = list(map(int, input().split()))
lis = list(map(int, input().split()))

lis.sort()

sum = 0
diff = []

if nm[1] - nm[0] <= 0:
    print(0)
else:
    for i in range (nm[1] - 1):
        diff.append(lis[i + 1] - lis[i])

    diff.sort()

    if nm[0] != 1:
        for j in range (nm[0] - 2):
            diff.pop(-1)

    for k in range (nm[1] - nm [0]):
        sum = sum + diff[k]

    print(sum)

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    xs.sort()
    diffs = [xs[i] - xs[i-1] for i in range(1, m)]
    if n == 1:
        ans = sum(diffs)
    else:
        indxs = list(np.array(diffs).argsort()[::-1][:n-1])
        indxs.sort()
        ans = sum(diffs[0:indxs[0]])
        if indxs[-1]+1 < len(diffs):
            ans += sum(diffs[indxs[-1]+1:])
        for i in range(len(indxs)-1):
            ans += sum(diffs[indxs[i]+1:indxs[i+1]])
    print(ans)

Submission status: Accepted

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

if(M <= N):
  print(0)
  
else :  
  for i in range(len(X)-1):
    sub = abs(X[i+1] - X[i])
    Y.append(sub)
  Y.sort()
  Y.reverse()
  xid = 0
  for i in range(N-1):
    Z.append(Y[i])

  ans = sum(Y)-sum(Z)
  if ans < 0:
    ans = 0
  print(ans)

Submission status: Accepted

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

X.sort()

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

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

Submission status: Accepted

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

Submission status: Accepted

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