// Arup Guha
// 8/24/2021
// Alternate Solution to 2021 UCF Qualifying Contest Problem: Tower Climbing

import java.util.*;

public class tower {

	public static void main(String[] args) {
	
		// Read in all of the input.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int maxJ = stdin.nextInt();
		long[] xVal = new long[n];
		for (int i=0; i<n; i++)
			xVal[i] = stdin.nextLong();
		
		// dp[i] is least score to get exactly to platform i and height i+1.
		long[] dp = new long[n];
		Arrays.fill(dp, 1000000000000000000L);
		dp[0] = 0;
		
		// Go through each platform.
		for (int i=1; i<n; i++) 
		
			// j represents a possible platform from which you could jump to platform i.
			for (int j=Math.max(0,i-maxJ); j<i; j++) 
				dp[i] = Math.min(dp[i], dp[j] + (i-j)*(i-j) + Math.abs(xVal[i]-xVal[j]));
			
		// Ta da!
		System.out.println(dp[n-1]);
	}
}
