Modified subarrays
Practice
5 (1 votes)
Medium
Problem
48% Success 125 Attempts 30 Points 0.5s Time Limit 256MB Memory 1024 KB Max Code

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 ≤ \(10^{5}\)

1 ≤ K ≤ \(10^{4}\) and K ≤N

1 ≤ A[i] ≤ \(10^{9}\)

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

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
Dynamic ProgrammingAlgorithmsApplications of Dynamic Programming
Points:30
6 votes
Tags:
AlgorithmsApprovedMediumDynamic programming
Points:30
2 votes
Tags:
AlgorithmsMediumDynamic programming