unittest
{
	assert( [ "2 5", "10 12 1 2 14" ].parse.expand.solve == 5 );
	assert( [ "3 7", "-10 -3 0 9 -100 2 17" ].parse.expand.solve == 19 );
	assert( [ "100 1", "-100000" ].parse.expand.solve == 0 );
}



import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nm = input.front.split.to!( long[] );
	input.popFront;
	auto xs = input.front.split.to!( long[] );
	return tuple( nm[ 0 ], xs );
}

auto solve( long n, long[] xs )
{
	// 初期配置で全部の座標にコマを置けるなら移動不要
	if( xs.length <= n ) return 0;
	
	xs.sort();
	
	// 座標間の距離を計算
	auto ds = new long[ xs.length - 1 ];
	for( auto i = 0L; i < ds.length; i++ )
	{
		ds[ i ] = ( xs[ i ] - xs[ i + 1 ] ).abs;
	}
	
	// 距離が近い順の合計を計算
	ds.sort();
	return ds.take( xs.length - n ).sum;
}
