main :: IO () main = do n <- getLine s <- getLine print $ solve s 0 solve :: String -> Int -> Int solve [] acc = acc solve xs acc | length xs < 3 = acc solve xs@('U' : xs') acc = solve (getNext xs) (acc + c) where steps = getSteps xs 0 c = foldl (\acc' step -> if findUMG xs 0 step == Found then acc' + 1 else acc') 0 steps solve xs acc = solve (getNext xs) acc data UMG = Found | NotFound | Over deriving (Show, Eq) findUMG :: String -> Int -> Int -> UMG findUMG s start step = if start + step + step + 1 > length s then Over else if s !! start == 'U' && s !! (start+step) == 'M' && s !! (start+step*2) == 'G' then Found else NotFound getNext :: String -> String getNext (x : xs) = dropWhile (/= 'U') xs getSteps :: String -> Int -> [Int] getSteps [] start = [] getSteps (x : []) start = [] getSteps (x : xs) start = if length d > 0 then start + i : getSteps d (start + i) else getSteps (drop 1 d) (start + i) where d = dropWhile (/='M') xs i = length xs - length d + 1