import qualified Data.ByteString.Char8 as B
import qualified Data.IntMap as M
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

readInt :: B.ByteString -> Int
readInt = maybe undefined fst . B.readInt

readInts :: B.ByteString -> [Int]
readInts = map readInt . B.words

main = do
  n <- readLn :: IO Int
  ps <- readInts <$> B.getLine
  putStrLn $ unwords $ map show (maids n ps)

maids n ps = go n ipm pim q pset []
  where
    ipm = A.array (1,n) (zip [1..] ps) :: A.UArray Int Int
    pim = A.array (1,n) (zip ps [1..]) :: A.UArray Int Int
    sums = zipWith (+) ps (tail ps)
    pairs = zip ps (tail ps)
    q = S.fromList (zip sums pairs)
    pset = S.fromList ps

go n ipm pim q pset acc
  | S.null q = acc
  | S.notMember x pset || S.notMember y pset = go n ipm pim q1 pset acc
  | left == 0 || right == n+1 = go n ipm pim q1 (S.delete y (S.delete x pset)) (x:y:acc)
  | otherwise = go n ipm pim (S.insert (x1+y1, (x1,y1)) q1) (S.delete y (S.delete x pset)) (x:y:acc)
  where
    ((_,(x,y)), q1) = S.deleteFindMax q
    left = pim A.! x - 1
    right = pim A.! y + 1
    x1 = ipm A.! left
    y1 = ipm A.! right