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 = map(int, input().split())
X = sorted(list(map(int, input().split())))

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

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

Submission status: Accepted

Solution code:
def main():
    N,M = map(int,input().split())
    X = list(map(int,input().split()))
    X.sort()
    if N >= M:
        print(0)
        exit()
    L = X[-1]-X[0]
    Y = [X[i+1]-X[i] for i in range(M-1)]
    Y.sort()
    Y.reverse()
    Z = Y[:N-1]
    ans = X[-1]-X[0]
    for z in Z:
        ans -= z
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

if N > M:
    print(0)
else:
    Y = [None] * (M - 1)
    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:
a,b = map(int,input().split())
num_list = list(map(int,input().split()))
num_list.sort(reverse=1)
ans_list = []
for i in range(b-1):
  ans_list.append(abs(num_list[i]-num_list[i+1]))
#print(ans_list)
ans_list.sort(reverse=1)
#print(ans_list)
ans = sum(ans_list[a-1:])
print(ans)

Submission status: Accepted

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

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

diff = sorted(diff)
print(sum(diff[0:max(M-N, 0)]))

Submission status: Accepted

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

li=list(map(int,input().split()))
li=sorted(li)
ll=[]
for i in range(1,len(li)):
    ll.append(li[i]-li[i-1])
ll=sorted(ll,reverse=True)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    N, M = map(int, input().split())
    X = sorted(map(int, input().split()))
    h = [j - i for i, j in zip(X, X[1:])]
    heapify(h)
    print(sum(heappop(h) for _ in range(M - N)))

main()

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

l=list(map(int,input().split()))
l=sorted(l)
ll=[]
for i in range(1,len(l)):
    ll.append(l[i]-l[i-1])
ll=sorted(ll,reverse=True)

