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:
# M: the number of inputs
# N: koma
M, N = map(int, input().split())
# each length of edges
Xs= list(map(int, input().split()))

# searching largest one
largestX = -10**5

# searching smallest one
smallestX = 10**5

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import itertools

n, m = (int(x) for x in input().split())
x = sorted(int(x) for x in input().split())

ans = max(x) - min(x)

for sep in itertools.combinations(range(m-1), n-1):
  count = 0
  count += x[sep[0]] - x[0]
  for i in range(n-2):
    count += x[sep[i+1]] - x[sep[i]+1]
  count += x[-1] - x[sep[-1]+1]
  if count < ans:
    ans = count

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import sys

n, m = (int(x) for x in input().split())
x = sorted(int(x) for x in input().split())

ans = max(x) - min(x)

print(x, file=sys.stderr)
for sep in itertools.combinations(range(m-1), n-1):
  print(sep, end=" ", file=sys.stderr)
  count = 0
  count += x[sep[0]] - x[0]
  print(x[sep[0]] - x[0], end="+", file=sys.stderr)
  for i in range(n-2):
    count += x[sep[i+1]] - x[sep[i]+1]
    print(x[sep[i+1]] - x[sep[i]], end="+", file=sys.stderr)
  count += x[-1] - x[sep[-1]+1]
  print(x[-1] - x[sep[-1]+1], end="=", file=sys.stderr)
  print(count, file=sys.stderr)
  if count < ans:
    ans = count

print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
y =[x[i+1] - x[i] for i in range(m-1)]
a=[0]
for i in range(n-1):
    a.append(y.index(max(y)) + 1)
    y[y.index(max(y))] = 0
a.append(m)
s = 0
for i in range(n):
    s += x[a[i+1]-1]-x[a[i]]
print(s)

Submission status: Runtime Error

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

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

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

print(game(n, m, x))

Submission status: Runtime Error

Solution code:
N ,M = input().split()
X = list(map(int, input().split()))
X.sort()
#print(X)
ans = 0
div = []
sliceNo = []
for i in range(int(M)-1):
    #print(i)
    div.append(abs(X[i + 1] - X[i]))
div.sort()
#print(div)
for i in range(int(N)-1):
    div.pop()
#print(div)
for i in div:
    ans += i
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
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])
if n >= m:
  print(0)
  else:
    for i in range(n-1):
      a.remove(max(a))
    print(sum(a))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if m == 1:
    print(0)
else:
    for i in range(len(locations) - 1):
        distance.append(locations[i+1] - locations[i])
    
    distance = sorted(distance)

    if n != 1:
        for i in range(n-1):
            distance.pop()
    
    print(sum(distance))

Submission status: Runtime Error

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

if m == 1:
    print(0)
else:
    for i in range(len(locations) - 1):
        distance.append(locations[i+1] - locations[i])
    
    distance = sorted(distance)

    for i in range(n-1):
        distance.pop()
    
    print(sum(distance))

Submission status: Runtime Error

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

for i in range(len(locations) - 1):
    distance.append(locations[i+1] - locations[i])
    
distance = sorted(distance)

for i in range(n-1):
    distance.pop()
    
print(sum(distance))

Submission status: Runtime Error

Solution code:
N, M= map(int, input().split())
zahyo = []
for i in map(int,input().split()):
    zahyo += [i]
sum=0
#マージソート用関数
# merge(p, q): あらかじめソートされた配列 p と q をまとめる。
def merge(p,q):
    result=[]
    i,j=0,0
    #p[i]とq[j]を比べて小さい方をresultに加える。
    while i<len(p) and j<len(q):
        #.appendで加える。小さかった方の数字を一つ進める。
        #これで小さいものから順にresultに加えることができる。
        if p[i] <= q[j]:
            result.append(p[i])
            i+=1
        else:
            result.append(q[j])
            j+=1

    #片方の配列が全て比較済みとなると、そこで上のwhile分は終了してしまう
    #なのでまだ比較していない要素が残っている配列をそのままresultに連結する
    #これは.extendを使う。まだ比較していない要素はスライスで簡単に分けることができる。
    if i<len(p):
        result.extend(p[i:])
    if j<len(q):
        result.extend(q[j:])
    #resultを返して終了
    return result

