Increasing Subsequence
Practice
4 (5 votes)
Advanced data structures
Data structures
Fenwick tree
Medium
Problem
69% Success 833 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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\)

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
17 votes
Tags:
ApprovedBit ManipulationException handlingMediumReady
Points:30
19 votes
Tags:
ApprovedDepth First SearchGraphsMediumReadySegment TreesTrees
Points:30
25 votes
Tags:
Advanced Data StructuresData StructuresFenwick Tree