print(sum(ll[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(len(x)-1):
    d.append(x[i+1] - x[i])

d.sort()

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

Submission status: Accepted

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


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

    
arr.sort()
arr.reverse()
print(x[-1] - x[0] - sum(arr[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

li.sort()

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

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():

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



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

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




resolve()

Submission status: Accepted

Solution code:
import sys

N,M = (int(x) for x in input().split())
if N >= M:
  print("0")
  sys.exit()
  
cnt = 0
  
x = list(map(int,input().split()))
x.sort()

cost = {}
for i in range(len(x)-1):
  cost[i] = x[i+1] - x[i]
num = -1
cost = sorted(cost.items(), key=lambda x:x[1],reverse=True)
for i in range(N-1):
  a = cost[i][0]
  cnt += x[a] - x[num+1]
  num = a
cnt += x[len(x)-1] - x[num+1]
print(cnt)

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()
    gap = []
    for i in range(1,M):
        gap.append(X[i]-X[i-1])
    print(sum(sorted(gap)[0:M-N]))

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

Submission status: Accepted

Solution code:
def chunk_list(index_list, data):
  result = []
  start = 0
  for i in index_list:
    result.append(data[start:i+1])
    start = i+2
    print(start)
  result.append(data[start:len(data)])
  return result
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce

def main():
  n, m = input_list()
  if n >= m:
    print(0)
    exit(0)
  x = list(set(input_list()))
  sx = list(sorted(x))
  diffs = []
  for i, xv in enumerate(range(len(x)-1)):
    diff = abs(sx[i+1]-sx[i])
    diffs.append(diff)
  sd = list(sorted(diffs, reverse=True))

  for i in range(n-1):
    sd[i] = 0
    if i == m-1:
      break

  ans = 0
  for a in sd:
    ans += a
  print(ans)
main()

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()
ikanai = sum(Y[M-1-(N-1):M-1])
#print(Y)
#print(ikanai)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

list_X.sort()
list_Y = []

for i in range(m-1):
    list_Y.append(list_X[i+1]-list_X[i])

list_Y.sort()

if n >= m:
    print(0)
else:
    print(sum(list_Y[:m-n]))

Submission status: Accepted

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

if N >=M:
  print(0)
else:
  diff = sorted([ X[i+1] - X[i] for i in range(M-1) ] ,reverse = True)
  print(sum(diff[N-1:]))

Submission status: Accepted

Solution code:
import sys
N,M = map(int,input().split())
X = [i for i in map(int,input().split())]
X.sort()
if M == 1 or N>=M:
  print(0)
  sys.exit(0)
res = max(X)-min(X)
Length = []
for i in range(len(X)-1):
  Length.append(X[i+1]-X[i])
Length.sort()
for i in range(N-1):
  res -= Length[len(Length)-1-i]
print(res)

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()
    X_div = [0] * (M-1)
    for i in range(1,M):
        X_div[i-1] = X[i] - X[i-1]
    X_div.sort(reverse=True)
    m = 0
    for i in range(N-1):
        m += X_div[i]
    print(X[-1]-X[0]-m)

Submission status: Accepted

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

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

    X.sort()
    Xd = []
    prev = X[0]
    for i in range(1, m):
        Xd.append(X[i] - prev)
        prev = X[i]

    Xd.sort()
    for i in range(n - 1):
        Xd.pop()

    print(sum(Xd))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    X.sort()
    Xd = []
    prev = X[0]
    for i in range(1, m):
        Xd.append(X[i] - prev)
        prev = X[i]

    d = 0
    Xd.sort()
    for i in range(n - 1):
        Xd.pop()

    print(sum(Xd))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

if n >= len(x):
    print(0)
    exit()

ls = []
x.sort()

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

ls.sort(reverse=True)
ans = sum(ls)

for i in range(n-1):
    ans -= ls[i]

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

ls = []
x.sort()

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

ls.sort(reverse=True)
ans = sum(ls)

for i in range(n-1):
    ans -= ls[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#h=int(input())
n,m=map(int,input().split())
xl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

if n>=m:
    ans=0
else:
    xl.sort()
    bet=[]
    for i in range(1,m):
        bet.append(xl[i]-xl[i-1])
    bet.sort(reverse=True)
    ans=xl[m-1]-xl[0]-(sum(bet[0:n-1]))
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
x_dis=[0 for _ in range(m-1)]
for i in range(m-1):
    x_dis[i]=x[i+1]-x[i]
x_dis.sort(reverse=True)
ans=0
for j in range(m-1):
    if j>=n-1:
        ans+=x_dis[j]
if n>=m:
    ans=0
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
inf = 10**6
n, m = map(int, input().split())

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

a = [int(x) for x in input().split()]
a.sort()

count = []
heapq.heapify(count)
for i in range(m-1):
  heapq.heappush(count, -a[i+1]+a[i])

ans = 0
for j in range(n-1):
  ans -= heapq.heappop(count)
ans = max(a)-min(a)-ans
print(ans)

Submission status: Accepted

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

X=sorted(X)
between=[0]*(M-1)
answer=0

for i in range(1,M):
    between[i-1]=X[i]-X[i-1]
bet=sorted(between)
for i in range(M-N):
    answer+=bet[i]

print(answer)

Submission status: Accepted

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

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

    num_list.sort()

    dist_list = []
    for i in range(1,m):
        dist_list.append(num_list[i]-num_list[i-1])

    dist_list.sort(reverse=True)


    ans = num_list[m-1]-num_list[0]
    for i in range(n-1):
        ans -= dist_list[i]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

Submission status: Accepted

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

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()
dis = [0] * (m - 1)
for i in range(m - 1):
    dis[i] = x[i + 1] - x[i]

dis.sort()
ans = x[m - 1] - x[0]
for i in range(len(dis) - 1, len(dis)- n, -1):
    ans -= dis[i]

print(ans)

Submission status: Accepted

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

ans = [abs(x[i+1] - x[i]) for i in range(m-1)]
ans.sort()

print(sum([ans[i] for i in range(m-n)]))

Submission status: Accepted

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

Submission status: Accepted

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

ans = [abs(x[i+1] - x[i]) for i in range(m-1)]
ans.sort()

print(sum([ans[i] for i in range(m-n)]))

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

if n >= m:
    print(0)
else:
    print(sum(d[n-1:]))

Submission status: Accepted

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


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

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

N,M = list(map(int, input().rstrip().split()))
X_list = list(map(int, input().rstrip().split()))
#-----

X_list.sort()

diff_list=[0]
for i in range(1,len(X_list)):
    diff = X_list[i] - X_list[i-1]
    diff_list.append(diff)

diff_list.sort()

if N > M:
    ans = 0
elif N==1:
    ans = sum(diff_list[1:])
else:
    ex_num = N-1
    ans = sum(diff_list[1:]) - sum( diff_list[-ex_num:] )

print(ans)

Submission status: Accepted

Solution code:
koma,spot=map(int,input().split())
if koma>=spot:print(0);exit()
l=list(map(int,input().split()))
l.sort()
k=[l[i+1]-l[i]for i in range(spot-1)]
k.sort()
print(sum(k[:spot-koma]))

Submission status: Accepted

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

# 使えるコマが1つだけだとして考えると
# X[0]にコマを置いて、右に向かって順番に移動していくのが最善(一度通った道をもう一度通るのは無駄)
# "ジャンプ[i]"：X[i]から[i+1]への移動 と定義する
# ジャンプはM-1回
# 総移動回数はsum(ジャンプ[i]での移動回数)

# 使えるコマが1つ増えると、1ジャンプ減らすのと同じ
# コマがN個使えるとき、"操作"をN-1回減らすことができる
# sum((ジャンプ[i]での移動回数 (i<=0<M-1))のうち小さい方からM-N個)が答え

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

Submission status: Accepted

Solution code:


def submit():
    n, m = map(int, input().split())
    xlist = list(map(int, input().split()))

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

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


if __name__ == "__main__":
    submit()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
mi=X[0]
ma=X[len(X)-1]
l=[]
c=0
#print(X)
#for i in (mi,ma+1,1):
for i in range(M-1):
    l.append(X[i+1]-X[i])
l.sort()
for i in range(len(l)-(N-1)):
    c=c+l[i]
print(c)

Submission status: Accepted

Solution code:
import numpy as np

n, m = [int(i) for i in input().split()]
x = np.array([int(i) for i in input().split()])

res = 0
if n < m:
    x.sort()
    d = np.diff(x)
    d = np.sort(d)[::-1]
    res = max(x) - min(x) - sum(d[:n-1])

print(res)

Submission status: Accepted

Solution code:
#n=int(input())
n,m=map(int,input().split())
xl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
cuml=[]
if n>=m:
    print(0)
else:
    xl.sort()
    rng=xl[m-1]-xl[0]
    ans=rng
    for i in range(1,m):
        cuml.append(xl[i]-xl[i-1])
    cuml.sort()
    for i in range(n-1):
        ans-=cuml[m-2-i]
    print(ans)

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(reverse=True)
del b[:n-1]
print(sum(b))

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 = [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:
N,M=map(int, input().split())
point=list(map(int,input().split()))
point.sort()
d=[point[i+1]-point[i] for i in range(M-1)]
d.sort()
if N<M:
    print(sum(d[:M-N]))
else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np

def a():
  n, m = list(map(int,input().split()))
  x = list(map(int,input().split()))
  if(m <= n):
    return(0)
  x = np.array(sorted(x))
  d = x[1:] - x[:-1]
  d.sort()
  return((x[-1] - x[0]) - sum(d[m-n:]))
print(a())

Submission status: Accepted

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

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

# gapのエントリのうち大きい方からN - 1個は使わない
# 使うのは (M - 1) - (N - 1) = M - N個
if M <= N:
    ans = 0
else:
    ans = sum(gap[0 : M - N])

print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    if N >= M:
        print(0)
    else:
        X.sort()
        diff = [y-x for x,y in zip(X[0:M-1], X[1:])]
        diff.sort()
        ans = sum(diff[:M-N])
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

x.sort()
x_dis = []

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

x_dis.sort(reverse=True)
num = 0

for i in range(n-1):
    num += x_dis[i]

print(x[m-1]-x[0]-num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N, M = map(int, input().split())
	Xlst = [int(a) for a in input().split()]
	Xlst.sort()
	lst = []
	for i in range(M-1):
		lst.append(Xlst[i+1] - Xlst[i])
	lst.sort()
	if N >= M:
		print(0)
	else:
		print(sum(lst[0: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()
L = []
for i in range(1,M):
    L.append(X[i]-X[i-1])
L.sort(reverse=True)

tmp = 0
for i in range(N-1):
    tmp += L[i]
print(X[-1]-X[0] - tmp)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

dis.sort()

#入力が、コマ数＞地点の場合がありうるため、合計算出にmaxを使用する
ans = sum(dis[:max(m-n, 0)])
print(ans)

Submission status: Accepted

Solution code:
n,m = [int(e) for e in input().split()]
a = sorted([int(e) for e in input().split()])
l=sum(sorted([y-x for x,y in zip(a,a[1:])],reverse=True)[:n-1])
print(max(a)-min(a)-l)

Submission status: Accepted

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

Submission status: Accepted

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

xlist.sort()

diflist=[]


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

if n >= m:
    print(0)
else:
    print(sum(diflist[:(m-n)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m, *spot = map(int, open(0).read().split())
spot.sort()
dist = [spot[i+1]-spot[i] for i in range(m-1)]
dist.sort(reverse = True)
print(sum(dist[n-1:]) if dist else 0)

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int,ipti().split()))
    x = list(map(int,ipti().split()))
    x.sort()

    dist = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dist.sort()

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = map(int, sys.stdin.readline().split())
    lines = list(map(int, sys.stdin.readline().split()))
    lines.sort()

    cost = {}
    for i in range(len(lines) - 1):
        tmp = lines[i + 1] - lines[i]
        cost[i] = tmp

    sortedCost = sorted(cost.items(), key=lambda x: x[1], reverse=True)
    ret = 0
    for i, c in sortedCost[N - 1:]:
        ret += c

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    X_n.insert(0, X[0])

    del X_n[-1]

    X_section  = [X[i] - X_n[i] for i in range(len(X))]

    X_section.sort(reverse = True)

    X_result = X_section[N-1:]

    result = sum(X_result)

    print(result)
else:
    print(0)

Submission status: Accepted

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

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

Y.sort()
if N >= M:
    print(0)
else:
    print(sum(Y[:M-N]))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

    print(sum(diff))

Submission status: Accepted

Solution code:


coma, prot = map(int, input().split())
nums = list(map(int, input().split()))


if coma >= prot:
    print(0)
    exit()

nums.sort()

intervals = []
for i in range(prot-1):
    intervals.append(nums[i+1] - nums[i])

intervals.sort()
li = len(intervals)
print(sum(intervals[:li-(coma-1)]))

Submission status: Accepted

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

ans = 0
if M > N:
    dist = []
    for i in range(1,M):
        dist.append(abs(X[i]-X[i-1]))
    dist.sort()
    for i in range(M-N):
        ans += dist[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
ans = 0
X.sort()
if N >= M:
  print(0)
  exit()
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(N-1):
  xx[j] = 0
ans = sum(xx)
print(ans)