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:
# C
import numpy as np
N, M = list(map(int, input().split()))
X = np.array(list(map(int, input().split())))

if N >= M:
    re = 0
else:
    X = np.sort(X) # 小さい順に並べる
    diff = np.insert(X[1:], X.size - 1, 0) - X
    re = diff[0:X.size-1].sum()
    diff_sort = np.sort(diff[0:X.size-1])[::-1]
    for i in range(N - 1):
        re -= diff_sort[i]
        
print(re)

Submission status: Accepted

Solution code:
n,m=list(map(int,input().split()))
l=list(map(int,input().split()))
l.sort()
d=[]
for i in range(0,m-1):
    d.append(l[i+1] - l[i])
d.sort()
for i in range(0,n-1):
    ld = len(d)
    if ld == 0:
        print(0)
        import sys
        sys.exit()
    d.pop(ld-1)
print(sum(d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
Y=input()#1行に沢山の数値(または文字)が並んでいる場合にリストに格納していく
y=input()
X=[]
x=[]
X=Y.split(" ") #数値YをリストXに格納
x=y.split(" ")
Z=[int(i) for i in X] #リストXの要素を"文字"から"数値"に変換
z=[int(j) for j in x]
z.sort()
N=Z[0]
M=Z[1]
a=z[M-1]-z[0]
b=[]
c=0
if N<M :
    for i in range(0,M-1):
        b.append(int(abs(int(z[i+1])-int(z[i]))))
    b.sort()
    b.reverse()
    for j in range(0,N-1):
        c=b[j]+c
    print(a-c)
else :
    print('0')

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
t,x = LI()
print(t/x)
"""

#B
"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""

#C
n,m = LI()
x = LI()
x.sort()
l = [x[i+1]-x[i] for i in range(m-1)]
l.sort()
l = l[::-1]
ans = sum(l[max(n-1,0):])
print(ans)
#D
"""
n,k = LI()
a = LI()
l = 0
b = [0 for i in range(n)]
for i in range(n):
    b[i] = list(bin(a[i]))[2:]
    l = max(l,len(b[i]))
s = [0 for i in range(l)]
for i in range(n):
    for j in range(l-len(b[i])):
        b[i].insert(0,"0")
for i in range(n):
    for j in range(l):
        s[j] += 1-int(b[i][j])
ke = 1
for i in range(l)[::-1]:
    if s[i] <= n//2:
        s[i] = [(n-s[i])*ke,0,float("inf")]
    else:
        s[i] = [(n-s[i])*ke,s[i]*ke,ke]
    ke *= 2
s.sort(key = lambda x:x[0])
d = 0
ans = 0
key = len(list(bin(k)))-2
key -= l
ke = 2**l
print(s)
for i in range(key):
    d += ke
    ans += n*ke
    ke *= 2
for i in range(l):
    r,q,p = s[i]
    if d+p <= k:
        d += p
        ans += q
    else:
        ans += r
print(ans)
"""
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,m = [int(i) for i in 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[:max(m-n,0)]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
a = X[0]
d = []
for i in X[1:]:
    d.append(i - a)
    a = i
d.sort()
if N < M:
    print(sum(d[:M - N]))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

x = input().split()

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

x.sort()

if(n>=m):
    print(0)
else:
    dis = []
    for i in range(len(x)-1):
        dis.append(x[i+1] - x[i])

    dis.sort(reverse=True)
    for i in range(n-1):
        dis[i] = 0

    sum = 0
    for i in dis:
        sum += i

    print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

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

a.sort()

su = 0

b = [[0,0] for i in range(n-1)]
for i in range(n-1):
	b[i][0] = a[i+1]-a[i]
	b[i][1] = i
	su += a[i+1]-a[i]
b.sort(key = lambda x:x[0])
b.reverse()

if m >= n:
	print(0)
else:
	for i in range(m-1):
		su -= b[i][0]
	print(su)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
data = input().split()  
n = int(data[0])
m = int(data[1])
x = [int(s) for s in input().split()]

x.sort()

y=[]
tmp=0
for i in x:
    y.append(i-tmp)
    tmp=i
y.pop(0)

y.sort()
y.reverse()

res=x[-1]-x[0]

if(len(y)+1 < n):
    n = len(y)+1

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

print(res)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
x_str = input().split()
X = [0]*M
for i in range(M):
    X[i] = int(x_str[i])
X.sort()
if N >= M:
    print("0")
else:
    #隣の点との差分
    dx = [0]*(M-1)
    for j in range(M-1):
        dx[j] = X[j+1] - X[j]
    dx.sort()
    dx.reverse()
    length = 0
    for k in range(N-1,M-1):
        length += dx[k]
    print(length)

Submission status: Accepted

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

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

distance = sorted(distance, reverse=True)
moved = 0
for j in range(n-1, len(distance)):
    moved = moved + distance[j]

print(moved)

Submission status: Accepted

Solution code:
N, M = map(int, raw_input().split())
X = sorted(map(int, raw_input().split()))
D = [X[i+1]-X[i] for i in range(M-1)]
indexes = reversed([D.index(x) for x in sorted(D)[-(N-1):]])
start = 0
ans = 0
for index in indexes:
  ans += X[index]-X[start]
  start = index+1
ans += X[-1]-X[start]
if N == 1:
  print X[-1]-X[0]
else:
  print ans

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X_arr = list(map(int, input().split()))
X_sorted  = sorted(X_arr)
X_diff = []
for i in range(M - 1):
    X_diff.append(X_sorted[i + 1] - X_sorted[i])
X_diff_sorted  = sorted(X_diff)
for n in range(N - 1):
    if X_diff_sorted == []:
        break
    else:
        X_diff_sorted.pop()
print(sum(X_diff_sorted))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
N,M=[int(x) for x in stdin.readline().rstrip().split()]
data=[int(x) for x in stdin.readline().rstrip().split()]
if N >= M:
    ans=0
else:
    data=np.array(sorted(data))
    diff=sorted(np.diff(data))
    ans=sum(diff[0:len(diff)-N+1])
print(ans)

Submission status: Accepted

Solution code:
s = list(map(int, input().split()))
t = list(map(int, input().split()))

if(s[0] >= s[1]):
  print(0)
else:
  t.sort()
  u=[]
  for i in range(s[1]-1):
    u.append(t[i+1]-t[i])
  u.sort()
  total=0
  for i in range(s[1]-s[0]):
    total+=u[i]
  print(total)

Submission status: Accepted

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

if n>=m:
    print(0)
    exit()
if n==1:
    print(max(xl)-min(xl))
    exit()


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

res = sum(l) - sum(l[0:n-1])
print(res)

Submission status: Accepted

Solution code:
import numpy as np

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

xs=sorted(x)
# xs

# def sabun(x,i):
#     return x[i+1]-x[i]

def sabun(x):
    a=np.array(x)
    return list(a[1:]-a[:-1])

# sabunx=[sabun(xs,i) for i in range(m-1)]

sabunx=sabun(xs)
# sabunx

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  3 21:15:56 2019

@author: doppe
"""

A, B = map(int, input().split())
L = [int(i) for i in input().split()]

if (A >= B):
    print(0)
else:
    saList = []
    L.sort()    
    for i in range(0, len(L) -1):
        saList.append(L[i + 1] - L[i])
    li1 = sorted(range(len(saList)), key=lambda k: saList[k])

    splitChiten = []
    for i in range(0, A - 1):
        splitChiten.append(li1[len(li1) - 1 - i])
    splitChiten.sort()
    sum = 0
    j = 0
    for i in range(0, len(splitChiten)):
        sum += (L[splitChiten[i]] - L[j])
        j = splitChiten[i] + 1
    
    sum += (L[len(L) - 1] - L[j])
    print(sum)

Submission status: Accepted

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

xs = input()
xs = xs.split()
for i in range(m):
    xs[i] = int(xs[i])
xs = sorted(xs)

if (n>=m):
    print(0)
else:
    diff= []
    for i in range(m-1):
        diff.append(xs[i+1]-xs[i])
    s = np.argsort(diff)
    loop = 0
    total = sum(diff)
    if n == 1:
        print(total)
    else:
        for i in range(n-1):
            total -= diff[s[-(i+1)]]
        print(total)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

n, m = map(int, stdin.readline().rstrip().split())
x = [int(x) for x in stdin.readline().rstrip().split()]

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

Submission status: Accepted

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

x.sort()
y = [abs(x2 - x1) for x1, x2 in zip(x[1:], x[:-1])]
y.sort()

if n == 1:
    print(sum(y))
else:
    print(sum(y[:-(n-1)]))

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
if N >= M:
    res = 0
else:
    array = np.array(sorted([int(i) for i in input().split()]))
    res = np.sort(np.diff(array, n=1))[::-1]
    res = sum(res[N-1:])
print(res)

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
x.sort()
a=[]
if m==1 or n>=m:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    if n==1:
        print(max(x)-min(x))
    else:
        result=0
        for i in range(n-1):
            result+=a[i]
        print(max(x)-min(x)-result)

Submission status: Accepted

Solution code:
def solve():
    [N, M] = [int(x) for x in input().split()]
    X = sorted([int(x) for x in input().split()])

    delta = sorted([X[i]-X[i-1] for i in range(1, M)], reverse=True)

    return sum(delta[N-1:])


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
poti=list(input().split())
poti= [ int(s) for s in poti ]
poti=sorted(poti)
sa=[]
if N>=M:
    print(0)
else:
    for i in range(0,M-1):
        a=poti[i+1]-poti[i]
        sa.append(a)


    if len(sa)!=0:  
        sa=sorted(sa)
        sa=sa[0:(M-1)-(N-1)]
        print(sum(sa))
    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()

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

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

d.sort()
s = sum(d)
print(s-sum(d[-(n-1):]))

Submission status: Accepted

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

if M <= N:
	print(0)
elif M == 2:
	if N == 1:
		X_list = [int(_) for _ in list(input().split())]
		print(abs(X_list[0]-X_list[1]))
	else:
		print(0)
else:
	X_list = [int(_) for _ in list(input().split())]

	X_list.sort()
	X_list.reverse()

	X_diff_list = [X_list[i-1]-X_list[i] for i in range(1, M)]
	X_diff_list.sort()
	X_diff_list.reverse()

	answer = sum(X_diff_list)

	if N == 1:
		print(answer)
	else:
		for n in range(N-1):
			answer -= X_diff_list[n]
		print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nm=[int(x) for x in input().split()]
x=[int(y) for y in input().split()]
margin=[]
sort_x=sorted(x)
for p in range(nm[1]-1):
    margin.append(sort_x[p+1]-sort_x[p])
sort_mar=sorted(margin)
if(nm[0]>=nm[1]):
    print(0)
else:
    print(sum(sort_mar[:nm[1]-nm[0]]))

Submission status: Accepted

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))


