object Main extends App {
	val N = scala.io.StdIn.readLine.split(" ").map(_.toInt)
	val L = scala.io.StdIn.readLine.split(" ").map(_.toInt).toList

	def sort(x: List[Int]): List[Int] = {
		x match {
			case Nil => List[Int]()
			case r::Nil => List(r)
			case r::rest =>{
				val temp = rest.toArray
				var right = List(r)
				var left = List[Int]()

				for(i<-0 until rest.size){
					if(temp(i) > r) right = temp(i)::right
					else left = temp(i)::left
				}

				sort(left):::sort(right)
			}
		}
	}

	def sum(x: List[Int], y: Int): Int = {
		x match {
			case Nil => y
			case r::rest => sum(rest,r+y)
		}
	}

	def get(x: List[Int], y: Int): List[Int] = {
		y match {
			case 0 => x
			case _ => get(x.tail, y-1) 
		}
	}

	println(sum(get(sort(L), N(0)-N(1)),0))
}

