#C9637. Maximum Packages Delivered
Maximum Packages Delivered
Maximum Packages Delivered
You are given (m) teams, each with a specific capacity, and (k) packages, each with an associated weight. A team can deliver several packages provided that the total weight of the packages does not exceed its capacity. The goal is to determine the maximum number of packages that can be delivered by optimally assigning packages to teams.
A typical strategy is to sort the teams by their capacities in descending order and the packages by their weights in ascending order. Then, for each team, repeatedly select the smallest available package that fits within the remaining capacity of the team. This greedy method ensures that as many packages as possible are assigned.
Formally, if the capacities are (c_1, c_2, \dots, c_m) and the package weights are (w_1, w_2, \dots, w_k), you aim to maximize the number of selected packages such that for each team, the sum of the weights of the packages delivered to that team does not exceed its capacity.
inputFormat
Input is read from standard input (stdin) with the following format:
(m)
(c_1) (c_2) ... (c_m)
(k)
(w_1) (w_2) ... (w_k)
If (m = 0) or (k = 0), the corresponding list is empty.
outputFormat
Output to standard output (stdout) a single integer representing the maximum number of packages that can be delivered.## sample
4
10 25 15 20
5
8 5 12 18 10
4