b = sorted(b)
x = [[b[0],0]]

for i in range(1,a[1]):
    x.extend([[b[i],b[i]-b[i-1]]])

x = sorted(x ,key = lambda x: -x[1])

for i in range(a[1]):
    if i < a[0]-1:
        x[i].append(1)
    else:
        x[i].append(0)

x = sorted(x ,key = lambda x: x[0])

x[0][2] = 1

tmp = 0
res = 0

for i in range(1,a[1]):
    if x[i-1][2] == 1:
        tmp = x[i-1][0]
    if x[i][2]   == 1:
        res = res - tmp + x[i-1][0]
    elif i+1 == a[1]:
        res = res - tmp + x[i][0]
        
print(res)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
X = [int(x) for x in input().split()]
X.sort(reverse=True)
valX = [X[x] - X[x + 1] for x in range(M - 1)]
valX.sort(reverse=True)

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

if N == 1:
    print(max(X) - min(X))
    exit()

for i in range(N - 1):
    valX[i] = 0

print(sum(valX))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

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 LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, m = LI()
X = LI()

X.sort()
# print(X)
C = []
for i in range(m - 1):
    C.append(abs(X[i + 1] - X[i]))

# print(C)
C.sort()
C.reverse()
ans = 0
for i in range(n - 1, m):
    if i >= m - 1:
        break
    ans += C[i]
    # print(ans)

