module Main where
import           Control.Monad
import           Data.Bits
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.Monoid

data SegmentTree m = Empty | SegmentTree {node :: m,left :: SegmentTree m,right :: SegmentTree m} deriving (Show)

build :: Monoid m => Int -> SegmentTree m
build 0    = Empty
build 1    = SegmentTree mempty Empty Empty
build size = SegmentTree mempty (build newSize) (build newSize)
    where newSize = (size + 1) `div` 2

depth :: SegmentTree m -> Int
depth Empty = 0
depth tree  = (depth $ left tree) + 1

update :: Monoid m => SegmentTree m -> m -> Int -> SegmentTree m
update tree@(SegmentTree _ Empty Empty) _ _ = tree
update tree@(SegmentTree n l r) value idx =
    update' tree value idx (2 ^ (depth tree - 1))

update' :: Monoid m => SegmentTree m -> m -> Int -> Int -> SegmentTree m
update' tree@(SegmentTree n l r) value idx size
    | size == 1
    = SegmentTree value Empty Empty
    | idx <= newSize
    = let newNode = node (update' l value idx newSize) `mappend` node r
      in  SegmentTree newNode (update' l value idx newSize) r
    | otherwise
    = let newNode = node l
              `mappend` node (update' r value (idx - newSize) newSize)
      in  SegmentTree newNode l (update' r value (idx - newSize) newSize)
    where newSize = size `div` 2


query :: Monoid m => SegmentTree m -> Int -> Int -> m --[a,b)に対しての演算の結果
query tree = query' tree size where size = 2 ^ (depth tree-1)

query' :: Monoid m => SegmentTree m -> Int -> Int -> Int -> m
query' Empty _ _ _ =mempty 
query' tree@(SegmentTree n l r) size a b
    | a >= b  = mempty 
    | a == 0 && b == size  =   n
    | b <= newSize   =   query' l newSize a b
    | a >= newSize  =   query' r newSize (a-newSize) (b-newSize)
    | otherwise     =   (query' l newSize a newSize) `mappend` (query' r newSize 0 (b-newSize))
    where newSize = size `div` 2

    
get :: Monoid m => SegmentTree m -> Int -> m
get tree idx = query tree idx (idx + 1)

initial :: Monoid m => [m] -> SegmentTree m
initial list = init' list 1

init' :: Monoid m => [m] -> Int -> SegmentTree m
init' []       idx = build (idx - 1)
init' (m : ms) idx = update (init' ms (idx + 1)) m idx


newtype Gcd = Gcd Integer deriving (Show,Eq,Ord,Read)


instance Monoid Gcd where
    mempty = Gcd 0
    Gcd x `mappend` Gcd y = Gcd (gcd x y)

toI :: Gcd -> Integer
toI (Gcd x) = x

tuplify2 (x:y:_) = (x,y)
tuplify2 _ = undefined


readInt = fst . fromJust . BS.readInteger
readIntTuple = tuplify2 . map readInt . BS.words
readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine
getIntMatrix = map readIntList . BS.lines <$> BS.getContents
getIntTuple = readIntTuple <$> BS.getLine
getIntNTuples n = map readIntTuple <$> replicateM (fromIntegral n) BS.getLine
getIntTuples = map readIntTuple . BS.lines <$> BS.getContents

solve :: [Integer] -> Integer
solve a =
    let seg = initial (map Gcd a) :: SegmentTree Gcd
    in maximum $ map toI [(query seg 0 i) `mappend` (query seg (i+1) (length a)) | i <- [0..length a ]]

main :: IO ()
main = do 
    getLine
    a <- getIntList
    print (solve a)

    return()


Measured memory_bytes: 47740