Gross LCS
Time limit10sMemory limit16 MB
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.
denotes the length of the longest common subsequence of integer sequences and .
For an integer , is the sequence , obtained by adding to every element of .
You are given two integer sequences and . Find the sum of over all integers from to .
Input
The first line contains two integers and ().
The second line contains integers ().
The third line contains integers ().
Output
Print the sum of over all integers from to .
Hint
A sequence is a subsequence of if can be obtained from by deleting several elements, possibly zero or all of them. The longest common subsequence of and is the longest sequence that is a subsequence of both.