/* -*- coding: utf-8 -*- * * 2548.cc: No.2548 Problem Selection - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 100000; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; /* global variables */ int as[MAX_N]; ll dds[MAX_N], ddss[MAX_N]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", as + i); sort(as, as + n); for (int i = 0; i + 1 < n; i++) { int d = as[i + 1] - as[i]; dds[i] = (ll)d * d; ddss[i + 1] = ddss[i] + dds[i]; } ll mins = LINF; for (int i = 0; i + m - 1 < n; i++) mins = min(mins, ddss[i + m - 1] - ddss[i]); printf("%lld\n", mins); return 0; }