Problem:
Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

2 \leq N \leq 2\times 10^5

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295

Solution in Haskell (GHC 8.8.3) (judge verdict: Wrong Answer)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE BangPatterns #-}

-- Default Imports

import Control.Monad
import Control.Monad.ST
import Control.Monad.Trans.Class

import qualified Control.Monad.Reader as Reader
import Control.Monad.Reader (Reader, ReaderT, runReader, runReaderT)

import qualified Data.Array.MArray as MArray
import qualified Data.Array.ST as STArray
import Data.Array.ST (STArray, STUArray, runSTArray, runSTUArray)
import Data.Ix (Ix)

import qualified Data.Bits as Bits
import Data.Bits ((.&.), (.|.))

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)

import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.Map.Strict (Map)
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import Data.Set (Set)

import qualified Data.Vector as Vector
import qualified Data.Vector.Generic as GVector
import qualified Data.Vector.Mutable as MVector
import Data.Vector.Mutable (STVector)
import qualified Data.Vector.Unboxed as UVector

import Numeric

import Debug.Trace
-- ----------

-- Custom Import Here
import Data.Vector ((!))


-- ----------

-- Default Definitions

type Vector = Vector.Vector
-- type GVector = GVector.Vector
type UVector = UVector.Vector

readIntToList :: ByteString -> [Int]
readIntToList = List.unfoldr (fmap (second $ BS.dropWhile Char.isSpace) . BS.readInt)

readToList :: Read a => ByteString -> [a]
readToList = List.unfoldr $ fmap (mapTaken . mapRemain) . span' (not . Char.isSpace)
	where
		mapTaken  = first $ read . BS.unpack
		mapRemain = second $ BS.dropWhile Char.isSpace
		span' pred s
			| BS.null taken = Nothing
			| otherwise     = Just (taken, remain')
			where
				(taken, remain) = BS.span pred s
				remain' = BS.dropWhile Char.isSpace remain

readToListN :: Read a => Int -> ByteString -> [a]
readToListN n = take n . readToList

readIntToVector :: ByteString -> Vector Int
readIntToVector = Vector.unfoldr (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVector :: ByteString -> UVector.Vector Int
readIntToUVector = UVector.unfoldr (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToVectorN :: Int -> ByteString -> Vector.Vector Int
readIntToVectorN n = Vector.unfoldrN n (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVectorN :: Int -> ByteString -> UVector.Vector Int
readIntToUVectorN n = UVector.unfoldrN n (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace


first :: (a -> a') -> (a, b) -> (a', b)
first f (a, b) = (f a, b)

second :: (b -> b') -> (a, b) -> (a, b')
second f (a, b) = (a, f b)

average :: Fractional a => [a] -> a
average xs = sum xs / (fromIntegral . length) xs

-- ----------

takeOdd :: [a] -> [a]
takeOdd [] = []
takeOdd [_] = []
takeOdd (a:as) = takeEven as

takeEven :: [a] -> [a]
takeEven [] = []
takeEven (a:as) = a : takeOdd as

dropTail :: Vector a -> Vector a
dropTail v = Vector.take (Vector.length v - 1) v

calcPass1 :: Vector Int -> Vector Int -> Int -> Int -> Int
calcPass1 pass0 odds n acc = max t p
	where
		p = pass0 ! (n + 1)
		t = odds ! n + acc

calcPass2 :: Vector Int -> Vector Int -> Int -> Int -> Int
calcPass2 pass1 as n acc = max t p
	where
		p = pass1 ! n
		t = as ! n + acc

ansOdd :: Int -> [Int] -> Int
ansOdd n as = Vector.head pass2
	where
		pass0 =  Vector.scanr1 (+) $ Vector.fromList $ takeEven as
		calcPass1' = calcPass1 pass0 (Vector.fromList $ takeOdd as)
		pass1 =  dropTail $ Vector.scanr calcPass1' 0 $ Vector.fromList [0..(n `div` 2 - 1)]
		calcPass2' = calcPass2 pass1 (Vector.fromList $ takeEven as)
		pass2 =  dropTail $ Vector.scanr calcPass2' 0 $ Vector.fromList [0..(n `div` 2 - 1)]

ansEven as = max (sum $ takeEven as) (sum $ takeOdd as)

main :: IO ()
main = do
	n <- readLn
	as <- readToListN n <$> BS.getLine
	-- let n = 7
	-- let as = [1..7]

	print $ if n `mod` 2 == 0
		then ansEven as
		else ansOdd n as

	-- let as' = dropTail $ Vector.fromList as
	-- let pass0 = Vector.scanr1 (+) $ Vector.fromList $ takeEven as
	-- print pass0
	-- let calcPass1' = calcPass1 pass0 (Vector.fromList $ takeOdd as)
	-- let pass1 = dropTail $ Vector.scanr calcPass1' 0 $ Vector.fromList [0..(n `div` 2 - 1)]
	-- print pass1
	-- let calcPass2' = calcPass2 pass1 (Vector.fromList $ takeOdd as)
	-- let pass2 = Vector.scanr calcPass2' 0 $ Vector.fromList [0..(n `div` 2 - 1)]
	-- print $ maximum pass2