We need to solve problem "C2. Increasing Subsequence (hard version)" which is essentially: given an array a of n integers (not necessarily distinct). We can repeatedly take either leftmost or rightmost element, and append it to our output sequence. The output sequence must be strictly increasing. We want to maximize its length. Output the maximum length and a string of moves ('L' for left, 'R' for right).
The easy version had distinct values, making it simpler. The hard version allows duplicates. But the constraints: n up to 2e5, a_i up to 2e5. So we need O(n) or O(n log n).
We need to pick a subsequence (by taking from ends) that is strictly increasing. This is similar to building an increasing sequence from the two ends, like in the classic "Double Ended Queue" problem where we choose the smaller element that is larger than the last taken. But with duplicates, we have to be careful about ties. The typical solution for distinct values: maintain two pointers left=0, right=n-1, and a variable last = -inf (or 0). While left <= right, compare a[left] and a[right]. If both > last, choose the smaller one (to leave more options). If only one > last, choose that. Else break.
But with duplicates, this greedy might fail? Let's test scenarios. Consider array: [2, 2, 1, 3, 4]. Actually distinct? Not all distinct. Let's think: starting last = -inf. a[left]=2, a[right]=4. Both > last. Smaller is 2, so we take left (L). last=2. Now left=1, a[1]=2, right=4 (value 4). Both? a[left]=2 not > last (2>2? no, must be strictly greater). So only a[right]=4 >2. So we would take R, last=4. Now left=1, right=3, a[1]=2, a[3]=3? Actually after taking index4 (value 4), right becomes 3, value=3? Wait original: indices 0:2,1:2,2:1,3:3,4:4. After first L, left=1, right=4. Then we took R: right becomes 3, value=3? Actually we took a[4]=4, right becomes 3. So now left=1 (value2), right=3 (value3). last=4. a[left]=2<4, a[right]=3<4, so break. Total length 2. But maybe we could do better: Take left first? Actually we took left, got 2. Could we have taken right first? If we take right first (R): last=4, left=0, right=3 (value3). Then both <4, break, length 1. So no. But perhaps we could take left then left? After first L, last=2, left=1 (value2) not >2, so can't. So max is 2. Could we get 3? Sequence: 2,3,4? But after 2 from left, we need 3, but we can't get 3 without removing the second 2? Actually if we take left again, we get 2 which is not >2. If we take right, we get 4, skipping 3. Could we take right first? 4, then left? left then? left=0 (2) not >4. So not. So 2 is max. But maybe there is a better sequence: 1,3,4? To get 1, we need to take from somewhere. The array is [2,2,1,3,4]. Initially we could take right? Not 1. To get 1, we need to take left twice? Actually leftmost is 2, rightmost is 4. 1 is inside. To get 1 we must remove either left or right until 1 becomes an end. For example: take left (L) -> 2 removed, array becomes [2,1,3,4]. Now ends: 2 and 4. To get 1, we need to remove the leftmost 2 as well. So take L again (but 2 is not > last? last=2 after first? Actually if we want 1 as second element, we need last < 1. But last after first is 2, so 1 is not >2. So cannot take 1 after 2. So we cannot have 2 then 1 because must be increasing. So maybe start with 1? To get 1, we must first remove ends until 1 becomes an end. Initially ends are 2 and 4. To make 1 an end, we could take right repeatedly? Taking R: remove 4 -> array [2,2,1,3]; ends 2 and 3. Take R: remove 3 -> [2,2,1]; ends 2 and 1. Now we can take 1 from right because last = -inf? Actually if we haven't taken any yet, we can take either end. To get 1 as first element, we could take R (value1). So moves: R, R, R? Actually we need to do moves that pick ends, but we only count moves when we take element into our sequence; but we can also skip elements? Wait the process: we must take either leftmost or rightmost on each move, and we write it down if we want? Actually the problem says: "During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence." So we cannot skip writing down; every removal adds to the output sequence. So we cannot just remove elements without taking them into the output. So if we want to get to 1, we have to take those preceding elements into our sequence, and they must be part of the strictly increasing sequence. So we cannot discard elements; each removal is part of the final sequence. So the sequence is exactly the order of removals. Thus we cannot "remove" elements without them being considered. So we must construct an increasing sequence directly by picking ends.
Thus the classic greedy for distinct values might still work for duplicates? But there is nuance when a[left] == a[right] and both > last. In distinct version, they are not equal, so there is a clear smaller. In duplicates, they can be equal. Then we need to decide which side to take. The choice may affect future possibilities. The typical solution for the easy version is to take the smaller, but if equal, we need to look ahead to decide which side leads to a longer sequence. For distinct values, equal cannot happen. So we need to handle equality.
The problem is known: Codeforces problem 1157C2 - "Increasing Subsequence (hard version)". I recall the solution: we use two pointers and while left <= right, we compare a[left] and a[right] to last. If one is > last, we consider it. If both > last, then if a[left] < a[right], choose left. If a[left] > a[right], choose right. If a[left] == a[right], we need to look at the future: we need to compute how many consecutive increasing from that side? Actually we need to find the length of the longest strictly increasing sequence taking from that side only (like taking only leftwards or only rightwards) starting from that element, with values strictly increasing. Because if we pick the left side, we are committing to take from left; after that, we might continue taking from left only if it yields a longer chain? But not exactly; we can still switch sides later. However, when the two ends are equal and both > last, picking either one is possible. But we need to decide which leads to a longer overall sequence. In such cases, we can simulate taking all from the left side as long as it's strictly increasing (without considering the right side) and similarly for the right side. Then we compare the lengths of those "runs". Actually we can compute: for the left side, starting from current left pointer, how many elements can we take in a strictly increasing sequence if we only take from the left (i.e., moving left pointer forward) given that we must start from that value and each next must be greater than previous? But note that after taking the left element, the next element from left will be a[left+1]. For the left run, we need a strictly increasing sequence of consecutive elements from left to right? Actually it's like: if we decide to take the left element, we could later either take from left or right. But if the two ends are equal, the optimal strategy might be to commit to taking all from one side until the condition breaks? But we need to verify.
Consider an example: a = [1, 2, 1, 2, 1]. Let's try to find optimal increasing sequence. The array: indices: 0:1,1:2,2:1,3:2,4:1. We can pick ends only. Let's think manually. Starting last = 0 (or -inf). Ends: left=1, right=1. Both equal. If we take left (L): we get 1, last=1, array becomes [2,1,2,1]. Ends: left=2, right=1. Only left > last (2>1) and right=1 not >1, so we must take left. So we take L: 2, last=2, array becomes [1,2,1]. Ends: left=1, right=1, both <=2, so stop. Total length 2. If we take right first (R): we get 1, last=1, array becomes [1,2,1,2]? Actually removing rightmost (index4 value1) leaves [1,2,1,2] indices 0-3: 1,2,1,2. Ends: left=1, right=2. left=1 not >1, right=2 >1, so take right: 2, last=2, array becomes [1,2,1] after removing rightmost (index3 value2) leaves [1,2,1]. Ends: left=1, right=1, both <=2, stop. Length also 2. So both give 2.
Now consider: a = [2, 3, 2, 3, 4] maybe? Let's try to find a case where greedy by comparing runs works. Known solution for this problem: compute two pointers and handle equality by comparing the "increasing run" from each side. Actually I recall a Codeforces editorial for problem 1157C2: "We can simulate the process. At each step, we have last value and two pointers. If a[l] and a[r] are both > last, we choose the smaller one if they are different. If they are equal, we look at the length of the increasing sequence we can get by taking from left only and from right only, and choose the side with longer run." Something like that.
Let's search memory: There's a known problem "Increasing Subsequence (hard version)" from Round #555 Div.3. Editorial: They compute arrays incL and incR? Actually they compute for each position i, the length of increasing consecutive sequence from i moving inward? Hmm.
Better to think: When we have a[l] == a[r] > last, picking either side will give us that value. After picking, the last becomes that value. The rest of the array is the subarray from l+1 to r if we pick left, or l to r-1 if we pick right. Since the ends are equal, the future decisions may differ. We need to decide which choice yields a longer overall sequence. Since n is up to 2e5, we need O(n). We could try both? But that would be exponential. However, we can precompute something: For any position i, what is the length of the longest strictly increasing contiguous sequence starting from i moving rightwards? But that's not exactly because we can also take from the other side later. However, after picking one side, the next choices will again be based on the two ends. The only tricky case is when ends are equal. In many greedy solutions, they resolve equality by looking at the "run" of strictly increasing numbers from each end inward. That is, from the left pointer, moving to the right, count how many consecutive numbers are strictly increasing (i.e., a[l] < a[l+1] < a[l+2] < ...). Similarly from the right pointer, moving leftwards, count how many consecutive numbers are strictly increasing (a[r] > a[r-1] > a[r-2]? Actually strictly increasing when moving inward from right: we need to consider the order of taking from the right side only: if we keep taking from the right, we would take a[r], then a[r-1], then a[r-2], etc. But the sequence taken would be a[r], a[r-1], a[r-2], ... but it must be strictly increasing. Since we are taking from the rightmost each time, the next rightmost becomes the previous element (r-1). To have increasing sequence, we need a[r] < a[r-1] < a[r-2] < ... Actually if we take from the right repeatedly, we are taking elements in order: first a[r], then after removal, the new rightmost is a[r-1], so we take a[r-1] next, then a[r-2], etc. The sequence of taken values would be a[r], a[r-1], a[r-2], ... So we need these to be strictly increasing: a[r] < a[r-1] < a[r-2] < ...? That is decreasing from left to right but we need increasing over time, so we need that as we go inward, values are larger. So it's a strictly decreasing sequence in the array (from right to left) to be increasing over time. More clearly: if we only take from the right, the taken sequence will be the reverse of the array's order from right to left. So for that sequence to be strictly increasing, we need the array values from right to left to be strictly decreasing (so that when reversed, they are increasing). Equivalent: moving leftwards from r, we need a[r] < a[r-1] < a[r-2] < ...? Actually check: Suppose array: [1,2,3] - if we take right repeatedly: take 3 (last=3), then array becomes [1,2]; take 2 (2 >3? no, 2<3, not increasing). So not increasing. For increasing, we need larger values each time. If we start with a small and then larger, then the order from right to left should be increasing as we move leftwards (so that when we take rightmost, it's the smallest, then next is larger). Example: [3,2,1] -> rightmost 1, then 2, then 3 gives increasing: 1,2,3. That works: array values from right to left: 1,2,3 (increasing). So the condition for a right-only run to be strictly increasing is that the sequence from right to left (i.e., a[r], a[r-1], a[r-2], ...) is strictly increasing. Which means as we move leftwards, values increase. So it's a strictly increasing sequence when traversing from right to left. Similarly, for a left-only run, we take a[l], a[l+1], a[l+2], ...; for this to be strictly increasing, we need a[l] < a[l+1] < a[l+2] < ... (natural increasing order from left to right). So indeed, if we commit to taking only from one side, the maximum number we can take from that side is the length of the longest increasing contiguous subsequence from that side inward (starting from the end) until it breaks.
Now, in the situation where a[l] == a[r] > last, we may not necessarily commit to taking only from one side; we might later switch. But often the optimal strategy in such a tie is to choose the side that gives a longer "run" of increasing values from that side because after picking that side, if the next element from the same side is also greater than the current value, we might continue; but if not, we could still take from the other side. However, if both sides have equal current values, it's often beneficial to take the side that allows more future moves. But we need to prove that simply comparing the lengths of the strictly increasing runs from each side (starting at l and moving right for left run, and starting at r and moving left for right run) and picking the larger is correct.
I think that is the standard solution. I recall reading editorial: For the hard version, when a[l] and a[r] are both greater than last, if a[l] < a[r] -> take L; if a[l] > a[r] -> take R; if a[l] == a[r], then compute cntL = number of consecutive strictly increasing elements starting from l (i.e., while i+1 <= r and a[i] < a[i+1], count++) and cntR = number of consecutive strictly increasing elements starting from r moving leftwards (i.e., while j-1 >= l and a[j] < a[j-1]? Actually careful: For right side, we need that a[r] < a[r-1] < a[r-2] ... i.e., strictly increasing as we go leftwards. So cntR = number of elements from r down to l such that a[r] < a[r-1] < a[r-2] < ...; that is, while (j-1 >= l and a[j-1] > a[j])? Wait condition: For the sequence taken from right to be increasing, we need a[r] < a[r-1] < a[r-2] < ... So as we move leftwards, each next element must be greater than the previous (from right side). So if we define an array b where b[i] = a[i], then for the right run we need a[r] < a[r-1] < a[r-2] < ...; that means for indices: r, r-1, r-2,... each successive must be greater. So we can compute the length of the longest prefix of this monotonic chain. Equivalent to computing for each position the length of increasing sequence going leftwards. Then we compare cntL and cntR; if cntL > cntR, take L else take R. The editorial might have said: If a[l] == a[r], then we need to look ahead: count how many consecutive increasing steps we can take from left, and similarly from right. Choose the side with greater count.
Let's test with examples to see if this logic works. Simple example: a = [1, 3, 5, 6, 5, 4, 2] from sample 2. Let's compute. L=0, R=6, last=0. a[l]=1, a[r]=2, both > last, a[l] < a[r], so take L. Moves: L, last=1, l=1. Now a[l]=3, a[r]=2, last=1. Both > last? 3>1 yes, 2>1 yes. Compare: 3 vs 2, 3>2, so take R? Actually if a[l] > a[r], we take the smaller? Wait rule: if both > last, choose the smaller one? In distinct case we choose the smaller to keep options open. So if a[l] > a[r], we take R (since a[r] is smaller). If a[l] < a[r], take L. So here a[l]=3 > a[r]=2, so we take R. So R. last=2, r=5. Now a[l]=3, a[r]=4? Actually after r--, r=5 (value 4). Wait original: indices: 0:1,1:3,2:5,3:6,4:5,5:4,6:2. After first L: l=1 (3), r=6 (2). We took R: we took a[6]=2, so r becomes 5 (value 4). Now last=2, a[l]=3, a[r]=4. Both >2, a[l]=3 < a[r]=4, so take L. So L, last=3, l=2. Now a[l]=5, a[r]=4. Both >3? 5>3 yes, 4>3 yes. Compare: 5>4 so take R. Take R: value 4, last=4, r=4. Now a[l]=5, a[r]=5? index4 value5. Both >4? 5>4 yes, 5>4 yes. a[l]==a[r]==5. Now we need to decide tie. Compute cntL from l: from index2 to ... we have array: indices 2:5,3:6,4:5. left run: starting at index2, increasing consecutive? Check: a[2]=5, a[3]=6 >5, then a[4]=5? Actually after index3, a[4]=5 which is not >6, so break. So cntL = 2? Actually length of strictly increasing from l moving right while a[i] < a[i+1]: indices 2 (5) < 3 (6) true; then 3 (6) < 4 (5) false. So we can take at most 2 elements if we keep taking from left? But careful: We are at state l=2, r=4. The subarray is [5,6,5] (indices 2,3,4). If we take left (L), we take a[2]=5, then l becomes 3. Then we have subarray [6,5], ends: 6 and 5. last=5. Both? 6>5 yes, 5 not >5. So we would take 6 (L) because it's >5 and smaller? Actually 6 is only candidate. Then last=6, l=4. Then subarray [5], ends:5. 5>6? no. So stop. So total additional moves after picking left: we get 2 more (5 and 6) but we also took the initial left? Actually we already counted the move for picking left. After that we can get 2 moves? Let's simulate: after tie, if we pick left: we take 5 (move L). Then we have subarray [6,5], last=5. Now we can take left (6) because 6>5, and we take it (L). Then last=6, subarray [5], cannot take. So from the tie state we get 2 moves (the left pick plus one more). If we pick right: take 5 (R), r becomes 3, subarray [5,6] (indices 2,3). last=5. Ends: left=5, right=6. Both >5? 5 not >5, 6>5. So we must take right (6) (R). Then last=6, subarray [5], stop. So also 2 moves. So both give equal length. But sample output for sample2 is length 6 and moves "LRLRRR". Let's continue from tie: we need to decide. In sample output, after the previous steps (L R L R) we have taken 4 moves? Actually sample2: n=7, output length 6, moves LRLRRR. Let's simulate with sample's moves: a = [1,3,5,6,5,4,2]. Moves: L: take 1, array->[3,5,6,5,4,2]; last=1. R: take 2? Wait after L, rightmost is 2. So R: take 2, array->[3,5,6,5,4]; last=2. L: take 3, array->[5,6,5,4]; last=3. R: take 4, array->[5,6,5]; last=4. R: take 5? Actually next move in string is R? It says "LRLRRR": after L R L R, we have taken 4 moves, we have 2 more R? Let's see: Moves: index: 1 L, 2 R, 3 L, 4 R, 5 R, 6 R? Actually string length 6: positions: 1 L, 2 R, 3 L, 4 R, 5 R, 6 R. But we just did up to move4: after move4 we had array [5,6,5] and last=4? Wait step-by-step:
Start: [1,3,5,6,5,4,2]
1: L -> take 1, array [3,5,6,5,4,2], last=1
2: R -> take 2, array [3,5,6,5,4], last=2
3: L -> take 3, array [5,6,5,4], last=3
4: R -> take 4, array [5,6,5], last=4
Now array [5,6,5]. Ends: left=5, right=5.
Both > last=4. They are equal. According to sample, they take R (since next move is R). So move5: R -> take rightmost 5, array [5,6], last=5. Now array [5,6]. Ends: left=5, right=6. Both? 5 not >5 (since must be strictly greater), 6>5. So we take the only viable: R? Actually left=5 is not >5, so only right=6 works, which is R. So move6: R -> take 6, array [5], last=6. Then done. So total 6 moves. That's optimal.
Now, at tie (both 5), they chose R. Let's compute cntL and cntR at that state. l=0? Actually indices after step4: we have subarray [5,6,5] with positions relative: left index (0) =5, right index (2)=5. Compute cntL: from left: starting at 5, moving right: check if 5 < 6? yes. Then check if 6 < next? next is 5, not less. So cntL (including starting) length = 2? Actually if we count the number of elements we can take consecutively from left while strictly increasing: we can take left (5), then left+1 (6) because 5<6; then left+2 (5) not >6, so stop. So we can take 2 elements from left if we keep taking left. So cntL (run length) = 2. For right: starting at rightmost (5), moving leftwards: we need increasing sequence when taking from right. That means check if 5 < the element to its left (6)? Because if we take from right: first take 5, then next rightmost would be 6 (since after removal, the new rightmost is the previous element). For that to be increasing, we need 5 < 6. So yes. Then after taking 6, next rightmost would be 5 (the leftmost). But need 6 < 5? false. So we can take 2 elements from right as well. So cntL = cntR = 2. According to the rule "choose side with longer run", both equal, so either is fine. Sample chose R.
Thus rule seems to be: when equal, if cntL > cntR, choose L, else choose R (or if equal, any). So it's consistent.
Now test other cases to see if this rule always yields optimal. I think yes. Editorial probably states: "If a[l] == a[r], then we need to look further. Let cntL be the number of consecutive elements from l such that a[l] < a[l+1] < a[l+2] < ... (strictly increasing). Let cntR be the number of consecutive elements from r such that a[r] < a[r-1] < a[r-2] < ... (strictly increasing). Then if cntL > cntR, take L, else take R."
But is that always optimal? Let's think of scenario where equal but one side gives longer immediate increasing run but the other side might allow more flexibility later because we could switch sides. However, if both sides have equal current value, after we pick one side, the last value becomes that value. The other side's end might still have that same value (since they were equal). But if we pick left, we remove left element, so new left pointer becomes l+1. The right pointer stays at r. So the new ends are a[l+1] and a[r] (which was equal to old a[l]). Now a[l+1] may be >, =, or < last. The future may depend. The run length cntL essentially tells us how many consecutive increasing elements we can take from left if we keep taking from left. But could we do better by sometimes switching? Possibly if cntL is shorter but switching yields longer total? I'd like to test potential counterexamples.
Consider array: [2, 3, 1, 3, 2] maybe? Let's try to see. Write down indices: 0:2,1:3,2:1,3:3,4:2. We need to find optimal increasing sequence. We'll simulate greedy with run comparison.
Initialize last=0 (or -inf). l=0, r=4. a[l]=2, a[r]=2, both > last. They equal. Compute cntL: from l=0, increasing consecutive? Check: a[0]=2 < a[1]=3 -> yes. Then a[1]=3 < a[2]=1 -> no. So cntL = 2 (elements 2,3). cntR: from r=4 moving leftwards: a[4]=2 < a[3]=3? Need increasing from right: a[4] < a[3]? 2 < 3 yes. Then a[3]=3 < a[2]=1? no. So cntR = 2 as well. So tie. Both choices symmetric? If we take L: take 2, last=2, l=1, r=4, a[l]=3, a[r]=2. Now both > last? 3>2 yes, 2 not >2. So only left works. So take L: 3, last=3, l=2, r=4, a[l]=1, a[r]=2. Both? 1 not >3, 2 not >3. Stop. Total moves=2. If we take R first: take 2, last=2, r=3, l=0, a[l]=2, a[r]=3. Both? 2 not >2, 3>2 -> take R: 3, last=3, r=2, l=0, a[l]=2, a[r]=1? Actually after taking rightmost (index3=3), r becomes 2 (value1). Now ends: left=2, right=1. Both <=3, stop. Total moves=2. So same.
Now try to design a case where one side yields longer total than the other. The run lengths being compared might indicate which side gives more consecutive increasing picks if we stick to that side. But we might be able to switch later to get more than the run length? For instance, after picking left, we might later take from right if the right side offers a bigger value that fits. But the run length only counts consecutive increasing from that side; after we exhaust that side (i.e., the next element on that side is not greater), we could still consider the other side. However, the run length might be shorter than the total we could achieve by picking the other side and then maybe switching. Let's try to construct an example.
We need a[l] = a[r] = X. Suppose from left, the immediate next element is just slightly larger than X, then later it becomes complicated. From right, the immediate previous element is also larger, but then there might be opportunity to switch. The run length captures how many we can take if we stick to that side only. But the total we can achieve might be more than that run length because after taking some from that side, we could switch to the other side and get more. However, after we pick one side, the other side's end might become accessible. So we need to consider which initial pick yields a longer overall sequence, not just the run.
But I recall that the editorial for Codeforces Round 1157 (Div. 3) problem C2 indeed uses this method. Let me recall exactly: The solution for the easy version is greedy comparing ends. For the hard version, the only change is when both ends are greater than last and equal, we need to compare the "increasing sequences" from both ends. They compute L and R as the length of the longest increasing contiguous subarray starting at the left or right respectively. Then choose the larger. The proof: Because after picking one side, the only way to get more elements is to continue from that side until the increasing condition fails, because the other side's end is less than or equal to the current last (since we started with equal values). Actually consider: After we pick left (value X), the new last = X. The right end still has value X (since they were equal). Since we need strictly increasing, we cannot take the right end now because it's equal to last. So we are forced to take from the left again if possible (i.e., if a[l+1] > X). If we can take from left repeatedly, we will. Once we encounter a left element that is <= last, we might consider the right side, but by that time the right side's end may have changed (since we haven't touched right). But note: As we take from left, the left pointer moves right; the right pointer stays at original r. The rightmost element remains X until we take it or something else? Actually the rightmost element is still the same X we didn't take. But after we take some from left, last becomes some value Y > X presumably. At that point, the rightmost value is still X, which is < Y? Actually X < Y because Y > X. But is X > last? Since last = Y > X, X is less than Y, so it's not > last; it's smaller. So we cannot take X because it must be strictly greater than last. So the rightmost element X becomes useless as soon as we increase last beyond X. So effectively, after picking left, we cannot ever take the rightmost X because it's stuck as a small value. So we are forced to continue taking from left as long as we can. Similarly, if we pick right first, we'll be forced to continue taking from right as long as possible because the leftmost X becomes too small after we increase last. Therefore, the total number of moves we can make after picking a side is exactly the number of consecutive increasing elements we can take from that side starting from that end (including the first pick) because we cannot switch to the other side until the opposite end becomes something larger after we exhaust that side? But wait: Could there be a scenario where after taking some from left, the right end (which initially was X) becomes replaced by a new right end after we've taken some from left? No, the right end remains X as long as we don't take it. The array size shrinks only from the side we take. So if we only take from left, the right end stays fixed at the original rightmost element, which is X. So after first left pick, we cannot take the right end because it's equal to last? Actually last becomes X after first pick. So right end = X is not > X (needs >). So it's not available. Then we take left again only if a[l+1] > X. Suppose we take a[l+1] = Y > X, then last becomes Y. Right end is still X, which is less than Y, so still not available. So indeed we can never take from the right because it's always too small (or equal) once we increase last beyond X. Unless we never increase last beyond X? But we must take increasing values, so after first pick we need a value > X; the only possible sources are from left (if next is >X) or from right (if right's next after removal? But we haven't removed right, so rightmost stays X. Even if we later remove some left elements, the rightmost is still X because we haven't touched it. The only way to get a larger value from the right is to first take the right end (X) and then the new right end becomes some other value. But we didn't take it, so it's stuck. So after picking left, we are forced to continue taking from left as long as possible. If at some point the next left is not > last, then we cannot take anything else because right end is still X (<= last). So the total length from that point is exactly 1 + (number of consecutive increases from that side starting after the first). That's precisely the run length from that side (including the first element). Therefore, the total number of moves we can get from this state (including the move we are about to make) is equal to the run length from that side.
Similarly, if we pick right first, the total number of moves we can get is the run length from the right side. Therefore, to maximize total moves, we should pick the side with larger run length. And after that run is exhausted, we cannot continue because the other side is stuck with a value too small. That seems logically sound.
But is it always the case that after picking one side, the other side becomes permanently unusable? Let's examine carefully: Suppose we have a[l] = a[r] = X. We pick left: we remove left, last = X. Now right end is still X (since we didn't take it). For any future move, we can only take an element > X. The right end is X, not > X. So it's not eligible. The left end now is a[l+1] (call it Y). If Y > X, we can take it (L). Then last = Y. Now right end is still X, which is < Y, so still not eligible. So we continue left as long as each new a[l] is > last. If at some point the new left element <= last, then we cannot take it, and right end is still X (< last) or maybe something else? Wait if left becomes not > last, but maybe right end has changed? The right end is still X because we never removed it. So both ends are not > last, so we stop. So indeed, after picking left, the only possible moves are taking from left as long as the sequence is strictly increasing. So the total moves from that decision point is exactly the length of the longest strictly increasing prefix from the current left pointer (including current) where each next element is greater than the previous.
But what about the possibility that after picking left, we might later take from right if the right end eventually becomes something larger? The right end only changes if we take from right. But we cannot take from right because it's X and last is at least X, and we need > last. So we would never take from right. Unless we could take from right after some left removals? But the right end is still X, because we haven't taken it. Even if we remove all left elements, eventually the array would consist of the original rightmost element(s). But as we remove left elements, the array shrinks from the left; the rightmost element remains the same (the original rightmost). So the right end is always X until we take it. But we cannot take it because it's not > last. So indeed, after we pick left, the right side is forever locked out because its value is X, and our sequence must be increasing beyond X. So the maximum we can get is the run from left.
Similarly, picking right locks out left.
Thus the decision when ends are equal and > last is simply to compare the lengths of the strictly increasing runs from each end and choose the longer one. This yields the maximum total moves from this state.
Now, what if both ends are > last but not equal? Then we choose the smaller element. Classic reasoning: picking the smaller leaves the larger as a possible future option, and since values are not equal, picking the smaller is always safe and optimal. But is that always true when duplicates are allowed? Suppose a[l] = 2, a[r] = 3, last=1. If we pick left (2), last=2. Now remaining ends: new left (maybe 4) and right is still 3. Since 3 > 2, we could take 3 later, but also we could take left if >2. This seems fine. If we pick right (3), last=3, and left is 2 which is <3, so not usable. So picking the smaller gives more options. This is optimal.
What if both ends are > last but one is smaller and the other is larger, but the smaller leads to a shorter future than the larger? The typical proof in distinct case shows picking the smaller cannot be worse. With duplicates allowed, but values could be equal? If they are not equal, the smaller is strictly less than larger. Picking the larger would immediately make the smaller unavailable because it's now too small (since last becomes larger). Picking the smaller keeps the larger available. So picking smaller is at least as good. So that remains valid.
Now, what if only one end > last? Then we take that one.
Edge cases: when both ends <= last, we stop.
Thus algorithm:
-
Initialize l = 0, r = n-1, last = 0 (since a_i >= 1, we can use 0 as initial lower bound, but better to use -inf or 0; but we need strictly increasing, so any positive >0 works. Actually a_i >=1, so we can set last = 0.
-
Also we need to precompute for each index i the length of the increasing run from i to the right (consecutive increasing) and from i to the left (increasing to the left). But we only need from the current ends at each step. However, the ends move as we take elements. But we only need the run lengths from the current l and r when we encounter a tie (a[l] == a[r] > last). So we could compute arrays that give the length of increasing run starting at i going right (i.e., the number of elements in the longest prefix of the subarray starting at i that is strictly increasing). Let's define incR[i] = length of the longest increasing sequence starting at i and moving rightwards, i.e., the maximum k such that a[i] < a[i+1] < ... < a[i+k-1] (strictly increasing). Actually it's the length of consecutive increasing segment from i. Similarly, incL[i] for moving leftwards? Actually we need from r moving leftwards: the length of strictly increasing sequence when moving leftwards, i.e., a[r] < a[r-1] < a[r-2] < ... So we can compute decR? Let's define: For each index i, let inc_right[i] = the length of the longest strictly increasing contiguous segment starting at i (to the right). For the right end run, we need the length of strictly increasing segment starting at i and moving leftwards (i.e., decreasing index). Let's define inc_left[i] = the length of the longest strictly increasing contiguous segment starting at i and moving leftwards (i.e., a[i] < a[i-1] < a[i-2] ...). Or we can compute an array dec[i] where we look backward: if a[i] < a[i-1] then dec[i] = dec[i-1] + 1? Actually we need strictly increasing as we go left, which is equivalent to a[i] < a[i-1] and then a[i-1] < a[i-2] etc. So we can compute from left to right: For i from 0 to n-1, we can compute an array left_run[i] = 1 + (if i>0 and a[i-1] > a[i] then left_run[i-1] else 0)? Let's think: Suppose we want for index i, the length of increasing sequence when moving leftwards, meaning we start at i and go to i-1, i-2, ... with the condition a[i] < a[i-1] < a[i-2] < ... So it's like a strictly decreasing sequence in the original array order (from left to right) but increasing as we move left. Equivalent to: from i, if a[i] < a[i-1], then we can extend. So we can compute an array inc_from_left[i] = 1 + (if i>0 and a[i-1] > a[i] then inc_from_left[i-1] else 0). Actually let's test: For array indices: 0:5,1:6,2:5. For i=2 (value 5), we need run: starting at i=2, moving left: check a[2]=5 < a[1]=6? yes. Then a[1]=6 < a[0]=5? no. So run length = 2. According to recurrence: inc_from_left[2] = 1 + (if a[1] > a[2] then inc_from_left[1] else 0). a[1]=6 > a[2]=5 true, so inc_from_left[2] = 1 + inc_from_left[1]. Now inc_from_left[1] for value 6: check if a[0] > a[1]? a[0]=5 >6? false. So inc_from_left[1]=1. Then inc_from_left[2]=2. Good. For i=1 (6): inc_from_left[1] = 1 + (if a[0] > a[1]? 5>6 false) 0 =1. That is correct: starting at 6, moving left: only 6, because 5<6? Actually condition is a[i] < a[i-1]? For i=1, we need 6 < 5? false, so run length 1. So recurrence works: if a[i-1] > a[i] then inc_from_left[i] = 1 + inc_from_left[i-1], else 1.
Similarly, for inc_right, we can compute from right to left: inc_right[i] = 1 + (if i+1 < n and a[i+1] > a[i] then inc_right[i+1] else 0). But careful: We need strictly increasing to the right: a[i] < a[i+1] < a[i+2]... So condition a[i+1] > a[i]. So inc_right[i] = 1 + (if i+1 < n and a[i+1] > a[i] then inc_right[i+1] else 0). Or we can compute from right to left: for i from n-1 downto 0.
Now when we have tie at l and r, we compare inc_right[l] and inc_left[r]. inc_right[l] gives the length of the run if we start taking from left repeatedly. inc_left[r] gives the length if we start taking from right repeatedly. Then we choose the larger.
But wait: inc_right[l] counts the number of elements in the strictly increasing contiguous segment starting at l moving right. That includes l itself. So if we take left, the maximum number of moves we can make from this state (including this move) is inc_right[l]. However, is it guaranteed that we will actually be able to take all those inc_right[l] elements? Because as we take left repeatedly, the condition for each next element is that it must be > last, and last is the previous taken value, which is exactly the previous element in the array if the segment is strictly increasing. Since inc_right[l] is defined as consecutive strictly increasing values, taking them in order will satisfy last < next. So yes, we can take all inc_right[l] elements, provided that the subarray we are considering is exactly from l to some index, and we don't run into interference from the right side. But note: inc_right[l] might extend beyond the current right pointer? Actually the run is computed on the whole original array, but we are only allowed elements up to r (since we haven't taken from the right yet). However, inc_right[l] counts increasing as long as the next index exists and a[i+1] > a[i]. But if the run goes beyond r, that's okay because we will only take until we reach r? But if we take from left repeatedly, we will eventually reach the right pointer. But inc_right[l] might extend beyond r if the increasing sequence continues past r? But r is the current rightmost index. Since we haven't taken any from the right, the subarray is from l to r. The run inc_right[l] is defined on the whole array, but if the increasing sequence goes beyond r, that means there are indices > r that are not in the current subarray? Actually r is the rightmost index of the current subarray. Since we have only removed from ends, the current subarray is a contiguous segment [l, r] of the original array. So any index outside this segment is already removed. So inc_right[l] computed on the original array might include indices > r that are no longer present. But we can compute inc_right based on the original array and then when we use it at a given state, we need to ensure we only consider up to r. However, note that when we are in state [l, r], we have not removed any interior elements; we have only removed from the ends. So the segment [l, r] is a contiguous part of the original array. So if inc_right[l] as computed from the original array includes indices beyond r, those indices are not present because they would have been removed? But could they be present? The only way indices beyond r would be present is if we removed some from the right, which would decrement r. But in state [l, r], we have removed some elements from the right? Actually we remove from both ends as we go. So r is current right boundary. The elements between l and r are exactly those not yet taken. So any index i with i > r has already been taken (if we removed from right) or is not part of the segment because we removed from right? Wait, we remove from ends; so when we take from right, we decrement r. So the current r is the rightmost index not yet taken. So indices > r are already taken. So inc_right[l] computed on the original array might include indices that have been taken already, but they are not available. However, note that when we are in a tie situation, we have not yet taken any element from either side; we are about to make a decision. The current subarray is [l, r]. The inc_right[l] on the original array, if it extends beyond r, would mean that originally there were indices > r that form a strictly increasing chain starting from l. But are those indices still present? Since r is the right boundary, any index > r has been removed, but why were they removed? They could have been removed earlier by taking from the right. So they are gone. But if we are considering taking from left repeatedly, we cannot take elements beyond r because they are not in the subarray. However, the run inc_right[l] as computed on the current subarray might be truncated because some elements to the right may have been removed. But can inc_right[l] exceed r-l+1? It could if we haven't removed those elements? Actually if inc_right[l] originally is longer than the current segment, that means some elements that were part of the increasing run have already been taken from the right. But if they were taken, they are not available. So we need the run length within the current subarray. However, we need to consider that when we are in a tie situation, the current subarray is contiguous. The property we need is: If we start taking from left, how many consecutive elements can we take from the left while maintaining strictly increasing? This is exactly the length of the longest prefix of the current subarray that is strictly increasing, i.e., the maximum k such that for i from 0 to k-2, a[l+i] < a[l+i+1]. Since we haven't removed interior elements, the subarray is exactly [l, r]. So we need to compute that on the fly or maintain it.
But we can precompute arrays that allow us to query the length of the strictly increasing prefix from a given index within the current segment, but the segment shrinks. However, note that when we are in a tie, we have not taken any elements from that side yet; the left side is untouched in the sense that we haven't taken from left since the last time we may have taken from left? Actually we might have taken from left earlier. But currently, l is the leftmost index. The subarray [l, r] is contiguous. The increasing run from l to the right within this subarray is simply the number of consecutive indices i starting from l where a[i] < a[i+1] < ... until the condition breaks or we hit r. Since the subarray is exactly a contiguous slice of the original array, and the original array's increasing runs are based on the whole array, but we might have cut off part of the run due to earlier removals from the right. However, if we had previously removed some elements from the right, the run from l might be longer than what's left? Actually suppose original array: [1,2,3,4,5]. We start with l=0,r=4. inc_right[0]=5. That's fine. Now suppose we take some from right: e.g., we take 5 (R), then r=3. Now l=0,r=3. The increasing run from l within current subarray is 4 (1,2,3,4 are increasing). But inc_right[0] computed on original is 5, which