CF BUDDY
← Problems·

1479B1 · Painting the Array I

1900 · constructive algorithms, data structures, dp

Problem: The only difference between the two versions is that this version asks the maximal possible answer.

Homer likes arrays a lot. Today he is painting an array a1,a2,,ana_1, a_2, \dots, a_n with two kinds of colors, white and black. A painting assignment for a1,a2,,ana_1, a_2, \dots, a_n is described by an array b1,b2,,bnb_1, b_2, \dots, b_n that bib_i indicates the color of aia_i (00 for white and 11 for black).

According to a painting assignment b1,b2,,bnb_1, b_2, \dots, b_n, the array aa is split into two new arrays a(0)a^{(0)} and a(1)a^{(1)}, where a(0)a^{(0)} is the sub-sequence of all white elements in aa and a(1)a^{(1)} is the sub-sequence of all black elements in aa. For example, if a=[1,2,3,4,5,6]a = [1,2,3,4,5,6] and b=[0,1,0,1,0,0]b = [0,1,0,1,0,0], then a(0)=[1,3,5,6]a^{(0)} = [1,3,5,6] and a(1)=[2,4]a^{(1)} = [2,4].

The number of segments in an array c1,c2,,ckc_1, c_2, \dots, c_k, denoted seg(c)\mathit{seg}(c), is the number of elements if we merge all adjacent elements with the same value in cc. For example, the number of segments in [1,1,2,2,3,3,3,2][1,1,2,2,3,3,3,2] is 44, because the array will become [1,2,3,2][1,2,3,2] after merging adjacent elements with the same value. Especially, the number of segments in an empty array is 00.

Homer wants to find a painting assignment bb, according to which the number of segments in both a(0)a^{(0)} and a(1)a^{(1)}, i.e. seg(a(0))+seg(a(1))\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)}), is as large as possible. Find this number.

Input Format: The first line contains an integer nn (1n1051 \leq n \leq 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \leq a_i \leq n).

Output Format: Output a single integer, indicating the maximal possible total number of segments.

Note: In the first example, we can choose a(0)=[1,2,3,3]a^{(0)} = [1,2,3,3], a(1)=[1,2,3]a^{(1)} = [1,2,3] and seg(a(0))=seg(a(1))=3\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3. So the answer is 3+3=63+3 = 6.

In the second example, we can choose a(0)=[1,2,3,4,5,6,7]a^{(0)} = [1,2,3,4,5,6,7] and a(1)a^{(1)} is empty. We can see that seg(a(0))=7\mathit{seg}(a^{(0)}) = 7 and seg(a(1))=0\mathit{seg}(a^{(1)}) = 0. So the answer is 7+0=77+0 = 7.

Sample Cases

Case 1

Input

7
1 1 2 2 3 3 3

Output

6

Case 2

Input

7
1 2 3 4 5 6 7

Output

7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback