• + 0 comments

    scala:

    import scala.io.StdIn
    
    object Solution {
      private def solve(
          n: Int,
          m: BigInt,
          a: Seq[Int],
          h: Seq[Int]
      ): Int = {
        val ah = a zip h
    
        def canDistribute(k: Int): Boolean = {
          ah.map { case (ai, hi) =>
            ai + BigInt(k - 1) * hi
          }.sorted
            .take(k)
            .sum <= m
        }
    
        def binarySearch(low: Int, high: Int): Int = {
          if (low > high)
            return high
          val mid = (low + high) / 2
          if (canDistribute(mid))
            binarySearch(mid + 1, high)
          else
            binarySearch(low, mid - 1)
        }
    
        binarySearch(1, n)
      }
    
      def main(args: Array[String]): Unit = {
        val arr = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val nm = arr.next().split(' ')
        val n = nm.head.toInt
        val m = BigInt(nm.last)
        val a = arr.next().split(' ').map(_.toInt).toSeq
        val h = arr.next().split(' ').map(_.toInt).toSeq
        println(solve(n, m, a, h))
      }
    }