Modified subarrays

5

1 votes
Medium
Problem

Given an Array of N elements also the array is divided into K subarrays.

The ending indices of each subarray will be given as input. Now we will choose one element from each subarray and form a sequence of k elements with those elements in the order in which they are chosen. i.e the element from first subarray will be at first position and that from second subarray will be at second position and so on.

Now we need to form such a sequence such that the sum of the absolute differences of consecutive elements of sequence is Maximum. Output the Maximum possible sum.

Input:

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains two space separated integers N and K

The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array A

The Third line contains K space-separated integers B1, B2, ..., BK denoting the ending indices of the subarrays.The ending index of last subarray will always be N.

Output:

For each test case, print the maximum possible Sum.

Constraints:

1 ≤ T ≤ 10

1 ≤ N ≤ 105

1 ≤ K ≤ 104 and K ≤N

1 ≤ A[i] ≤ 109

Note The ith subarray starts at (B[i-1]+1) and ends at B[i] and first subarray starts from index 1.

Time Limit: 0.5
Memory Limit: 256
Source Limit:
Explanation

The first subarray consists of {1,2,3};

second subarray is {7,8,9};

and third subarray is {2,3};

if we make sequence as {1,9,2};

than sum=|1-9|+|9-2|=15;

Editor Image

?