K-Removal
Practice
0 (0 votes)
Dynamic programming
Algorithms
3 dimensional
Medium
Problem
38% Success 2353 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Teacher assigns a programming assignment to all the students in their computer programming lab. One of the students is your friend and he asks you to help him solve the problem.

Teacher gave students an Array \(A\) of size \(N\) and an integer \(K\). Now the students have to remove exactly \(K\) elements from the array such that the array obtained after removing exactly \(K\) elements have \(\sum_{i=1}^{i=L-1}abs(a[i]-a[i+1])\) as maximum.

Here \(L\) is the length of the array after removing exactly \(K\) elements. So if the initial array size is \(N\) and \(K\) elements are removed from it then \(L=N-K.\)

Suppose our original array \(A\) is \([1,2,3,4]\) and we remove \(2^{nd}\) and \(4^{th}\) element from the array then length of the new array formed is \(2\) and the array is \([1,3]\).

Now the student's task is to output the maximum value of  \(\sum_{i=1}^{i=L-1}abs(a[i]-a[i+1])\).

Input
First line contains 2 integers \(N\) and \(K\) denoting the size of the array and the elements to be removed respectively.

Next line contains \(N\) space separated integers denoting the array \(A\).

Output
Output the required answer.

Constraints

\(1 \leq N \leq {10^4}\)

\(1 \leq K \leq min(N,20)\)

\(1 \leq A[i] \leq {10^5}\)

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
1 votes
Tags:
AlgorithmsMedium
Points:30
Tags:
Dynamic ProgrammingAlgorithms3 DimensionalMedium
Points:30
Tags:
Medium