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=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
c=0
for i in range(n+1):
    c+=x[i]
print(c)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
count=0
a=max(x)
i=min(x)
if m<=2:
  print(count)
else:
  while True:
    x.pop(a)
    x.pop(b)
    a-=1
    b+=1
    count+=1
    if x==[]:
      print(count)
      break

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
count=0
a=max(x)
i=min(x)
if m<=2:
  print(count)
else:
  while True:
    x.remove(a)
    x.remove(b)
    a-=1
    b+=1
    count+=1
    if x==[]:
      print(count)
      break

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
count=0
a=max(x)
i=min(x)
if m<=2:
  print(count)
while True:
  x.remove(a)
  x.remove(b)
  a-=1
  b+=1
  count+=1
  if x==[]:
    print(count)
    break

Submission status: Runtime Error

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

if N>=M:
    out=0
else:
    aida=[0]*(M-1)
    for i in range(M-1):
        aida[i] = X[i+1] - X[i]
    #print(aida)
    aida = sorted(aida, reverse=True)

    out = X[-1] - X[0]
    for j in range(N-1):
        out-=aida[j]

print(out)[N,M] = list(map(int,input().split()))
X = list(map(int,input().split()))
X = sorted(X)
#print('X',X)

if N>=M:
    out=0
else:
    aida=[0]*(M-1)
    for i in range(M-1):
        aida[i] = X[i+1] - X[i]
    #print(aida)
    aida = sorted(aida, reverse=True)

    out = X[-1] - X[0]
    for j in range(N-1):
        out-=aida[j]

print(out)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m,*x = map(int,open(0).read().split())
x.sort()
a = sorted([x[i+1]-x[i] for i in range(m-1)],reverse=True)
ans = x[-1]-x[0]
for i in range(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()))
l=[]
for i in range(n-1):
  l.append(x[i+1]-x[i])
l.sort(reverse=True)
cnt=sum(l[:m-1])
print(x[m-1]-x[0]-cnt)

Submission status: Runtime Error

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

X.sort()
K = [
    abs(X[i] - X[i - 1])
    for i in range(1, N)
]

print(sum(K[:M - (N - 1)])

Submission status: Runtime Error

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

X.sort()
K = [
    abs(X[i] - X[i - 1])
    for i in range(1, N)
]

print(sum(K[:M - (N - 1)]))

Submission status: Runtime Error

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

X.sort()
K = [
    abs(X[n] - X[n - 1])
    for i in range(1, N)
]

print(sum(K[:M - (N - 1)]))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import bisect
import math
import itertools
from queue import deque
from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N, M = inp_list()
X = inp_list()
X.sort()
# print(X)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

cost_li = []
for i in range(m-1):
    heapq.heappush(cost_li,-(x_input[i+1]-x_input[i]))

if len(cost_li)==0:
    print(0);exit()
for i in range(n-1):
    heapq.heappop(cost_li)

print(-sum(cost_li))

Submission status: Runtime Error

Solution code:
if M <= N:
    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[-1]-X[0]-sum(L[:N-1]))

Submission status: Runtime Error

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

if M == 1:
    print(0)
else:
    l.sort()
    l_dist =[]

    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    for i in range(N-1):
       del l_dist[-1]

    print(sum(l_dist))

Submission status: Runtime Error

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

if M == 1:
    print(0)
else:
    l.sort()
    l_dist =[]
    
    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    for i in range(N-1):
        l_dist.pop()

    print(sum(l_dist))

Submission status: Runtime Error

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

if M == 1:
    print(0)
else:
    l.sort()
    l_dist =[]
    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    for i in range(N-1):
        l_dist.pop()

    print(sum(l_dist))

Submission status: Runtime Error

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

if M == 1:
    print(1)
else:
    l.sort()
    l_dist =[]
    for i in range(1, M):
        l_dist.append(abs(l[i]-l[i-1]))

    l_dist.sort()

    for i in range(N-1):
        l_dist.pop()

    print(sum(l_dist))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

    x.sort()
    dx = [(i, x[i+1]-x[i]) for i in range(m-1)]
    dx.sort(key = lambda x: x[1], reverse = True)
    d = [0] + [dx[i][0] for i in range(n)] + [m-1]
    d.sort()

    ans = []
    for i in range(n+1):
        ans.append(x[d[i+1]] - x[d[i]])
    ans.sort()

    return sum(ans[:n])


