A plane journey

3.6

54 votes
Algorithms, Binary Search, C++
Problem

A flight company has to schedule a journey of N groups of people from the same source to the same destination. Here, A1, A2, ..., AN represents the number of people in each group. All groups are present at the source. The flight company has M planes where B1, B2, ..., Bm 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 A1, A2, ..., AN.
  3. The next line contains M space-separated integers B1, B2, ..., Bm.

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
1N, M1e51Ai, Bi1e9
  

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In T = 3.

Every plane can make 2 trips.

Plane with capacity 6 , send one group with capacity 6 and other with capacity 2.
Other two planes can send each group in single trip.

It is not possible to send groups in time unit less than 3 

Editor Image

?