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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def examA():
    T, X = LI()
    ans = T/X
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    ans = "Yes"
    if max(L)*2>=sum(L):
        ans = "No"
    print(ans)
    return

def examC():
    N, M = LI()
    X = LI(); X.sort()
    x = []
    for i in range(M-1):
        x.append(abs(X[i+1]-X[i]))
    x.sort()
    ans = sum(x[:M-N])
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

Solution code:
def examA():
    T, X = LI()
    ans = T/X
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    ans = "Yes"
    if max(L)*2>=sum(L):
        ans = "No"
    print(ans)
    return

def examC():
    N, M = LI()
    X = LI(); X.sort()
    x = []
    for i in range(M-1):
        x.append(X[i+1]-X[i])
    x.sort()
    ans = sum(x[:M-N])
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Wrong Answer

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

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()
z=[]
for i in range(1,m):
    z.append(x[i]-x[i-1])
z.sort()
print(x[m-1]-x[0]-sum(z[-(n-1):]))

Submission status: Wrong Answer

Solution code:
N,M=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls.sort()
d=[ls[i+1]-ls[i] for i in range(M-1)]
d.sort()
for _ in range(N-1):
  if len(d)==0:
    break
  del d[0]
print(sum(d))

Submission status: Wrong Answer

Solution code:
N,M=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls.sort()
d=[ls[i+1]-ls[i] for i in range(M-1)]
d.sort()
for _ in range(N):
  if len(d)==0:
    break
  del d[0]
print(sum(d))

Submission status: Wrong Answer

Solution code:
N,M=[int(s) for s in input().split()]
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[:M-N]))

Submission status: Wrong Answer

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)
print(sum(s[:M-N]))

Submission status: Wrong Answer

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)
print(sum(s[:M-N]))
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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)
print(sum(s[:N]))

Submission status: Wrong Answer

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

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

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

table.sort()
desk = []
for x in range(0, len(table) - 1):
    desk.append(table[x + 1] - table[x])
desk.sort()
for x in range(0, n - 1):
    if len(desk) < 2:
        break
    desk.pop(-1)
cnt = 0
for x in desk:
    cnt += x
print(cnt)

Submission status: Wrong Answer

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

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

table.sort()
desk = []
for x in range(0, len(table) - 1):
    desk.append(table[x + 1] - table[x])
desk.sort()
for x in range(0, n - 1):
    if len(desk) < 2:
        break
    desk.pop(-1)
cnt = 0
for x in desk:
    cnt += x
print(cnt)

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(abs(x[i]-x[i-1]))
sa=sorted(sa)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

x.sort()
for i in range(1, m):
    dist.append(x[i] - x[i-1])
dist.sort()
print(sum(dist[:m-n]))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
ans=0
cnt=x[0]
c=max(x)-min(x)
a=0
for i in range(m):
    #print(ans,a)
    if x[i]-cnt>c/n and a<n-1:
        cnt=x[i]
        a+=1
        continue
    else:
        if i!=m-1:
            ans+=x[i]-cnt
            cnt=x[i]
        elif a<n-1 and i==m-1:
            continue
        else:
            ans+=x[i]-cnt
            cnt=x[i]
print(ans)
#print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve(n, m, x):
    ans = 0
    if m <= n:
        ans = 0
    else:
        x.sort()
        diff = [j-i for i, j in zip(x[0:-1], x[1:])]
        diff.sort()
        ans = (x[-1]-x[0])-sum(diff[(-(n-1)):])

    return ans


if __name__ == '__main__':
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    print(solve(n, m, x))

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
X = list(map(int, input().split()))
sortX = sorted(X)
dist = []
if M == 1:
    print(0)
