CF1859E.Maximum Monogonosity

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

Recall that two segments [l1,r1][l_1, r_1] , 1l1r1n1 \le l_1 \le r_1 \le n , and [l2,r2][l_2, r_2] , 1l2r2n1 \le l_2 \le r_2 \le n , are non-intersecting if one of the following conditions is satisfied: r1<l2r_1 < l_2 or r2<l1r_2 < l_1 .

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

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

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1t1000)(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 nn and kk ( 1kn30001 \le k \le n \le 3000 ) — the length of array aa and the total length of segments.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 109ai109-10^9 \le a_i \le 10^9 ) — the elements of array aa .

The third line of each test case contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n ( 109bi109-10^9 \le b_i \le 10^9 ) — the elements of array bb .

It is guaranteed that the sum of nn over all test case does not exceed 30003000 .

输出格式

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

说明/提示

In the first test case, the cost of any segment is 00 , so the total cost is 00 .

In the second test case, we can take the segment [1,1][1, 1] with a cost of 88 and the segment [3,3][3, 3] with a cost of 22 to get a total sum of 1010 . It can be shown that this is the optimal solution.

In the third test case, we are only interested in segments of length 11 , and the cost of any such segment is 00 .

In the fourth test case, it can be shown that the optimal sum is 1616 . For example, we can take the segments [3,3][3, 3] and [4,4][4, 4] .

首页