print(ans)

Submission status: Accepted

Solution code:
import itertools
n,m = map(int,input().split())
zahyou_list = [i for i in map(int,input().split())]
zahyou_list.sort()
#print(zahyou_list)
kaisa_list = []

#print(sum(kaisa_list))

for i in range(len(zahyou_list)-1):
    kaisa_list.append(zahyou_list[i+1] - zahyou_list[i])
mini = sum(kaisa_list)
kaisa_list.sort()
#print(kaisa_list)
if n >= m:
    print(0)
elif n == 1:
    print(sum(kaisa_list))
else:
    print(sum(kaisa_list[:-(n-1)]))

Submission status: Accepted

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

tmp = sorted([X[i+1] - X[i] for i in range(M-1)])
if(tmp != []):
  if(len(tmp) == 1):
    print(*tmp)
  elif(N == 1):
    print(sum(tmp))
  else:
    print(sum(tmp[:-(N-1)]))
else:
  print(0)

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(m-1)]
diff.sort(reverse=True)

diffsum = 0
for i in range(m-1):
    diffsum = diffsum + diff[i]

if n >= m:
    print(0)
else:
    for j in range(n-1):
        diffsum = diffsum - diff[j]
    print(diffsum)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split()))) # 昇順に並び替え
diff = sorted([abs(e1 - e2) for e1, e2 in zip(x, [x[0]]+x[:-1])], reverse = True)

