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:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
import math
import string
import fractions
import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop

# from itertools import accumulate
# from collections import deque
import random




def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n,m = map(int, input().split())
    l = sorted(list(map(int, input().split())))
    
    l_diff = [(l[i+1] - l[i]) for i in range(len(l)-1)]
    # eprint(l)
    # eprint(l_diff)
    l_diff.sort()
    # eprint(l_diff)

    for i in range(n-1):
        l_diff.pop()
    print(sum(l_diff))
    # for i in range(0,n-1):
    #     sum += l[i+1] -l[i]

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

data = [int(s) for s in input().split()]
data.sort()
l = []
for i in range(m-1):
    l.append(data[i+1]-data[i])
l.sort(reverse = True)

x = 0
for i in range(n-1):
    x += l[i]
print(data[m-1] - data[0] - x)

Submission status: Runtime Error

Solution code:
memo = {}

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

sum_num_list = []
sum = 0
prev = 0
for i in range(len(val)):
    if i == 0:
        sum_num_list.append(0)
    else:
        sum += val[i] - prev
        sum_num_list.append(sum)
    prev = val[i]
#print(sum_num_list)

def dp(start, remain_point):
    if remain_point == 0:
        return sum_num_list[-1] - sum_num_list[start]
    if (start, remain_point) in memo:
        return memo[(start, remain_point)]
    cur_min = 10 ** 9
    for i in range(start, len(sum_num_list) - remain_point):
        res = dp(i+1, remain_point - 1)
        cur_min = min( res + sum_num_list[i] - sum_num_list[start], cur_min)
    memo[(start, remain_point)] = cur_min
    return cur_min


if N == 1:
    print(sum_num_list[-1])
elif N >= M:
    print(0)
else:
    print(dp(0, N - 1))

Submission status: Runtime Error

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

sum_num_list = []
sum = 0
prev = 0
for i in range(len(val)):
    if i == 0:
        sum_num_list.append(0)
    else:
        sum += val[i] - prev
        sum_num_list.append(sum)
    prev = val[i]
#print(sum_num_list)

def dp(start, remain_point):
    if remain_point == 0:
        return sum_num_list[-1] - sum_num_list[start]
    cur_min = 10 ** 9
    for i in range(start, len(sum_num_list) - remain_point):
        res = dp(i+1, remain_point - 1)
        cur_min = min( res + sum_num_list[i] - sum_num_list[start], cur_min)
    return cur_min


if N == 1:
    print(sum_num_list[-1])
elif N >= M:
    print(0)
else:
    print(dp(0, N - 1))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append(x[mi+1]-x[mi])
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff.index(max(x_diff))
    split_list.append(split_idx)
    x_diff[split_idx] = 0
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
using namespace std;

int N, M;
vector<int> x;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M;  
  x.resize(M);
  vector<int> diff(M-1);
  for(int i=0; i<M; ++i) cin >> x[i];
  sort(all(x));
  for(int i=1; i<M; ++i)
      diff[i-1] = abs(x[i] - x[i-1]);
  sort(all(diff), greater<int>());
  int res = 0;
  if(N >= M){
    res = 0;
  }
  else{
    int k = 0;
    while(N > 0){
      N -= 2;
      ++k;
    }
    while(k < (int)diff.size()){
       res += diff[k];
       ++k;
    }
  }
  cout << res << "\n";
  return 0;
}

Submission status: Runtime Error

Solution code:
N, M = [int(a) for a in input().split(" ")]
X = sorted([int(a) for a in input().split(" ")])
intervals = [a-b for a, b in zip(X[1:], X[:-1])]
prev, curr = [0]*(M-1), [0]*(M-1)

if M==1:
    print(0)
else:
    maxv = 0
    for i in range(M-1):
        maxv = max(intervals[i], maxv)
        prev[i] = maxv

    for i in range(N-2):
        curr[i+1] = -10**10
        for j in range(i+2, M-1):
            curr[j] = max(prev[j-2]+intervals[j], curr[j-1])
        prev, curr = curr, prev
    print(sum(intervals)-prev[-1])

Submission status: Runtime Error

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

if m == 1:
    S =0
else:
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)

    if n > 1:
        for i in range(n-1):
            S -= d[i]
    else:
        S = l[0]

