#P8776. Longest Non-decreasing Subsequence After Modification
Longest Non-decreasing Subsequence After Modification
Longest Non-decreasing Subsequence After Modification
Given an integer sequence \(A_1, A_2, \cdots, A_N\) of length \(N\), you are allowed one modification: you can change a contiguous block of exactly \(K\) numbers into an arbitrary constant value (all changed values are equal to some \(X\), which can be chosen arbitrarily). After the modification the sequence becomes \(A'_1, A'_2, \cdots, A'_N\). Your task is to determine the maximum possible length of a longest non-decreasing subsequence of the modified sequence.
A non-decreasing subsequence is a sequence \(a_{i_1}, a_{i_2}, \ldots, a_{i_m}\) with \(1 \le i_1 < i_2 < \cdots < i_m \le N\) such that \[ a_{i_1} \le a_{i_2} \le \cdots \le a_{i_m}. \]
You must choose the block (its starting position) and the value \(X\) optimally in order to maximize the length of the longest non-decreasing subsequence in the final sequence. Print this maximum length.
inputFormat
The first line contains two space‐separated integers \(N\) and \(K\) \( (1 \le K \le N)\).
The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) separated by spaces.
outputFormat
Output a single integer, the maximum length of a longest non-decreasing subsequence after performing the modification.
sample
5 2
1 3 2 4 5
5
</p>