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: Runtime Error

Solution code:
n, m = list(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])

for i in range(n-1):
    y.remove(max(y))

w = sum(y)
print(w)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if M == 1:
	print(0)
else:
	X_list = [int(_) for _ in list(input().split())]

	X_list.sort()
	X_list.reverse()

	X_diff_list = [X_list[i-1]-X_list[i] for i in range(1, M)]
	X_diff_list.sort()
	X_diff_list.reverse()

	answer = sum(X_diff_list)

	if N == 1:
		print(answer)
	else:
		for n in range(N-1):
			answer -= X_diff_list[n]
		print(answer)

Submission status: Runtime Error

Solution code:
a=input()
b=input()
c=a.split()
n=int(b[0])
m=b[1]
d=b.split()
d=[int(i) for i in d]
d.sort(reverse=True)
count=0
c=0
ll=list()
for i in d:
    if count!=0:
        ll.append(c-i)
    c=i
    count+=1
ll.sort()
for i in range(n):
    del ll[-1]
print(sum(ll))

Submission status: Runtime Error

Solution code:
def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X_sort = sorted(X)
    diff = []
    for i in range(M - 1):
        diff.append(X_sort[i + 1] - X_sort[i])
    diff_sort = sorted(diff)
    if M == 1:
        print("0")
        return
    for _ in range(N - 1):
        diff_sort.pop()
    ans = 0
    for d in diff_sort:
        ans += d
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

tmp = [abs(X[i+1] - X[i]) for i in range(M-1)]
if(tmp != []):
  for _ in range(N-1):
    tmp.remove(max(tmp))
  print(sum(tmp))
else:
  print(0)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=[]
if m==1:
    print(0)
    exit()
if n==1:
    print(max(a)-min(a))
    exit()
for i in range(m-1):
    ans.append(abs(a[i+1]-a[i]))
total=0
ans.sort(reverse=True)
for i in range(n-1):
    total+=ans[i]
print(sum(ans)-total)

Submission status: Runtime Error

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

X = map(int,raw_input().split())

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

gap.sort()
gap.reverse()

gapsum = 0
counter = 0
if len(gap) > 0:
    while N-counter-1 > 0:
        gapsum += gap[counter]
        counter += 1


print abs(X[0]-X[-1])-gapsum

Submission status: Runtime Error

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

