import Control.Applicative ((<$>), (<*>), (<*), (*>), (<$))
import Control.Monad
import Data.List

main :: IO ()
main = do
  solve <$> getl read <*> getl (map read . words) >>= print

solve :: Int -> [Int] -> Int
solve n as
  | length bs == 1 = head bs
  | otherwise = solve n bs
  where
    f xs
      | length xs < 3 = []
      | otherwise = ((!! 1) . sort . take 3 $ xs) : f (tail xs)
    bs = f as

getl :: (String -> a) -> IO a
getl f = f <$> getLine
