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

if N >= M:
    print(0)
else:
    L=[]
    for i in range(M-1):
        L.append(li[i+1]-li[i])
    L = sorted(L, reverse=True)
    x=0
    for j in range(N-1):
        x+=L[j]
    answer = li[-1]-li[0]-x
    print(answer)

Submission status: Accepted

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


def solve(n, m, x):

    if m <= n:
        return 0

    x.sort()
    total = x[-1] - x[0]

    if n == 1:
        return total

    px = x[0]
    y = []
    for xi in x[1:]:
        y.append(xi - px)
        px = xi

    y.sort(reverse = True)
    r = sum(y[:n - 1])

    return total - r

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

    print(solve(n, m, x))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

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

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
#A
def A():
    t,x = LI()
    print(t/x)

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


#C
def C():
    n,m = LI()
    x = LI()
    x.sort()
    l = [x[i+1]-x[i] for i in range(m-1)]
    l.sort()
    l = l[::-1]
    ans = sum(l[max(n-1,0):])
    print(ans)

#D
def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])

#E
if __name__ == "__main__":
    C()

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,(input().split()))))
dif_X = sorted([X[i+1]-x for i,x in enumerate(X[:-1])])
print(sum(dif_X[:max(0,M-N)]))

Submission status: Accepted

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

X.sort()

dif_X = [X[i+1]-x for i,x in enumerate(X[:-1])]
dif_X.sort()
print(sum(dif_X[:max(0,M-N)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
#print(a)
if N > M:
    ans = 0
else:
    distance = [abs(a[i+1] - a[i]) for i in range(M-1)]
    #print(distance)
    distance.sort()
    #print(distance)
    del distance[M-N:]
    ans = 0 
    for j in distance:
        ans += j
print(ans)

Submission status: Accepted

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

X.sort()

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x = sorted(x)
for i in range(1, m):
  x[i-1] = x[i] - x[i-1]
x[-1] = 10**6

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

Submission status: Accepted

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

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

X.sort()

Y = []

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

Y.sort()

if M-N <= 0:
    ans = 0
else:
    ans = sum(Y[:M-N])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N >= M:
  print(0)
  exit()
else:
  D = np.sort(X[1:] - X[:-1])
  print(D[:M-N].sum())

Submission status: Accepted

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

Submission status: Accepted

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

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

X = sorted(X)
L = [ X[i] - X[i-1] for i in range(1, M)]
L = sorted(L)
#print(X)
#print(L)

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

Submission status: Accepted

Solution code:
n,m,*x=map(int,open(0).read().split());x.sort();print(sum(sorted(j-i for i,j in zip(x,x[1:]))[-n::-1]))

Submission status: Accepted

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

print(result)

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)])
ans = sum(tmp[:M-N])
if(M <= N):
  ans = 0
print(ans)

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(len(X)-1)]
L.sort(reverse=True)
K = L[N-1:]
print(sum(K) if K != [] else 0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
xls = list(map(int,input().split()))
xls.sort()
dls =[xls[i+1]-xls[i] for i in range(M-1)]
dls.sort()

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

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
ans = 0
diff = []
 
X.sort()
 
if (N < M): 
    for i in range(M-1):
        dif = X[i+1] - X[i]
        diff.append(dif)
    
    diff.sort()
    
    for i in range(M-N):
        ans += diff[i]
    # for n in range(N):
    #     diff.pop(-1)
   
    # for i in range(len(diff)):
    #     ans += diff[i]
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
line=list(map(int,input().split()))
if n >= m:
  print(0)
else:
  line.sort()
  stock=0
  distance=[]
  for i in line:
    distance.append(i-stock)
    stock=i
  distance.pop(0)
  distance.sort()
  ans=0
  for i in range(m-n):
    ans+= distance[i]
  print(ans)

Submission status: Accepted

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

N,M = [int(x) for x in input().split()]
xls = [int(x) for x in input().split()]
xls.sort()
dls = [xls[i+1]-xls[i] for i in range(M-1)]
if N >= M:
    ans = 0
else:
    dls.sort()
    ans = sum(dls[0:M-N])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

if n > 1:
    x_diff = sorted(x_diff)[:-(n-1)]

print(sum(x_diff))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ans():
  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]))
ans()

Submission status: Accepted

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


if N >= M:
    print(0)
else:
    Di = []
    for i in range(M - 1):
        d = Xi[i + 1] - Xi[i]
        Di.append([i, d])
    Di.sort(key=lambda x: x[1], reverse=True)

    put_idx = [0]
    for i in range(N - 1):
        put_idx.append(Di[i][0] + 1)
    put_idx.append(M)

    total = 0
    for i in range(N):
        si = put_idx[i]
        ei = put_idx[i + 1] - 1
        s = Xi[si]
        e = Xi[ei]
        total += e - s
    print(total)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort()
