Problem:


Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Accepted

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

Submission status: Accepted

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

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

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

xs.sort()

ls = [xs[i] - xs[i - 1] for i in range(1, len(xs))]
ls.sort()

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

Submission status: Accepted

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

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

ds.sort()
to = m - n

if to > 0:
    print(sum(ds[0:to]))
else:
    print(0)

Submission status: Accepted

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

    x.sort()

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

    L=[0]*(m-1)

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

    L.sort(reverse=True)

    ans=sum(L[n-1:])


    return ans
print(stream())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc_sum(width, wall):
    for i in wall:
        width -= Xs[i] - Xs[i-1]
    return width

N, M = map(int, input().split())
Xs = sorted([int(i) for i in input().split()])
diff_ls = [Xs[i+1] - Xs[i] for i in range(M-1)]
max_diff = sorted(diff_ls, reverse=True)[:N-1]
print(Xs[-1] - Xs[0] - sum(max_diff))

Submission status: Accepted

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

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

diff.sort(reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit, exit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

n, m = getInts()
x = sorted(getInts())

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

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

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

Submission status: Accepted

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

Xd = [0]
for i in range(len(X)-1):
    Xd.append(X[i+1]-X[i])
Xd.sort()

# 幅の小さい順にソートして、小さい方から移動にする
if N >= M:
    print(0)
else:
    print(sum(Xd[0:M-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(M <= N):
  print(0)
else:
  print(sum(tmp[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

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

x_list = sorted(x_list, reverse = True)
diff_list = [0] * (M - 1)

ans = 0
if M > N:
    for i in range(M-1):
        diff_list[i] = x_list[i] - x_list[i+1]

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

Submission status: Accepted

Solution code:
# C - Streamline


def main():
    s1 = input()
    s2 = input()
    ret = solve(s1, s2)
    print(ret)


def solve(s1, s2):
    N, M = list(map(int, s1.split()))
    X = list(map(int, s2.split()))
    if (N >= M):
        return 0
    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]
    return ct


main()

Submission status: Accepted

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

x.sort()

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

sub_array.sort()

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

print (sum(sub_array))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys

N, M = [int(x) for x in input().split()]
nums = list(set([int(x) for x in input().split()]))

if not nums:
    print(0)
    sys.exit()

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

nums.sort()

rank = [(abs(nums[i] - nums[i-1]), (i-1, i)) for i in range(1, len(nums))]
rank = sorted(rank, reverse=True)[:N-1]

pos = [0] + [pos[1] for dis, pos in rank]
pos.sort()
ans = 0
for i in range(len(pos)):
    if i == len(pos)-1:
        ans += abs(nums[pos[i]] - nums[-1])
    else:
        ans += abs(nums[pos[i]] - nums[pos[i+1]-1])

print(ans)

Submission status: Accepted

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

if(n>=m):
  print('0')
  exit(0)
l.sort()
 
gap = [l[i+1]-l[i] for i in range(m-1)]
 
gap.sort()

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

Submission status: Accepted

Solution code:
list1 = list(map(int, input().split()))
N = list1[0]
M = list1[1]
list2 = list(map(int, input().split()))
if list1[0] - list1[1] >= 0:
    print(0)
    exit(0)
list2 = sorted(list2, reverse=True)
# print(list2)
list3 = []
for i in range(len(list2) - 1):
    list3.append(list2[i] - list2[i + 1])
list3 = sorted(list3, reverse=True)
# print(list3)
sum = 0
for i in range(N - 1):
    sum = sum + list3.pop(0)
ans = list2[0] - list2[-1] - sum
print(ans)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split(' ')]
XP = [int(x) for x in input().split(' ')]
XP.sort()
D = [0] * (len(XP)-1)
for x in range(len(XP)-1):
    d = XP[x+1] - XP[x]
    D[x] = d
D.sort()
s = 0
for i in range(len(D) - N + 1):
    s += D[i]
print(s)

Submission status: Accepted

Solution code:
import math
N,num = map(int, input().split())
list = [int(i) for i in input().split()]
if N >= num:
    print(0)
else:
    
    list.sort()
    list2 = []
    for i in range(0,num-1):
        list2.append(abs(list[i+1] - list[i]))
    list2.sort()
    print(sum(list2[:num-N]))

Submission status: Accepted

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

X.sort()
array = [0] * (M-1)
for i in range(M-1):
    array[i] = X[i+1] - X[i]
array.sort()
counter = 0
for i in range(M-N):
    counter += array[i]
print(counter)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
if n >= m:
  print(0)
else:
  ls = list(map(int, input().split()))
  ls.sort()
  #print(ls)

  difls= []
  for h in range(len(ls)-1):
    difls.append(ls[h+1]-ls[h])
  difls.sort(reverse=True)
  #print(difls)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().strip().split())
xs=[i for i in map(int,input().strip().split())]
xs.sort()
if n>=m:
    print(0)
else:
    ls=[xs[i+1]-xs[i] for i in range(len(xs)-1)]
    ls.sort()
    ans=xs[-1]-xs[0]
    for i in range(n-1):
        d=ls.pop()
        ans-=d
    print(ans)

Submission status: Accepted

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

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

print (sum)

Submission status: Accepted

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

Submission status: Accepted

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

if n < m:
    l = sorted([b - a for a, b in zip(xs, xs[1:])])
    step = sum(l[:1-n]) if n > 1 else sum(l)

print(step)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    length = abs(X[-1] - X[0])

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

    t = sum(sorted(space, reverse=True)[:N - 1])
    print(length - t)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C Streamline
n,m = map(int,input().split())
Xs = list(map(int,input().split()))

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

Xs.sort()
XsDist = [b - a for a, b in zip(Xs, Xs[1:])]

XsDist.sort()
print(sum(XsDist[:m - n]))

Submission status: Accepted

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

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

    M.sort(reverse=True)
    O = sum(M[0:X-1])
    print(sum(M)-O)

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 = [x[i+1] - x[i] for i in range(m-1)]
    d.sort()
    #print(d)
    print(sum(d[:m-n]))

Submission status: Accepted

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

list_x.sort()
list_d=[]

if m<=n:
    print(0)
else:
    i=0
    while i<=m-2:
        list_d.append(list_x[i+1]-list_x[i])
        i+=1
    list_d.sort()
    print(sum(list_d[0:m-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)
    exit()

X.sort()
D = []

for i in range(m-1):
    D.append(X[i+1] - X[i])

D.sort()

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,M  = map(int,input().split(" "))
num = input().split(" ")
ans = 0
for i, x in enumerate(num):
    t = int(x)
    num[i] = t
num.sort()
kyori = []
for i in range(M-1):
    x = num[i+1]-num[i]
    kyori.append(x)
kyori.sort()
l = len(kyori)-(N-1)
for i in range(l):
    ans +=kyori[i]
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    A.sort()

    if N >= M:
        print(0)   
        return

    D = M - N

    B = []
    for i in range(1, len(A)):
        B.append(A[i] - A[i-1])

    B.sort()

    print(sum(B[:D]))

main()

Submission status: Accepted

Solution code:
import sys
n = map(int, raw_input().split())
x = map(int, raw_input().split())
if n[0]>n[1]:
    print(0)
else:
    x.sort()
    ma = max(x)
    mi = min(x)
    z=[]
    for y in range(len(x)-1):
        z.append(x[y+1]-x[y])
    z.sort()
    z.reverse()
    z=z[:n[0]-1]
    print(ma-mi-sum(z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
L = input()
X = input()
L = L.split()
X = X.split()
for i, val in enumerate(L):
    L[i] = int(val)
for i, val in enumerate(X):
    X[i] = int(val)
times = 0
N = L[0]
M = L[1]
if N >= M:
    times = 0
    print(times)
else:
    #count distance between Xi
    X.sort()
    dis = []
    for i in range(M-1): # M>1
        distance = X[i+1]-X[i]
        dis.append(distance)
    dis.sort()
    times = 0
    for i in range(M-N):
        times += dis[i]
    print(times)

Submission status: Accepted

Solution code:
# import bisect
# import heapq
# import math
# import random
# from collections import Counter, defaultdict, deque
# from decimal import Decimal
# from functools import lru_cache, reduce
# from itertools import combinations, combinations_with_replacement, product, permutations
# from operator import add, mul, sub, itemgetter
# import numpy as np

import sys
sys.setrecursionlimit(10000)

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time
	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def read_int(input=None):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [int(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret

def read_float(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [float(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
	
def read_str(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [x for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
		
def read_int(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return int(read)

def read_int_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [int(x) for x in read.split()]

def read_float(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return float(read)

def read_float_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [float(x) for x in read.split()]

def read_str(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return str(read)

def read_str_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [str(x) for x in read.split()]	

@mt
def slv(*args):
	X.sort()
	
	'''
	Y = []
	for i in range(M-1):
		Y.append(X[i+1] - X[i])
	'''
	Y = [X[i+1] - X[i] for i in range(M-1)]
	Y.sort(reverse=True)
	
	ans = X[-1] - X[0]
	for y in Y[:N-1]:
		ans -= y
	
	return ans

if __name__ == '__main__':
	t = '''3 7
-10 -3 0 9 -100 2 17'''.splitlines()
	
	N, M = read_int_n()
	X = read_int_n()
	
	print(slv(N,M,X))

Submission status: Accepted

Solution code:
# import bisect
# import heapq
# import math
# import random
# from collections import Counter, defaultdict, deque
# from decimal import Decimal
# from functools import lru_cache, reduce
# from itertools import combinations, combinations_with_replacement, product, permutations
# from operator import add, mul, sub, itemgetter
# import numpy as np

import sys
sys.setrecursionlimit(10000)

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time
	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def read_int(input=None):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [int(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret

def read_float(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [float(x) for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
	
def read_str(input):
	if input == None:
		input = sys.stdin.readline().strip()
	ret = [x for x in input.split()]
	if len(ret) == 1:
		ret = ret[0]
	return ret
		
def read_int(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return int(read)

def read_int_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [int(x) for x in read.split()]

def read_float(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return float(read)

def read_float_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [float(x) for x in read.split()]

def read_str(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return str(read)

def read_str_n(read=None):
	if read == None:
		read = sys.stdin.readline().strip()
	return [str(x) for x in read.split()]	

@mt
def slv(*args):
	X.sort()
	
	Y = []
	for i in range(M-1):
		Y.append(X[i+1] - X[i])
	Y.sort(reverse=True)
	
	ans = X[-1] - X[0]
	for y in Y[:N-1]:
		ans -= y
	
	return ans

if __name__ == '__main__':
	t = '''3 7
-10 -3 0 9 -100 2 17'''.splitlines()
	
	N, M = read_int_n()
	X = read_int_n()
	
	print(slv(N,M,X))

Submission status: Accepted

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

if M <= N:
  print(0)
else:
  xs = sorted(XS)
  ds = list(map(lambda a,b: b-a, xs, xs[1:]))
  print(sum(sorted(ds)[:M-N]))

Submission status: Accepted

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

if n > m:
    print(0)
else:
    ranges = []
    prev_x = None
    for x in sorted(xs):
        if prev_x is not None:
            ranges.append(x - prev_x)
        prev_x = x

    ret = 0
    n_omit = n - 1
    omit = 0
    for r in sorted(ranges, reverse=True):
        if omit < n_omit:
            omit += 1
        else:
            ret += r
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N,M=map(int,input().split())
A=[int(x) for x in input().split()]
A.sort()
pos=[0 for x in range(M)]
dis=[]
for i in range(M-1):
  dis.append([abs(A[i]-A[i+1]),i+1])

if N>=M:
  print(0)
  sys.exit()
  
dis=sorted(dis,key=lambda x:-x[0])
for i in range(N-1):
  pos[dis[i][1]]=1
  
ans=0
cnt=A[0]
for i in range(M-1):
  if pos[i+1]==1:
    cnt=A[i+1]
  else:
    ans+=abs(cnt-A[i+1])
    cnt=A[i+1]
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

diff = np.sort(diff)
result = 0
for i in range(len(diff) - N + 1):
    result += diff[i]
print(result)

Submission status: Accepted

Solution code:
#入力
N,M = map(int,input().split())
X = [int(i) for i in input().split()]
#ソートする
X.sort()
if N>=M:
    print("0")
else:
    L = list()
    for i in range(M-1):
        L.append(X[i+1]-X[i])
    a=0 
    L.sort()
    for i in range(N-1):
        L.pop(-1)
    for i in L:
        a+=i
    print(a)

Submission status: Accepted

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

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

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

	dis=sorted(dis)
	ans=0
	for i in range(M-N):
		ans+=dis[i]
	print(ans)

Submission status: Accepted

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

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

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

diff.sort()

ans = 0
for i in range(m-n):
    ans += diff[i]

print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
li = list(map(int,input().split()))
li2 = []

li.sort()

for i in range(m-1):
    li2.append(li[i+1]-li[i])

li2.sort()
sum = 0
for i in range(m-n):
    sum += li2[i]

print(sum)

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

l = []

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

l.sort()

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

Submission status: Accepted

Solution code:
##N Mの入力                                                                                                                                                                                                 
N,M = [int(x) for x in input().split()]
##地点の保存                                                                                                                                                                                                
x = [int(num) for num in input().split()]
##地点のソート                                                                                                                                                                                              
x = sorted(x)
##それぞれの幅の決定                                                                                                                                                                                        
dis = [x[i+1]-x[i] for i in range(M-1)]
##幅の長さのソート                                                                                                                                                                                          
dis = sorted(dis)
##答えの算出                                                                                                                                                                                                
dis_top = 0
for i in range(1,N):
    if((M-1)-i < 0):
        break;
    dis_top = dis_top + dis[(M-1)-i]
##答えの出力                                                                                                                                                                                                
ans = sum(dis) - dis_top
print(ans)

Submission status: Accepted

Solution code:
import sys

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

if n >= m :
  print("0")
  sys.exit()
xlist = [int(_) for _ in input().split()]
xlist.sort()
nlist = []
for i in range(m-1):
  nlist.append(xlist[i+1] - xlist[i])
nlist.sort()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
if N>=M:
  print(0)
  exit()
X = list(map(int,input().split()))
X.sort()
diffX = []
for i in range(len(X)-1):
  diffX.append(X[i+1]-X[i])
diffX.sort()
totalWidth = X[len(X)-1]-X[0] 
for i in range(N-1):
  totalWidth -= diffX[len(X)-2-i]
print(totalWidth)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n, m = map(int, input().split())
    xs = sorted(list(map(int, input().split())))

    if n >= m:
        print(0)
    else:
        ans = xs[-1] - xs[0]
        diff = [0 for _ in range(m - 1)]

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

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


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

#sub[i]:x[i+1]-x[i]
sub = [0]*(m-1)
for i in range(m-1):
    sub[i] = x[i+1]-x[i]
sub.sort()

ass = 0
for i in range(m-n):
    ass += sub[i]
print(ass)

Submission status: Accepted

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

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

if N > M:
    print(0)
    sys.exit()
sorted_x = sorted(list(X))

sub_X = [abs(sorted_x[i+1] - sorted_x[i]) for i in range(0,M-1)]

sorted_sub_x = sorted(sub_X,reverse=True)

print( sorted_x[M-1] -sorted_x[0] - sum([ sorted_sub_x[i] for i in range(0,N-1)]) )

Submission status: Accepted

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

if M == 1:
    print(0)
else:
    sorted_X_list = sorted(X_list)
    sabun_list = []

    for i in range(0,len(sorted_X_list)-1):
        sabun_list.append(sorted_X_list[i+1]-sorted_X_list[i])

    sabun_list.sort(reverse=True)
    del sabun_list[:N-1]

    print(sum(sabun_list))

Submission status: Accepted

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

x_list.sort()
diff_list = [x_list[i] - x_list[i-1] for i in range(1, m)]
diff_list.sort()
if m-n < 0:
    nokori = 0
else:
    nokori = m - n
print(sum(diff_list[:nokori]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

X.sort()

Y = []
x = X[0]
for i in range(1,M):
    Y.append(abs(x - X[i]))
    x = X[i]

Y.sort(reverse=True)
ans = max(X) - min(X) - sum(Y[0:N-1])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

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

diffX = [X[i + 1] - X[i] for i in range(len(X) - 1)]
diffX.sort()

ans = X[-1] - X[0]
for i in range(N - 1):
    ans -= diffX[-(i + 1)]

print(ans)

Submission status: Accepted

Solution code:
# _*_ coding:utf-8 _*_
#  Atcoder_Beginner_Contest117-C
#  TODO https://atcoder.jp/contests/abc117/tasks/abc117_c

def solveProblem(chessman,checkPointCount,checkPointListOrg):
	if checkPointCount <= chessman:
		answer = 0
	else:
		checkPointList = sorted(checkPointListOrg)
		allLong = checkPointList[-1]-checkPointList[0]
		distanceList=list()
		checkPointRange= range(0,len(checkPointList)-1,+1)
		for i in checkPointRange:
			distanceList.append(checkPointList[i+1]-checkPointList[i])
		distanceList.sort(reverse=True)
		openLine = sum(distanceList[0:chessman-1])
		answer = allLong-openLine
	return answer

if __name__ == '__main__':
	N,M = map(int,(input().strip().split(' ')))
	Xi = list(map(int,input().strip().split(' ')))
	solution = solveProblem(N,M,Xi)
	print("{}".format(solution))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = sorted(map(int, input().split()))
D = sorted(abs(x - y) for x, y in zip(X, X[1:]))
if N > 1:
    print(sum(D[:-(N-1)]))
else:
    print(sum(D))

Submission status: Accepted

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

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

b = []
for i in range(m-1):
    b.append(abs(a[i+1]-a[i]))

b.sort()
print(sum(b[:m-n]))

Submission status: Accepted

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