Farmer John is arranging his N cows in a line to take a photo (1≤N≤50). The height of the i-th cow in the line is a(i). Farmer John thinks the photo will look good if the lineup has a long increasing subsequence of cows by height.
A subsequence is a selection of elements a(i1),a(i2),…,a(ik) at indices i1<i2<⋯<ik. The subsequence is increasing if a(i1)≤a(i2)≤⋯≤a(ik).
To make the increasing subsequence long, Farmer John may first choose any one subsequence and reverse the order of its elements.
For example, take the list
1 6 2 3 4 3 5 3 4
Reversing the marked elements
1 6 2 3 4 3 5 3 4
^ ^ ^ ^
gives
1 4 2 3 4 3 3 5 6
^ ^ ^ ^
The reversed subsequence keeps the indices it originally occupied, and every other element stays unchanged.
Find the maximum possible length of an increasing subsequence, given that you may reverse one arbitrary subsequence once.