import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int k = sc.nextInt(); Place[] places = new Place[n]; PriorityQueue queue = new PriorityQueue<>(); for (int i = 0; i < n; i++) { places[i] = new Place(i, sc.nextInt()); if (i < k) { queue.add(places[i]); } } TreeSet front = new TreeSet<>(); long frontSum = 0; for (int i = 0; i < k / 2; i++) { Place x = queue.poll(); frontSum += x.value; front.add(x); } TreeSet rear = new TreeSet<>(); long rearSum = 0; while (queue.size() > 0) { Place x = queue.poll(); rearSum += x.value; rear.add(x); } long mid = rear.first().value; long ans = mid * front.size() - frontSum + rearSum - mid * rear.size(); for (int i = k; i < n; i++) { if (front.contains(places[i - k])) { front.remove(places[i - k]); frontSum -= places[i - k].value; rear.add(places[i]); rearSum += places[i].value; Place x = rear.pollFirst(); rearSum -= x.value; frontSum += x.value; front.add(x); } else { rear.remove(places[i - k]); rearSum -= places[i - k].value; front.add(places[i]); frontSum += places[i].value; Place x = front.pollLast(); frontSum -= x.value; rearSum += x.value; rear.add(x); } mid = rear.first().value; ans = Math.min(ans, mid * front.size() - frontSum + rearSum - mid * rear.size()); } System.out.println(ans); } static class Place implements Comparable { int idx; int value; public Place(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Place another) { if (value == another.value) { return idx - another.idx; } else { return value - another.value; } } public int hashCode() { return idx; } public boolean equals(Object o) { return hashCode() == o.hashCode(); } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }