module Main where import Control.Monad import Control.Applicative import Data.Maybe import Data.List import qualified Text.Printf import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString as BS import Debug.Trace ------------------------------------------ data Tile = Front | Back deriving (Eq, Show) fromChar :: Char -> Tile fromChar '#' = Front fromChar _ = Back flipTile Front = Back flipTile Back = Front main :: IO () main = do n <- readInt s <- map fromChar <$> BC.unpack <$> BC.getLine let leng = length s res = min (solve (n-1) s) (solve (leng-n) $ reverse s) print res solve :: Int -> [Tile] -> Int solve n ts' = let ts = foldr arrange [] $ dropWhile (== Front) (mapn n flipTile ts') where arrange Back [] =[] arrange x xs = x:xs n0 = n - min n (length ts' - length ts) score acc [] = acc score acc (Back:[]) = acc score acc (x:y:[]) = case (x, y) of (Back, Back) -> acc (Back, Front) -> acc+1 (Front, Front) -> acc+2 (Front, Back) -> acc+3 score acc (x:y:ys) = case x of Back -> score (acc+1) (flipTile y:ys) Front -> score (acc+3) (y:ys) in score n0 ts mapn :: Int -> (a -> a) -> [a] -> [a] mapn _ _ [] = [] mapn n f (x:xs) | n <= 0 = x:xs | otherwise = f x : mapn (n-1) f xs ------------------------------------------ {- Int input -} parseInt :: BC.ByteString -> Int parseInt = fst . fromJust . BC.readInt parseInts :: BC.ByteString -> [Int] parseInts = map parseInt <$> BC.words readInt :: IO Int readInt = parseInt <$> BC.getLine readInts :: IO [Int] readInts = parseInts <$> BC.getLine {- Double Formatting -} doubleFmt :: Double -> String doubleFmt = Text.Printf.printf "%.12f"