Problem:


Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0



Sample input:
2 5
10 12 1 2 14


Sample output:
5

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()
    if ans is not None:
        print(ans)


def solve():
    N, M = iip(False)
    X = iip()
    X.sort()

    if N >= M:
        return 0

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

    #print(A)
    #print(X)
    #print(A[-N+1:])

    return sum(A[:-N+1])


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()
    if ans is not None:
        print(ans)


def solve():
    N, M = iip(False)
    X = iip()
    X.sort()

    if N >= M:
        return 0

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

    return X[-1] - X[0] - sum(A[-N+1:])
    print(A)


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()
    if ans is not None:
        print(ans)


def solve():
    N, M = iip(False)
    X = iip()
    X.sort()

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

    return X[-1] - X[0] - sum(A[-N+1:])
    print(A)


#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, m, *x = map(int, open(0).read().split())
    if n >= m:
        print(0)
        return
    x.sort()
    a = [x[i + 1] - x[i] for i in range(m - 1)]
    a.sort()
    ans = sum(a) - sum(a[-n+1:])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m, *x = map(int, open(0).read().split())
    if m >= n:
        print(0)
        return
    x.sort()
    a = [x[i + 1] - x[i] for i in range(m - 1)]
    a.sort()
    ans = sum(a) - sum(a[-n+1:])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m, *x = map(int, open(0).read().split())
    x.sort()
    a = [x[i + 1] - x[i] for i in range(m - 1)]
    a.sort()
    ans = sum(a) - sum(a[-n+1:])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

x.sort()

print(sum(x[:-(N-1)]))

Submission status: Wrong Answer

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

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

x.sort()

print(sum(x[:-(N-1)]))

Submission status: Wrong Answer

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

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

x.sort()

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
if N >= M:
    print(0)
    quit()

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

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

interval.sort(reverse=True)
ans = 0
if N == 1:
    print(sum(interval))