print(solve())

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

xSort = sorted(x)

dif = [0] * (m - 1)
for i in range(m - 1):
    dif[i] = xSort[i + 1] - xSort[i]
ans = sum(dif)

if (n == 1):
    print(ans)
else:
    for j in range(n - 1):
        tar = j + 1
        tarInd = tar * -1
        tarVal = sorted(dif)[tarInd]
        ans -= tarVal

    print(ans)
# n番目の要素を取得
# sorted(list)[-1] > 1番大きい
# sorted(list)[-２] > 2番大きい
# or
# array.argsort()
# > array ([1,0,2,4,3]) みたいな
'''
  import numpy as np
  difArray = np.array(dif)
  difIndex = difArray.argsort()

  for i in range(n - 1):
    tar = m - i
    maxIndex = difIndex[tar]
    maxVal = diff
'''

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop

import collections

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted([int(i) for i in input().split()])
diff_x = [x[i + 1] - x[i] for i in range(m - 1)]
max_diff = []
if n >= m:
    print(0)
else:
    for _ in range(n - 1):
        max_diff_i = diff_x.index(max(diff_x))
        max_diff.append(max_diff_i)
        diff_x[max_diff_i] = -10**6
    max_diff.sort()

    move = x[max_diff[0]] - x[0]
    for i in range(n - 2):
        move += x[max_diff[i + 1]] - x[max_diff[i] + 1]
    move += x[m - 1] - x[max_diff[n - 2] + 1]
    print(move)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted([int(i) for i in input().split()])
diff_x = [x[i + 1] - x[i] for i in range(m - 1)]
max_diff = []
if m == 1:
    print(0)
else:
    for _ in range(n - 1):
        max_diff_i = diff_x.index(max(diff_x))
        max_diff.append(max_diff_i)
        diff_x[max_diff_i] = -10**6
    max_diff.sort()

    move = x[max_diff[0]] - x[0]
    for i in range(n - 2):
        move += x[max_diff[i + 1]] - x[max_diff[i] + 1]
    move += x[m - 1] - x[max_diff[n - 2] + 1]
    print(move)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
x = sorted([int(i) for i in input().split()])
diff_x = [x[i + 1] - x[i] for i in range(m - 1)]
max_diff = []
for _ in range(n - 1):
    max_diff_i = diff_x.index(max(diff_x))
    max_diff.append(max_diff_i)
    diff_x[max_diff_i] = -1
max_diff.sort()

move = x[max_diff[0]] - x[0]
for i in range(n - 2):
    move += x[max_diff[i + 1]] - x[max_diff[i] + 1]
move += x[m - 1] - x[max_diff[n - 2] + 1]
print(move)

Submission status: Runtime Error

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

a.sort()
term=[]
if n-m>=0:
    print(0)
else:
    for i in range(m-1):
        term.append(s[i+1]-s[i])
    
    term.sort()

    for i in range(n-1):
        del term[-1]
    
    print(sum(term))

Submission status: Runtime Error

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

s.sort()
term=[]

for i in range(m-1):
    term.append(s[i+1]-s[i])

term.sort()

for i in range(n-1):
    del term[-1]

print(sum(term))

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

X.sort()

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

dist = []

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

dist.sort(reverse=True)

ans = X[-1] - X[0]
for i in range(N-1):
    ans -= dist[i]
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
a=0;b=0;m=0;l=0
N,M=map(int,input().split())
X=list(map(int,input().split()))
l=M-N; m=sum(X)
for i in itertools.combinations(X,l):
  L=sorted(list(map(int,i)))
  for j in range(1,l):
    a=a+(L[j]-L[j-1])
  if a<m:
    m=a
  a=0
print(m)

Submission status: Runtime Error

Solution code:
import sys
import math
from fractions import gcd
import numpy as np
readline = sys.stdin.readline
INF = 10**9


def main():
  N, M = map(int, readline().split())
  X = np.array(readline().split(), dtype=np.int64)
  X.sort()
  X = np.diff(X)
  X.sort()
  if M-N>=0:
    answer = X[:M-N].sum()
  else:
    answe = 0
  print(answer)
  
  
  
  
  
