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:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
d = []
if m==1:
    print(0)
elif n>=m:
    print(1)
elif n<m:
    for i in range(m-1):
        d.append(x[i+1] - x[i])
    print(sum(sorted(d)[:m-n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = []
if n<m:
    for i in range(m-1):
        d.append(x[i+1] - x[i])
    print(sum(sorted(d)[:-(n-1)]))
else:
    print(1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    if m <= n:
        print(0)
        return

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

    sort_d = sorted(d)

    del sort_d[n+1:]

    print(sum(sort_d))
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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:M-N]))

Submission status: Wrong Answer

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

    if m <= n:
        print(0)
        return

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

    sort_d = sorted(d)

    del sort_d[n+1:]

    print(sum(sort_d))
    return


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 m <= n:
        print(0)
        return

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

    sort_d = sorted(d)
    del sort_d[n+1:]

    print(sum(sort_d))
    return


if __name__ == '__main__':
    main()

'''
マス同士の距離を算出
距離が離れている順にコマを置く

'''

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(1, M):
	dif.append(abs(X[i] - X[i-1]))

dif.sort()
difLen = len(dif)
# M - N = (M-1) - (N-1)
e = M - N
ans = sum(dif[0:e])

print(ans)

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(1, M):
	dif.append(abs(X[i] - X[i-1]))

dif.sort()
difLen = len(dif)
# M - N = (M-1) - (N-1)
ans = sum(dif[0:difLen - (N-1)])

print(ans)

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(1, M):
	dif.append(abs(X[i] - X[i-1]))

dif.sort()

ans = sum(dif[0:len(dif)-(N-1)])

print(ans)

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(1, M):
	dif.append(abs(X[i] - X[i-1]))

dif.sort()

ans = sum(dif[0:-(N-1)])

print(ans)

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(1, M):
	dif.append(abs(X[i] - X[i-1]))

dif.sort()

ans = sum(dif[0:N+1])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    # n = int(input())
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    # s = input()
    # h = [int(input()) for _ in range(n)]

    x.sort()

    subs = []

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

    subs.sort()
    print(subs)
    if (m <= n):
        print(0)
    else:
        print(sum(subs[0:m-n]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    # n = int(input())
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    # s = input()
    # h = [int(input()) for _ in range(n)]

    x.sort()

    subs = []

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import tee


def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


N, M, *X = map(int, open(0).read().split())
X.sort()
diffs = [cur - prev for prev, cur in pairwise(X)]
diffs.sort()
print(sum(diffs[: M - 1 - (N - 1)]))

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float('inf')

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float('inf')
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float('inf')
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(' ')
        elif type(ls[0])==str:
            print(' '.join(ls))
        elif type(ls[0])==int:
            print(' '.join(map(str,ls)))
        else:
            print('Output Error')
    else:
        if len(ls)==0:
            print('')
        elif type(ls[0])==str:
            print(''.join(ls))
        elif type(ls[0])==int:
            print(''.join(map(str,ls)))
        else:
            print('Output Error')

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

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

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

mod = 10**9 + 7

n,m = MI()
x = RI()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

yy = [abs(xx[i]-xx[i+1]) for i in range(m-1)]

sy = sorted(yy)
print(sum(sy[:-n+1]))

Submission status: Wrong Answer

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    x = sorted(Input())

    if n >= m:
        print(0)
        exit()
    index = -1 * (n-1)
    data = sorted(abs(x[i]-x[i+1]) for i in range(m-1))
    print(sum(data[:index]))


main()

Submission status: Wrong Answer

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    x = sorted(Input())

    index = -1 * (n-1)
    data = sorted(abs(x[i]-x[i+1]) for i in range(m-1))
    print(sum(data[:index]))


main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 18:32:27 2020

@author: liang
"""

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

def solve():
    if N >= M :
        return 0
    X.sort()
    d = [X[i+1] - X[i] for i in range(M-1)]
    d.sort()
    #print(X)
    #print(d)
    return sum(d[0:-(N-1)])
print(solve())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 18:32:27 2020

@author: liang
"""

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

def solve():
    if N >= M :
        return 0
    X.sort()
    d = [X[i+1] - X[i] for i in range(M-1)]
    d.sort()
    return sum(d[0:-(N-1):1])
print(solve())

Submission status: Wrong Answer

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

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

x.sort()

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

d.sort()

print(sum(d[:-n+1]))

Submission status: Wrong Answer

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

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

al_s = sorted(al)
res=[]
for i in range(m-1):
    res.append(al_s[i+1]-al_s[i])

res_s = sorted(res)

print(sum(res)-sum(res_s[n+1:]))

Submission status: Wrong Answer

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

if m==1:
  print(0)
else:
  num=n-1
  sl=sorted(l)

  dsl=[abs(sl[i+1]-sl[i]) for i in range(m-1)]

  dsl=sorted(dsl)
  print(sum(dsl[0:len(dsl)-num]))

Submission status: Wrong Answer

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

l.sort()
# print(l)
list=[]
if n != m:
    for i in range(m - 1):
        list.append(abs(l[i + 1] - l[i]))

    list.sort()
    # print(list)
    print(sum(list[0:(m - n)]))
else:
    print(0)

Submission status: Wrong Answer

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

l.sort()
# print(l)
list=[]
for i in range(m-1):
    list.append(abs(l[i+1]-l[i]))


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = np.array(sorted(X), dtype=np.int64)
diffX = np.sort(np.diff(sortX))
ans = np.sum(diffX[:-(N-1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = np.array(sorted(X))
diffX = np.sort(np.diff(sortX))
ans = np.sum(diffX[:-(N-1)])
print(ans)

Submission status: Wrong Answer

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

gaps = [x[i] - x[i-1] for i in range(1, len(x))]

if n == 1:
    print(sum(gaps) - 1)
else:
    print(sum(sorted(gaps)[:-n+1]))

Submission status: Wrong Answer

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

gaps = [x[i] - x[i-1] for i in range(1, len(x))]
print(sum(sorted(gaps)[:-n+1]))

Submission status: Wrong Answer

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

gaps = sorted([x[i] - x[i-1] for i in range(1, len(x))])[-n+1:]


count = 0
for i in range(1, len(x)):
    if x[i] - x[i-1] in gaps:
        gaps.remove(x[i] - x[i-1])
    else:
        count += x[i] - x[i-1]

print(count)

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,M = mint()
X = lint()
X.sort()
D = [X[i+1]-X[i] for i in range(M-1)]
D.sort()
print(sum(D[:M-N]))

Submission status: Wrong Answer

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

print(X[M-1]-X[0]+1-ans-N)

Submission status: Wrong Answer

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

X.sort()

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return float(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()



def main():
	n,m=MI()
	x=LI()
	x.sort()
	A=[]
	for i,j in zip(x[1:],x[:-1]):
		A.append(i-j)
	#print(A)
	A.sort()
	print(sum(A[:m-n]))


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

if(m==1):
    print(0)

else:
    li_X.sort()

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

    li_X_delta_sorted_rev = sorted(li_X_delta, reverse=True)
    path_ignorable = []
    cnt = 0

    for i in range(min(n-1, len(li_X_delta))):
        path_ignorable.append(li_X_delta.index(li_X_delta_sorted_rev[cnt]))
        cnt += 1
    # ignore path from li_X[i] to li_X[i+1] for i in path_ignorable
    
    ans = 0
    for i in range(len(li_X_delta)):
        if(i in path_ignorable):
            continue
        ans += li_X_delta[i]
    print(ans)

Submission status: Wrong Answer

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

# 区間の距離のリスト
if len(X) == 1:
    print(0)
else:
    dist = []
    for i in range(1,M):
        dist.append(X[i]-X[i-1])
    dist = sorted(dist,reverse = False)
    print(dist)
    # 区間の距離を降順に N-1 個分消去
    for k in range(N-1):
        dist.pop()
        if len(dist) == 0:
            break
    print(sum(dist))

Submission status: Wrong Answer

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

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

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

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

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:
from itertools import accumulate
import sys


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


N, M = LI()
X = LI()
if M <= N:
    print(0)
    sys.exit()
sa = []
X.sort()
for i in range(M-1):
    sa.append(X[i+1]-X[i])
sa.sort(reverse=True)
sa = list(accumulate(sa))
if N == 1:
    print(sa)
else:
    print(sa[M-2]-sa[N-2])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

X.sort()

Y = []
for i in range(m-1):
    Y.append(X[i+1]-X[i])
#print(Y)
Y.sort()
ans = sum(Y[0:-(n-1)])
print(ans)

Submission status: Wrong Answer

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

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

    B.sort()
    print(sum(B[:-(N-1)]))

Submission status: Wrong Answer

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

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

    B.sort()
    print(sum(B[:-(N-1)]))

Submission status: Wrong Answer

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

if N>=M:
    print(0)
    exit()
else:
    for i in range(M-2):
        B.append(X[i+1]-X[i])

    B.sort()
    print(sum(B[:-(N-1)]))

Submission status: Wrong Answer

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

if N<=M:
    print(0)
    exit()
else:
    for i in range(M-1):
        B.append(X[i+1]-X[i])

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

Submission status: Wrong Answer

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

#print(X)
for i in range(M-1):
    B.append(X[i+1]-X[i])

B.sort()
#print(B)


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

Submission status: Wrong Answer

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

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

B.sort()
#print(B)


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

Submission status: Wrong Answer

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

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

B.sort()
#print(B)


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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc117/tasks/abc117_c

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, m = input_int_list()
    X = sorted(input_int_list())
    diffs = []
    for i in range(1, m):
        diffs.append(abs(X[i - 1] - X[i]))
    diffs = sorted(diffs)
    ans = sum(diffs[:-n + 1])
    if n >= m:
        print(0)
        return
    else:
        print(ans)

    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
'''
Created on 2020/08/28

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N,M=map(int,pin().split())
  X=list(map(int,pin().split()))
  X.sort()
  d=[]
  for i in range(M-1):
    d.append(abs(X[i]-X[i+1]))
  d.sort()
  print(sum(d[:M-N]))
  return 
main()
#解説AC

Submission status: Wrong Answer

Solution code:


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

X.sort()
cnt = 0
arr = []
for i in range(1,M):
    arr.append(abs(X[i-1]-X[i]))
#print(arr)
arr.sort()
ans = sum(arr[0:M-(N)])
print(ans)

Submission status: Wrong Answer

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

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

ans = sum(res[:len(res)-n+1])
print(ans)

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(len(X)-1):
    D.append(X[i+1]-X[i])
D.sort()
print(sum(D[:M-N]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n,m = list(map(int, input().split()))
    pos_list = list(map(int, input().split()))
    pos_list.sort()
    if n > m:
        return 0
    diff_list =[0]
    for i in range(m-1):
        diff_list.append(pos_list[i+1]-pos_list[i])
    diff_list.sort()
    #print(pos_list)
    #print(diff_list)
    return sum(diff_list[:-n+1])

print(main())

Submission status: Wrong Answer

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

else:
    x.sort()
    for i in range(m-1):
        d.append(abs(x[i] - x[i + 1]))
    d.sort()
    print(sum(d[:min(m-n, n+1)]))

Submission status: Wrong Answer

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

else:
    x.sort()
    for i in range(m - 1):
        d.append(abs(x[i] - x[i + 1]))
    d.sort()
    print(sum(d[:n+1]))

Submission status: Wrong Answer

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

nums = [0]*(m-1)
for i in range(m-1): nums[i] = x[i+1] - x[i]
nums.sort()
print(sum(nums[:m-1-(n-1)]))

Submission status: Wrong Answer

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

nums = [0]*(m-1)
for i in range(m-1): nums[i] = x[i+1] - x[i]
nums.sort()
print(sum(nums[:n+1]))

Submission status: Wrong Answer

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

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

X.sort()

memo = []

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

memo.sort()

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

Submission status: Wrong Answer

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

diffs = []
for x1,x2 in zip(xl[:-1],xl[1:]):
    diff = x2-x1
    diffs.append(diff)

diffs.sort()
ans = sum(diffs[:selected])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
if n >= m:
    print(0)
    exit()
x = sorted(map(int, input().split()))
l = sorted(i - j for i, j in zip(x[1:], x[:-1]))
print(sum(l[: 1 - n]))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    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()
    
    print(sum(B[:m-n]))

Submission status: Wrong Answer

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

x = sorted(x)
dist = [x[i+1] - x[i] for i in range(len(x) - 1)]

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

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    Xs = sorted(list(map(int, input().split())))
    Xs_diff = []
    for i in range(1, m):
        Xs_diff.append(Xs[i]-Xs[i-1])

    Xs_diff.sort()
    ans = sum(Xs_diff[:(m-n)])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    Xs = sorted(list(map(int, input().split())))
    Xs_diff = []
    for i in range(1, m):
        Xs_diff.append(Xs[i]-Xs[i-1])

    Xs_diff.sort()
    ans = sum(Xs_diff[:(n+1)])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, m = nm()
    X = nl()
    if n >= m:
        print(0)
    else:
        X = sorted(X)
        D = [X[i + 1] - X[i] for i in range(m - 1)]
        D = sorted(D)
        print(sum(D[:-n + 1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, m = nm()
    X = nl()
    X = sorted(X)
    D = [X[i + 1] - X[i] for i in range(m - 1)]
    D = sorted(D)
    print(sum(D[:-n + 1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

X.sort()

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

Y=sorted(Y)[:M-N]
print(sum(Y))

Submission status: Wrong Answer

Solution code:
import sys

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

X.sort()

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

X.sort()

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

Y=sorted(Y)[:-(N-M)]
print(sum(Y))

Submission status: Wrong Answer

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

df.sort()
print(sum(df[: n - k]))

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 = sorted(X)
  list = [0]*(M-1)
  for i in range(M-1):
    list[i] += X[i+1]-X[i]
    list = sorted(list)
    list1 = list[:M-N]
  print(sum(list1))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, m = inintm()
X = inintl()

X.sort()
diff = []

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

diff.sort()

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

Submission status: Wrong Answer

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, m = inintm()
X = inintl()

X.sort()
diff = []

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

diff.sort()

print(sum(diff[:n+1]))