import Data.Maybe import Data.IORef import qualified Data.ByteString.Char8 as B import qualified Data.Vector as V import Control.Applicative main :: IO () main = do rd <- intReader [n, k] <- rd 2 cv <- V.fromList <$> rd n print (solve cv k) solve :: V.Vector Int -> Int -> Int solve cv k = minimum [f i | i <- [0..min k (n-1)]] where n = V.length cv f i = let x = g (i + 2 * k + 1) in if x == maxBound then x else cv V.! i + x g i | j < n && i >= n = maxBound | i >= n = 0 | otherwise = f i where j = i - k intReader :: IO (Int -> IO [Int]) intReader = do ws <- fmap ((concatMap B.words) . B.lines) B.getContents >>= newIORef return $ \n -> do xs <- readIORef ws writeIORef ws (drop n xs) return (take n . map (fst . fromJust . B.readInt) $ xs)