Problem:
Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

import qualified Data.ByteString.Char8 as BS
import Data.Maybe
data SegmentTree m = Leaf m  | Node m (SegmentTree m) (SegmentTree m) Int
    deriving (Eq, Show)

val :: SegmentTree m -> m
val (Leaf v)     = v
val (Node v _ _ _) = v

update :: (Monoid m) => SegmentTree m  -> Int -> m -> SegmentTree m
update (Leaf v) _ x = Leaf x
update (Node v l r size)  i x
    | i < n'   =   Node (val left `mappend` val r) left r size
    | otherwise =   Node (val l `mappend` val right) l right size
    where n' = size `div` 2
          left = update l  i x
          right = update r (i-n') x


query :: (Monoid m) => SegmentTree m -> Int -> Int -> m

query (Leaf v) 0 1 = v
query (Node v l r size) a b
    | a >= b    = mempty
    | a == 0 && b == size   = v
    | b <= n'             = query l  a b
    | a >= n'                = query r  (a-n') (b-n')
    | otherwise             = query l  a n' `mappend` query r 0 (b-n')
    where n' = size `div` 2

resize :: Int -> Int -> Int
resize n m
    | m < n = resize n (2*m)
    | otherwise  = m


fromList :: Monoid m => [m] -> SegmentTree m
fromList xs =
    let n = length xs
        m = resize n 1
        plus = replicate (m-n) mempty
        xs' = xs ++ plus
    in fromList' xs' m

fromList' ::Monoid m => [m] -> Int ->SegmentTree m
fromList' _ 0 = Leaf mempty
fromList' [x] 1 = Leaf x
fromList' xs n = Node (val left `mappend` val right) left right n
    where n' = n `div` 2
          (xs1,xs2) = splitAt n' xs
          left = fromList'  xs1 n'
          right = fromList' xs2 (n-n')

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

newtype Gcd = Gcd Int deriving (Eq,Ord,Read)

instance Show Gcd where
  show (Gcd x) = show x
instance Semigroup Gcd where
  Gcd x <> Gcd y = Gcd (gcd x y)

instance Monoid Gcd where
  mempty = Gcd 0
  
readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  n <- read <$> getLine
  a <- map Gcd <$> getIntList
  let seg = fromList a
  print $ maximum $ [query seg 0 i <> query seg (i+1) n | i <- [0..n]]