Why Did the Cow Cross the Road (Silver)
Farmer John's cows are trying to learn to cross the road effectively. Remembering the old "why did the chicken cross the road?" joke, they figure the chickens must be experts on crossing the road, and go off in search of chickens to help them.
As it turns out, chickens are very busy creatures and have limited time to help the cows. There are c chickens on the farm, conveniently numbered 1 .. c, and each chicken i is only willing to help a cow at precisely time t[i]
. The cows, never in a hurry, have more flexibility in their schedules. There are n cows on the farm, conveniently numbered 1 .. n, where cow j is able to cross the road between time A[j]
and time B[j]
. Figuring the "buddy system" is the best way to proceed, each cow j would ideally like to find a chicken i to help her cross the road; in order for their schedules to be compatible, i and j must satisfy A[j]
≤ t[i]
≤ B[j]
.
If each cow can be paired with at most one chicken and each chicken with at most one cow, please help compute the maximum number of cow-chicken pairs that can be constructed.
Input
The first line contains c (1 ≤ c ≤ 20000) and n (1 ≤ n ≤ 20000). The next c lines contain t[1]
.. t[c]
, and the next n lines contain A[j]
and B[j]
(A[j]
≤ B[j]
) for j = 1 .. n. The A's, B's and t's are all non-negative integers (not necessarily distinct) of size at most 10^9
.
Output
Please compute the maximum possible number of cow-chicken pairs.