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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Threading;
using System.Xml.Serialization;

namespace AtCoderCs
{

    class Program
    {

        const int mod = 1000000007;

        static void Main()
        {

            var str = Console.ReadLine();
            var min = new List<int>();
            var max = new List<int>();
            var num = str.Length;
            var startIdx = 0;
            var count = 0;
            var result = 0L;

            for (int i = 0, l = str.Length; i + 1 < l; i++)
            {

                if (str[i] == '<' && str[i + 1] == '>')
                    max.Add(i + 1);
                else if (str[i] == '>' && str[i + 1] == '<')
                    min.Add(i + 1);

            }

            if (min.Count == 0 && max.Count == 0)
            {
                Console.WriteLine(Enumerable.Range(1, str.Length).Sum(x => (long)x));
                return;
            }

            var buf = new long[num];

            if (max.Count == 0) 
                result += Enumerable.Range(1, min[0]).Sum(x => (long)x) + Enumerable.Range(1, num - min[0]).Sum(x => (long)x);
            else if (min.Count != 0 && min[0] < max[0])
                result += Enumerable.Range(1, min[0]).Sum(x => (long)x);
            else
            {
                var right = ((min.Count != 0 ? min[0] : num) - max[0]);
                result +=
                    Enumerable.Range(1, max[0] - 1).Sum(x => (long)x) +
                    Enumerable.Range(1, right - 1).Sum(x => (long)x) +
                    (right < max[0] ? max[0] : right);
                startIdx = 1;

            }

            if (min.Count != 0 && max.Count != 0)
            {

                if (min[min.Count - 1] < max[max.Count - 1])
                {

                    var right = num - max[max.Count - 1];
                    var left = max[max.Count - 1] - min[min.Count - 1];

                    result += Enumerable.Range(1, right - 1).Sum(x => (long)x) +
                        Enumerable.Range(1, left - 1).Sum(x => (long)x) +
                        (right < left ? left : right);

                    count = max.Count - 1;

                }
                else
                {

                    result += Enumerable.Range(1, num - min[min.Count - 1]).Sum(x => (long)x);

                    count = max.Count;

                }

                for (int i = startIdx; i < count; i++)
                {

                    var left = max[i] - min[i - startIdx];
                    var right = min[i + 1 - startIdx] - max[i];

                    result +=
                        Enumerable.Range(1, left - 1).Sum(x => (long)x) +
                        Enumerable.Range(1, right - 1).Sum(x => (long)x) +
                        (right < left ? left : right);

                }

            }
            

            Console.WriteLine(result);

        }



    }
}