import Data.Monoid import qualified Data.ByteString.Char8 as B readInt :: B.ByteString -> Int readInt = maybe undefined fst . B.readInt readInts :: B.ByteString -> [Int] readInts = map readInt . B.words main = do [n,d,k] <- map read . words <$> getLine xs <- readInts <$> B.getContents let (pmax, (i,j)) = kabu n d xs print (k * pmax) if pmax == 0 then return () else putStrLn (show i ++ " " ++ show j) kabu n d xs = foldl (slide n d seg) (0, (undefined, undefined)) (zip xs [0 .. n-2]) where seg = fromList n (zipWith (\x i -> V x i) xs [0,-1..]) slide n d seg acc@(pmax,_) (x,i) | v - x > pmax = (v - x, (i, negate j)) | otherwise = acc where (V v j) = query n seg (succ i + 1) (min (succ i + d) n) data V = V Int Index deriving (Eq, Ord, Show) instance Monoid V where mempty = V 0 undefined mappend = max data SegTree m = Leaf !m | Node !m !(SegTree m) !(SegTree m) deriving (Eq, Show) type Index = Int type Size = Int val :: Monoid m => SegTree m -> m val (Leaf v) = v val (Node v _ _) = v fromList :: Monoid m => Size -> [m] -> SegTree m fromList 1 [x] = Leaf x fromList n xs = Node (val left <> val right) left right where m = div n 2 (xs1, xs2) = splitAt m xs left = fromList m xs1 right = fromList (n - m) xs2 update :: Monoid m => Size -> SegTree m -> Index -> (m -> m) -> SegTree m update 1 (Leaf v) 1 f = Leaf (f v) update _ (Leaf _) _ _ = error "update: Pattern match failed." update n (Node _ l r) i x | i <= m = Node (val left <> val r) left r | otherwise = Node (val l <> val right) l right where m = div n 2 left = update m l i x right = update (n - m) r (i - m) x query :: Monoid m => Size -> SegTree m -> Index -> Index -> m query 1 (Leaf v) 1 1 = v query _ (Leaf _) _ _ = error "query: Pattern match failed." query n (Node v l r) i j | (i, j) == (1, n) = v | j <= m = query m l i j | i > m = query (n - m) r (i - m) (j - m) | otherwise = query m l i m <> query (n - m) r 1 (j - m) where m = div n 2