import java.util.Scanner import scala.collection.mutable object Problem247 { case class State(count: Int, total: Int) def proc(total: Int, n: Int, ai: Seq[Int]): Int = { if (total % ai.max == 0) { return total / ai.max } val dp = Array.fill(total + 1)(Int.MaxValue) val q = new mutable.PriorityQueue[State]()(Ordering.by(_.count)) q += State(0, 0) while (!q.isEmpty) { val s = q.dequeue() for (a <- ai) { if (s.total + a <= total) { val next = State(s.count + 1, s.total + a) if (next.count < dp(next.total)) { q += next dp(next.total) = next.count } } } } dp(total) match { case Int.MaxValue => -1 case x => x } } def main(args: Array[String]) { val sc = new Scanner(System.in) val total = sc.nextInt() val n = sc.nextInt() val ai = Seq.fill(n)(sc.nextInt()) val result = proc(total, n, ai) println(result) } }