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 Rust (1.15.1) (judge verdict: Accepted)

use std::cmp::max;
use std::io;
use std::io::prelude::*;

const DEBUG: bool = false;

fn main() {
    let stdin = io::stdin();
    let mut input = String::new();
    stdin.lock().read_line(&mut input).expect("read line!");
    let input: String = input;
    println!("{}", solve(&input.trim()))
}

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
enum GL {
    GT,
    LT,
}
impl GL {
    fn from_char(c: char) -> Option<Self> {
        match c {
            '>' => Some(GL::GT),
            '<' => Some(GL::LT),
            _ => None,
        }
    }
}

fn solve(input: &str) -> u64 {
    let mut signs = input
        .chars()
        .map(|c| GL::from_char(c).expect(&format!("invalid! {}", c)));
    let mut ups_and_downs: Vec<(GL, u64)> = vec![];
    if let Some(init) = signs.next() {
        let mut streak = 1;
        let mut current = init;
        for sign in signs {
            if sign == current {
                streak += 1;
            } else {
                ups_and_downs.push((current, streak));
                streak = 1;
                current = sign;
            }
        }
        ups_and_downs.push((current, streak));
    };
    let ups_and_downs = ups_and_downs;
    if DEBUG {
        println!("{:?}", ups_and_downs);
    }
    let mut sum: u64 = 0;
    let mut current_bound: u64 = 0;
    for &(sign, streak) in &ups_and_downs {
        if DEBUG {
            println!("++NEXT! {:?} {:?}", sign, streak);
        }
        let s = streak as u64;
        match sign {
            GL::GT => {
                sum += max(current_bound, s);
                if DEBUG {
                    println!("GT adds\t\t\t {}", max(current_bound, s));
                    println!("GT adds sequence of {}\t that's {}", s, s * (s - 1) / 2);
                }
                sum += s * (s - 1) / 2;
                current_bound = 0;
            }
            GL::LT => {
                current_bound = s;
                sum += s * (s - 1) / 2;
                if DEBUG {
                    println!("LT {}\t {}", s, s * (s - 1) / 2);
                    println!("LT adds sequence of {}\t that's {}", s, s * (s - 1) / 2);
                }
            }
        }
    }
    sum += current_bound;
    sum
}