Editorial
For subtask , let be the length of a longest increasing subsequence ending at position . Check every with and add to the largest such . If no such exists, set . Store the preceding index as well to reconstruct a sequence. This takes time.
For the full problem, maintain the smallest possible final value of an increasing subsequence for each length. When processing , find the first stored value at least by binary search and update it. Equal values cannot extend a strictly increasing subsequence, so the search must find the first value greater than or equal to , not the first strictly greater value.
Along with each final value, store the index of the element that produced it. When updates the state for length , record the final index for length as its predecessor. Follow these predecessor indices from the final index of the longest length, then reverse the collected elements.
The time complexity is and the space complexity is per case.
Solution written by GPT6