#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, m; cin >> n >> m; vector a(n); rep(i,n) cin >> a[i]; vector sum(n+1), ab(n+1); sort(a.begin(),a.end()); rep(i,n) sum[i+1] = sum[i] + a[i]*a[i]; rep(i,n) ab[i+1] = ab[i] + a[i+1] * a[i]; ll ans = 5e18; dbg(sum, ab); rep(i,n){ int j = i + m -1; if(j>=n) break; ll tmp = sum[j] - sum[i+1]; tmp *= 2; tmp += a[i]*a[i]; tmp += a[j]*a[j]; tmp -= 2 * (ab[j]-ab[i]); ans = min(ans, tmp); } cout << ans << endl; return 0; }