else:
    for i in range(N - 1):
        ans += interval[i]
        print(sum(interval) - ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    if N>=M:
        print(0)
        return

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

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

    rmdiff = diff[:-(N-1)]
    print(sum(rmdiff))
main()

Submission status: Wrong Answer

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

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

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

rmdiff = diff[:-(N-1)]
print(sum(rmdiff))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X=list(map(int, input().split()))
X=sorted(X)
distance=[]
for i in range(M-1):
    distance.append(X[i+1]-X[i])


distance=sorted(distance, reverse=True)
print(distance)
answer=sum(distance[(N-1):])
print(answer)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
X=list(map(int, input().split()))
X=sorted(X)
distance=[]
for i in range(M-1):
    distance.append(X[i+1]-X[i])

#print(distance)
distance=sorted(distance)
answer=sum(distance[:-(N-1)])
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Y.sort()
print(sum(Y[:M-N]))

Submission status: Wrong Answer

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

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

Y.sort()
print(sum(Y[:-N+1]))

Submission status: Wrong Answer

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

if N >= M:
    print(0)
else:
    dists = [X[i+1]-X[i] for i in range(M-1)]
    sm = sum(sorted(dists)[:-(N-1)])
    print(sm)

Submission status: Wrong Answer

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

diff = [0]*(M-1)
for i in range(M-1):
  diff[i] = X[i+1] - X[i]

diff = sorted(diff)
print(sum(diff[0:M-N]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

N,M = (int(x) for x in input().split())
if N >= M:
  print("0")
  sys.exit()
  
cnt = 0
  
x = list(map(int,input().split()))
x.sort()

cost = {}
for i in range(len(x)-1):
  cost[i] = x[i+1] - x[i]
num = 0
cost = sorted(cost.items(), key=lambda x:x[1],reverse=True)
for i in range(N-1):
  if i == 0:
    cnt += x[cost[i][0]] - x[0]
  else:
    cnt += x[cost[i][0]] - x[num+1]
  num = cost[i][0]
cnt += x[len(x)-1] - x[num+1]
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq

def heapsort(iterable):
    h = []
    for value in iterable:
        heapq.heappush(h, value)
    return [heapq.heappop(h) for i in range(len(h))]

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

X = heapsort(X)
d = [0] * (len(X)-1)

for ii in range(len(d)):
	d[ii] = abs(X[ii]- X[ii+1])
d = heapsort(d)

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

Submission status: Wrong Answer

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

Y.sort()
ikanai = sum(Y[M-1-(N-1):M-1])
#print(Y)
#print(ikanai)

print(X[-1]-X[0]-ikanai)

Submission status: Wrong Answer

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

list_X.sort()
list_Y = []

for i in range(m-1):
    list_Y.append(list_X[i+1]-list_X[i])

list_Y.sort()

print(sum(list_Y[:m-n]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = [x[i+1] - x[i] for i in range(m-1)]
ans.sort()

print(sum([ans[i] for i in range(m-3)]))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N,M = list(map(int, input().rstrip().split()))
X_list = list(map(int, input().rstrip().split()))
#-----

X_list.sort()

diff_list=[0]
for i in range(1,len(X_list)):
    diff = X_list[i] - X_list[i-1]
    diff_list.append(diff)

diff_list.sort()

if N > M:
    ans = 0
else:
    ex_num = N-1
    ans = sum(diff_list[1:]) - sum( diff_list[-ex_num:] )

print(ans)

Submission status: Wrong Answer

Solution code:
koma,spot=map(int,input().split())
if koma>=spot:print(0);exit()
l=list(map(int,input().split()))
l.sort()
k=[l[i+1]-l[i]for i in range(spot-1)]
k.sort()
print(l[-1]-l[0]-sum(k[-koma+1:]))

Submission status: Wrong Answer

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

# 使えるコマが1つだけだとして考えると
# X[0]にコマを置いて、右に向かって順番に移動していくのが最善(一度通った道をもう一度通るのは無駄)
# "ジャンプ[i]"：X[i]から[i+1]への移動 と定義する
# ジャンプはM-1回
# 総移動回数はsum(ジャンプ[i]での移動回数)

# 使えるコマが1つ増えると、1ジャンプ減らすのと同じ
# コマがN個使えるとき、"操作"をN-1回減らすことができる
# sum((ジャンプ[i]での移動回数 (i<=0<M-1))のうち小さい方からM-N個)が答え

print(sum(sorted([X[i + 1] - X[i] for i in range(M - 1)])[: min(M - N, 0)]))

Submission status: Wrong Answer

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

# X[0]だけにコマを置いて、右に向かって順番に移動していく
# "操作"：X[i]->x[i+1] (0<=i<M-1)
# L[i] = X[i+1]-X[i] (0<=i<M-1) とする(次の地点に行くために必要な移動回数)
# コマがN個使えるとき、"操作"をN-1回減らすことができる。
# Lの小さい方からM-(N-1)個の総和が答え

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

Submission status: Wrong Answer

Solution code:

def submit():
    n, m = map(int, input().split())
    xlist = list(map(int, input().split()))

    if n >= m:
        print(0)
    else:
        xlist.sort()
        dlist = [xlist[i + 1] - xlist[i] for i in range(m - 1)]
        dlist.sort()
        print(sum(dlist[:-n + 1]))


if __name__ == "__main__":
    submit()

Submission status: Wrong Answer

Solution code:
import sys
INF = 1000000
# N:コマ M:ポイント
N, M = map(int, input().split())
points = list(map(int, input().split()))

points.sort()

minLength = [INF for _ in range(M)]
arrays = []

for n in range(N):
  tmp_array = []
  index = minLength.index(max(minLength)) 
  minLength[index] = 0
  tmp_array.append(points[index])
  for r in arrays:
    if points[index] in r:
      r.remove(points[index])
  for i in range(M):
    if minLength[i] > abs(points[i] - points[index]):
      for r in arrays:
        if points[i] in r:
          r.remove(points[i])
      tmp_array.append(points[i])
      minLength[i] = abs(points[i] - points[index])
  arrays.append(tmp_array)
  if all([True if i == 0 else False for i in minLength]):
    print(0)
    sys.exit()

# スタート
ans = 0
for a in arrays:
  mi = min(a)
  ma = max(a)
  ans += abs(ma - mi)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
INF = 1000000
# N:コマ M:ポイント
N, M = map(int, input().split())
points = list(map(int, input().split()))

points.sort()
print(points)

minLength = [INF for _ in range(M)]
arrays = []

for n in range(N):
  tmp_array = []
  index = minLength.index(max(minLength)) 
  minLength[index] = 0
  tmp_array.append(points[index])
  for r in arrays:
    if points[index] in r:
      r.remove(points[index])
  for i in range(M):
    if minLength[i] > abs(points[i] - points[index]):
      for r in arrays:
        if points[i] in r:
          r.remove(points[i])
      tmp_array.append(points[i])
      minLength[i] = abs(points[i] - points[index])
  arrays.append(tmp_array)
  if all([True if i == 0 else False for i in minLength]):
    print(0)
    sys.exit()

# スタート
ans = 0
for a in arrays:
  mi = min(a)
  ma = max(a)
  ans += abs(ma - mi)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
INF = 1000000
# N:コマ M:ポイント
N, M = map(int, input().split())
points = list(map(int, input().split()))

points.sort()
print(points)

minLength = [INF for _ in range(M)]
arrays = []

for n in range(N):
  tmp_array = []
  index = minLength.index(max(minLength)) 
  minLength[index] = 0
  tmp_array.append(points[index])
  for r in arrays:
    if points[index] in r:
      r.remove(points[index])
  for i in range(M):
    if minLength[i] > abs(points[i] - points[index]):
      for r in arrays:
        if points[i] in r:
          r.remove(points[i])
      tmp_array.append(points[i])
      minLength[i] = abs(points[i] - points[index])
  arrays.append(tmp_array)
  if all([True if i == 0 else False for i in minLength]):
    print(0)
    sys.exit()

# スタート
ans = 0
for a in arrays:
  mi = min(a)
  ma = max(a)
  ans += abs(ma - mi)
print(ans)

Submission status: Wrong Answer

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

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

# gapのエントリのうち大きい方からN - 1個は使わない
# 使うのは (M - 1) - (N - 1) = M - N個
print(sum(gap[0 : M - N]))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    diff = [y-x for x,y in zip(X[0:M-1], X[1:])]
    diff.sort()
    ans = sum(diff[:M-N])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = sum(dis[:len(x) - n])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Y.sort()
print(sum(Y[:M-N]))

Submission status: Wrong Answer

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

Y = []

X.sort()

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

Y.sort()

print(sum(Y[:-(N-1)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N,M = map(int,input().split())
X = list(map(int,input().split()))
x = []
ans = 0
X.sort()
if N >= M:
  print(0)
  exit()
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[j] = 0
ans = sum(xx)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n, *a = map(int, open(0).read().split())
a.sort()
ans = a[-1]-a[0]
for i in range(n-1,0,-1): a[i] -= a[i-1]
a.sort()
print(max(0,ans - sum(a[-k+1:])))

Submission status: Wrong Answer

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

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: Wrong Answer

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