print(S)

Submission status: Runtime Error

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

if m == 1 and n == 1:
    S =0
else:
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)

    for i in range(n-1):
        S -= d[i]

print(S)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
if m == 1 and n == 1:
    S =0
else:
    l = sorted(list(map(int, input().split())), reverse=True)
    d = sorted([l[i] - l[i+1]  for i in range(m-1)], reverse=True)
    S = max(l) - min(l)

    for i in range(n-1):
        S -= d[i]

print(S)

Submission status: Runtime Error

Solution code:
import sys
#入力
N, M = map(int, input().split())

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

lst.sort()

sa = []

for i in range(M-1):
    sa += [abs(lst[i] - lst[i+1])]

sa.sort(reverse = True)

ans = -(lst[0] - lst[M-1])
#print (ans)
if not sa ==[]:
    for j in range(N-1):
        ans -= sa[j]
print (ans)

Submission status: Runtime Error

Solution code:
import sys
#入力
N, M = map(int, input().split())

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

lst.sort()

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

sa = []

for i in range(M-1):
    sa += [abs(lst[i] - lst[i+1])]

sa.sort(reverse = True)

ans = -(lst[0] - lst[M-1])
#print (ans)
for j in range(N-1):
    ans -= sa[j]
print (ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
d = [0]*m
for i in range(1,m):
    d[i] = x[i]-x[i-1]
d.sort()
ans = 0
if n < m:
    for i in range(0,n+2):
        ans += d[i]
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)
    exit()

x.sort()
kyori = [0]*(M-1)
for i in range(M-1):
    kyori[i] = (x[i+1] - x[i], i)
kyori.sort(key=lambda k: k[0], reverse=True)
kyori = kyori[:N-1]
kyori.sort(key=lambda k: k[1])
cost = 0
now = 0
for i in range(len(kyori)):
    t = kyori[i][1]
    cost += x[t]-x[now]
    now += t+1
if now < M:
    cost += x[-1] - x[now]
print(cost)

Submission status: Runtime Error

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

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

if N >= M:
    print(0)
    exit()
diffs = [X[i+1] - X[i] for i in range(M-1)]
diffs.sort()
print(sum[diffs[:N-M]])

Submission status: Runtime Error

Solution code:
import pandas as pd

n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()



if m == 1:
    print(0)
else:
    x_df = pd.Series(x)
    x_d_df = x_df.diff()
    x_d = x_d_df.values.tolist()
    x_d.pop(0)

    for i in range(n-1):
        x_d.remove(max(x_d))
#         print(x_d)
    
    sumd = sum(x_d)  
    print(sumd)

Submission status: Runtime Error

Solution code:
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

if m == 1:
    print(0)
else:
    for i in range(m-1):
        x_d.append(x[i+1] - x[i])   
    
    for i in range(n-1):
        x_d.remove(max(x_d))
#         print(x_d)
    
    sumd = sum(x_d)  
    print(sumd)

Submission status: Runtime Error

Solution code:
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

if m ==1:
    print(0)
else:
    for i in range(m-1):
        x_d.append(x[i+1] - x[i])

    print(x,x_d)

    for i in range(n-1):
        x_d.remove(max(x_d))
    sumd = sum(x_d)  
    print(sumd)

Submission status: Runtime Error

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

if M == 1:
    print(0)
else:
    X.sort()
    X_difs = np.array(X)
    X_difs = np.diff(X_difs)
    X_difs[::-1].sort()
    for _ in range(N-1):
        X_difs = np.delete(X_difs, 0)
        
    print(sum(X_difs))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

x = sorted(x)

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

y = sorted(y, reverse = True)

no = 0
for j in range(n-1):
    no += y[j]

print(x[m-1] - x[0] - no)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
X.sort()
if N >= M: #設置できるコマが座標よりも大きければ終了
    print(0)
    exit()
diff = []
for i in range(1,len(X)):           #座標の差の最大可
    diff.append()(X[i]-X[i-1])
diff.sort(reverse=True)             
total = sum(diff)
for i in range(N-1):                #移動範囲が広い座標間をショートカット
    total -= diff[i]
print(total)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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()
rui=[]
for i in range(len(X)-1):
  rui.append(X[i+1]-X[i])
