import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.List (mapAccumL)
import Data.Maybe (catMaybes)

main :: IO ()
main = do
  let
    go :: Int -> [Int] -> Int
    go c (x : x' : xs) = go c' xs
      where
        (q, r) = c `divMod` x
        c' = q * x' + r
    go c _             = c
  n <- unsafeTextToInt <$> T.getLine :: IO Int
  as <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    ((b, a), cs) = fmap catMaybes . mapAccumL step (False, 200) $ as :: ((Bool, Int), [Int])
      where
        step :: (Bool, Int) -> Int -> ((Bool, Int), Maybe Int)
        step (False, old) new | old < new = ((True, new), Just old)
                              | otherwise = ((False, new), Nothing)
        step (True, old)  new | new < old = ((False, new), Just old)
                              | otherwise = ((True, new), Nothing)
    cs' :: [Int]
    cs' | b         = cs ++ [a]
        | otherwise = cs
  print . go 1000 $ cs'

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}
