Problem:
Score : 300 points

Problem Statement

Given is a string S of length N-1.
Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

If S_i= <: a_i<a_{i+1}

If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

2 \leq N \leq 5 \times 10^5

S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3.
There is no good sequence whose sum is less than 3.

Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28

Solution in Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

import Control.Arrow

main :: IO ()
main = print . solve =<< getLine

solve :: String -> Int
solve = iter 0 . runlen
  where
    iter s []               = s
    iter s (('>',m):[])     = s + m*(m-1)`div`2
    iter s ((_,m):[])       = s + m*(m+1)`div`2
    iter s (('>',m):rs)     = iter (s + m*(m-1)`div`2) rs
    iter s ((_,m):(_,n):rs) = iter (s+s') rs
      where
        s' = if m > n then m*(m+1)`div`2 + n*(n-1)`div`2
             else m*(m-1)`div`2 + n*(n+1)`div`2
        
runlen :: String -> [(Char, Int)]
runlen ""     = []
runlen (c:cs) = case spanCount (c ==) cs of
  (m, rs) -> (c, succ m) : runlen rs

spanCount :: (a -> Bool) -> [a] -> (Int, [a])
spanCount _ [] = (0, [])
spanCount p xxs@(x:xs) 
  | p x       = first succ $ spanCount p xs
  | otherwise = (0, xxs)