blist = []

if n >= m :
  print(0)
elif n == 1:
  print(alist[m-1] - alist[0])
else:
  for i in range(0,m-1):
    blist.append(alist[i+1] - alist[i])
  blist.sort()
  d = -1 * (n-1)
  c = sum(blist[d:])
  b1 = alist[m-1] - alist[0]
  print(b1 - c)

Submission status: Accepted

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

if m <= n:
	print(0)
elif n == 1:
	print(abs(x[-1]-x[0]))
else:
	diff = [0]*(m-1)
	for it in range(m-1):
		diff[it] = x[it+1]-x[it]
	diff = sorted(diff)
	diff = diff[:m-n]
	# print(diff)
	print(sum(diff))

Submission status: Accepted

Solution code:
import numpy as np
N,M=list(map(int,input().split()))
X=np.array(list(map(int,input().split())))
if N>=M:
  print(0)
else:
	sX=np.sort(X)
	dsX=np.diff(sX)
	sdsX=np.sort(dsX)
	print(np.sum(sdsX[:M-N]))

Submission status: Accepted

Solution code:
import pdb
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
d=[]
if n<m:
    for i in range(0,len(x)-1):
        d.append(x[i+1]-x[i])
    d.sort(reverse=True)
    for i in range(n-1):
        d.pop(0)
    ans=sum(d)
    print(ans)
else:#koma are assigned
    print(0)
#pdb.set_trace()

Submission status: Accepted

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

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(x[-1]-x[0]-sum(l[:n-1]))

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:
def streamline(N: int, M: int, X: list) -> int:
    # 隣の点との距離を大きい順に N-1 こ選ぶ.
    # 選んだ部分で N 区間に分割する。
    # 各部分の和が解答
    # => つまり、総距離から N-1 個の最大距離を引けば良い
    _X = sorted(X)
    dist = [_X[i+1] - _X[i] for i in range(M-1)]
    max_dist = sorted(dist, reverse=True)[:N-1]
    return sum(dist) - sum(max_dist)


if __name__ == "__main__":
    N, M = [int(s) for s in input().split()]
    X = [int(s) for s in input().split()]

    ans = streamline(N, M, X)
    print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = list(map(int, input().split()))
x = list(sorted(x))
y = [xb - xa for xa, xb in zip(x, x[1:])]
y = list(sorted(y, reverse=True))
print(sum(y) - sum(y[:N - 1]))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
def search():
  if N>=M:
    return 0
  X.sort()
  cost=[X[i+1]-X[i] for i in range(M-1)]
  ans_tmp=0
  cost.sort()
  for i in range(M-N):
    ans_tmp+=cost[i]
  return ans_tmp

print(search())

Submission status: Accepted

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

    ans = f(N, M, X)
    print(ans)


def f(N, M, X):
    """
    1 <= N, M <= 10^5
    |Xi| <= 10^5
    Xi != Xj

    入力例1より初期位置をマスにしてもいいので、0
    N < M なら1回以上動くコマが存在する
    N=1 なら
        X をsortして最小値から最大値まで動かす
    M=N+1 なら
        1動かす
        最後のXに1番近いコマを動かす
        1番近いかを調べるには、X同士の比較なのでN^2: TLE
        外側から内側に移動したほうがよい
            内側から外側に行ってから再度内側に行く場合、損する
            好きな整数座標なので-10^5-1でもよい
        Xをsortしたときの前との差分をとると、どこの間隔が長いか分かる
            その中で短い部分を移動
    """
    if N >= M:
        return 0

    X = sorted(X)
    diff = [abs(X[i+1] - X[i]) for i in range(M-1)]

    # 長い部分は除外する
    ans = sum(sorted(diff, reverse=True)[N-1:])

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

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

X.sort()

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

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Submission status: Accepted

Solution code:
import numpy as np

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

X.sort()

D = np.diff(X)

D.sort()

print(0 if M <= N else np.sum(D[:M-N]))

Submission status: Accepted

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

X.sort()

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

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Submission status: Accepted

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

X.sort()

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

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Submission status: Accepted

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

X.sort()

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

D.sort()

print(0 if M <= N else sum(D[:M-N]))

Submission status: Accepted

Solution code:
import numpy as np

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

# 昇順
X.sort()

# numpy array 配列の差分
D = np.diff(X)

# numpy array 降順
D = np.sort(D)[::-1]

