题库练习 Maximum Monogonosity
← 上一题 下一题 →

A16111 | Maximum Monogonosity

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

You are given an array $a$ of length $n$ and an array $b$ of length $n$ . The cost of a segment $[l, r]$ , $1 \le l \le r \le n$ , is defined as $|b_l - a_r| + |b_r - a_l|$ .

Recall that two segments $[l_1, r_1]$ , $1 \le l_1 \le r_1 \le n$ , and $[l_2, r_2]$ , $1 \le l_2 \le r_2 \le n$ , are non-intersecting if one of the following conditions is satisfied: $r_1 < l_2$ or $r_2 < l_1$ .

The length of a segment $[l, r]$ , $1 \le l \le r \le n$ , is defined as $r - l + 1$ .

Find the maximum possible sum of costs of non-intersecting segments $[l_j, r_j]$ , $1 \le l_j \le r_j \le n$ , whose total length is equal to $k$ .

输入格式

Each test consists of multiple test cases. The first line contains a single integer $t$ $(1 \le t \le 1000)$ — the number of sets of input data. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ( $1 \le k \le n \le 3000$ ) — the length of array $a$ and the total length of segments.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-10^9 \le a_i \le 10^9$ ) — the elements of array $a$ .

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ( $-10^9 \le b_i \le 10^9$ ) — the elements of array $b$ .

It is guaranteed that the sum of $n$ over all test case does not exceed $3000$ .

输出格式

For each test case, output a single number — the maximum possible sum of costs of such segments.

输入输出样例

输入 #1
5
4 4
1 1 1 1
1 1 1 1
3 2
1 3 2
5 2 3
5 1
1 2 3 4 5
1 2 3 4 5
7 2
1 3 7 6 4 7 2
1 5 3 2 7 4 5
4 2
17 3 5 8
16 2 5 9
输出 #1
0
10
0
16
28
C++ 编辑器
输入
输出