# 2個目以降はコストが大きいものから消えていくと考える
if n >= m:
    res = 0
else:
    res = sum(diff[n-1:])

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

d = []

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

res = sum(d) - sum(d[0:N-1])

print(res)

Submission status: Accepted

Solution code:
def distance_to_right(p, point_list):
    if p == len(point_list) - 1:
        return None
    else:
        return point_list[p+1] - point_list[p]


# main

num_koma, num_to_visit = [int(element) for element in input().split(' ')]
points = [int(element) for element in input().split(' ')]

points.sort()

costs = [distance_to_right(p, points) for p in range(0, num_to_visit-1)]
costs.sort()

if num_koma == 1:
    print(sum(costs))
else:
    print(sum(costs[:-(num_koma-1)]))

Submission status: Accepted

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

X.sort()

if N >= len(X):
    print(0)
    quit()

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

dif.sort(reverse=1)
sum = sum(dif)

for i in range(N-1):
    sum = sum - dif[i]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
array = list(map(int, input().strip().split(' ')))
array.sort()
array2 = []
for i in range(M-1):
  array2.append(array[i+1] - array[i])
array2.sort()
array2.reverse()
del array2[0:N-1]
sum = 0
for i in range(M-N):
  sum = sum + array2[i]
print(sum)

Submission status: Accepted

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

dist = []
for i in range(len(x)-1):
    dist.append([x[i+1]-x[i],i])
sorted_dist = sorted(dist,key=lambda x:x[0])
top_n = sorted(sorted_dist[-1*n+1:],key=lambda x:x[1])

total = 0
idx = 0
for _,i in top_n:
    total += abs(x[i] - x[idx])
    idx = i+1
total += abs(x[-1] - x[idx])

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

Submission status: Accepted

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

x_sort = sorted(x)
check = []
for i in range(len(x)-1):
    check.append(abs(x_sort[i+1] - x_sort[i]))

check_sorted = sorted(check)
ans = 0
if (m-n) >= 0:
    for i in check_sorted[0:m-n]:
        ans += i
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
spots_and_people = input().split()
spot_values = input().split()

N_spot = int(spots_and_people[1])
N_people = int(spots_and_people[0])