rui.sort()
print(sum(rui[0:-(N-1)]))N,M=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
X.sort()
rui=[]
for i in range(len(X)-1):
  rui.append(X[i+1]-X[i])
rui.sort()
print(sum(rui[0:-(N-1)]))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
if 2 * l[-1] < sum(l):
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

print(sum(dif))

Submission status: Runtime Error

Solution code:
import itertools 

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

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

if N == 1:
    print(X[M-1]-X[0])
    exit()


X.sort()
index = range(1,M)

anslist = []
for c in itertools.combinations(index, N-1):
    ans = X[c[0]-1] - X[0]
    #print(ans)
    for n in range(N-1):
        if n == N-2:
            ans += X[M-1] - X[c[n]]
        else:
            ans += X[c[n+1]-1] - X[c[n]]
    anslist.append(ans)

print(min(anslist))

Submission status: Runtime Error

Solution code:
_,n=input()
print(n)

Submission status: Runtime Error

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)])
if M < L:print(0)
else:print(sum(L[:(M-N)]))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])
X_distance.sort(reverse=True)

if N == M:
    print(0)
elif M == 1:
    print(0)
else :
    for i in range(N-1):
        X_distance[i] = 0
    print(sum(X_distance))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])

if N == M:
    print(0)
else :
    for i in range(N-1):
        max_index = X_distance.index(max(X_distance))
        X_distance[max_index] = 0
    print(sum(X_distance))

Submission status: Runtime Error

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

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

if X_subtraction != []:
    for _ in range(N-1):
        X_subtraction.remove(max(X_subtraction))

print(sum(X_subtraction))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a.sort()
start1=a[0]
start2=a[m-1]

a.pop(0)
a.pop(m-2)

centre=(start1+start2)/2
a.append(centre)
a.sort()
c=a.index(centre)
cent_left=a[c-1]
cent_right=a[c+1]

print(abs(cent_left-start1) + abs(start2-cent_right))

Submission status: Runtime Error

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
li = na()
list.sort(li)
ls = []
for i in range(len(li)-1):
    ls.append(li[i+1]-li[i])
for j in range(n - 1):
    ls.remove(max(ls))
print(sum(ls))

Submission status: Runtime Error

Solution code:
N, M= (int(i) for i in input().split())
if (N<M):
    X = list(int(i) for i in input().split())
    X.sort()
    Y = [X[i] - X[i-1] for i in range(1, len(X))].sort()
    print(sum(Y[0:M-N]))
else:
    print(0)


# -100 | -10 -3 0 2 9 | 17
# 0 | 90 97 100 102 109 | 117
#   90  7  3   2   7    6
# 90 7 7 6 3 2


"""
N個のコマ：N-1個の閉経路が存在する
各座標の距離を求める(M-1個)
M：座標の数　N:駒の数
移動しなくても通っている座標の数: N
M - N
"""

Submission status: Runtime Error

Solution code:
N, M= (int(i) for i in input().split())
if (N<M)
    X = list(int(i) for i in input().split())
    X.sort()
    Y = [X[i] - X[i-1] for i in range(1, len(X))].sort()
    print(sum(Y[0:M-N]))
else:
    print(0)


# -100 | -10 -3 0 2 9 | 17
# 0 | 90 97 100 102 109 | 117
#   90  7  3   2   7    6
# 90 7 7 6 3 2


"""
N個のコマ：N-1個の閉経路が存在する
各座標の距離を求める(M-1個)
M：座標の数　N:駒の数
移動しなくても通っている座標の数: N
M - N
"""

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())  # N個のコマ, M個の訪れるべき座標

if N >= M:
    print(0)
else:
    A = sorted(list(map(int, input().split())))
    B = sorted(A[i]-A[i-1] for i in range(1, len(A)))
    print(sum[B[:M-N]])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())  # N個のコマ, M個の訪れるべき座標

if N >= M:
    print(0)
else:
    A = sorted(list(map(int, input().split())))
    B = sorted(A[i]-A[i-1] for i in range(1, len(A)))
    print(sum[B[0:M-N]])

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = list(map(int, input().split()))
x_new = sorted(x)
intervals = [0]*(m-1)
for i in range(m-1):
  intervals[i] = abs(x_new[i+1] - x_new[i])