if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
import sys

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

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

Submission status: Runtime Error

Solution code:
import sys

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

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

Submission status: Runtime Error

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

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

s.sort(reverse=True)

res = sum(s)
if M == 1:
    print(0)
    quit()
else:
    for i in range(N - 1):
        res -= s[i]

print(res)

Submission status: Runtime Error

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

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

s.sort(reverse=True)

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

print(res)

Submission status: Runtime Error

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

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

s.sort(reverse=True)
res = sum(s)
for i in range(N - 1):
    res -= s[i]
print(res)

Submission status: Runtime Error

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

X.sort()

x = X[:]
p = x.pop(0)
a = []
for b in x:
    a.append(b - p)
    p = b

if a:
    for _ in range(N-1):
        a.pop(a.index(max(a)))
    print(sum(a))
else:
    print(0)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define mfill(x,y) memset(x,y,sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x,y,h,w) if(0<=x&&x<h&&0<=y&&y<w)
#define y0 y12345
#define y1 y54321

#ifdef LOCAL
#define debug(...) fprintf(stderr,__VA_ARGS__)
#else
#define debug(...) 42
#endif

using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;using vvint = vector<vector<int>>;
using vll = vector<ll>;using vvll = vector<vector<ll>>;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b;return 1;}return 0;}
template <class T>void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}
template <class T>T gcd(T &a, T &b){if(a<b){swap(a,b);} T r = a%b; while(r!=0){a=b;b=r;r=a%b;} return b;}

vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    debug("debug test\n");
    int n,m;cin>>n>>m;
    vint x(m);
    rep(i, m){
        cin>>x[i];
    }
    if(n>=m){
        cout << 0 << endl;
        return 0;
    }
    sort(all(x));
    vint aid;
    ll sum = 0;
    rep(i, m-1){
        aid.push_back(x[i+1]-x[i]);
        sum += x[i+1]-x[i];
    }
    sort(all(aid), greater<int>());
    ll rem = 0;
    rep(i, n-1){
        rem += aid[i];
    }
    cout << sum - rem << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
def main():
  n, m = input_list()
  if n >= m:
    print(0)
    exit(0)
  x = list(set(input_list()))
  sx = list(sorted(x))
  diffs = []
  for i, xv in enumerate(range(len(x)-1)):
    diff = abs(sx[i+1]-sx[i])
    diffs.append((i, diff))
  sd = list(sorted(diffs, key=lambda x: x[1], reverse=True))
  
  points = []
  for i in range(n-1):
    if i > len(sd)-1:
      break
    points.append(sd[i][0])
  r = chunk_list(points, sx)
  ans = 0
  for v in r:
    ans += max(v) - min(v)
  print(ans)
    

def chunk_list(index_list, data):
  result = []
  start = 0
  for i in index_list:
    result.append(data[start:i+1])
    start = i+1
  result.append(data[start:len(data)])
  return result
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Runtime Error

Solution code:
def main():
  n, m = input_list()
  if n >= m:
    print(0)
    exit(0)
  x = input_list()
  sx = list(sorted(x))
  diffs = []
  for i, xv in enumerate(range(m-1)):
    diff = abs(sx[i+1]-sx[i])
    diffs.append((i, diff))
  sd = list(sorted(diffs, key=lambda x: x[1], reverse=True))
  
  points = []
  for i in range(n-1):
    points.append(sd[i][0])
  
  r = chunk_list(points, sx)
  ans = 0
  for v in r:
    ans += max(v) - min(v)
  print(ans)
    

def chunk_list(index_list, data):
  result = []
  start = 0
  for i in index_list:
    result.append(data[start:i+1])
    start = i+1
  result.append(data[start:len(data)])
  return result
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

l.sort(reverse=True)
ans = sum(ls)
 
for i in range(n-1):
    ans -= ls[i]
print(ans)

Submission status: Runtime Error

Solution code:

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

x = list(map(int, input().split()))
x.sort()
dis = [0] * (m - 1)
for i in range(m - 1):
    dis[i] = x[i + 1] - x[i]

dis.sort()
ans = x[m - 1] - x[0]
for i in range(len(dis) - 1, len(dis)- n, -1):
    ans -= dis[i]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
