Given an array, A, having N integers, an increasing subsequence of this array of length k is a set of k indices \(i_1, i_2, ... i_k\), such that \(1 \le i_1 \lt i_2 \lt ... i_k \le N\) and \(A[i_1] \lt A[i_2] \lt A[i_3] \lt ... \lt A[i_k]\).
Energy of a subsequence is defined as sum of difference of consecutive numbers in the subsequence. For a given integer k, you need to find out the maximum value of energy among energies of all increasing subsequences of length k.
Hint: Among all increasing sequences of length k ending at an index, find the sequence with minimum value of starting point
Input:
First line consists of two space separated integer denoting N and k.
Second line consists of N space separated integers denoting the array A.
Output:
Print the maximum value of energy among energies of all increasing subsequences of length k.
If there are no increasing subsequences of length k in the array, print "-1" (without quotes")
Input Constraints:
\(1 \le k \times N \le 10^6\)
\(2 \le k \le N\)
\(1 \le A[i] \le 10^9\)
All increasing subsequences of length 2 and their respective energies are given below:
1 2, energy = 1
1 3, energy = 2
1 4, energy = 3
2 3, energy = 1
2 4, energy = 2
3 4, energy = 1
So clearly the maximum value of energy among all energies of all increasing subsequences of length 2 is 3.
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
Login to unlock the editorial
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