Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])

diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort(reverse=True)
diff = diff[:N-1]

    
if N >= M:
    print(0)
elif N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)
    



Fixed submission:
import sys

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

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])

diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff = diff[:N-1]

if N >= M:
    print(0)
elif N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)
    



Fixed submission:
import sys

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

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])

diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff0 = diff[:N-1]
diff1 = set(diff[:N-1])


if N >= M:
    print(0)
elif N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff1 and len(diff0) != 0:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff0.remove(a)
            j = i+1
            if len(diff0) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)
    



Fixed submission:
import sys

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

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])

diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff0 = diff[:N-1]
diff1 = set(diff[:N-1])


if N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff1 and len(diff0) != 0:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff0.remove(a)
            j = i+1
            if len(diff0) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)
    



Fixed submission:
import sys

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

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
x = sorted([int(x) for x in input().split()])
diff = sorted([i - j for i, j in zip(x[1:], x[:-1])], reverse=True)
diff = diff[:N-1]


if N == 1:
    print(max(x)-min(x))
else:
    j = 0
    ans = 0
    for i in range(M-1):
        a = x[i+1] - x[i]
        if a in diff:
            if i - j >= 1:
                ans += x[i] - x[j]
            diff.remove(a)
            j = i+1
            if len(diff) == 0:
                ans += x[-1] - x[i+1]
                break
    print(ans)
    



Fixed submission:
import sys

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

if N >= M:
    print(0)
    sys.exit()
diff = [0]*(M-1)
for i in range(M-1):
    diff[i] = x[i+1] - x[i]
diff.sort()
ans = sum(diff[:M-N])
print(ans)