import java.util.* fun main(args: Array) { val scanner = Scanner(System.`in`) val T = scanner.nextInt() val N = scanner.nextInt() val times = (1..N).map { scanner.nextInt() }.sortedDescending().toMutableList() val combinationsCache = mutableMapOf>>() var count = 0 for (t in T downTo 1) { for (i in 1..t) { generateSequence { combinationsCache .getOrPut(i) { times.combinations(i) } .find { it.sum() == t } }.forEach { c -> c.forEach { times.remove(it) } combinationsCache.clear() ++count if (times.size == 0) { println(count) return } } } } } fun List.combinations(n: Int): List> { return when { n > size -> emptyList() n == 1 -> map { mutableListOf(it) } else -> { val last = size (0 until last).fold(emptyList()) { all, index -> all + subList(index + 1, last) .combinations(n - 1) .map { it.add(0, get(index)) it } } } } }