• + 0 comments

    scala:

    import scala.io.StdIn
    import scala.collection.Searching.{Found, InsertionPoint}
    
    object Solution {
      private def minSubset(acc: Seq[BigInt])(s: BigInt): Int = {
        acc.search(s) match {
          case Found(idx)          => idx
          case InsertionPoint(idx) => if (idx >= acc.length) -1 else idx
        }
      }
    
      def main(args: Array[String]): Unit = {
        val arr = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        arr.next().toInt
        val numbers = arr.next().split(" ").map(_.toInt).sorted(Ordering[Int].reverse).toIndexedSeq
        val sums = numbers.scanLeft(BigInt(0))(_ + _)
        val t = arr.next().toInt
        (1 to t)
          .map(_ => BigInt(arr.next()))
          .map(minSubset(sums))
          .foreach(println)
      }
    }