#P1184. Following Her Schedule

    ID: 13940 Type: Default 1000ms 256MiB

Following Her Schedule

Following Her Schedule

In this problem, you are given a schedule describing the whereabouts of a girl over m days. Each day, she will be at one of the n convenient places (numbered from 1 to n). A devoted admirer wants to accompany her as many days as possible. However, there is one subtle restriction: if he is already with her on some day and she remains at the same place on the very next day, then he can continue without any penalty. But if she changes her location, he needs to travel; and since his schedule is fixed by his classes, traveling costs him a full day – that is, if he chooses to follow her to a new location, he must skip the very next day (even though he can travel to that location and be there in time for the day after).

Formally, you are given an array p of length m where for each day i (1 ≤ i ≤ m) the value pi (1 ≤ pi ≤ n) indicates the convenient place where she will be on that day. The admirer can choose an arbitrary day to start following her (which counts as being together on that day). For any two days i and j (with i < j) that he chooses to be with her consecutively, the following must hold:

  • If pi = pj, then it is allowed to have j = i+1 (or any gap, as he may wait around).
  • If pi ≠ pj, then he must take at least one day off before rejoining her. In other words, if he is with her on day i and then travels to a different place, he cannot be with her on day i+1 but may rejoin on day i+2 or later.

Determine the maximum number of days during which he can be with her, under these constraints.

Note: Even if a day is skipped due to travel, it does not reset his streak. He may start at any day independently as well. In other words, his schedule is not required to be contiguous in the original day order – it is the number of days on which he is with her that matters.

The underlying recurrence can be described using the following idea. Define dp[i] as the maximum number of days he can be together with her if he is with her on day i. Then:

$$\begin{aligned} dp[i] &= \max\Bigl(1,\; &\max_{\substack{j < i \;\;\text{with}\\ p[j]=p[i]}}\{dp[j]+1\} ,\\[1mm] &\quad \max_{\substack{j \le i-2 \\ p[j]\\\neq p[i]}}\{dp[j]+1\}\Bigr). \end{aligned} $$

Your task is to compute \(\max\{dp[i]\}\) for \(1 \le i \le m\).

inputFormat

The first line of input contains two integers n and m (1 ≤ n, m ≤ 105), where n is the number of convenient places and m is the number of days.

The second line contains m integers p1, p2, ..., pm (1 ≤ pi ≤ n) where pi is the location where the girl will be on day i.

outputFormat

Output a single integer — the maximum number of days the admirer can be with her under the given constraints.

sample

2 5
1 1 2 2 1
3

</p>