n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort() d = [] for i in range(1, n): d.append((a[i] - a[i-1]) ** 2) s = sum(d[:m-1]) ans = s for i in range(n - m): s += d[i+m-1] - d[i] ans = min(s, ans) print(ans)