Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if m == 1:
    print('0')
    exit()
if n >= m:
    print(0)
    exit()
elif abs(m-n)==1:
    x = sorted(x)
    prev = x[0]
    diff = []
    for i, v in enumerate(x[1:]):
        diff.append([i, abs(prev-v)])
        prev = v

    diff = sorted(diff, key=lambda x: x[1], reverse=True)

    print(min(diff)[1])
    exit()

x = sorted(x)
prev = x[0]
diff = []
for i, v in enumerate(x[1:]):
    diff.append([i, abs(prev-v)])
    prev = v

diff = sorted(diff, key=lambda x: x[1], reverse=True)

split_index = list(map(lambda x: x[0]+1, diff[:n-1]))

ans = x[split_index[-1]:][-1] - x[split_index[-1]:][0]
index_from = 0
for i in range(n-1):
    index_to = split_index[i]
    ans += (x[index_from:index_to][-1] - x[index_from:index_to][0])
    index_from = index_to
print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

ans = a[-1] - a[0]
for i in range(n-1):
    ans -= diff[i]

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if m == 1:
    print('0')
    exit()
if n >= m:
    print(0)
    exit()
elif n == m-1:
    x = sorted(x)
    prev = x[0]
    diff = []
    for i, v in enumerate(x[1:]):
        diff.append([i, abs(prev-v)])
        prev = v

    diff = sorted(diff, key=lambda x: x[1], reverse=True)

    print(min(diff))

x = sorted(x)
prev = x[0]
diff = []
for i, v in enumerate(x[1:]):
    diff.append([i, abs(prev-v)])
    prev = v

diff = sorted(diff, key=lambda x: x[1], reverse=True)

split_index = list(map(lambda x: x[0]+1, diff[:n-1]))

ans = x[split_index[-1]:][-1] - x[split_index[-1]:][0]
index_from = 0
for i in range(n-1):
    index_to = split_index[i]
    ans += (x[index_from:index_to][-1] - x[index_from:index_to][0])
    index_from = index_to
print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

ans = a[-1] - a[0]
for i in range(n-1):
    ans -= diff[i]

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if m == 1:
    print('0')
    exit()
if n >= m:
    print(0)
    exit()

x = sorted(x)
prev = x[0]
diff = []
for i, v in enumerate(x[1:]):
    diff.append([i, abs(prev-v)])
    prev = v

diff = sorted(diff, key=lambda x: x[1], reverse=True)

split_index = list(map(lambda x: x[0]+1, diff[:n-1]))

ans = x[split_index[-1]:][-1] - x[split_index[-1]:][0]
index_from = 0
for i in range(n-1):
    index_to = split_index[i]
    ans += (x[index_from:index_to][-1] - x[index_from:index_to][0])
    index_from = index_to
print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

ans = a[-1] - a[0]
for i in range(n-1):
    ans -= diff[i]

print(ans)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))

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

x = sorted(x)
prev = x[0]
diff = []
for i, v in enumerate(x[1:]):
    diff.append([i, abs(prev-v)])
    prev = v

diff = sorted(diff, key=lambda x: x[1], reverse=True)

split_index = list(map(lambda x: x[0]+1, diff[:n-1]))

ans = x[split_index[-1]:][-1] - x[split_index[-1]:][0]
index_from = 0
for i in range(n-1):
    index_to = split_index[i]
    ans += (x[index_from:index_to][-1] - x[index_from:index_to][0])
    index_from = index_to
print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()

a = sorted(a)
diff = []
for i in range(1, m):
    diff += [abs(a[i] - a[i-1])]

diff = sorted(diff, reverse=True)

ans = a[-1] - a[0]
for i in range(n-1):
    ans -= diff[i]

print(ans)