# mergesort(a): 配列 a をマージソートしたものを返す。
def mergesort(a):
    #要素が一つになるまで分割したらそれ自体を返す。
    if len(a)==1:
        return a
    #最初の半分を切り取ってソート
    p=mergesort(a[:len(a)//2])
    #残りの半分を切り取ってソート
    q=mergesort(a[len(a)//2:])
    #merge関数で連結させる
    return merge(p,q)

zahyo = mergesort(zahyo)
gap = []
for i in range(M-1):
    gap += [zahyo[i+1]-zahyo[i]] 
gap = mergesort(gap)
if N >= M:
    print(0)
else:
    for i in gap[:M-N]:
        sum += i
    print(sum)

Submission status: Runtime Error

Solution code:
N, M= map(int, input().split())
zahyo = []
for i in map(int,input().split()):
    zahyo += [i]
sum=0
#マージソート用関数
# merge(p, q): あらかじめソートされた配列 p と q をまとめる。
def merge(p,q):
    result=[]
    i,j=0,0
    #p[i]とq[j]を比べて小さい方をresultに加える。
    while i<len(p) and j<len(q):
        #.appendで加える。小さかった方の数字を一つ進める。
        #これで小さいものから順にresultに加えることができる。
        if p[i] <= q[j]:
            result.append(p[i])
            i+=1
        else:
            result.append(q[j])
            j+=1

    #片方の配列が全て比較済みとなると、そこで上のwhile分は終了してしまう
    #なのでまだ比較していない要素が残っている配列をそのままresultに連結する
    #これは.extendを使う。まだ比較していない要素はスライスで簡単に分けることができる。
    if i<len(p):
        result.extend(p[i:])
    if j<len(q):
        result.extend(q[j:])
    #resultを返して終了
    return result

# mergesort(a): 配列 a をマージソートしたものを返す。
def mergesort(a):
    #要素が一つになるまで分割したらそれ自体を返す。
    if len(a)==1:
        return a
    #最初の半分を切り取ってソート
    p=mergesort(a[:len(a)//2])
    #残りの半分を切り取ってソート
    q=mergesort(a[len(a)//2:])
    #merge関数で連結させる
    return merge(p,q)

mergesort(zahyo)
gap = []
for i in range(M-1):
    gap += [zahyo[i+1]-zahyo[i]] 
mergesort(gap)
if N >= M:
    print(0)
elif N == 1:
    print(zahyo[M-1] - zahyo[0])
else:
    for i in gap[N-1:]:
        sum += i
    print(sum)

Submission status: Runtime Error

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

x = sorted(x)
diff = np.asarray([x[i + 1] - x[i] for i in range(len(x) - 1)])
argsort = diff.argsort()[::-1][:n - 1]

x_list = []
start = 0
for split_idx in argsort:
    x_list.append(x[start:split_idx+1])
    start = split_idx+1
x_list.append(x[start:])

sum_length = 0
for cur_x in x_list:
    sum_length += cur_x[-1] - cur_x[0]

print(sum_length)

Submission status: Runtime Error

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

if N >= M:
  print(0)
else:
  X.sort()
  L = []
  for i in range(M-1):
    L.append(X[i+1]-X[i])
  L.sort(reverse=True)
  print(X[M-1]-X[0]-sum(L[:N-1]))

Submission status: Runtime Error

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

if N >= M:
    print(0)
else:
    X.sort()
    L = []
    for i in range(M-1):
        L.append(X[i+1]-X[i])
    L.sort(reverse=True)
    print(X[M-1]-X[0]-sum(L[:N-1]))

Submission status: Runtime Error

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

if N >= M:
    print(0)
else:
    X.sort()
    L = []
    for i in range(M-1):
        L.append(X[i+1]-X[i])
    L.sort(reverse=True)
    print(X[M]-X[0]-sum(L[:N-1]))

Submission status: Runtime Error

Solution code:
N,M = map(int(input())) 
X = [int(input()) for i in range(N)] 
  
if N-M => 0:
  print(0)
else:
	count = 0
	Y=[]
	while count<N:
  		Y.append(X[count]-X[count+1])
  		count+=1
    i=0
    sum = 0
	while i<M-N:
      sum = sum + min(Y)
      Y.remove(min(Y))
      i+=1
	print(sum)

Submission status: Runtime Error

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

if M != 1:
    for i in range(0,M-1):
        tousa.append(abs(Xls[i+1] - Xls[i]))




tousa.sort()

if N>M:
    w = M-1
else:
    w = N
for i in range(0,w):
    tousa.pop()

ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Runtime Error

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

if M != 1:
    for i in range(0,M-1):
        tousa.append(abs(Xls[i+1] - Xls[i]))

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

tousa.sort()
if N != 1:
    for i in range(0,N-1):
        tousa.pop()

ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Runtime Error

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

if M != 1:
    for i in range(0,M-1):
        tousa.append(abs(Xls[i+1] - Xls[i]))

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

tousa.sort()
for i in range(0,N-1):
    tousa.pop()

ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Runtime Error

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

for i in range(0,M-1):
    tousa.append(abs(Xls[i+1] - Xls[i]))

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

tousa.sort()
for i in range(0,N-1):
    tousa.pop()

ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Runtime Error

Solution code:
NM = input().split(" ")
N = int(NM[0])
M = int(NM[1])

Xin= input().split(" ")


Xls = []
tousa = []
for i in range(0,M):
    Xls.append(int(Xin[i]))

Xls.sort()

for i in range(0,M-1):
    tousa.append(abs(Xls[i+1] - Xls[i]))

if M == 1:
    print(0)

tousa.sort()
for i in range(0,N-1):
    tousa.pop()

ret = 0

for i in tousa:
    ret = i + ret

print(ret)

Submission status: Runtime Error

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

l.sort()
distance = []
sum = 0

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

if m == 2:
    print(l[len(l)-1] - l[0])
    exit()

for i in range(len(l) - 1):
    distance.append(l[i+1] - l[i])

distance.sort(reverse=True)

for i in range(n-1):
    sum += distance[i]

print(l[len(l)-1] - l[0] - sum)

Submission status: Runtime Error

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

l.sort()
distance = []
sum = 0

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

for i in range(len(l) - 1):
    distance.append(l[i+1] - l[i])

distance.sort(reverse=True)

for i in range(n-1):
    sum += distance[i]

print(l[len(l)-1] - l[0] - sum)

Submission status: Runtime Error

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

import copy

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

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

Submission status: Runtime Error

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

import copy

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

ary.sort()

ary2 = copy.copy(ary)
ary2.pop(0)
ary2.append(0)

sa = []

for i in range(0, m - 1):
    sa.append(ary2[i] - ary[i])

#print(sa)

for i in range(0, n -1):
    sa.remove(max(sa))

#print(sa)

print(sum(sa))

Submission status: Runtime Error

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

x.sort()

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

sub_array.sort()

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

print (sum(sub_array))

Submission status: Runtime Error

Solution code:

import sys

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

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

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

mem = {}
nums.sort()

def find_max(pivot, places):

    if (pivot, len(places)) in mem:
        return mem[(pivot, len(places))]

    if len(places) == N:
        mem[(pivot, len(places))] = abs(nums[pivot] - nums[-1])
        return mem[(pivot, len(places))]

    local_min = float('inf')
    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = abs(nums[pivot] - nums[i-1]) + find_max(i, places + [i])
        if tmp < local_min:
            local_min = tmp
    mem[(pivot, len(places))] = local_min
    return mem[(pivot, len(places))]

ans = find_max(0, [0])

print(ans)

Submission status: Runtime Error

Solution code:

import sys

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

ans = [float('inf')]

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

mem = {}

def find_max(pivot, places):

    if (pivot, len(places)) in mem: return mem[(pivot, len(places))]

    if len(places) == N:
        mem[(pivot, len(places))] = abs(nums[pivot] - nums[-1])
        return mem[(pivot, len(places))]

    local_min = float('inf')
    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = abs(nums[pivot] - nums[i-1]) + find_max(i, places + [i])
        if tmp < local_min:
            local_min = tmp
    mem[(pivot, len(places))] = local_min
    return mem[(pivot, len(places))]

ans = find_max(0, [0])

print(ans)

Submission status: Runtime Error

Solution code:

import sys

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

ans = [float('inf')]

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


def find_max(pivot, places, s):
    if len(places) == N:
        s += abs(nums[places[-1]] - nums[-1])
        if s < ans[0]:
            ans[0] = s
        return

    for i in range(pivot+1, len(nums) - (N - len(places) - 1)):
        tmp = s + abs(nums[pivot] - nums[i-1])
        if tmp > ans[0]: continue
        find_max(i, places + [i], tmp)

find_max(0, [0], 0)

print(ans[0])

Submission status: Runtime Error

Solution code:
list1 = list(map(int, input().split()))
N = list1[0]
M = list1[1]
list2 = list(map(int, input().split()))
list2 = sorted(list2, reverse=True)
# print(list2)
list3 = []
for i in range(len(list2) - 1):
    list3.append(list2[i] - list2[i + 1])
list3 = sorted(list3, reverse=True)
print(list3)
sum = 0
for i in range(N - 1):
    sum = sum + list3.pop(0)
ans = list2[0] - list2[-1] - sum
print(ans)

Submission status: Runtime Error

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

l.sort()

gap = [l[i]-l[i-1] for i in range(1,len(l))]

gap = gap.sort()

print(sum(gap[:-(m-1)]))

Submission status: Runtime Error

Solution code:
#define _USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <ctype.h>
#include <map>
#include <vector>
#include <stdlib.h>
#define REP(i,n) for(int i=0;i<(int)n;i++)
#define REP1(j,n) for(int j=0;j<(int)n;j++)
#define REPD(i,n) for(int i=n;i>=0;i--)
#define REPD1(j,n) for(int j=n;j=>0;j--)
#define REPS(i,n) for(int i=1;i<=(int)n;i++)
#define REPS1(j,n) for(int j=1;j<=(int)n;j++)
#define rep(i,a,n) for(int i=a;i<=(int)n;i++)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define DescSort(a) sort(a.begin(),a.end(),std::greater<int>())

using namespace std;
typedef long long ll;

int main(void) {
	int n, m,x[10005];
	int max_sum=0;
	cin >> n >> m;
	REP(i, m)cin >> x[i];
	if (n >= m) cout << "0" << endl;
	else {
		sort(x, x + m);
		int diff[10005];
		REP(i, m - 1) { diff[i] = x[i + 1] - x[i]; }
		sort(diff, diff + (m - 1), std::greater<int>());
		REP(i, n - 1) { max_sum += diff[i]; }
		cout << (x[m - 1] - x[0]) - max_sum << endl;
	}
	
	return 0;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = itertools.combinations(seq,n-1)
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += g[-1] - g[0]
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)

Submission status: Runtime Error

Solution code:
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = list(itertools.combinations(seq,n-1))
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += g[-1] - g[0]
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)

Submission status: Runtime Error

Solution code:
import itertools

n, m = map(int, input().split(' '))
x = map(int, input().split(' '))
if n >= m:
    print(0)
else:
    x = sorted(x)
    seq = [i for i in range(1,m)]
    locations = list(itertools.combinations(seq,n-1))
    best_sc = None
    for location in locations:
        sc = 0
        for i in range(0,n):
            g = None
            if i == 0:
                g = x[:location[i]]
            elif i == n-1:
                g = x[location[i-1]:]
            else:
                g = x[location[i-1]:location[i]]
            sc += max(g) - min(g)
        if (best_sc is None) or (best_sc > sc):
            best_sc = sc
    print(best_sc)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#入力
N,M = map(int,input().split())
X = [int(i) for i in input().split()]
#ソートする
X.sort()

L = list()
for i in range(M-1):
    L.append(X[i+1]-X[i])
a=0
L.sort()
for i in range(N-1):
    L.pop(-1)
for i in L:
    a+=i
print(a)

Submission status: Runtime Error

Solution code:
##N Mの入力                                                                                                                                                                                                 
N,M = [int(x) for x in input().split()]
##地点の保存                                                                                                                                                                                                
x = [int(num) for num in input().split()]
##地点のソート                                                                                                                                                                                              
x = sorted(x)
##それぞれの幅の決定                                                                                                                                                                                        
dis = [x[i+1]-x[i] for i in range(M-1)]
##幅の長さのソート                                                                                                                                                                                          
dis = sorted(dis)
##答えの算出                                                                                                                                                                                                
dis_top = 0
for i in range(1,N):
    dis_top = dis_top + dis[(M-1)-i]
##答えの出力                                                                                                                                                                                                
ans = sum(dis) - dis_top
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N, M; cin >> N >> M;
    vector<int> X(M);
    for (auto& xi : X) cin >> xi;
    sort(X.begin(), X.end());
    vector<int> diff(M - 1);
    for (int i = 0; i < M - 1; i++) {
        diff[i] = abs(X[i] - X[i + 1]);
    }
    sort(diff.begin(), diff.end());
    int res = 0;
    for (int i = 0; i < (M - 1) - (N - 1); i++) {
        res += diff[i];
    }
    cout << res << endl;
    return 0;
}

Submission status: Runtime Error

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

    if N < 1 or N > 10**5:
        exit()
    if M < 1 or M > 10**5:
        exit()
    if M != len(Xi):
        exit()
    for i in Xi:
        if Xi[i] < -10**5 or Xi[i] > 10**5:
            exit()
    
except ValueError:
    exit()

Submission status: Runtime Error

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

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

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

  print(sum(c))

Submission status: Runtime Error

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

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

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

  print(sum(c))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted(map(int, input().split())) + [10**6]
y = sorted(((i,j) for i in range(m) for j in (i-1,i+1)),
        key=lambda t: abs(x[t[0]]-x[t[1]]))[::-1]
r = 0
v = set()
for _ in range(m-n):
    while True:
        i, j = y.pop()
        if j not in v:
            break
    v |= {i,j}
    r += abs(x[i]-x[j])
print(r)

Submission status: Runtime Error

Solution code:
nandm = input().split()
numlist = input().split()
N = int(nandm[0])
M = int(nandm[1])
if N >= M:
    print(0)
else:
    intlist = [int(num) for num in numlist]
    intlist.sort()
    subtract = [intlist[i + 1] - intlist[i] for i in range(len(intlist) - 1)]
    subtract.sort()
    print(sum(subtract[M - N]))

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools
import copy

class state:
  def __init__(self, pieces, goals):
    self.n_pieces = len(pieces)
    self.n_goals = len(goals)
    self.pieces = pieces
    self.goals = goals
    self.hands = 0
    self.arrived = [False for i in range(n_goals)]
    self.update_arrived_list()
  
  def update_arrived_list(self):
    for piece in self.pieces:
      if piece in goals:
        self.arrived[self.goals.index(piece)] = True
  
  def isFinished(self):
    for isArrived in self.arrived:
      if not isArrived:
        return False
    return True
  
  def move(self, i_piece, direction):
    if direction >= 0:
      self.pieces[i_piece] += 1
    else:
      self.pieces[i_piece] -= 1
    self.hands += 1
    self.update_arrived_list()
      
    
n_pieces, n_goals = [int(num) for num in input().split()]
goals = [int(goal) for goal in input().split()]

state_queue = []
init_pieces = list(itertools.combinations(goals,n_pieces))

for pieces in init_pieces:
  pieces = [int(piece) for piece in pieces]
  init = state(pieces, goals)
  state_queue.append(init)

while(True):
  current = state_queue.pop(0)
  if current.isFinished():
    break
  for i in range(current.n_pieces):
    next_p = copy.deepcopy(current)
    next_m = copy.deepcopy(current)
    
    next_p.move(i, 1)
    next_m.move(i, -1)

    state_queue.append(next_p)
    state_queue.append(next_m)

print(current.hands)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
b=[0]*(m-1)
for i in range(0,m-1):
  b[i]=a[i+1]-a[i]
c=sorted(b)
s=a[m-1]-a[0]
for i in range(0,n-1):
  s=s-c[m-i-2]
print(s)

Submission status: Runtime Error

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

Ms = M -1
dest = list()

for i in range(Ms):
  K = X[i+1] - X[i]
  dest = list.append(K)

dest.sort()
L = 0
NM = N - M
for j in range(NM):
  L = L + dest(j)

print(L)

Submission status: Runtime Error

Solution code:
N, M = map(int,input().split())
X = list(map(int,input().split()))
li = []
i = j = ans = 0
X.sort()

for i in range(M-1):
  a = abs(int(X[i+1])-int(X[i]))
  li.append(a)
  i += 1
  
li.sort(reverse=True)
del li[0:N-1]

for j in range(N+1):
  if int(len(li)) == 0:
    break
  ans = ans + int(li[j])
  j += 1

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

    x = sorted(x, reverse=True)
    x = xx[n-1:]
    ans = 0
    for i in x:
        ans+=i
    print(ans)

Submission status: Runtime Error

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

l.sort()
for i in range(m-1):
  sa.append(l[i+1]-l[i])

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

print(sum(sa))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split(' '))
X = list(map(int,input().split(' ')))
X.sort(reverse = True)
dis = []
div = 0
for i in range(m - 1):
    dis.append(X[i + 1] = X[i])
dis.sort(Reverse = True)
for i in range(n - 1):
    div += dis[i]
print(sum(dis) - div)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
a=list(map(int, input().split()))
a.sort(reversed=True)
print(sum(a[m-1:]))

Submission status: Runtime Error

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

# print(n, m, x)

# #%%
# n, m =2, 5
# x = [10, 12, 1, 2, 14]


#%%
x.sort()

#%%




#%%

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


#%%
diff_list.sort()


#%%
if n-1>0:
    print(sum(diff_list[:-(n-1))])
else:
    print(0)    

#%%


#%%

Submission status: Runtime Error

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

X_dif = []

excep = 0

if M == 1:
    excep = 'M1'
    
if not excep == 'M1':
    for i in range(M-1):
        X_dif.append(X[i+1]-X[i])
    
    X_dif.sort()
    X_dif.reverse()

    if len(X_dif) == 1:
        excep = 'X_dif1'
    
    if not excep == 'X_dif1':
        for N in range(N-1):
            X_dif.pop(0)

if excep == 'M1':
    print(1)
elif excep == 'X_dif1':
    print(2)
else:
    print(sum(X_dif))