X=list(map(int,input().split()))
X=sorted(X)
S=[]
t=0
for i in range(N-1):
  s=X[i+1]-X[i]
  S.append(s)
S=sorted(S,reverse=True)
t+=S[0:N]
print(t)

Submission status: Runtime Error

Solution code:
import math
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
ans = 0
X.sort()
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(math.ceil(N/2)):
  xx.pop(0)
ans = sum(xx)
print(ans)

Submission status: Runtime Error

Solution code:
import math
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
count = 0
ans = 0
X.sort()
if N >= M:
  print(0)
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(math.ceil(N/2)):
  xx.pop(0)
ans = sum(xx)
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
count = 0
ans = 0
X.sort()
if N >= M:
  print(0)
for i in range(M-1):
  x.append(abs(X[i]-X[i+1]))
xx = sorted(x,reverse=True)
for j in range(N//2):
  xx.pop(j)
ans = xx[-1] - xx[0]
print(ans)

Submission status: Runtime Error

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

if N == 1:
    print(abs(H[M-1] - H[0]))
else:
    A = []
    for i in range(M-1):
        A.append(H[i+1] - H[i])
        A = sorted(A,reverse = False) 
print(sum(A[:-N+1]))

Submission status: Runtime Error

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

if N == 1:
    print(max(H)- min(H))
else:
    A = []
    for i in range(M-1):
        A.append(H[i+1] - H[i])
        A = sorted(A,reverse = False) 
print(sum(A[:-N+1]))

Submission status: Runtime Error

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

if m==1:
    print(0)
else:
    remainder_lst = []
    for i in range(m-1):
        x1 = x_lst[i]
        x2 = x_lst[i+1]

        remainder = x2 - x1
        remainder_lst.append(remainder)

    remainder1_lst = sorted(remainder_lst, reverse=True)
    delete = 0

    if n==1:
        answer = sum(remainder_lst)
    else:
        for i in range(n-1):
            remainder1 = remainder1_lst[i]
            delete += remainder1
        answer = sum(remainder_lst) - delete

    print(answer)

Submission status: Runtime Error

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

if m==1:
    print(0)
else:
    remainder_lst = []
    for i in range(m-1):
        x1 = x_lst[i]
        x2 = x_lst[i+1]

        remainder = x2 - x1
        remainder_lst.append(remainder)

    remainder1_lst = sorted(remainder_lst, reverse=True)
    delete = 0

    for i in range(n-1):
        remainder1 = remainder1_lst[i]
        delete += remainder1

    answer = sum(remainder_lst) - delete
    print(answer)

Submission status: Runtime Error

Solution code:
N , M = (map(int , input().split()))
X = list(map(int , input().split()))
sorted_x = sorted(X)
ans= 0
total_kukann = 0
kukann_kyori = []
a = N-1
for i in range(len(sorted_x)-1):
    kukann_kyori.append(sorted_x[i + 1] - sorted_x[i])
    total_kukann += sorted_x[i + 1] - sorted_x[i]
kukann_kyori.sort()
for j in range(1,a+1):
    total_kukann -= kukann_kyori[-j]
if N >= M:
    total_kukann = 0
print(total_kukann)

Submission status: Runtime Error

Solution code:
入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""
入力Pを何個かの塊にわかる
塊の中では1個の駒による操作が行われる、
1回にとなりの1ますに歯科移動できない
すなわちそのなかでの最大と最小の差の絶対値

"""
#solution:
#潜 影 蛇 手 ! !
N,M=pin()
P=sorted(list(pin()))#大小関係を整理する
#print("*",P)
PP=[abs(P[j+1]-P[j]) for j in range(M-1)]
#print(PP)
Q=sorted(PP)
#print(Q)
if N>=M:#この場合必ず1個の拠点に1個以上のコマが置けるのでこのままでいい
    print(0)
else:
    for i in range(N-1):
        Q.pop()
    print(sum(Q))

Submission status: Runtime Error

Solution code:
import sys
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N, M = MAP()
    X = LIST()

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

    X = sorted(X)

    H = []
    heapify(H)
    for i in range(len(X)-1):
        heappush(H, (X[i+1] - X[i])*(-1))

    for i in range(N-1): heappop(H)

    print(sum(H)*(-1))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N, M = MAP()
    X = LIST()
    
    if M == 1:
        print(0)
        sys.exit()
        
    X = sorted(X)

    H = []
    heapify(H)
    for i in range(len(X)-1):
        heappush(H, (X[i+1] - X[i])*(-1))

    for i in range(N-1): heappop(H)
    
    print(sum(H)*(-1))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)
 
	diff_x = []
	diff = 0
	result = 0
 
	for i in range(m-1):
		diff = sorted_x[i+1] - sorted_x[i]
		result += diff
		diff_x.append(diff)

	sorted_diff = sorted(diff_x, reverse=True)[:n-1]
 
	if n == 1:
 		result -= sorted_diff[0]
	else:
		for i in range(n-1):
			result -= sorted_diff[i]
	 
	print(result)

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)
 
	diff_x = []
	diff = 0
	result = 0
 
	for i in range(m-1):
		diff = sorted_x[i+1] - sorted_x[i]
		result += diff
		diff_x.append(diff)

	sorted_diff = sorted(diff_x, reverse=True)[:n-1]
 
 	if n == 1:
 		result -= sorted_diff[0]
	else:
		for i in range(n-1):
			result -= sorted_diff[i]
	 
	print(result)

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)
 
	diff_x = []
	diff = 0
	result = 0
 
	for i in range(m-1):
		diff = sorted_x[i+1] - sorted_x[i]
		result += diff
		diff_x.append(diff)

	sorted_diff = sorted(diff_x, reverse=True)[:n-1]
 
	for i in range(n-1):
		result -= sorted_diff[i]
 
	print(result)

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	x.sort()

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

	list_diff_x = list(tupple_diff_x)

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

	print(sum(list_diff_x))

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)

	tupple_diff_x = (sorted_x[i+1] - sorted_x[i] for i in range(m-1))

	list_diff_x = list(tupple_diff_x)

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

	print(sum(list_diff_x))

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)

	diff_x = [sorted_x[i+1] - sorted_x[i] for i in range(m-1)]

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

	print(sum(diff_x))

Submission status: Runtime Error

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

if m == 1:
	print(0)
else:
	sorted_x = sorted(x)

	diff_x = []

	for i in range(m-1):
		diff_x.append(sorted_x[i+1] - sorted_x[i])

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

	print(sum(diff_x))

Submission status: Runtime Error

Solution code:
import heapq
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_range = []
cut = [0]
count = 0
for i in range(m-1):
    x_range.append(abs(x[i]-x[i+1]))
h = heapq.nlargest(n-1,x_range)


for i in h:
    cut.append(x_range.index(i)+1)
cut.append(len(x))

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

Submission status: Runtime Error

Solution code:
import heapq
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_range = []
cut = [0]
count = 0
for i in range(m-1):
    x_range.append(abs(x[i]-x[i+1]))
h = heapq.nlargest(n-1,x_range)


for i in h:
    cut.append(x_range.index(i)+1)
cut.append(len(x))


for i in range(len(cut)-1):
    sum_range = x[cut[i]:cut[i+1]]
    print(1)
    count += abs(sum_range[0]-sum_range[len(sum_range)-1])
#print(count)

Submission status: Runtime Error

Solution code:
import heapq
n,m = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
x_range = []
cut = [0]
count = 0
for i in range(m-1):
    x_range.append(abs(x[i]-x[i+1]))
h = heapq.nlargest(n-1,x_range)
for i in h:
    cut.append(x_range.index(i)+1)
cut.append(len(x))
for i in range(len(cut)-1):
    sum_range = x[cut[i]:cut[i+1]]
    count += abs(sum_range[0]-sum_range[len(sum_range)-1])
print(count)

Submission status: Runtime Error

Solution code:
N,C,K = map(int,input().split())
T = []
for _ in range(N):
    t = int(input())
    T.append(t)
T.sort()
angry = 0
ans = 0
c = 0
for i in T:
    if angry == 0:
        angry = i + K
    c += 1
    if i >= angry :
        #print(i,angry)
        angry = i + K
        ans += 1
        c = 1
    if c == C:
        angry = 0
        ans += 1
        c = 0
    #print(ans,angry,c)
if c > 0:
    ans += 1
if C == 1:
    ans = C
print(ans)