spot_values = [ int(value) for value in spot_values ]
spot_values.sort()

line_length = [ spot_values[i+1] - spot_values[i] for i in range(N_spot-1) ]
line_length.sort()


if N_spot <= N_people:
	travel = 0

else:
	for i in range(N_people-1):
  		line_length.pop(-1)
  

	travel = sum(line_length)

print(travel)

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:
    sub_X = []
    for i in range(0, M - 1):
        sub_X.append((i, abs(X[i+1] - X[i])))
    sub_X.sort(key=lambda x: x[1], reverse=True)

    division = set()
    if len(sub_X) != 0:
        for i in range(N-1):
            division.add(sub_X[i][0])
    ans = 0
    start = X[0]
    for i in range(M):
        if i in division:
            ans += X[i] - start
            start = X[i+1]
        if i == M-1:
            ans += X[i] - start

    print(ans)

Submission status: Accepted

Solution code:

N,M = map(int, input().split(" "))

X = []

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

X.sort()
#print(X)

Y = []

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

Y.sort()
#print(Y)

kazu = len(Y)

kazu= kazu- (N-1)

ans2 = 0
for j in range(kazu):
    ans2 = ans2 + Y[j]

print(ans2)

Submission status: Accepted

Solution code:
import numpy as np

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

X = sorted(X)
X = np.array(X)
diff = np.diff(X)
diff = np.sort(diff)[::-1]

#print(X)
#print(diff)

if N >= M:
  ans = 0
elif N == 1:
  ans = np.sum(diff)
else:
  ans = np.sum(diff[N-1:])

print(ans)

Submission status: Accepted

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

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

y.sort()


'''
for i in range(n-1):
    y.remove(max(y))
'''
if m > n: w = sum(y[:m-n])
else: w = 0
print(w)

Submission status: Accepted

Solution code:
def solve(n, m, xl):
    s = sorted(xl)
    l = s[:-1]
    r = s[1:]
    diff = []
    for i in range(len(l)):
        diff.append(r[i] - l[i])
    max_list = list(reversed(sorted(diff)))
    all_length = s[-1] - s[0]
    cur = all_length
    for i in range(n-1):
        if i >= len(max_list):
            break
        cur = cur - max_list[i]

    if cur < 0:
        cur = 0
    print(cur)

def main():
    n, m = map(int, input().split())
    xl = list(map(int, input().split()))
    solve(n, m, xl)        

main()

Submission status: Accepted

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


N, M = [int(elem) for elem in fin().split()]
X_list = sorted([int(elem) for elem in fin().split()])
min_X = X_list[0]
max_X = X_list[-1]
exists = [False] * (max_X - min_X + 1)
for X in X_list:
    X -= min_X
    exists[X] = True

distances = []
last_idx = 0
for i, flag in enumerate(exists[1:], start=1):
    if flag:
        distances.append(i - last_idx - 1)
        last_idx = i

distances.sort(reverse=True)
# print(distances)
# print(len(exists))
minimum_moves = max(0, max(0, len(exists) - N) - sum(distances[:N - 1]))
# minimum_moves = max(0, len(exists) - N) - sum(distances[:N - 1])
print(minimum_moves)

Submission status: Accepted

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

X.sort()
difX = [abs(i-j) for i,j in zip(X[1:],X[:-1])]
difX.sort()
if n == 1:
    ans = sum(difX)
else:
    ans = sum(difX[:-(n-1)])
print(ans)

Submission status: Accepted

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

sa = sorted(sa)


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

Submission status: Accepted

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

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

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

x.sort()

ans = 0
for j in range(0, len(x)-(N-1)):
    ans += x[j]

print(ans)

Submission status: Accepted

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

if N==1:
    print(X[-1] - X[0])
