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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X = sorted(X)

if len(X) >= 2:
    diff = list()
    for i in range(len(X)-1):
        diff.append(abs(X[i+1]-X[i]))
    diff = sorted(diff)[:-(N-1)]

    print(sum(diff))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M = list(map(int, input().split()))
X = list(map(int, input().split()))
X = sorted(X)

if len(X) >= 2:
    diff = list()
    for i in range(len(X)-1):
        diff.append(abs(X[i+1]-X[i]))
    diff = diff[:-(N-1)]
    print(sum(diff))
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
import sys


def solution(arr, moves):
    if len(arr) <= moves:
        return 0

    arr.sort()
    if moves == 1:
        return abs(arr[0] - arr[-1])

    diff = [None] * len(arr)
    diff[0] = abs(arr[0] - arr[1])
    diff[-1] = abs(arr[-2] - arr[-1])
    for i in range(1, len(arr) - 1):
        diff[i] = abs(arr[i] - arr[i - 1]) if  abs(arr[i] - arr[i - 1]) > abs(arr[i] - arr[i + 1]) else abs(arr[i] - arr[i + 1])
    d = enumerate(diff)

    visited = {}
    current_positions = {}

    for i in list(reversed(sorted(d, key=lambda x: x[1])))[:moves]:
        visited[i[0]] = True
        current_positions[i[0]] = True

    counter = 0
    while len(visited) < len(arr):
        min_on_position = sys.maxsize
        minimum_element = None
        side = None
        for i in current_positions:
            if i - 1 not in visited and i > 0:
                if min_on_position > abs(arr[i - 1] - arr[i]):
                    min_on_position = abs(arr[i - 1] - arr[i])
                    minimum_element = i
                    side = i - 1

            if i + 1 not in visited and i < len(arr) - 1:
                if min_on_position > abs(arr[i + 1] - arr[i]):
                    min_on_position = abs(arr[i + 1] - arr[i])
                    minimum_element = i
                    side = i + 1


        current_positions[side] = True
        visited[side] = True
        counter += abs(arr[minimum_element] - arr[side])

    return counter

for i,line in enumerate(sys.stdin):
    if i == 0:
       M, N = line.split(" ")
    if i == 1:
        A = [int(i) for i in line.split(" ")]
        print(solution(A, int(M)))

# line = "-10 -3 0 9 -100 2 17"
#
# lines = [int(i) for i in line.split(" ")]
# a = [10,12,1,2,14]
# b = [-100000]
# print(solution(lines, 3))
# print(solution(a, 2))
# print(solution(b, 1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X.sort() #昇順に

if N>=M:
  print(0)

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

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

Submission status: Wrong Answer

Solution code:
s1=[int(i) for i in input().split()]
N=s1[0]
M=s1[1]
s2=[int(i) for i in input().split()]
a=sorted(s2)

if N>=M:
  print(0)

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

s_sabun=sorted(sabun)

trysum=0
for i in range(M-N):
  trysum=trysum+s_sabun[i]

print(trysum)

Submission status: Wrong Answer

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

