import Data.List

main = getLine >> getLine >>= print . solve . map read . words

solve :: [Int] -> Int
solve [x] = x
solve xs = solve $ calc xs
    where
        calc (w : x : y : zs) = r : calc (x : y : zs)
            where
                r = sort [w, x, y] !! 1
        calc _ = []
