#K96217. Optimal Cart Usage
Optimal Cart Usage
Optimal Cart Usage
You are given two sequences:
- A list of n integers representing the maximum capacities of carts.
- A list of m integers representing the weights of materials.
The task is to determine the maximum number of carts that can be used such that each selected cart carries one material whose weight does not exceed the cart’s capacity. Formally, for each matched pair, if w is the weight of the material and c is the capacity of the cart, it must satisfy: \(w \leq c\). Each cart and each material can be used at most once.
Note: It is optimal to try to match the heaviest available material with the cart having the highest capacity (i.e. use a greedy strategy after sorting both lists in descending order).
inputFormat
The first line contains two integers n and m representing the number of carts and the number of materials respectively.
The second line contains n integers denoting the capacities of the carts.
The third line contains m integers representing the weights of the materials.
outputFormat
Output a single integer, the maximum number of carts that can be used to transport the materials under the given conditions.
## sample5 6
10 15 20 25 30
5 10 15 20 25 30
5