#!/usr/bin/env perl use strict; use warnings; my ($n, $k) = split / /, <>; chomp $k; my @array = split / /, <>; chomp $array[$n - 1]; @array = sort { $a <=> $b } @array; my @narray; for (my $i = 0; $i < $n - 1; $i++) { push @narray, $array[$i + 1] - $array[$i]; } @narray = sort { $a <=> $b } @narray; my $res = 0; for (my $i = 0; $i < $n - $k; $i++) { $res += $narray[$i]; } print $res, "\n";