{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns #-}
module Flog1 (solve) where

import qualified Control.Arrow         as Arrow
import qualified Control.Monad         as Monad
import qualified Data.Bits             as Bits
import qualified Data.ByteString.Char8 as BSC8
import qualified Data.Char             as Char
import qualified Data.List             as List
import qualified Data.Vector.Unboxed   as VU

solve :: IO ()
solve = do
    !n  <- readLn :: IO Int
    !xv <- getAN n -- VU.Vector Int
    print $ solver xv (n - 1)

data FlogTreeF a
    = Flog !Int !(Maybe a)
instance Functor FlogTreeF where
    fmap f (Flog x Nothing)  = Flog x Nothing
    fmap f (Flog x (Just y)) = Flog x (Just (f y))

solver :: VU.Vector Int -> Int -> Int
solver !xv = dyna phi psi
    where
        psi 0 = Flog (xv VU.! 0) Nothing
        psi i = Flog (xv VU.! i) (Just (i - 1))

        {-
        dp[i] := 足場iに到着したときの最小のコスト
        i-1 から来る場合 + そこからのコスト
        と
        i-2 から来る場合 + そこからのコスト
        のうち小さいものを選ぶ
        -}
        phi (Flog h Nothing)  = 0 -- 最初は0なので dp[0] = 0
        phi (Flog h (Just t)) = case sub t of
            Flog h' Nothing -> abs (h - h') -- dp[1] = |h - h'|最初のコスト
            Flog h' (Just t') -> case sub t' of
                Flog h'' _ -> min f1 f2 -- dp[h] 一般の場合
                    where
                        f1 = extract t  + abs (h - h')  -- i - 1から来た場合のコスト extract t がdp[h-1]を表す
                        f2 = extract t' + abs (h - h'') -- i - 2から来た場合のコスト extract t'がdp[h-2]を表す

-- * 入出力
-- Int一文字入力
getI :: BSC8.ByteString -> Maybe (Int, BSC8.ByteString)
getI = fmap (Arrow.second BSC8.tail) . BSC8.readInt
-- Vector Intによるn文字入力
getAN :: Int -> IO (VU.Vector Int)
getAN n = VU.unfoldrN n getI <$> BSC8.getLine

-- ! 射
newtype Fix f
    = InF { outF :: f (Fix f) }

-- 補助関数
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f, g) x = (f x, g x)

-- ! 下方射
cata :: Functor f => (f a -> a) -> (Fix f -> a)
cata phi = phi . fmap (cata phi) . outF
-- ! 上方射
ana  :: Functor f => (b -> f b) -> (b -> Fix f)
ana psi = InF . fmap (ana psi) . psi
-- ! 双方射
hylo :: Functor f => (f b -> b) -> (a -> f a) -> (a -> b)
hylo phi psi = cata phi . ana psi

-- * 未来用データ(直和)
data Fx f a x
    = Fx { unFx :: Either a (f x) }
-- * 過去用データ（直積）
data Hx f a x
    = Hx { unHx :: (a, f x) }

-- * Fx f a は関手
instance Functor f => Functor (Fx f a) where
    fmap f (Fx (Left x))  = Fx (Left x)
    fmap f (Fx (Right x)) = Fx (Right (fmap f x))
-- * Hx f a は関手
instance Functor f => Functor (Hx f a) where
    fmap f (Hx (x, y)) = Hx (x, fmap f y)

-- * Fx f a の不動点
newtype Free f a
    = Free { unFree :: Fix (Fx f a) }
-- * Hx f a の不動点
newtype CoFree f a
    = CoFree { unCoFree :: Fix (Hx f a) }

-- * Free f は関手
instance Functor f => Functor (Free f) where
    fmap f = Free . cata (InF . phi) . unFree
        where
            phi (Fx (Left a))  = Fx (Left (f a))
            phi (Fx (Right b)) = Fx (Right b)
-- * CoFree f は関手
instance Functor f => Functor (CoFree f) where
    fmap f = CoFree . ana (psi . outF) . unCoFree
        where
            psi (Hx (a, x)) = Hx (f a, x)

-- * Free と CoFree の情報を出し入れする関数
extract :: Functor f => CoFree f t -> t
extract cf = case outF (unCoFree cf) of
    Hx (a, _) -> a

sub :: Functor f => CoFree f a -> f (CoFree f a)
sub cf = case outF (unCoFree cf) of
    Hx (_, b) -> fmap CoFree b

inject :: Functor f => a -> Free f a
inject = Free
        . InF
        . Fx
        . Left

-- ! 時空射
chrono :: Functor f => (f (CoFree f b) -> b) -> (a -> f (Free f a)) -> (a -> b)
chrono phi psi = extract . hylo phi' psi' . inject
    where
        phi' = CoFree
            . InF
            . fmap unCoFree
            . Hx
            . pair (phi, id)
        psi' = uncurry either (psi, id)
            . unFx
            . fmap Free
            . outF
            . unFree

-- ! dp射
dyna :: Functor f => (f (CoFree f b) -> b) -> (a -> f a) -> (a -> b)
dyna phi psi = chrono phi (fmap inject . psi)