import Control.Monad
import Data.List
import Control.Applicative

main = do
  s <- getContents
  let hs = map read . tail . lines $ s :: [Int]
  let [_,k] = map read . words . head . lines $ s :: [Int]
--  let his = hm (hi hs k)  (del (hmax (hi hs k)) hs) k
  putStrLn $ show . hsub $ hm (hi hs k)  (del (hmax (hi hs k)) hs) k
--  putStrLn $ show $ hsub his

hmax :: [Int] -> Int
hmax hs = foldl max 0 hs

hmin :: [Int] -> Int
hmin hs  = foldl min (hmax hs)  hs

hsub :: [Int] -> Int
hsub hs = (hmax hs) - (hmin hs)

del :: Int -> [Int] -> [Int]
del x xs = filter (/= x) xs

hi :: [Int] -> Int -> [Int]
hi hs k
  |k == 0 = []
  |otherwise = [hmax hs] ++ hi (delete (hmax hs) hs) (k - 1)

hm :: [Int] -> [Int] -> Int -> [Int]
hm x m k
  |length m < k = x
  |(hsub x) < (hsub (hi m k)) = hm x (del (hmax (hi m k)) m) k
  |otherwise = hm (hi m k) (del (hmax (hi m k)) m) k

