import java.util.*;
import java.io.*;
// 定长滑动窗口统计满足条件的最小区间值
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] line1 = bf.readLine().split(" ");
int n = Integer.parseInt(line1[0]);
int m = Integer.parseInt(line1[1]);
int[] str = Arrays.stream(bf.readLine().split(" ")).mapToInt(
Integer::parseInt).toArray();
Arrays.sort(str);
long minL = 0; //注意精度问题,L超过21亿的范围
//第一个窗口的minL
for (int i = 1; i < m; i++) {
minL += Math.pow(str[i], 2) - Math.pow(str[i - 1], 2);
}
//定长窗口的L
long conL = minL;
for (int r = m; r < n; r++) {
//注意精度损失,int*int结果仍为int,会有损失。pow内隐式转换int为double不会损失。
//新增元素r的L大小控制变化
conL += Math.pow(str[r], 2) - Math.pow(str[r - 1], 2);
//减少元素r-m的L大小控制变化
conL -= Math.pow(str[r - m + 1], 2) - Math.pow(str[r - m], 2);
if (conL < minL) {
minL = conL;
}
}
System.out.println(minL);
}
}