#include #include #include #include using namespace std; using ll = long long; int main() { int n, m; cin >> n >> m; vector a(n); for (int i = 0; i < n; ++i) cin >> a[i]; assert(1 <= m && m <= n); assert(1 <= n && n <= 100000); for (int i = 0; i < n; ++i) assert(1 <= a[i] && a[i] <= 1000000000); sort(a.begin(), a.end()); ll ans = 0; for (int i = 0; i < m - 1; ++i) { ll d = a[i] - a[i + 1]; ans += d * d; } ll s = ans; for (int l = 1; l < n - m; ++l) { int r = l + m - 1; ll ld = a[l - 1] - a[l]; ll rd = a[r - 1] - a[r]; s = s - (ld * ld) + (rd * rd); if (ans > s) { ans = s; } } cout << ans << endl; return 0; }