Sequence differences
Limits: 3 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Given two sequences of integers \(a, b\) of lengths \(n\) and \(m\) respectively. The elements of both sequences are 0-indexed.
For starting indices \(i, j\) starting from zero and a sequence length \(k \ge 1\) such that \(0 \leq i < n, 0 \leq j < m, 0 \leq i + k - 1 < n, 0 \leq j + k - 1 < m\), let us call the difference of the sequences the value \(S_{ijk} = |a_i - b_j| + |a_{i+1} - b_{j+1}| + ... + |a_{i+k-1} - b_{j+k-1}|\). For all possible valid \(i, j, k\), find the maximum possible value of \(S_{ijk}\).
Input
The first line contains two numbers \(n\) and \(m\) — the lengths of the sequences.
The second line contains \(n\) numbers \(a_i\).
The third line contains \(m\) numbers \(b_j\).
Output
Output a single number — the maximum possible value of \(S_{ijk}\).
Constraints
\(1 \leq n, m \leq 2000\),
\(-10^5 \leq a_i, b_j \leq 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 2 4 7 11 3 6 | 9 |
| Input (stdin) | Output (stdout) |
|---|---|
| 1 1 150 -150 | 300 |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 2 4 4 7 4 | 3 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|