#C6277. Marble Distribution

    ID: 50019 Type: Default 1000ms 256MiB

Marble Distribution

Marble Distribution

You are given a set of containers and a set of marbles. Each container has a capacity and each marble has a weight. The task is to determine whether it is possible to distribute all the marbles into the containers without exceeding the capacity of any container.

More formally, you are provided with an integer ( n ) representing the number of containers, and a list of ( n ) integers representing their respective capacities. You are also given an integer ( m ) representing the number of marbles, and a list of ( m ) integers representing the weights of the marbles. Determine if there exists a way to assign each marble to a container such that the sum of weights assigned to any container does not exceed its capacity.

A greedy strategy is suggested: sort the containers and the marbles in descending order, then try to fit each marble into the first container that can accommodate it. If every marble can be placed, output "YES"; otherwise, output "NO".

inputFormat

Input is read from standard input (stdin) and consists of four lines.

The first line contains an integer ( n ), representing the number of containers.
The second line contains ( n ) space-separated integers representing the capacities of the containers.
The third line contains an integer ( m ), representing the number of marbles.
The fourth line contains ( m ) space-separated integers representing the weights of the marbles.

outputFormat

Output to standard output (stdout) a single line containing either "YES" or "NO" based on whether it is possible to distribute all the marbles without exceeding any container's capacity.## sample

3
10 5 8
4
3 5 3 2
YES