This page is still under construction.

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

Gross LCS

Time limit10sMemory limit16 MB

Summary
Given sequences A and B, find the sum of LCS(A + x, B) over every integer shift x, where x is added to every element of A.
Level

Hard8 of 10

Topics
Dynamic programming, Hash map, Sorting
Solved
No attempts yet

Problem

The memory limit for this problem is unusually low.

LCS⁡(A,B)\operatorname{LCS}(A, B) denotes the length of the longest common subsequence of integer sequences A=⟨a_1,a_2,…,a_n⟩A = \langle a\_1, a\_2, \ldots, a\_n \rangle and B=⟨b_1,b_2,…,b_m⟩B = \langle b\_1, b\_2, \ldots, b\_m \rangle.

For an integer xx, A+xA + x is the sequence ⟨a_1+x,a_2+x,…,a_n+x⟩\langle a\_1 + x, a\_2 + x, \ldots, a\_n + x \rangle, obtained by adding xx to every element of AA.

You are given two integer sequences AA and BB. Find the sum of LCS⁡(A+x,B)\operatorname{LCS}(A + x, B) over all integers xx from −10100-10^{100} to 1010010^{100}.

Input

The first line contains two integers nn and mm (1≤n,m≤40001 \le n, m \le 4000).

The second line contains nn integers a_1,…,a_na\_1, \ldots, a\_n (−108≤a_i≤108-10^8 \le a\_i \le 10^8).

The third line contains mm integers b_1,…,b_mb\_1, \ldots, b\_m (−108≤b_i≤108-10^8 \le b\_i \le 10^8).

Output

Print the sum of LCS⁡(A+x,B)\operatorname{LCS}(A + x, B) over all integers xx from −10100-10^{100} to 1010010^{100}.

Hint

A sequence PP is a subsequence of QQ if PP can be obtained from QQ by deleting several elements, possibly zero or all of them. The longest common subsequence of AA and BB is the longest sequence CC that is a subsequence of both.

Examples1

  1. Example 1

    Input
    3 4
    5 5 8
    3 6 3 6
    
    Expected output
    6