Exhibition
Time limit1sMemory limit512 MB
Choose the largest set of pictures and assign each to a distinct frame so that the sequence is nondecreasing in both assigned frame size and picture value.
- Level
Medium7 of 10
- Topics
- Sorting, Greedy, Binary search, Dynamic programming
- Solved
- No attempts yet
Problem
You are going to hold an exhibition of pictures. In the exhibition, you will put some pictures into frames and exhibit them, lining them up in a row.
There are N candidate pictures for the exhibition, numbered from 1 through N. The picture i (1 ≤ i ≤ N) has size Si and value Vi.
Also, there are M frames for the pictures, numbered from 1 through M. The frame j (1 ≤ j ≤ M) has size Cj. Only pictures of size at most Cj can be put into the frame j. At most one picture can be put into a frame.
Every picture to be exhibited must be put into a frame. For the sake of appearance, they must satisfy the following conditions:
- For any neighboring two pictures, the size of the frame containing the right picture must be at least the size of the frame containing the left picture.
- For any neighboring two pictures, the value of the right picture must be at least the value of the left picture.
You want to exhibit as many pictures as possible.
Write a program which, given the number of pictures, the number of frames, and their sizes and values, calculates the maximum number of pictures to be exhibited.
Input
Read the following data from the standard input.
N M
S1 V1
.
.
.
SN VN
C1
.
.
.
CM
Output
Write one line to the standard output. The output should contain the maximum number of pictures to be exhibited.
Constraints
- 1 ≤ N ≤ 100 000.
- 1 ≤ M ≤ 100 000.
- 1 ≤ Si ≤ 1 000 000 000 (1 ≤ i ≤ N).
- 1 ≤ Vi ≤ 1 000 000 000 (1 ≤ i ≤ N).
- 1 ≤ Cj ≤ 1 000 000 000 (1 ≤ j ≤ M).