CB Open 2024 Problem 4 - A Perfect Contest


Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Seyon is writing a contest for CougarForces. He has \(N\) problems, where the \(i\)-th problem has a difficulty of \(A_i\). However, Seyon doesn't want this to be just any old contest, he wants it to be a perfect contest.

A perfect contest is a contest, where its problems go up in difficulty and there are no big jumps in difficulty between adjacent problems.

Specifically if \(B_{1},\ldots,B_{M}\), are the difficulties of the problems in a contest, it is a perfect contest if and only if, \(0 \le B_{i+1}-B_i \le 1\) is satisfied for all \(1 \le i \lt M\) .

In other words, the difference between the difficulties in adjacent problems is either 0 or 1

Seyon wants to know the length of the longest subsequence of his problems such that the difficulties form a perfect contest.

A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by deletion of several (possibly, zero or all) elements.

Constraints

\(1 \le N \le 2 \cdot 10^5\)

\(1 \le A_i \le N\)

Input Specification

The first line contains an integer \(N\), which is the number of problems.

The next line contains \(N\) space-separated integers, \(A_i\), which is the difficulty of the \(i\)-th problem.

Output Specification

Output the length of the longest subsequence of problems such that they form a perfect contest.

Sample Input

7
5 3 4 4 7 1 5

Sample Output

4

Explanation

The longest subsequence that forms a perfect contest is \([3, 4, 4, 5]\).


Comments

There are no comments at the moment.