This page is still under construction.

Parts of this page are still being built. What you see may change.

Longest Common Increasing Subsequence

Interview

Time limit1sMemory limit128 MB

Summary
Find the length of the longest strictly increasing sequence that is a subsequence of both given sequences.
Level

Medium6 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

You are given two sequences of positive integers, AA and BB. A common increasing subsequence is a sequence that appears as a subsequence of both AA and BB and is strictly increasing, meaning every element is greater than the one before it. (The elements of a subsequence need not be adjacent in the original sequence.) Find the length of the longest common increasing subsequence of AA and BB.

Input

The first line contains two integers nn and mm (1≤n,m≤20001 \le n, m \le 2000), the lengths of AA and BB. The second line contains the nn elements of AA, and the third line contains the mm elements of BB, separated by single spaces. Every element is a positive integer not exceeding 10910^9.

Output

Print a single integer: the length of the longest common increasing subsequence of AA and BB. If AA and BB share no common element, print 00.

Examples1

  1. Example 1

    Input
    9 9
    2 3 1 4 2 1 3 5 4
    1 3 2 1 4 2 5 3 4
    
    Expected output
    4