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:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, X: "List[int]"):
    if(N >= M):
        print(0)
        exit()
    X.sort()
    L = [X[i+1] - X[i] for i in range(M-1)]
    L.sort(reverse=True)
    ans = sum([L[i] for i in range(N-1)])
    print(X[M-1]-X[0] - ans)

    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    X = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=list(map(int,input().split()))
L.sort()
if n>=m:
  print(0)
elif m!=1:

  d=[L[i+1]-L[i] for i in range(m-1)]
  d.sort()
  d.reverse()
  ans=L[-1]-L[0]


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

else:
  print(0)

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(a[i+1]-a[i])
b.sort()
b.reverse()
print(a[-1]-a[0]-sum(b[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main(args):
    N, M = parse(args, int, int)
    X = tuple(map(int, next(args).split()))

    x = sorted(X)
    offset_x = [x[n] - x[n - 1] for n in range(1, M)]
    offset_x.sort(reverse=True)

    debug(offset_x)
    print(sum(offset_x[N - 1:]))

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

x = sorted(x)

dist = [0]*(m-1)
for i in range(m-1):
    dist[i] = x[i+1] - x[i]

dist = sorted(dist, reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

if N >= M:
  print(0)
else:
  interval = [0] * (M-1)
  for i in range(M-1):
    interval[i] = Xs[i+1] - Xs[i]
  interval.sort(reverse = True)
  
  answer = Xs[M-1] - Xs[0]
  for i in range(N-1):
    answer -= interval[i]
  print(answer)

Submission status: Accepted

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

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

X.sort()

buff = []

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


buff.sort()
buff = buff[::-1]

for i in range(min(N-1, len(buff))):
    buff.pop(0)

print(sum(buff))

Submission status: Accepted

Solution code:

import bisect
import collections
import itertools


def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()
xs=getints()
xs=list(sorted(xs))

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

spaces = list(sorted(spaces))

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

print(sum(spaces))

Submission status: Accepted

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

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    x.sort()
    interval = [0]*(m-1)
    for i in range(m-1):
        interval[i] = x[i+1] - x[i]
    interval.sort(reverse = True)
    answer = x[m-1] - x[0]
    for i in range(min(m,n-1)):
        answer -= interval[i]
    print(answer)

Submission status: Accepted

Solution code:
import heapq
N,M = list(map(int, input().split(" ")))
Xs = sorted(list(map(int, input().split(" "))))

if N >= M :
	print(0)
	exit()
	
darr = [Xs[i+1] - Xs[i]for i in range(M-1)]

# print(darr)
heapq.heapify(darr)
res = 0

for _ in range(M - N):
	res += heapq.heappop(darr)
	
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq
n, m = map(int, input().split())
if n >= m:
    print(0)
else:
    x = list(map(int, input().split()))
    x.sort()
    d = []
    heapq.heapify(d)
    for i in range(1, m):
        heapq.heappush(d, [-abs(x[i] - x[i-1]), i, i - 1])
    total = 0
    a = [0, m - 1]
    for i in range(n-1):
        p = heapq.heappop(d)
        a.append(p[1])
        a.append(p[2])
    a.sort()
    total = 0
    for i in range(1, len(a), 2):
        total += abs(x[a[i]] - x[a[i-1]])
    print(total)

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])
fin_diff=sorted(diff, reverse=True)

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

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
x = list(map(int, input().split()))
a = []

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

#x.sort()
list.sort(x)
#print(x)

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

#list.sort(a, reverse=True)
a.sort()
#print(a)

count = max(x) - min(x)
for i in range(n-1):
    if len(a) == 0:
        break
    a.pop()
#    count -= a[i]

#print(count)
print(sum(a))

Submission status: Accepted

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

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

ans = sum(xx)
print(ans)

Submission status: Accepted

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

if N >= M:
  print(0)
elif N == 1:
  ans = max(X) - min(X)
  print(ans)
else:
  X.sort()
  mn_X = X[0]
  for m in range(M):
    X[m] = X[m] - mn_X #一番座標が小さい点を原点とする

  DX = [] #隣り合う二点間の距離
  for mm in range(M-1):
    dx = X[mm+1] - X[mm] #隣り合う二点間の距離を求める
    DX.append(dx)
  #print(DX)
  
  DXX = list(tuple(DX))
  DXX.sort(reverse=True) #DXXのi=0,1,2,...,N-2、つまり大きい値を取ってきたい
  I = [] #「XをN個に分割するとき、どの座標で分割するか」
  for i in range(N-1):
    big_dx = DXX[i]
    idx = DX.index(big_dx)
    I.append(idx)
  I.sort()
  #print(I)
  
  #最初のコマは原点に置く
  move = X[I[0]] - 0
  #print(move)
  
  for j in range(0, N-2):
    move += (X[I[j+1]] - X[I[j]+1])
  #print(move)
  
  #最後のコマ
  move += (X[-1] - X[I[N-2]+1])
  
  print(move)

Submission status: Accepted

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

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

  y_sorted = sorted(y, reverse=True)

  ans = 0
  for _y in y_sorted[n-1:]:
    ans += _y
  print(ans)

Submission status: Accepted

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

ans = 0

if N < M:
    XList = sorted(XList)
    XXList = [0] * (M-1)
    for i in range(M-1):
        XXList[i] = XList[i+1] - XList[i]
    XXList = sorted(XXList)
    for i in range(M-N):
        ans += XXList[i]

print(ans)

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

Submission status: Accepted

Solution code:
# 2019/08/10

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

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

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

Submission status: Accepted

Solution code:
import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

# ソートした後グルーピングする
# 最大距離を避けるように配置する？
if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    if N >= M:
        print(0)
        exit()

    X = list(map(int, sys.stdin.readline().split()))
    X = sorted(X)
    length = max(X) - min(X)
    distance = []
    for i in range(len(X) - 1):
        distance.append(abs(X[i] - X[i + 1]))
    distance = sorted(distance, reverse=True)

    for i in range(N - 1):
        length -= distance[i]

    print(length)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
lst = []
N, M = map(int, input().split())
lst = list(map(int, input().split()))
lst.sort()
ans = 0
if (N < M):
  dis = [lst[i+1] - lst[i] for i in range(len(lst)-1)]
  dis.sort(reverse=True)
  ans = lst[-1] - lst[0] - sum(dis[:N-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

diff = sorted([abs(x[i] - x[i-1]) for i in range(1, m)])
for i in range(n-1):
    diff.pop()

print(sum(diff))

Submission status: Accepted

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

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

xdelta.sort()

if N < M:
	if N == 1:
		print(sum(xdelta))
	else:
		for i in range(N-1):
			xdelta.pop(-1)
		print(sum(xdelta))
else:
	print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd
from itertools import accumulate
from functools import reduce


def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n,m = map(int, sys.stdin.readline().split())
    x = list(map(int, sys.stdin.readline().split()))
    ans=0
    if n > m:
        print(0)
    else:
        l=[]
        x.sort()

        for i in range(m-1):
            l.append(x[i+1]-x[i])
        l.sort()
        for i in range(m-n):
            ans += l[i]
        print(ans)

Submission status: Accepted

Solution code:
def iread():
    return int(input())


def sread():
    return input()


def aread_int():
    tmp = input().split()
    ret = [int(i) for i in tmp]
    return ret


def aread_str():
    return input().split()

if __name__ == '__main__':
    n, m = map(int, input().split())
    
    x = aread_int()
    x = sorted(x)
    x_dist = [0] * (m - 1)
    for i in range(m - 1) :
        x_dist[i] += abs(x[i] - x[i + 1])
    
    if n > m :
        print(0)
        exit()
    
    x_dist = sorted(x_dist)
    idx = len(x_dist) - 1
    cost = sum(x_dist)
    for i in range(n - 1) :
        cost -= x_dist[idx]
        x_dist[idx] = 0
        idx -= 1

    print(cost)

Submission status: Accepted

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

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

if N >= M:
    res = 0
elif N == 1:
    res = max(x) - min(x)
else:
    res = sum(steps[:-N + 1])
print(res)

Submission status: Accepted

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

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

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

sort_diff = sorted(diff, reverse=True)

sum = 0

if len(sort_diff) == 0:
    print(0)
    exit()

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

for i in range(len(sort_diff)):
    sum += sort_diff[i]
print(sum)

Submission status: Accepted

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

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

x = sorted(x)
sub = [x[i+1] - x[i] for i in range(len(x) - 1)]
sub = sorted(sub)
ans = 0

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

Submission status: Accepted

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

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

dist.sort()

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

print(ans)

Submission status: Accepted

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

if n == 1:
  print(xs[-1] - xs[0])
  exit()

diff = [(i, b-a) for i, (a, b) in enumerate(zip(xs, xs[1:]))]
diff.sort(key=lambda x: x[1])

start_indexes = [0] + list(map(lambda x:x[0] + 1, diff[-n+1:]))
start_indexes.sort()

steps = 0
for i_start, i_end in zip(start_indexes, start_indexes[1:] + [m]):
    steps += xs[i_end - 1] - xs[i_start]
print(steps)

Submission status: Accepted

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

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

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

    diff_list.sort(reverse=True)
    ans = x_list[-1] - x_list[0]
    for i in range(N-1):
        ans -= diff_list[i]
    print(ans)

Submission status: Accepted

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

if n >= m:
    print(0)
else:
    l_x.sort()
    l_d = [l_x[i+1]-l_x[i] for i in range(m-1)]
    l_d.sort(reverse=True)

    big_ds = 0
    for j in range(n-1):
        big_ds += l_d[j]

    print(sum(l_d)-big_ds)

Submission status: Accepted

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

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

for i in range(1,m):
    dst.append(lst[i]-lst[i-1])
dst.sort()
if n==1:
    ans = sum(dst)
else:
    ans = sum(dst[:-n+1])
print(ans)

Submission status: Accepted

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

if M <= N:
    ans = 0
elif N == 1:
    ans = max(nums) - min(nums)
else:
    nums.sort()
    diffNums = [nums[i+1] - nums[i] for i in range(M-1)]
    diffNums.sort()
    ans = sum(diffNums[:-(N-1)])

print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X=sorted(map(int, input().split()))
if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  print(sum(sorted([i-j for i,j in zip(X[1:],X)],reverse=True)[N-1:]))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X=sorted([int(i) for i in input().split()])
if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  print(sum(sorted([i-j for i,j in zip(X[1:],X)],reverse=True)[N-1:]))

Submission status: Accepted

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

if N==1:
  print(X[M-1]-X[0])
elif N>=M:
  print(0)
else:
  Y=list()
  [Y.append(i-j) for i,j in zip(X[1:],X)] 
  Y.sort(reverse=True)
  print(sum(Y[N-1:]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Y.sort()

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

Submission status: Accepted

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

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

Y=sorted(Y)

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

Submission status: Accepted

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

if M <= N:
    ans = 0
elif N == 1:
    ans = max(X) - min(X)
else:
    X.sort()
    List = []
    for i in range(M-1):
        List.append(X[i+1]-X[i])
    List.sort()
    ans = 0
    for j in range(M-N):
        ans += List[j]

print(ans)

Submission status: Accepted

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

ps=sorted(int(i) for i in input().split())

if n>=m:
    print(0)
    exit()
else:
    dis2=sorted(ps[i+1]-ps[i] for i in range(m-1))
    print(sum(dis2[:m-n]))

Submission status: Accepted

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

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

  if N>= 2:
    for i in range(N-1):
      ans -= subX[-i-1]

  print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	n,m = map(int,input().split())
	x = list(map(int,input().split()))
	if(n>=m):
		print('0')
		exit()
	x.sort()
	ans = x[-1] - x[0] 
	y = []
	for i in range(m-1):
		y.append(x[i+1]-x[i])
	y.sort(reverse=True)
	for i in range(n-1):
		ans -= y[i]
	print(ans)

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

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

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

XXX = list(map(int, input().split()))
XXX.sort()

sections = [abs(XXX[i] - XXX[i + 1]) for i in range(M) if i < M - 1]
sections.sort(reverse=True)
not_arrived = sum(sections[:N-1])

print((XXX[-1] - XXX[0]) - not_arrived)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

x.sort()

x_dif = []

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

if m <= n:
    print(0)
else:
    print(sum(x_dif[:m-n]))

Submission status: Accepted

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

x.sort()
diffs = [0]
for i in range(len(x) - 1):
    diffs.append(abs(x[i] - x[i + 1]))
diffs.sort()
for i in range(n - 1):
    if len(diffs) <= 0:
        break
    diffs.pop()
print(sum(diffs))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = map(int, input().split())
X = sorted(X)
V = sorted(y - x for x, y in zip(X[:-1], X[1:]))
print(max(X)-min(X)-sum(V[max(0, len(V)-N+1):]))

Submission status: Accepted

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

x.sort()
diffs = [0]
for i in range(len(x) - 1):
    diffs.append(abs(x[i] - x[i + 1]))
diffs.sort()
for i in range(n - 1):
    if len(diffs) < 2:
        break
    diffs.pop()
print(sum(diffs))

Submission status: Accepted

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

l.sort()
if m-n>=0:
    ans=sum(l[0:m-n])
else:
    ans=0

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

y = [0] * (m - 1)
for i in range(m - 1):
    y[i] = x[i + 1] - x[i]

y.sort(reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

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

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n=hoge[0]
m=hoge[1]

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

hoge.sort()
#print(hoge)
x=[]
sum=0
for i in range(0,m-1):
	x.append(hoge[i+1]-hoge[i])
	sum+=hoge[i+1]-hoge[i]
x.sort(reverse=True)

#print(x)

sc=0
for i in range(0,min(n-1,len(x))):
	sc+=x[i]

print(sum-sc)

Submission status: Accepted

Solution code:
import math
a = input().split()
a = [int(i) for i in a]
x_list = input().split()
x_list = sorted([int(i) for i in x_list])


kankaku = [x_list[i + 1] - x_list[i] for i in range(len(x_list) - 1)]

kankaku = sorted(kankaku, key=lambda x: - x)


for i in range(a[0] - 1):
    if len(kankaku) == 0:
        break
    kankaku.pop(0)

if len(kankaku) > 0:
    print(sum(kankaku))
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)
ans = X[-1] - X[0] - sum(diff[:N - 1])
print(ans)

Submission status: Accepted

Solution code:
# ABC 117 C

import sys

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

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

x = list(map(int, input().split()))
x.sort()

diff = [0] * (M-1)

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

diff.sort()
idx = len(diff) - N + 1
print(sum(diff[:idx]))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

if k>=n or n==1:
	res=0
else:
	y=sorted(x)
	z=[0]*(n-1)
	for i in range(n-1):
		z[i]=y[i+1]-y[i]
	v=sorted(z,reverse=True)
	w=0
	if k>1:
		for i in range(k-1):
			w+=v[i]
		res=y[n-1]-y[0]-w
	else:
		res=y[n-1]-y[0] 

print(res)

Submission status: Accepted

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

dev = M - N
if dev <= 0:
    print(0)
else:
    distance_list = [X_list[i + 1] - X_list[i] for i in range(M - 1)]
    distance_list.sort()
    # print(distance_list)
    ans = 0
    for i in range(len(distance_list)-N+1):
        ans += distance_list[i]
    print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
li = list(map(int, input().split()))
 
li.sort()
move_cost = [li[i+1] - li[i] for i in range(m-1)]
move_cost.sort()
if n > 1:
    move_cost = move_cost[:-n+1]

print(sum(move_cost))

Submission status: Accepted

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

diff = []
for i in range(M-1):
    diff.append(X[i+1]-X[i])
print(sum(sorted(diff)[:max(0,M-N)]))

Submission status: Accepted

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

xm = [int(i) for i in input().split()]
xm.sort()
dist = [xm[i+1] - xm[i] for i in range(m - 1)]
dist.sort()
print(sum(dist[:m-n]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Y = list(reversed(sorted(Y)))
ans = sum(Y) - sum(Y[:N-1])

print(ans)

Submission status: Accepted

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