Note that the memory limit is unusually low.
Let LCS(A,B) denote the length of the longest common subsequence of integer sequences A=⟨a_1,a_2,…,a_n⟩ and B=⟨b_1,b_2,…,b_m⟩.
For an integer x, let A+x denote the sequence ⟨a_1+x,a_2+x,…,a_n+x⟩.
You are given two integer sequences A and B. Find the sum of LCS(A+x,B) over all integers x from −10100 to 10100.
The first line contains two integers n and m (1≤n,m≤4000).
The second line contains n integers a_1,a_2,…,a_n (−108≤a_i≤108).
The third line contains m integers b_1,b_2,…,b_m (−108≤b_i≤108).
Print the sum of LCS(A+x,B) over all integers x from −10100 to 10100.
An integer sequence P is a subsequence of an integer sequence Q if P can be obtained from Q by deletion of several (possibly zero or all) elements. The longest common subsequence of sequences A and B is the longest sequence C that is a subsequence of both A and B.
In the example test:
Therefore the answer is 1+3+2=6.