fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map { it.toInt() }
    val fruits = readLine()!!.split(" ").map { it.toInt() }.sorted()

    fun countFruit(fruits: List<Int>, limit: Int, amount: Int = 0): Int {
        return if (limit < 1 || fruits.isEmpty()) amount else countFruit(fruits.takeLast(fruits.size - 1), limit - 1, amount + fruits[0])
    }

    println(countFruit(fruits, K))
}