# numpy array 先頭から N - 2 まで 合計
print(X[-1] - X[0] - np.sum(D[:N - 1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lst = sorted([int(x) for x in input().split()])
lst2 = []
for x in range(1, len(lst)):
    lst2.append(lst[x] - lst[x - 1])

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

Submission status: Accepted

Solution code:
"""
配列をソート
差を取って、最も大きいところから切っていく
K個塊になるまで切っていく
塊の端っこから端っこまでを足していく
"""
import numpy as np

N, M  = map(int,input().split())
A = input().split()
A = np.array([int(a) for a in A])

def count(A):
    A_sorted = np.sort(A)
    delta = np.abs(A_sorted[:-1] - A_sorted[1:])
    delta_ind = delta.argsort()[::-1]
    delta_ind_sort = np.sort(delta_ind[:N-1]) #昇順
    if len(delta_ind_sort) == 0:
        return 0 
    N_all = np.abs(A_sorted[0] - A_sorted[delta_ind_sort[0]]) #初期
    for i in range(len(delta_ind_sort)-1):
        # print(A_sorted[delta_ind_sort[i]+1],  A_sorted[delta_ind_sort[i+1]])
        N_all += np.abs(A_sorted[delta_ind_sort[i]+1] - A_sorted[delta_ind_sort[i+1]])
    
    N_all += np.abs(A_sorted[-1]-A_sorted[delta_ind_sort[-1]+1])
    return N_all


if N == 1:
    print(A.max()-A.min())
else:
    print(count(A))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
def solve():
    # 解説放送を実装
    # 区切る問題
    N, M = [int(x) for x in input().split(" ")]
    X = [int(x) for x in input().split(" ")]

    if M == 1:
        print(0)
        return

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

    ans = 0
    for i in range(0, len(distance)-(N-1)):
        ans += distance[i]
    
    print(ans)



if __name__ == "__main__":
    solve()

Submission status: Accepted

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

X.sort()
dif = [ X[i+1] - X[i] for i in range(M-1) ]
dif.sort()
add = max(0,M - N)
print(sum(dif[:add]))

Submission status: Accepted

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

Submission status: Accepted

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

if n >= m:
    print(0)

else:
    arr.sort()

    diff = [arr[i+1] - arr[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())
line = [int(part) for part in input().split()]

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

line.sort()

sub_line = []
for num in range(len(line)-1):
    sub_line.append(line[num+1]-line[num])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

X_arr.sort()

D_arr = [X_arr[x+1]-X_arr[x] for x in range(len(X_arr)-1)]
D_arr.sort()

D_all = 0
for D in D_arr[:(M-N)]:
    D_all += D

print(D_all)

Submission status: Accepted

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

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

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

print(X[-1]-X[0]-sum(Y[:n-1]))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
X=sorted(set(list(map(int,input().split()))))
s=[]
for i in range(M-1):s.append(X[i+1]-X[i])
s=sorted(s)[::-1]
print(sum(s[N-1:]))

Submission status: Accepted

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

liB=[0]*(m-1)
for i in range(m-1):
  liB[i]=liA[i+1]-liA[i]
  
liB.sort()
#print(liB)
ans=0
for i in range(m-n):
  ans=ans+liB[i]
  #print(ans)
print(ans)

Submission status: Accepted

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

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

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

print(X[-1]-X[0]-sum(Y[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# N-1個のX_disから小さいものをN-M個選んで足す
X_dis = sorted(X_dis)
ans = sum(X_dis[:M-N])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

move = 0

if n >= m:
  print(move)
elif n==1:
  move = max(x) - min(x)
  print(move)
else:
  x_diff = [None]*(m-1)
  for i in range(m-1):
    x_diff[i] = [i+1, x[i+1] - x[i]]
  x_diff = sorted(x_diff, key=lambda x:-x[1])
  sep = sorted(x_diff[:n-1])
  for j in range(n):
    if j==0:
      move += max(x[0:sep[j][0]])-min(x[0:sep[j][0]])
    elif j==n-1:
      move += max(x[sep[j-1][0]:])-min(x[sep[j-1][0]:])
    else:
      move += max(x[sep[j-1][0]:sep[j][0]])-min(x[sep[j-1][0]:sep[j][0]])
  print(move)

Submission status: Accepted

Solution code:
def solve(n, m, x):
    if m == 1:
        return 0
    x = list(sorted(x))
    d = [x[i+1] - x[i] for i in range(m-1)]
    d = list(sorted(d))
    k = max(0, len(d) - (n-1))
    return sum(d[:k])

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

Submission status: Accepted

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

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

	print(ans)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
x = sorted([int(y) for y in input().split()])
sub = [x[i]-x[i-1] for i in range(1, M)]
if N >= M:
    print(0)
elif N == 1:
    print(sum(sub))
else:
    print(sum(sorted(sub)[:-(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()
    Y = Y = [X[i+1]-X[i] for i in range(M-1)]
    Y.sort()
    print(sum(Y[:M-N]))

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
X = list(map(int,input().split()))
X.sort()
def solve():
    ans = 0
    if(N>=M):
        return print(ans)
    else:
        sub = []
        for i in range(M-1):
            sub_next = abs(X[i+1]-X[i])
            sub.append(sub_next)
        sub.sort()
        return print(sum(sub[0:M-N]))
solve()

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:
    L=[X[i+1]-X[i] for i in range(M-1)]
    L.sort(reverse=True)
    s=sum([L[i] for i in range(N-1)])
    print(max(X)-min(X)-s)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    x = list(map(int, input().split()))
    
    if N >= M:
      print(0)
    else:
      x.sort()
      l_list = [x[i+1] - x[i] for i in range(M - 1)]
      l_list.sort(reverse=True)
      print(sum(l_list[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()
sa=[]
for i in range(M-1):
    sa.append(X[i+1]-X[i])
sa.sort(reverse=True)
use = sa[N-1:]    
print(sum(use))

Submission status: Accepted

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

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

Submission status: Accepted

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

#print(X)

for i in range(1,M):
    X_intvl[i-1] = X[i] - X[i-1]

#print(X_intvl)

X_intvl.sort(reverse=True)

#print(X_intvl)

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

Submission status: Accepted

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()

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

  if(N  >= 2):
    for i in range(N-1):
        new_dif_list.pop(0)

  print(sum(new_dif_list))

Submission status: Accepted

Solution code:
#python3.4
def inpul(): return list(map(int, input().split()))

N, M = inpul()
X = inpul()
new_list = sorted(X)
diff_list = [new_list[i+1] - new_list[i] for i in range(M-1)]
new_dif_list = sorted(diff_list, reverse = True)

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

else:
  if(N  >= 2):
      for i in range(N-1):
        new_dif_list.pop(0)

  print(sum(new_dif_list))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
def search():
  if N>=M:
    return 0
  X.sort()
  cost=[X[i+1]-X[i] for i in range(M-1)]
  ans_tmp=X[-1]-X[0]
  cost.sort(reverse=True)
  for i in range(N-1):
    ans_tmp -= cost[i]
  return ans_tmp

print(search())

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
x = sorted(x)
a=[]
if n>=m:
    print(0)
else:
    for i in range(len(x)-1):
        a.append(x[i+1]-x[i])
    a = sorted(a)
    ans = 0
    for i in range(m-n):
        ans+=a[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)
else:
    X = sorted(X)
    L = []
    for i in range(0, M-1):
        L.append(X[i+1] - X[i])
    L = sorted(L, reverse = True)
    S = 0
    for i in range(N-1):
        S += L[i]
    res = X[M-1] - X[0] - S
    print(res)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
xlist = list(sorted(map(int, input().split())))
dxlist = sorted([nx - px for px, nx in zip(xlist, xlist[1:])], key=lambda x: -x)
if n >= m:
  print(0)
else:
  print(xlist[-1] - xlist[0] - sum(dxlist[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(max(X) - min(X) - sum(d[:N-1]))

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
if m <= n:
  print(0)
  exit()
xx = list(map(int, input().split()))
xx.sort()
sub_x = [0]*(m-1)
for i, (front, end) in enumerate(zip(xx[:-1],xx[1:])):
  sub_x[i] = end - front
sub_x.sort()
print(sum(sub_x[:m-n]))

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
X = list(map(int, input().split(" ")))
X.sort()
total = X[-1]-X[0]
dif = [X[i+1] - X[i] for i in range(len(X)-1)]
dif.sort()
Y = [0 for i in range(N-1)]
for i in range(N-1):
  try:
    Y[i] = dif.pop()
  except:
    Y[i]=0
print(total-sum(Y))

Submission status: Accepted

Solution code:
num, num_of_coordinates = map(int, input().split(" "))
if num >= num_of_coordinates:
  print ("0")
else:
  coordinates = list(map(int, input().split(" ")))
  total = 0
  abs_distance = []
  coordinates.sort()
  for i in range(0, len(coordinates)-1):
    dist = abs(coordinates[i] - coordinates[i+1])
    total += dist
    abs_distance.append(dist)  

  #でかいやつ順に並べる
  abs_distance.sort()
  abs_distance.reverse()
  for i in range(num-1):
    total -= abs_distance[i]

  print(total)

Submission status: Accepted

Solution code:
nm_ent = list(map(int, input().split()))
n = nm_ent[0]
m = nm_ent[1]

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