def main():
    if N >= M:
        print(1)
        return
    XList.sort()
    dX = [XList[i+1]-XList[i] for i in range(len(XList)-1)]
    dX.sort(reverse=True)
    ans = XList[-1]-XList[0]-sum(dX[:N-1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if m <= n:
    print(0)
else:
    points.sort()
    dx = [ abs(points[i]-points[i+1]) for i in range(0,len(points)-1) ]
    dx.sort()
    print(sum(dx[:m-n]))
    print(dx[:m-n])

Submission status: Wrong Answer

Solution code:
# M: the number of KOMA
# N: the number of Xs
M, N = map(int, input().split())
# each length of edges
Xs= list(map(int, input().split()))
 
# searching largest one
largestX = -10**5
 
# searching smallest one
smallestX = 10**5
 
# straightforward searching way
for i in range(N):
  if Xs[i] > largestX:
    largestX = Xs[i]
  if Xs[i] < smallestX:
    smallestX = Xs[i]
 
 
if M > N:
    print(0)
elif (largestX - smallestX) % M == 0:
    print( int((largestX - smallestX) / M) )
else:
    print( int((largestX - smallestX) / M )+ 1)

Submission status: Wrong Answer

Solution code:
# M: the number of KOMA
# N: the number of Xs
M, N = map(int, input().split())
# each length of edges
Xs= list(map(int, input().split()))

# searching largest one
largestX = -10**5

# searching smallest one
smallestX = 10**5

# straightforward searching way
for i in range(N):
  if Xs[i] > largestX:
    largestX = Xs[i]
  if Xs[i] < smallestX:
    smallestX = Xs[i]


if (largestX - smallestX) % M == 0:
    print( int((largestX - smallestX) / M) )
else:
    print( int((largestX - smallestX) / M )+ 1)

Submission status: Wrong Answer

Solution code:
m,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
a.sort()
c=a[0]
diff=[]
for i in range(1,n):
    diff.append(a[i]-c)
    c=a[i]
diff.sort()
s=sum(diff[:n-m])
print(s)

Submission status: Wrong Answer

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

Xs.sort()
L=[]
for i in range(M-1):
  L.append(Xs[i+1]-Xs[i])
  
L.sort()
print(sum(L[:1-N]))

Submission status: Wrong Answer

Solution code:

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

aida = []
for i in range(len(x)-1):
    a = x[i]
    b = x[i + 1]
    sa = b - a
    aida.append(sa)

sum_ = sum(sorted(aida)[:-n+1])
print(sum_)

Submission status: Wrong Answer

Solution code:
# C
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    new_list = []
    for j in range(1, M):
        new_list.append(abs(X[j] - X[j-1]))
    new_list.sort()
    print(sum(new_list[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    new_list = []
    for j in range(1, M):
        new_list.append(X[j] - X[j-1])
    new_list.sort()
    print(sum(new_list[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
# C
N, M = map(int, input().split())
X = list(map(int, input().split()))
if N >= M:
    print(0)
else:
    X.sort()
    new_list = []
    for j in range(1, M):
        new_list.append(X[j] - X[j-1])
    new_list.sort()
    print(sum(new_list[:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def game(n, m, x_list):
  if n >= m:
    return 0
  
  x_list.sort()
  interval = []
  for i in range(m - 1):
    interval.append(abs(x_list[i + 1] - x_list[i]))
  interval.sort(reverse = True)
  sum_interval = 0
  for i in range(n - 1):
    sum_interval += interval[i]
  print(sum_interval)
  return abs(x_list[m - 1] - x_list[0]) - sum_interval

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

print(game(n, m, x))

Submission status: Wrong Answer

Solution code:
def game(n, m, x_list):
  if n >= m:
    return 0
  
  x_list.sort()
  interval = []
  for i in range(m - 1):
    interval.append(x_list[i + 1] - x_list[i])
  interval.sort(reverse = True)
  sum_interval = 0
  for i in range(n):
    sum_interval += interval[i]
  return abs(x_list[m - 1] - x_list[0]) - sum_interval

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

print(game(n, m, x))

Submission status: Wrong Answer

Solution code:
def game(n, m, x_list):
  if n >= m:
    return 0
  x_list.sort()
  count = 0
  stone = [x_list[0]]
  
  interval = []
  for i in range(m - 1):
    interval.append(x_list[i + 1] - x_list[i])
    interval.sort(reverse = True)
    if len(interval) > n - 1:
      del(interval[n - 1])
  for i in range(n - 1):
    for j in range(m - 1):
      if x_list[j + 1] - x_list[j] == interval[i]:
        stone.append(x_list[j + 1])
        break
  stone.sort()

  for i in range(n):
    for j in range(1, m):
      if i == n - 1:
        if x_list[j] == stone[n - 1]:
          count += abs(x_list[m - 1] - x_list[j])
          break
      elif x_list[j] == stone[i + 1]:
        count += abs(x_list[j - 1] - stone[i])
        break
  return count

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

print(game(n, m, x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

(N,M) = list(map(int,input().split()))
xlis = list(map(int,input().split()))
if N >= M:
    print("1")
else:
    xlis.sort()
    dist_lis = []
    for i in range(1,len(xlis)):
        dist_lis.append(xlis[i] - xlis[i-1])
    dist_lis.sort()

    for i in range(N-1):
        dist_lis.pop(-1)
    print(sum(dist_lis))

Submission status: Wrong Answer

Solution code:
import sys
(N,M) = list(map(int,input().split()))
xlis = list(map(int,input().split()))
if N >= M:
    print("1")
    sys.exit()

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

for i in range(N-1):
    dist_lis.pop(-1)
print(sum(dist_lis))

Submission status: Wrong Answer

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

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

print(sum(sa[:-N+1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, input().split())
X = list(map(int, input().split()))
if n >= m:
  print(0)
  sys.exit()
X.sort()
print(X)
L = []
for i in range(m-1):
  L.append(X[i+1] - X[i])
#print(L)
L.sort(reverse=True)
#print(L)
#print(sum(L[0:1]))
print((X[m-1]-X[0]) - (sum(L[0:n-1])))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if N >= M:
    c = 0
else:
    X = np.array(sorted(X))
    dist = np.sort(X[1:] - X[:-1])
    c = np.sum(dist[:-(N-1)])

print(c)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力

import copy

n, m = map(int, input().split())
ary = [int(s) for s in input().split()]

if n < m :
    ary.sort()
    sa = []
    
    for i in range(0, m - 1):
        sa.append(ary[i + 1] - ary[i])
    
    print(sa)
    
    sa.sort()
    
    for i in range(0, n - 1):
        sa.pop(-1)
    
    print(sum(sa))
else :
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if m <= n:
    print(0)
    exit

xs.sort()

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

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

Submission status: Wrong Answer

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

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

if m <= n:
    print(0)
    exit

x.sort()

l = [x[i] - x[i - 1] for i in range(1, len(x))]
l.sort(reverse=True)

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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
from itertools import combinations

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

N, M = map(int, input().split())
Xs = sorted([int(i) for i in input().split()])
width = Xs[-1] - Xs[0]
result = 10**5*2
for wall in combinations(list(range(1,M)), N-1):
    _sum = calc_sum(width, sorted(wall))
    if result > _sum:
        result = _sum
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

import sys

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

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

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

mem = {}
nums.sort()

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

mem = {}
nums.sort()

print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
x= list(map(int, input().split()))
x.sort()
l = [x]
if n >= m :
    print(0)
    exit()

for i in range(0,n-1) :
    max = 0 
    max_i = 0
    for j in range(i+1) :
        if (max < (l[j][-1]-l[j][0])) :
            max = l[j][-1]-l[j][0]
            max_i = j
    # max_iのリストが  最も差が大きく、分割の対象
    mid = (l[max_i][0] + l[max_i][-1])//2    
    for ii in range(0, len(l[max_i])) :
        if l[max_i][ii]>mid :
            l.append(l[max_i][ii:])
            l[max_i] = l[max_i][0:ii]
            break

ans = 0
for jj in range(n) :
    ans += l[jj][-1] - l[jj][0]
    #print(l[jj][-1] - l[jj][0])

print (ans)
#print (l)

Submission status: Wrong Answer

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

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

print(step)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

Xs.sort()
XsDist = [0]

for i in range(len(Xs)):
    if not i == 0:
        XsDist.append(Xs[i] - Xs[i-1])

XsDist.sort()
print(sum(XsDist[:n+2]))

Submission status: Wrong Answer

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

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

Xs.sort()
XsDist = [0]

for i in range(len(Xs)):
    if not i == 0:
        XsDist.append(Xs[i] - Xs[i-1])

XsDist.sort()
print(sum(XsDist[:n+1]))

Submission status: Wrong Answer

Solution code:
# C Streamline
import copy

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

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

Xs.sort()
XsDist = []

for i in range(len(Xs)):
    if not i == 0:
        XsDist.append(Xs[i] - Xs[i-1])

# XsDistSorted = []
XsDistSorted = copy.deepcopy(XsDist)
XsDistSorted.sort()
print(sum(XsDistSorted[:n+1]))

Submission status: Wrong Answer

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

list_x.sort()
list_d=[]

if m<=n:
    print(0)
else:
    i=0
    while i<=m-2:
        list_d.append(list_x[i+1]-list_x[i])
        i+=1
    list_d.sort()
    print(sum(list_d[0:m-n:1]))
    print(list_d)

Submission status: Wrong Answer

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

list_x.sort()
list_d=[]

if m<=n:
    print(0)
else:
    i=0
    while i<=m-2:
        list_d.append(list_x[i+1]-list_x[i])
        i+=1
    list_d.sort()
    print(sum(list_d[0:n-1:1]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np


def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N, M = lii()
    if N >= M: return 0

    X = lii()
    X = sorted(X)
    L = np.array([X[i+1] - X[i] for i in range(len(X) - 1)])
    Lsort = sorted(L)

    return sum(Lsort[:M-N+1])


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np


def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N, M = lii()
    if N >= M: return 0

    X = lii()
    X = sorted(X)
    L = np.array([X[i+1] - X[i] for i in range(len(X) - 1)])
    Lsort = sorted(L)

    return sum(Lsort[:-N+1])


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools as it

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

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

result = 100000
startPoints = list(it.combinations_with_replacement(X, N))
for start in startPoints:
    start = list(start)
    X_tmp = X.copy()
    [X_tmp.remove(i) for i in start if X_tmp.__contains__(i)]
    count = 0
    while X_tmp.__len__() != 0:
        comb = list(it.product(X_tmp, start))
        dist_list = [np.abs(n-d) for n, d in comb]
        dist_min = np.min(dist_list)
        dist_idx = dist_list.index(dist_min)
        dist, now = comb[dist_idx]
        for i in range(len(start)):
            if start[i] == now:
                start[i] = dist
                break
        [X_tmp.remove(i) for i in start if X_tmp.__contains__(i)]
        count = count + dist_min
    result = count if count < result else result

print(result)

Submission status: Wrong Answer

Solution code:
import sys

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

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

print(sum(nlist[0:-n+1]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

x_list.sort()
diff_list = [x_list[i] - x_list[i-1] for i in range(1, len(x_list))]
diff_list.sort()
print(sum(diff_list[:-(n-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.sort()

c = []
if len(b) != 1:
  for i in range(len(b) - 1):
    c.append(b[i+1] - b[i])

if len(b) == 1:
  print(1)
  
    
else:  
  for j in range(min(a[0] - 1,a[1])):
    for k in range(len(c)):
      if c[k] == max(c):
        c[k] = 0
        break
  print(sum(c))

Submission status: Wrong Answer

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

c = []
if len(b) != 1:
  for i in range(len(b) - 1):
    c.append(b[i+1] - b[i])

if len(b) == 1:
  print(1)
  
    
else:  
  for j in range(min(a[0] - 1,a[1])):
    for k in range(len(c)):
      if c[k] == max(c):
        c[k] = 0

  print(sum(c))

Submission status: Wrong Answer

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


main()

Submission status: Wrong Answer

Solution code:
import numpy as np

n, m = [int(_) for _ in input().split()]
x = np.array(sorted((int(_) for _ in input().split())))
d = np.sort(x[1:] - x[:-1])[:n:-1]

print(x[-1] - x[0] - np.sum(d))

Submission status: Wrong Answer

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

    if n >= m:
        print(0)
        return

    x.sort()
    x1 = x[1:]
    x2 = x[:-1]
    xs = [_x1 - _x2 for _x1, _x2 in zip(x1, x2)]

    xs.sort()
    print(x[-1] - x[0] - sum(xs[-1-(n-2):]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    if n >= m:
        print(0)
        return

    x.sort()
    x1 = x[1:]
    x2 = x[:-1]
    xs = [_x1 - _x2 for _x1, _x2 in zip(x1, x2)]

    argxs = sorted(enumerate(xs), key=lambda y: y[1])
    xs.sort()
    print(sum(xs[:n+1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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()
print(sum(Y[0:N+1]))

Submission status: Wrong Answer

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

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

print(sum(D[0:M-N]))

Submission status: Wrong Answer

Solution code:
import numpy as np
 
def select():
    max_=0
    index=0
    for i in X:
        temp=np.min(np.absolute(a-i))+i
        if temp>max_:
            max_=temp
            index=i
    return index
 
def walk(a,X,ans):
    min_=float('inf')
    x=0
    ppp=0
    for i in X:
        p=np.absolute(a-i)
        dis_min_index=np.argmin(p)
        temp=p[dis_min_index]
        if temp<min_:
            ppp=dis_min_index
            min_=temp
            x=i
    if a[ppp]>x:
        a[ppp]-=min_
    else:
        a[ppp]+=min_
    ans+=min_
    X=np.delete(X,np.where(X==x))
    return ans,X

N,M=list(map(int,input().split()))
X=np.sort(np.array(list(map(int,input().split()))))
if N==1:
    print(X[-1]-X[0])  
elif N>=M:
    print(0)
else:
    a=np.array([X[0],X[-1]])
    ans=0
    for i in range(N-2):
        a=np.append(a,select())
    for i in a:
        X=np.delete(X,np.where(X==i))
    while(np.size(X)!=0):
        ans,X=walk(a,X,ans)
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

xlist.sort()
ylist=[]

for i in range(1,m):
    ylist.append(xlist[i]-xlist[i-1])

ylist.sort()
a=0
for i in range(1,m-n):
    a+=ylist[i]
print(a)

Submission status: Wrong Answer

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