else:
    for i in range(M-1):
        dist.append(sortX[i + 1] - sortX[i])
    dist.sort()
    print(sum(dist[:M-N]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
IINF = 10 ** 18


def main():
    N, M = list(map(int, input().split()))
    X = list(map(int, input().split()))
    X = sorted(X)
    # left = -IINF
    # right = IINF

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

        # print(L)

        L.sort()
        ans = sum(L[0:-(N-2 + 1)])
        print(ans)

    # for i in range(N):
    #     if len(X) == 0:
    #         break
    #     elif i == 0:
    #         left = X[0]
    #         X.pop(0)
    #     elif i == 1:
    #         right = X[-1]
    #         X.pop(len(X)-1)
    #     else:
    #         if abs(X[1] - X[-1]) < abs(X[-2] - X[0]):
    #             left = X[0]
    #             X.pop(0)
    #         else:
    #             right = X[-1]
    #             X.pop(-1)

    # ans = 0
    # while len(X) > 0:
    #     # print(ans, X)
    #     if abs(left - X[0]) < abs(right - X[-1]):
    #         ans += abs(left - X[0])
    #         left = X[0]
    #         X.pop(0)
    #     else:
    #         ans += abs(right - X[-1])
    #         right = X[-1]
    #         X.pop(len(X)-1)
    #     # b = min(abs(left - X[0]), abs(right - X[-1]))
    #     # print(b, X)
    #     # print(abs(X[0] - X[-1]) + b)
    # print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
IINF = 10 ** 18


def main():
    N, M = list(map(int, input().split()))
    X = list(map(int, input().split()))
    X = sorted(X)
    left = -IINF
    right = IINF

    if M <= N:
        print(0)
        return

    for i in range(N):
        if len(X) == 0:
            break
        elif i == 0:
            left = X[0]
            X.pop(0)
        elif i == 1:
            right = X[-1]
            X.pop(len(X)-1)
        else:
            if abs(X[1] - X[-1]) < abs(X[-2] - X[0]):
                left = X[0]
                X.pop(0)
            else:
                right = X[-1]
                X.pop(-1)

    ans = 0
    while len(X) > 0:
        # print(ans, X)
        if abs(left - X[0]) < abs(right - X[-1]):
            ans += abs(left - X[0])
            left = X[0]
            X.pop(0)
        else:
            ans += abs(right - X[-1])
            right = X[-1]
            X.pop(len(X)-1)
        # b = min(abs(left - X[0]), abs(right - X[-1]))
        # print(b, X)
        # print(abs(X[0] - X[-1]) + b)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

D = sorted([X[i]-X[i+1] for i in range(M-1)])
print(sum(D[:M-N]))

Submission status: Wrong Answer

Solution code:


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


s=[a[i]-a[i-1] if i!=0 else 0 for i in range(1,m) ]

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

Submission status: Wrong Answer

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

if n >= m:
  print(0)
  exit(0)
  
x.sort()
d=[0]*(m-1)
for i in range(m-1):
  d[i] = x[i + 1] - x[i]
  
d.sort()

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000)

n, k = list(map(int, input().split(' ')))
l = sorted(list(map(int, input().split(' '))))
#print(l)
#l =     [1,2,10,12, 14]]
#diffs = [1, 8, 2, 2]
# f(n,k) = hoge
# f(5,5) = 0
# f(4,5) = 1
# f(3,5) = 3
# f(2,5) = 5
# f(1,5) = 13
diffs = [l[i+1]-l[i] for i in range(len(l)-1)] 
diffs = sorted(diffs)
print(sum(diffs[:k-n]))

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

X.sort()

groups = [X]
def get_max_delta():
    for group in groups:
        max_delta = 0
        for i in range(len(group) - 1):
            d = group[i + 1] - group[i]
            if d > max_delta:
                max_delta = d
                val1 = group[i]
                val2 = group[i + 1]
    return (val1, val2)

def split(vals):
    for i in range(len(groups)):
        group = groups[i]
        for j in range(len(group) - 1):
            if group[j:j+2] == list(vals):
                groups[i] = group[:j+1]
                groups.append(group[j+1:])

if (N >= M):
    print (0)
else:
    while len(groups) < N:
        split(get_max_delta())
    count = 0
    for group in groups:
        if len(group) > 1:
            count += max(group) - min(group)
    print (count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
l = sorted(list(map(int, input().split(' '))))
l2 = [(a,a) for a in l]  # [(hi, lo)]
while True:  # 1e5 * 1e5 ...
    if len(l2) <= n:
        break
    width = 2e5 + 1
    index = -1
    for i in range(len(l2)-1):
        pt = l2[i+1][0] - l2[i][1]
        if pt < width:
            width = pt
            index = i
    connected = [(l2[index+1][0], l2[index][1])]
    l2 = l2[:index] + connected + l2[index+2:]
    if index == -1:
        break
#[(1, 1), (3, 3), (5, 5), (9, 9), (19, 19)]
#connected
#[(5, 1), (9, 9), (19, 19)]
print(sum([a[0] - a[1] for a in l2]))  # TLEかも

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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()
ans = sum(sa[:m-n])
print (ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
MOD = 10**9+7

n,m = MAP()
x = MAP()
x.sort()

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

tmp = x[m-1] - x[0]
if n==1:
    print(tmp)
    exit(0)

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

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
MOD = 10**9+7

n,m = MAP()
x = MAP()
x.sort()

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

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

Submission status: Wrong Answer

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

xl = sorted(xl)

aidal = []
before_x = xl[0]
for idx,x in enumerate(xl):
    aida = x - before_x
    aidal.append((aida,idx))
    before_x = x
    
aidal = sorted(aidal,reverse=True)

ans = 0
before_x = xl[0]
new = sorted([i[1] for i in sorted(aidal[:n-1])])
for idx in new:
    ans += xl[idx-1] - before_x
    before_x = xl[idx]
    
ans += xl[-1] - before_x
print(ans)

Submission status: Wrong Answer

Solution code:
# 2019-11-12 13:20:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m, *x = (int(i) for i in sys.stdin.read().split())
    x.sort()
    if n >= m:
        print(0)
        sys.exit()

    ran = x[-1] - x[0] + 1
    quotient, remainder = ran // n, ran % n
    total_count = 0
    for i in range(x[0], x[-1-remainder], quotient):
        print(i)
        current = [j for j in x if i <= j < i + quotient]
        if not current:
            total_count -= 1
            continue
        count = max(current) - min(current)
        total_count += count
    
    total_count += remainder
    print(total_count)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[int(i) for i in input().split()]
if n>=m:
    print(0)
    exit()
l.sort() 
diff=[l[i]-l[i-1] for i in range(1,n)]
diff.sort(reverse=1) 
for i in range(n-1):
    diff.pop()
print(sum(diff))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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()
    
cost = []
for i in range(m-1):
    cost.append(x[i] - x[i+1])

l = sorted(cost)
ind = [0]
for i in range(n-1):
    ind.append(cost.index(l[i]) + 1)
ind.append(m)

X = []
for i in range(n):
    X.append(x[ind[i]:ind[i+1]])

ans = 0
for i in X:
    if i == []:
        continue
    ans += i[-1] - i[0]

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

x.sort()

ans = 0
if x[0] < 0 and x[-1] > 0:
    ans -= 1

def Streamline(a, x, n):
    bar = bisect.bisect_right(a, x)
    f = a[:bar]
    l = a[bar:]
    if f == [] or l == []:
        return
    if f[-1] - f[0] < l[-1] - l[0]:
        a = l
        A.append(f)
    else:
        a = f
        A.append(l)
    
    x= a[0] + ((a[-1] - a[0] + 1) // 2)
    
    n -= 1
    if n == 0:
        A.append(a)
        return
    
    return Streamline(a, x, n)

A = []
a = x
x = a[0] + ((a[-1] - a[0] + 1) // 2)
Streamline(a, x, n-1)

for i in A:
    ans += (i[-1] - i[0])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sub = []
for m in range(M-1):
    sub.append(X[m+1]-X[m])

sub = sorted(sub)
ans = sum(sub[:N+1])
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, M = [int(x) for x in input().split()]
X = sorted([int(x) for x in input().split()])
D = []
for i in range(len(X)-1):
  D.append(abs(X[i+1] - X[i]))
print(sum(sorted(D)[:(-N+1)]))

Submission status: Wrong Answer

Solution code:
N, M = [int(x) for x in input().split()]
X = sorted([int(x) for x in input().split()])
D = []
for i in range(len(X)-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(list(map(int,input().split())))

interval = []



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

interval.sort()

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

Submission status: Wrong Answer

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

interval = []



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

interval.sort()

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if n>=m:
    lists=list(map(int,input().split()))
    
    print(0)
elif n<m:

        lists=list(map(int,input().split()))
        lists=sorted(lists)
        dislist=[0]
        for i in range(m-1):
            dislist.append(lists[i+1]-lists[i])
        dislist=sorted(dislist)
        print(sum(dislist[:-(n-1)]))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums2.sort()
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:len(nums3)-n+1]
print(sum(nums3))

Submission status: Wrong Answer

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums2.sort()
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3 = nums3[:len(nums3)-n+1]
print(sum(nums3))

Submission status: Wrong Answer

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:len(nums3)-n+1]
print(sum(nums3))

Submission status: Wrong Answer

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:n-1]
print(sum(nums3))

Submission status: Wrong Answer

Solution code:
nm = input()
nm = nm.split()
n = int(nm[0])
m = int(nm[1])
nums = input()
nums = nums.split()
nums2 = []
for i in nums:
  nums2.append(int(i))
nums3 = []
i = 1
while i<len(nums2):
  nums3.append(nums2[i]-nums2[i-1])
  i+=1
nums3.sort()
nums3 = nums3[:n-2]
print(sum(nums3))

Submission status: Wrong Answer

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

difLocs = []
last = locs[-1]
for bot,top in zip(locs, locs[1:]):
  if bot == last:
    break
  difLocs.append(top-bot)

difLocs.sort()
newLocs = difLocs[0:m-n]

print(sum(newLocs))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
x = sorted(list(map(int,input().split())))
x_d = sorted([x[i+1]-x[i] for i in range(m-1)])
print(sum(x_d[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()
y = x[1::]
dlt = [i-j for (i,j) in zip(y,x)]
dlt.sort()
print(dlt)
if n==1:
  print(sum(dlt))
else:
  print(sum(dlt[0:-1*n+1]))

Submission status: Wrong Answer

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

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

x.sort()
y = x[1::]
dlt = [i-j for (i,j) in zip(y,x)]
dlt.sort()

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

Submission status: Wrong Answer

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

print(sum(li2[0:m-n]))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readlines()
N, M = map(int, input[0].strip().split(' '))
Xs = list(map(int, input[1].strip().split(' ')))
s = sorted(Xs)
gaps = [s[i+1]-s[i] for i in range(len(s)-1)]
sgaps = sorted(gaps)
if N>M:
    print(0)
else:
    print(sum(sgaps[:-(N-1)]))

Submission status: Wrong Answer

Solution code:
import sys

for line in sys.stdin:
    print(line)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
X = [int(x) for x in stdin.readline().rstrip().split()]
X.sort()
if N >= M:
    print('0')
else:
    r = []
    for i in range(M - 1):
        r.append(X[i + 1]-X[i])
    r.sort()
    print(r)
    if N != 1:
        print(sum(r[:-N+1]))
    else:
        print(sum(r))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
L=[i for i in map(int,input().split())]
D=[]
if N >= M or M==1 :
    print(0)
    exit

L.sort()
for i in range(0,M-1) :
    D.append(abs(L[i]-L[i+1]))
D.sort(reverse=True)
print(sum(D[N-1:]))

Submission status: Wrong Answer

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

dis=[]

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

for i in range(m-1):
    dis.append(x[i+1]-x[i])
# x_i+1 と x_iの距離の最小値を取っていく
# distanceをソートして、小さいものからn+1個得る
dis.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if M<=N:
  print(0)
  exit()

ans = float('inf')
for i in range(N):
  #print(i, M-N+i, X[M-N+i],X[i])
  ans = min(ans, X[M-N+i]-X[i])

for i in range(M-N+1):
  #print(i, i+N-1, X[i]-X[0]+X[-1]-X[i+N-1])
  ans =min(ans, X[i]-X[0]+X[-1]-X[i+N-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if M<=N:
  print(0)
  exit()

ans = float('inf')
for i in range(N):
  #print(i, M-N+i, X[M-1-N+i],X[i])
  ans = min(ans, abs(X[M-N+i]-X[i]))

for i in range(M-N+1):
  #print(i, i+N-1, X[i]-X[0]+X[-1]-X[i+N-1])
  ans =min(ans, X[i]-X[0]+X[-1]-X[i+N-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if M<=N:
  print(0)
  exit()
ans = float('inf')

for i in range(1,N):
  #print(i, M-N+i, X[M-1-N+i],X[i])
  ans = min(ans, abs(X[M-N+i]-X[i]))

for i in range(M-N):
  #print(i, i+N-1, X[i]-X[0]+X[-1]-X[i+N-1])
  ans =min(ans, X[i]-X[0]+X[-1]-X[i+N-1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = sorted(list(map(int,input().split())))
if M<=N:
  print(0)
  exit()
ans = float('inf')

for i in range(M):
  #print(i,M-(N-i),M,X[-(N-i)]-X[i])
  if i >= M-(N-i) or N-i<=0:break
  ans = min(ans, X[-(N-i)]-X[i])

for i in range(M-N):
  #print(i, i+N-1, X[i]-X[0]+X[-1]-X[i+N-1])
  ans =min(ans, X[i]-X[0]+X[-1]-X[i+N-1])

print(ans)