#C820. Storing Products in Racks

    ID: 52156 Type: Default 1000ms 256MiB

Storing Products in Racks

Storing Products in Racks

You are given (n) products with given weights and (m) racks with specified capacities. The task is to determine whether it is possible to store all products in the racks without exceeding any rack's capacity.

Formally, let the products have weights (w_1, w_2, \dots, w_n) and the racks have capacities (c_1, c_2, \dots, c_m). You need to decide if there exists an assignment of each product to a rack such that after placing the product, the sum of weights on that rack does not exceed its capacity. A greedy strategy by sorting the products and racks in descending order can be applied to solve this problem efficiently.

inputFormat

The input is read from standard input (stdin) and has the following format:

(n): An integer representing the number of products.
Next line: (n) space-separated integers representing the weights of the products.
(m): An integer representing the number of racks.
Next line: (m) space-separated integers representing the capacities of the racks.

outputFormat

Output a single line to standard output (stdout) containing either (YES) if it is possible to store all products without exceeding any rack's capacity, or (NO) otherwise.## sample

4
2 3 5 8
3
10 5 4
YES

</p>