import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as BS
import Data.Char (isSpace)
import Control.Monad
import Control.Monad.Primitive (PrimState)

main = do
  n <- readLn :: IO Int
  as <- VU.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  vm <- VUM.replicate (10^5+1) (0::Int) :: IO (VUM.MVector (PrimState IO) Int)
  forM_ [0..n-1] $ \i -> do
    let v = as VU.! i
    prev <- VUM.read vm v
    VUM.write vm v (prev+1)
  v <- VU.unsafeFreeze vm 
  print $ solve v (10^5)

solve :: VU.Vector Int -> Int -> Int
solve v n
  | n == 0 = current + front
  | n == VU.length v - 1 = max (prev + current) (solve v (n-1))  
  | otherwise = max (prev + current + front) (solve v (n-1))
    where
      prev = v VU.! (n-1)
      current = v VU.! n
      front = v VU.! (n+1)