c = sorted(intervals, reverse=True)
s = 0
for j in range(n-1):
  s += c[j]
 
min = sum(intervals) - s
print(min)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = list(map(int, input().split()))
x_new = sorted(x)
intervals = [0]*(m-1)
for i in range(m-1):
  intervals[i] = x[i+1] - x[i]
c = sorted(intervals, reverse=True)
s = 0
for j in range(n-1):
  s += c[j]

min = sum(intervals) - s
print(s)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
l = list(map(int,input().split()))
if M <= N:
    print('0')
else:
    l.sort()
    li = []
    for i in range(M-1):
        difference  = abs(l[i]-l[i+1])
        li.append(difference)
    li.sort().reverse()
    dif = 0
    for i in range(N-1):
        dif += li[i]
    print(l[M-1]-l[0]-dif)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
X=[int(i) for i in input().split()]
X.sort()
count=[]
sum=0
if n<m:
  for i in range(M):
    if i != 0:
      count.append(X[i]-get)
      sum+=count[i-1]
    get=X[i]
  count.sort(reverse=True)
  if count != []:
    for i in range(N-1):
      sum-=count[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()
count=[]
sum=0
if n<=m:
  for i in range(M):
    if i != 0:
      count.append(X[i]-get)
      sum+=count[i-1]
    get=X[i]
  count.sort(reverse=True)
  if count != []:
    for i in range(N-1):
      sum-=count[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()
count=[]
sum=0
for i in range(M):
  if i != 0:
    count.append(X[i]-get)
    sum+=count[i-1]
  get=X[i]
count.sort(reverse=True)
if count != []:
  for i in range(N-1):
    sum-=count[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()
count=[]
sum=0
for i in range(M):
  if i != 0:
    count.append(X[i]-get)
    sum+=count[i-1]
  get=X[i]
count.sort(reverse=True)
for i in range(N-1):
  sum-=count[i]
print(sum)

Submission status: Runtime Error

Solution code:
diN, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    if len(distance) > 0:
        del distance[-1]
    else:
        print(0)
        quit()
print(sum(distance))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
w=[[] for i in range(m-1)]
for i in range(m-1):
    w[i]=[x[i],x[i+1]-x[i]]
w=sorted(w,key=lambda y:y[1])
w.reverse()
skip=[]
if m!=1:
    for i in range(n-1):
        skip.append(w[i])
    skip=sorted(skip,key=lambda y:y[0])
ans=[]
k=x[0]
for i in skip:
    ans.append(i[0]-k)
    k=i[0]+i[1]
ans.append(x[m-1]-k)
print(sum(ans))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
w=[[] for i in range(m-1)]
for i in range(m-1):
    w[i]=[x[i],x[i+1]-x[i]]
w=sorted(w,key=lambda y:y[1])
w.reverse()
skip=[]
for i in range(n-1):
    skip.append(w[i])
skip=sorted(skip,key=lambda y:y[0])
ans=[]
k=x[0]
for i in skip:
    ans.append(i[0]-k)
    k=i[0]+i[1]
ans.append(x[m-1]-k)
print(sum(ans))

Submission status: Runtime Error

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

X_diff = [X[i+1]-X[i] for i in range(m-1)]
X_diff.sort(reverse=True)

if n >= m:
    print(0)
else:
    print(X[-1] - X[0] - sum(X_diff[:N-1]))

Submission status: Runtime Error

Solution code:
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
# print("First Set", first_set)

if len(first_set)==0:
    print(0)
else:

    rem = list(set(x_list) - set(first_set))
#     print("First Remain", rem)
    
    cost = 0
    
    current_point = first_set
    
    while len(rem)>0:
        d_list = []
        best = 10**5
        best_list = []
        
        for fp in current_point:
            for new in rem:
                if fp==new:
                    continue
                    
                d = abs(fp-new)
                if d<best:
                    best = d
                    best_list = [fp, new]
        
#         print("Next", best, best_list)
        cost += best
        
        bef_point = best_list[0]
        new_point = best_list[1]
        rem = set(rem) - set([new_point])
        
        idx = current_point.index(bef_point)
        current_point[idx] = new_point
        
    print(cost)

Submission status: Runtime Error

Solution code:
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print("First Set", first_set)

rem = list(set(x_list) - set(first_set))
print("First Remain", rem)

cost = 0

current_point = first_set

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    for fp in current_point:
        for r in rem:
            if fp==r:
                continue
                
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print("Next", best, best_list)
    cost += best
    
    bef_point = best_list[0]
    new_point = best_list[1]
    rem = set(rem) - set([new_point])
    
    idx = current_point.index(bef_point)
    current_point[idx] = new_point
    
print(cost)

Submission status: Runtime Error

Solution code:
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print(first_set)

rem = list(set(x_list) - set(first_set))
print(rem)

cost = 0

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    if cost==0:
        candidate = first_set
    else:
        candidate = [new_set]
    
    for fp in list(candidate):
        for r in rem:
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print(best, best_list)
    cost += best
    
    new_set = best_list[1]
    rem = set(rem) - set([new_set])
print(cost)

Submission status: Runtime Error

Solution code:
import numpy as np

# N = 3
# M = 7
# x_list = sorted([-10, -3, 0, 9, -100, 2, 17])

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


def rl_dist(x_point):

    p_num = len(x_point)
    d_list = []
    d_dict = {}
    for num, (a, b) in enumerate(zip(x_point, x_point[1:])):
        d = (b-a)
        if x_point[num] not in d_dict.keys():
            d_dict[x_point[num]] = [d]
            if num==0:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
        else:
            d_dict[x_point[num]].append(d)
            
            if len(d_dict[x_point[num]])==2:
                d_dict[x_point[num]] = np.mean(d_dict[x_point[num]])
                
            
        if x_point[num+1] not in d_dict.keys():
            d_dict[x_point[num+1]] = [d]
            
            if num+1==p_num-1:
                d_dict[x_point[num+1]] = np.mean(d_dict[x_point[num+1]])
                
    return d_dict
            
    
rl_dict = rl_dist(x_list)
        
first_set = list(set([p[0] for p in sorted(rl_dict.items(), key=lambda x: x[1])[-1*N:] ]))
print(first_set)

rem = list(set(x_list) - set(first_set))
print(rem)

cost = 0

while len(rem)>0:
    d_list = []
    best = 10**5
    best_list = []
    
    if cost==0:
        candidate = first_set
    else:
        candidate = [new_set]
    
    for fp in list(candidate):
        for r in rem:
            d = abs(fp-r)
            if d<best:
                best = d
                best_list = [fp, r]
    
    print(best, best_list)
    cost += best
    
    new_set = best_list[1]
    rem = set(rem) - set([new_set])
print(cost)

Submission status: Runtime Error

Solution code:
debug = 0
"""
N: state num
M: city num
"""


if debug:
    N = 2
    m = 10
else:
    (N, M) = list(map(int, input().split()))

if debug:
    p_y_list = [
        [1, 30]
        ,[1, 20]
        ,[1, 10]
        ,[1, 3]
        ,[2, 63]
        ,[2, 33]
        ,[2, 23]
        ,[3, 21]
        ,[3, 7]
    ]

city_id_list = []
for num, z in enumerate(range(M)):
# for num, (P, Y) in enumerate(p_y_list):
    
    if not debug:
        (P, Y) = list(map(int, input().split()))

    city_id_list.append([Y, P, num])
    
city_id_list = sorted( city_id_list, key= lambda x: x[0])

state_cnt = {}
tmp_list = []
for (Y, P, num) in city_id_list:
    
    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    tmp_id = str(P).zfill(6) + str(state_cnt[P]).zfill(6)
    
    tmp_list.append([num, tmp_id])
    
for cid in sorted(tmp_list, key=lambda x: x[0]):
    print(cid[1])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
lis = [int(x) for x in input().split()]
lis.sort()
if N >= M:
  print(0)
else:
  lis2 = []
  for i in range(M - 1):
    lis2.append(lis[i + 1] - lis[i])
  lis2.sort(reverse = True)
  k = 0
  for i in range(N - 1, M - 1)

Submission status: Runtime Error

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

if(len(d)<1):
    print("0")
    exit()

for _ in range(n-1):
    d.remove(max(d))

print(sum(d))