import java.io.{BufferedReader, InputStreamReader} object Problem005 { def inputFromStdin(): (Int, Int, Seq[Int]) = { val br = new BufferedReader(new InputStreamReader(System.in)) val width = br.readLine.toInt val blockNum = br.readLine.toInt val widthSeq = (br.readLine.split(" ").map(_.toInt)).toList (width, blockNum, widthSeq) } def proc(width: Int, blockNum: Int, widthSeq: Seq[Int]): Int = { // 一番小さいものから順に入れていけば答えが出る val sortedSeq = widthSeq.sorted def procR(remain: Int, s: Seq[Int]): Int = { s match { case (head :: tail) => { if (head > remain) { widthSeq.length - s.length // これ以上入らない } else { procR(remain - head, s.tail) // まだ入るから次へ } } case Nil => widthSeq.length // 全部入った } } procR(width, sortedSeq) } def main(args: Array[String]) { val (width, blockNum, widthList) = inputFromStdin() val result = proc(width, blockNum, widthList) println(result) // test } def test = { assert(proc(16, 3, Seq(10, 5, 7)) == 2) assert(proc(100, 10, Seq(14, 85, 77, 26, 50, 45, 66, 79, 10, 3)) == 5) assert(proc(10000, 3, Seq(10, 5, 7)) == 3) assert(proc(16, 0, Seq()) == 0) assert(proc(0, 1, Seq(1)) == 0) assert(proc(0, 0, Seq()) == 0) } }