Question
Maximize Student Selection

In a school playground, two lines of students are standing side by side - one line is made up of boys and the other of girls. Your task is to select some boys (or none) and some girls (or none) from these lines and arrange them in such a way that their heights form a strictly increasing order.
However, there are special rules you must follow:

  • No boy can stand in front of another boy who was originally ahead of him in the line.
  • No girl can stand in front of another girl who was originally ahead of her in the line.
The challenge is to select the maximum number of students you can while maintaining the original relative order of the boys and girls in their respective lines. How many students can you arrange while following these rules?
Input
The first line of the input contains two space-separated integers N (size of boys line) and M (size of girls line).
Second-line contains the heights of the boys in the order they are standing, N space-separated integers
Third-line contains the heights of the girls in the order they are standing, M space-separated integers

Constraints
1 ≤ N ≤ 100
1 ≤ M ≤ 100
1 ≤ Arr[i] ≤ 1000000000
Output
Print a single integer, the maximum number of students you can select.
Example
Sample Input
5 3
2 3 14 15 16
13 13 13
Sample Output
6
Explanation
Elements selected from array 1 : 2 3 14 15 16
Elements selected from array 2: 13
Final series: 2 3 13 14 15 16

Online