tmp = [abs(X[i+1] - X[i]) for i in range(M-1)]
if(tmp != []):
  tmp.remove([max(tmp) for _ in range(N-1)]
  print(sum(tmp))
else:
  print(0)

Submission status: Runtime Error

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

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

X = sorted(X)
s = []
for i in range(len(X) - 1):
    s.append(abs(X[i+1]- X[i]))
ans = sum(s)
s = sorted(s)
for i in range(1,N):
    ans -= s[-i]
print(ans)

Submission status: Runtime Error

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

if a[1] = 1:
  print("0")
  
if a[1] > 1:
  for i in range(0, a[1] - 1):
    d = b[i + 1] - b[i]
    if d < 0:
      d = d * -1
    c.append(d)
    
if a[0] >= 2:
  for i in range(0, a[0] - 1):
    f = max(c)
    c.remove(f)    
  
e = 0
for i in c:
  e += c

print(e)

Submission status: Runtime Error

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

Xs=[int(Xs[i]) for i in range(M)]

Xs.sort()
costs=[]

for i in range(M-1):
    costs.append(abs(Xs[i]-Xs[i+1]))





for i in range(N-1):
    costs.remove(max(costs))

if not costs:
    print(0)
    exit(0)

print(sum(costs))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
x = sorted(X)
B = []
for i in range(0, M-1):
	B.append(x[i+1]-x[i])
B.sort(reverse=True)
for l in range(N-1):
	B.pop(0)
print(sum(B))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

X.sort()
X.reverse()

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

Submission status: Runtime Error

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

if N == 1:
    ans = max(X) - min(X)
    print(ans)
    exit()

if M == 1:
    ans = 0
    print(ans)
    exit()

Xs = sorted(X)

diff = []
for i in range(M - 1):
    tmp = Xs[i + 1] - Xs[i]
    diff.append(tmp)

rest = N - 2
for _ in range(rest):
    if diff[0] > diff[len(diff) - 1]:
        diff.pop(0)
    else:
        diff.pop()

ans = 0

for _ in range(len(diff) - 1):
    if diff[0] > diff[len(diff) - 1]:
        ans += diff[len(diff) - 1]
        diff.pop()
    else:
        ans += diff[0]
        diff.pop(0)

print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n, m = ip.IL()
    x = ip.IL()

    id.S(x)
    s = []
    for i in r(0, m-1):
        s.append(x[i+1] - x[i])

    id.S(s, 1)
    if m == 1:
        print(0)

    elif len(s) >= m:
        print(0)

    else:
        for _ in r(n-1):
            s.pop(0)
        print(sum(s))


main()

Submission status: Runtime Error

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

if n>=m:
    print(0)
    import sys
    sys.quit()
l=[]
ans=x[-1]-x[0]
for i in range(m-1):
    l.append(abs(x[i]-x[i+1]))
l.sort()
l.reverse()
for i in range(n-1):
    ans-=l[i]
if ans>=0:
    print(ans)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
poti=list(input().split())
poti= [ int(s) for s in poti ]
poti=sorted(poti)
sa=[]
for i in range(0,M-1):
    a=poti[i+1]-poti[i]
    sa.append(a)
#print(sa)
if len(sa)!=0:
    while len(sa)!=M-N:
        maxi=max(sa)
        sa.remove(maxi)

    print(sum(sa))

else:
    print(0)
#print(poti)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [long(input_raw[0]), long(input_raw[1])]
    input_raw = read_func().strip().split()
    X = []
    for i in range(M):
        X.append(int(input_raw[i]))

    if len(X) == 1:
        print 0
        return
    if N == 1:
        print max(X) - min(X)
        return

    X.sort()

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

    subX.sort(reverse = True)

    count = 0
    for i in range(N):
        if i == N - 1:
            count += X[M - 1] - X[subX[i - 1][1]]
        elif i != 0:
            count += X[subX[i][1] - 1] - X[subX[i - 1][1]]
        else:
            count += X[subX[i][1] - 1] - X[0]
    print count


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
x.sort()
a=[]
if m==1:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    result=0
    for i in range(n-1):
        result+=a[i]
    print(max(x)-min(x)-result)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

b = []
for i in range(m-1):
    b.append(abs(a[i+1]-a[i]))
b.sort(reverse=True)

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

print(sum(b))

Submission status: Runtime Error

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

if M <= N:
    print(0)
else:
    X.sort()
    # print("X =", X)
    dX = np.diff(X)
    # print("dX =", dX)
    dX.sort()
    print(sum(dX[:M-N]))

f.close()

Submission status: Runtime Error

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

if M == 1:
    ans = max(X) - min(X)
    print(ans)
    exit()

Xs = sorted(X)

diff = []
for i in range(M - 1):
    tmp = Xs[i + 1] - Xs[i]
    diff.append(tmp)

rest = N - 2
for _ in range(rest):
    if diff[0] > diff[len(diff) - 1]:
        diff.pop(0)
    else:
        diff.pop()

ans = 0

for _ in range(len(diff) - 1):
    if diff[0] > diff[len(diff) - 1]:
        ans += diff[len(diff) - 1]
        diff.pop()
    else:
        ans += diff[0]
        diff.pop(0)

print(ans)

Submission status: Runtime Error

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

X = map(int,raw_input().split())

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

gap.sort()
gap.reverse()

gapsum = 0
counter = 0
if len(gap) > 0:
    while N-counter-1 > 0:
        gapsum += gap[counter]
        counter += 1


print abs(X[0]-X[-1])-gapsum

Submission status: Runtime Error

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

Xi.sort()

Li = [Xi[i] - Xi[i-1] for i in range(1,len(Xi))]
Li.sort()

if N == M:
    print(0)
else:
    for _ in range(N-1):
        Li.pop(-1)
print(sum(Li))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  3 21:07:35 2019

@author: Yamazaki Kenichi
"""

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

X.sort()
A = []

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

ans = X[M-1]-X[0]
if M !=1:
    for i in range(M-2,M-2-(N-1),-1):
        ans = ans - A[i] 
print(ans)

Submission status: Runtime Error

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

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

     diff = list(reversed(sorted(diff)))
     s = 0

     for i in range(N - 1):
         s += diff[i]
     answer = X[-1] - X[0] - s
     print(answer)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n, m = ip.IL()
    x = ip.IL()

    id.S(x)
    s = []
    for i in r(0, m-1):
        s.append(x[i+1] - x[i])

    id.S(s, 1)
    if m == 1:
        print(0)
    else:
        for _ in r(n-1):
            s.pop(0)
        print(sum(s))


main()

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
x.sort()
a=[]
if m==1:
    print("0")
else:
    for i in range(m-1):
        a.append(x[i+1]-x[i])
    a.sort(reverse=True)
    result=0
    print("0")
    for i in range(n-1):
        result+=a[i]
    print(max(x)-min(x)-result)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split(" "))
#Nはポーンの数 Mは入力の数
A=list(map(int,input().split(" ")).sort())
print(A)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n, m = ip.IL()
    x = ip.IL()

    id.S(x)
    s = []
    for i in r(0, m-1):
        s.append(x[i+1] - x[i])

    id.S(s, 1)
    if m == 1:
        print(0)
    else:
        for ＿ in r(n-1):
            s.pop(0)
        print(sum(s))


main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

n, m = map(int, input().split())
x = list(map(int, input().split()))
if m == 1:
    print(0)
    sys.exit()
if n == 1:
    print(max(x) - min(x))
    sys.exit()
x.sort()
a = []
for i in range(m - 1):
    a.append(abs(x[i] - x[i + 1]))
ans = sum(a)
a.sort(reverse=True)
for i in range(n - 1):
    ans -= a[i]
print(max(0, ans))

Submission status: Runtime Error

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

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

X.sort()

C=[]
for i in range(m-1):
    c=abs(X[i+1]-X[i])
    C.append(c)

C.sort()

sum_c=0
for  j in range(n+1):
    sum_c+=C[j]
    
print(sum_c)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n, m = ip.IL()
    x = ip.IL()

    x.sort()
    s = [0] * (m-1)
    for i in range(0, m-1):
        s[i] = x[i+1] - x[i]

    id.S(s, 1)
    if m == 1:
        print(0)
    else:
        for i in range(n-1):
            s.pop(0)
        print(sum(s))


main()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
if m==1:
  print(0)
  exit()
l.sort()
l2=[]
for i in range(len(l)-1):
  l2.append(l[i+1]-l[i])
for i in range(n-1):
  l2.remove(max(l2))
print(sum(l2))

Submission status: Runtime Error

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

if M <= N:
    print(0)
else:
    X.sort()
    dX = np.diff(X)
    dX.sort()
    print(sum(dX[:N+1]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = []
for i in range(M-1):
  b.append(a[i+1]-a[i])
b.sort()
if M == 1:
  print(0)
else:
  x = a[M-1]-a[0]
  y = M-2
  for i in range(N-1):
    x -= b[y]
    y -= 1
  print(x)