A plane journey
Practice
3.6 (54 votes)
Algorithms
Binary search
C++
Problem
89% Success 13134 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A flight company has to schedule a journey of \(N\) groups of people from the same source to the same destination. Here, \(A_1,\ A_2,\ ...,\ A_N\) represents the number of people in each group. All groups are present at the source. The flight company has \(M\) planes where \(B_1,\ B_2,\ ...,\ B_m\) represents the capacity of each plane.

You are required to send all groups to destination with the following conditions:

  1. Each plane can travel from the Source to Destination with only one group at a time such that capacity of a plane is enough to accommodate all people in that group.
  2. All people belonging to the same group travel together.
  3. Every plane can make multiple journeys between source and destination.
  4. It costs 1 unit of time to travel between source to destination and vice versa.

Note: Multiple planes can fly together and also it is not necessary for planes to end their journey at the source.

Determine the minimum time required to send all groups from the source to the destination. 

Input format

  1. The first line contains two integers \(N\) and \(M\).
  2. The next line contains \(N\) space-separated integers \(A_1,\ A_2,\ ...,\ A_N\).
  3. The next line contains \(M\) space-separated integers \(B_1,\ B_2,\ ...,\ B_m\).

Output format

Print a single integer denoting minimum time required to send all groups to the destination. If it is not possible to perform this operation, then print -1.

Constraints
\(1 \le N,\ M \le 1e5\\ 1 \le A_i,\ B_i \le 1e9\)
  

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
58 votes
Tags:
AlgorithmsEasyMonthly-ContestReady
Points:30
8 votes
Tags:
AlgorithmsBinary SearchDepth First SearchDisjoint setEasyGraphsLife-cycle assessmentSearching
Points:30
29 votes
Tags:
Easy