else:
    D = [X[i+1]-X[i] for i in range(len(X)-1)]
    #print(D)
    D.sort()
    print(sum(D[:-(N-1)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X = sorted(X)

if len(X) <= N:
	print(0)

else:
	l = []
	for n in range(len(X)):
		if n != 0:
			l.append(abs(X[n - 1] - X[n]))

	ans = 0
	l = sorted(l, reverse=False)
	for i in range(M - N):
		ans += l.pop(0)

	print(ans)

Submission status: Accepted

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

dis = [(L[i+1] - L[i],i) for i in range(M-1)]
dis1 = [L[i+1]-L[i] for i in range(M-1)]
dis = sorted(dis,key=lambda x:(x[0],-x[1]))[::-1]
l = [0]
for i in range(N-1):
    try:l.append(dis[i][1]+1)
    except:pass
#print(l,dis1)
l = sorted(l)
ans = 0
cnt = 0
for i in range(len(l)-1):
    cnt +=1
  #  print(dis1[l[i]:l[i+1]-1],l[i],l[i+1])
    ans += sum(dis1[l[i]:l[i+1]-1])
 #   print(ans)
ans += sum(dis1[l[cnt]:])
print(ans)

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()
xd = sorted(np.diff(x))
print(sum(xd[:m-n]))

Submission status: Accepted

Solution code:
""" AtCoder """

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

X.sort()

Y = []
ans = 0

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

Y.sort()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

li = sorted(list(map(int,input().split())),reverse=True)
if n>=m:
    print(0)
    
else:
    long=max(li)-min(li)
    dis=[li[i]-li[i+1] for i in range(m-1)]
    dis_sort=sorted(dis,reverse=True)
    print(sum(dis_sort[n-1:]))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
coods = list(li())
if len(coods) == 1:
    print(0)
elif N >= M:
    print(0)
else:

    coods = sorted(coods)
    deltas = []
    former = coods[0]
    for cood in coods:
        delta = cood - former
        deltas.append(delta)
        former = cood

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

    print(total_dis)

Submission status: Accepted

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

else:
  print(sum(B[:-n+1:]))

Submission status: Accepted

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

l = sorted([int(i) for i in input().split()])

d_l = []
if n >= m:
  print(0)
else:
  for i in range(m-1):
    delta = l[i+1] - l[i]
    d_l.append(delta) 
  d_l = sorted(d_l, reverse=True)
  print(sum(d_l[n-1:]))

Submission status: Accepted

Solution code:
#A

#B
"""
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = sum(l) - 2*l[-1]
if ans > 0:
    print("Yes")
else:
    print("No")
"""

#C
import bisect
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
xb = [abs(x[i] - x[i + 1]) for i in range(m-1)]
xb.sort(reverse=True)
ans = sum(xb[n - 1::])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
array = sorted([int(x) for x in input().split()])
tmp = array[0]
tmp_array = []
for i in range(len(array)):
    tmp_array.append(array[i]-tmp)
    tmp = array[i]

tmp_array = sorted(tmp_array)
print(sum(tmp_array[:-n+1]) if n != 1 else sum(tmp_array))

Submission status: Accepted

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

X = sorted(X)
gaps = sorted([X[i+1] - X[i] for i in range(M-1)], reverse=True)

if N >= M:
    print(0)
else:
    print(X[-1] - X[0] - sum(gaps[:N-1]))

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split(' ')]
X = [int(x) for x in input().split(' ')]
X = sorted(X)
dists = []
for i in range(len(X)-1):
    dist = X[i+1] - X[i]
    dists.append(dist)
dists = sorted(dists)

result = 0

for i in range(len(dists)-N+1):
    result += dists[i]

print(result)

Submission status: Accepted

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

ans = sum(sorted(sub)[:m-n+1])
if n>= m:
    ans = 0
print(ans)

Submission status: Accepted

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

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

x.sort()

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

z.sort()
for _ in range(n - 1):
    if len(z) > 0:
        z.pop()

print(sum(z))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C
# 
# input
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
sa = []
for i in range(m-1):
  sa.append(x[i+1]-x[i])
sa.sort()
g = 0
# calc
if n >= m:
  None
else:
  for j in range(m-n):
    g += sa[j]
# output
print(g)