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 (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def sum2n(n: int):
    return n*(n+1) // 2

s = lc()
n = len(s)+1

yama = []
tani = []
ans = 0


if s[0] == ">":
    if "<" in s:
        ans += sum2n(s.index("<"))
    else:
        ans += sum2n(n-1)
else:
    tani.append(0)


for i in range(1, n-1):
    if s[i-1] == "<" and s[i] == ">":
        yama.append(i)
    elif s[i-1] == ">" and s[i] == "<":
        tani.append(i)


if s[-1] == "<":
    if ">" in s:
        ans += sum2n(s[::-1].index(">"))
    else:
        ans += sum2n(n-1)
else:
    tani.append(n-1)


if len(yama) > 0:
    for i in range(len(yama)):
        mx = max(yama[i] - tani[i], tani[i+1] - yama[i])
        mn = min(yama[i] - tani[i], tani[i+1] - yama[i])
        ans += sum2n(mx)
        ans += sum2n(mn-1)

print(ans)

Solution (Python):
def nsum(n):
  if n%2==0:
    return (n+1)*(n/2)
  else:
    return (n+1)*((n-1)/2)+((n+1)/2)
S=input()
count=0
flag=-1
code=[]
for i in S:
  if count == 0:
    count+=1
    flag=i
  elif flag==i:
    count+=1
  else:
    code.append([flag,count])
    count=1
    flag=i
code.append([flag,count])

ans=0
if code[0][0]=='>':
  ans+=nsum(code[0][1])
if code[-1][0]=='<':
  ans+=nsum(code[-1][1])
for a in range(len(code)-1):
  if code[a][0]=='>':
    continue
  else:
    a1=code[a][1]
    a2=code[a+1][1]
    if a1<a2:
      a1,a2=a2,a1
    ans+=nsum(a1)+nsum(a2-1)
print(int(ans))

Solution (Python):
s = input()
sum = 0
R = 0
L = 0
flag = 0
for i in range(len(s)):
  if s[i] == "<" and L != 0:
    if L >= R:
      sum += L*(L+1)//2 + R*(R-1)//2
    else:
       sum += L*(L-1)//2 + R*(R+1)//2
    R = 0
    L = 0
  if s[i] == "<":
    R += 1 
  else:
    L += 1
    flag += 1
if L >= R:
  sum += L*(L+1)//2 + R*(R-1)//2
else:
  sum += L*(L-1)//2 + R*(R+1)//2

print(sum)

Solution (Python):
S = input()
N = len(S)
tmp = 1
List = []
for i in range(1, N):
    if S[i] != S[i-1]:
        List.append(tmp)
        tmp = 1
    else:
        tmp+=1
List.append(tmp)
def solve(ans, List):
    for i in range(0, len(List)-1, 2):
        r = List[i]
        l = List[i+1]
        ans+=r*(r-1)//2
        ans+=max(r, l)
        ans+=l*(l-1)//2
    if len(List)%2 == 1:
        l = List[-1]
        ans+=l*(l+1)//2
    print(ans)
if S[0] == '<':
    solve(0, List)
else:
    l = List[0]
    solve(l*(l+1)//2,List[1:])

Solution (Python):
s = list(input())

ans = []
li = []

def calc(n):
    s = (n*(n+1))//2
    return s

cnt = 1
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        cnt += 1

    elif s[i] != s[i-1]:
        li.append(cnt)
        cnt = 1
li.append(cnt)

ans = 0
if s[0] == "<":
    if len(li)%2 == 1:
        ans += calc(li.pop(-1))

    for i in range(0,len(li),2):
        a = max(li[i],li[i+1])
        b = min(li[i],li[i+1])

        ans += calc(a)
        ans += calc(b-1)

elif s[0] == ">":
    if len(li)%2 == 0:
        ans += calc(li.pop(-1))

    ans += calc(li[0])
    for i in range(1,len(li),2):
        a = max(li[i],li[i+1])
        b = min(li[i],li[i+1])

        ans += calc(a)
        ans += calc(b-1)

print(ans)

Solution (Python):


S = input()

ans = 0
stack = 0
height = 0
last_c = '>'
for c in S:
    if last_c == '<' and c == '<':
        stack += 1
    elif last_c == '>' and c == '>':
        stack += 1

    elif last_c == '>' and c == '<':
        
        ans += stack * (stack - 1) // 2
        ans += max(height, stack)
        stack = 1

    elif last_c == '<' and c == '>':  
        
        height = stack
        ans += stack * (stack - 1) // 2
        stack = 1

    last_c = c

if last_c == '<':
    ans += stack * (stack + 1) // 2
else:
    ans += stack * (stack - 1) // 2
    ans += max(height, stack)

print(ans)

Solution (Python):
S=list(input())
N=len(S)+1
P=[0]*N
Q=[0]*N
p=0
q=0
r=0
for i in range(0,N-1):
	if S[i]=="<":
		P[i+1]=P[i]+1
	p+=P[i+1]     
for j in range(0,N-1):
	if S[-(j+1)]==">":
		Q[-j-2]=Q[-j-1]+1
	q+=Q[-j-2]
for l in range(0,N):
	r-=min(P[l],Q[l])
print(p+q+r)

Solution (Python):
s = (input())

l=0
r=0
g=0
for x in range(len(s)-1) :
        if(s[x] == '<') :
                l+=1
        else :
                r+=1
                if(s[x+1] == '<') :
                        if(l>r) :
                            for ll in range(1,l+1) :
                                    g += ll
                            for rr in range(1,r) :
                                    g += rr
                        else :
                            for ll in range(1,l) :
                                    g += ll
                            for rr in range(1,r+1) :
                                    g += rr
                        l=0
                        r=0

if(s[len(s)-1] == '<') :
        l+=1
else :
        r+=1

if(l>r) :
        for ll in range(1,l+1) :
                g += ll
        for rr in range(1,r) :
                g += rr
else :
        for ll in range(1,l) :
                g += ll
        for rr in range(1,r+1) :
                g += rr


print(g)

Solution (Python):
s=input()
s=s+'0'
list=[]
a=0
b=0
c=0
d=0
e=0
f=0
for i in range(len(s)-1):
  if s[i]=='<':
    b+=1
  if s[i]=='>':
    c+=1
  if (s[i]=='>' and s[i+1]=='<') or i==len(s)-2:
    e=max(b,c)
    f=min(b,c)
    d+=(e*(e+1))/2
    d+=(f*(f-1))/2
    b=0
    c=0
print(int(d))

Solution (Python):
S = input()

L = []

cnt = 1
temp = S[0]
for i in range(len(S)-1):
    if S[i+1] == temp:
        cnt += 1
    else:
        L.append(cnt)
        temp = S[i+1]
        cnt = 1
        
L.append(cnt)

ans = 0
if S[0] == "<":
    for i in range(len(L)):
        if i%2 == 0:
            ans += L[i]*(L[i]+1)//2
        else:
            if L[i] > L[i-1]:
                ans += L[i]*(L[i]+1)//2
                ans -= L[i-1]
            else:
                ans += L[i]*(L[i]-1)//2
else:
    for i in range(len(L)):
        if i%2 == 1 or i == 0:
            ans += L[i]*(L[i]+1)//2
        else:
            if L[i] > L[i-1]:
                ans += L[i]*(L[i]+1)//2
                ans -= L[i-1]
            else:
                ans += L[i]*(L[i]-1)//2
            
print(ans)

Solution (Python):

s = input()
n = len(s)

a = [0] * (n + 1)
b = [0] * (n + 1)
for i in range(n):
    if s[i] == '<':
        a[i + 1] = a[i] + 1
for i in range(n)[::-1]:
    if s[i] == '>':
        b[i] = b[i + 1] + 1
ans = [0] * (n + 1)
for i in range(n + 1):
    ans[i] = max(a[i], b[i])
print(sum(ans))

Solution (Python):
def main():
    S = input()
    N = len(S)
    li = []
    pre = S[0]
    pre_cnt = 1
    l_cnt = 0
    for i in range(1, N):
        if (pre == '<' and S[i] == '>') or (pre == '>' and S[i] == '<'):
            li.append((pre*pre_cnt, l_cnt))
            pre = S[i]
            pre_cnt = 1
            l_cnt += 1
        else:
            pre_cnt += 1
    else:
        li.append((pre*pre_cnt, l_cnt))
    finished = [False] * (len(li)+1)
    li.sort(key=lambda s: len(s[0]), reverse=True)

    ans = 0
    for i in range(len(li)):
        if li[i][0][0] == '<':
            n = len(li[i][0])
            if finished[li[i][1] + 1]:
                n -= 1
            ans += (n*(n+1))//2
        else:
            n = len(li[i][0])
            if finished[li[i][1]]:
                n -= 1
            ans += (n*(n+1))//2
        finished[li[i][1]] = True
        finished[li[i][1] + 1] = True

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


def wa(n):
    cnt=0
    for i in range(n):
        cnt+=1+i
    return cnt

def wa_arr(a):
    cnt=0
    for i in range(0,len(a),2):
        cnt+=wa(a[i])+wa(a[i+1])-min(a[i],a[i+1])
    return cnt

a=input()
cnt=0
res=[]
for i in range(len(a)):
    if i==0:
        now=a[i]
        cnt+=1
    else:
        if a[i]==now:
            cnt+=1
        else:
            res.append(cnt)
            now=a[i]
            cnt=1
res.append(cnt)


if a[0]=="<":
    if len(res)%2==0:
        c=wa_arr(res)
    else:
        c=wa_arr(res[:-1])+wa(res[-1])
        
else:
    if len(res)%2==1:
        c=wa_arr(res[1:])+wa(res[0])
    else:
        c=wa_arr(res[1:-1])+wa(res[0])+wa(res[-1])

print(c)

Solution (Python):
S=input()


left=0 
right=0 
SUM=0

def count(l,r,SUM):
    if l<=r:
        SUM+=l*(l-1)//2+r*(r+1)//2
    else:
        SUM+=l*(l+1)//2+r*(r-1)//2
    return SUM
    
for i in range(len(S)):
    if i<len(S)-1 and S[i]==">" and S[i+1]=="<": 
        right+=1
        SUM=count(left,right,SUM)
        left=0
        right=0
    elif S[i]=="<":
        left+=1
    elif S[i]==">":
        right+=1


print(count(left,right,SUM))

Solution (Python):
s = input()
l_strk_count = 1
r_strk_count = 1
ans = 0
pre_s = "a"
for i in s:
    
    if pre_s == "<" and i == "<":
        l_strk_count += 1
    elif pre_s == ">" and i == ">":
        r_strk_count += 1

    
    
    if i == "<" and pre_s == ">":
        ans += (max(l_strk_count,r_strk_count) ** 2 + max(l_strk_count,r_strk_count))//2 + (min(l_strk_count,r_strk_count) ** 2 - min(l_strk_count,r_strk_count))//2
        l_strk_count = 1
        r_strk_count = 1
    pre_s = i
if pre_s == ">":
    ans += (max(l_strk_count,r_strk_count) ** 2 + max(l_strk_count,r_strk_count))//2 + (min(l_strk_count,r_strk_count) ** 2 - min(l_strk_count,r_strk_count))//2
else:
    ans += (l_strk_count**2 + l_strk_count) // 2
print(ans)

Solution (Python):
S=input()
N=len(S)
x=[-1]*(N+1)
M=[-1]*(N+1)
from collections import deque
l=deque()
m=2
for i in range(N-1):
    if S[i:i+2]=='><':
        l.append((i+1,0,m))
        m+=1
if S[0]=='<':
    l.append((0,0,0))
if S[-1]=='>':
    l.append((N,0,1))
while l:
    n,v,m=l.popleft()
    if x[n]<0:
        x[n]=v
        M[n]=m
        if n>0 and S[n-1]=='>':
            l.append((n-1,v+1,m))
        if n<N and S[n]=='<':
            l.append((n+1,v+1,m))
    elif M[n]!=m:
        x[n]=v
        M[n]=m
print(sum(x))

Solution (Python):
S = input()
L = len(S)
c = 0
l = 1
State = S[0]
ans = 0
for i in range(L):
    if S[i] == State:
        if S[i] == "<":
            ans += c+1
            c += 1
            l += 1
        else:
            ans += c-1
            c -= 1
            l += 1
    else:
        if State == ">":
            if c <= 0:
                ans += l * (-c)
            else:
                ans -= (l-1)*c
            ans += 1
            c = 1
        else:
            ans += c-1
            c -= 1
        State = S[i]
        l = 2
if State == ">":
    if c <= 0:
        ans += l * (-c)
    else:
        ans -= (l-1)*c
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
























def main():
	s=input().rstrip().decode('utf-8')
	n=len(s)
	if s[n-1]=="<":
		s+=">"
		n+=1
	
	dai=0
	syo=0
	
	ans=0
	s+="a"
	
	for i in range(n):
		if s[i]=="<":
			dai+=1
		elif s[i]==s[i+1]==">":
			syo+=1
		else:
			syo+=1
			deka=max(dai,syo)
			mini=min(dai,syo)-1
			ans+=deka*(deka+1)//2+(mini)*(mini+1)//2
			dai=syo=0
	print(ans)
	
	
	
	
	
	
	
	
	
	
	

	
if __name__ == '__main__':
	main()

Solution (Python):
def s_in():
    return input()

S = s_in()

if len(S) == 1:
    print(1)
    exit()


lis = []
cnt = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
    else:
        lis.append((S[i], cnt))
        cnt = 1
else:
    lis.append((S[len(S)-1], cnt))


res = 0
idx = 0


if lis[0][0] == ">":
    res += sum(range(lis[0][1]+1))
    idx += 1

while idx < len(lis):
    if idx+1 == len(lis):
        res += sum(range(lis[idx][1]+1))
        break

    x = lis[idx][1]
    y = lis[idx+1][1]

    res += sum(range(max(x,y)+1)) + sum(range(min(x,y)))

    idx += 2


print(res)

Solution (Python):
s=input()

a=[1]
if s[0]!="<":a=[0,1]
for i in range(len(s)-1):
  if s[i]!=s[i+1]:a.append(1)
  else:a[-1]+=1
if s[-1]!=">":a.append(0)

b=0
for i in range(len(a)//2):
  ma,mi=max(a[i*2],a[i*2+1]),min(a[i*2],a[i*2+1])
  b+=ma*(ma+1)//2
  b+=mi*(mi-1)//2
print(b)

Solution (Python):
s = input()
index = 0
total = 0
count_less = 0
count_more = 0
while index < len(s):
    count = 0
    while index < len(s) and s[index] == '<':
        count += 1
        index += 1
    count_more = count
    count = 0
    while  index < len(s) and s[index] == '>':
        count += 1
        index += 1
    count_less = count
    if count_more == count_less:
        total += count_less * (count_less + 1) - count_less
    elif count_less > count_more:
        total += count_less * (count_less + 1) // 2
        total += count_more * (count_more - 1) // 2
    else:
        total += count_more * (count_more + 1) // 2
        total += count_less * (count_less - 1) // 2
print(total)

Solution (Python):
s = input()
n = len(s)
a = []
 
p = 1
for i in range(n-1):
    if s[i] != s[i+1]:
        a.append(p)
        p = 1
    else:
        p += 1
a.append(p)

ans = 0
if s[0] == ">":
    ans += a[0]*(a[0]+1)//2
    a = a[1:]
if s[-1] == "<":
    ans += a[-1]*(a[-1]+1)//2
    a = a[:-1]

for i in range(len(a)//2):
    p, q = a[2*i], a[2*i+1]
    x, y = max(p,q), min(p,q) - 1
    ans += x*(x+1)//2 + y*(y+1)//2

print(ans)

Solution (Python):

import numpy as np

s = input()
n = len(s) + 1

less = np.zeros(n)
more = np.zeros(n)

for i, (ch, ct) in enumerate(zip(s, s[::-1])):
    if ch == '<':
        more[i+1] = more[i] + 1
    if ct == '>':
        less[n-i-2] = less[n-i-1] + 1

rst = [max(m, l) for m, l in zip(more, less)]
print(int(sum(rst)))

Solution (Python):
S = input()

gLen = 0
lLen = 0
greater = True
ans = 0
for c in S:
    if greater:
        if c == '>':
            gLen += 1
        else:
            if gLen > lLen:
                ans += lLen * (lLen-1) // 2
                ans += gLen * (gLen+1) // 2
            else:
                ans += lLen * (lLen+1) // 2
                ans += gLen * (gLen-1) // 2
            lLen = 1
            gLen = 0
            greater = False
    else:
        if c == '<':
            lLen += 1
        else:
            greater = True
            gLen = 1
if gLen > lLen:
    ans += lLen * (lLen-1) // 2
    ans += gLen * (gLen+1) // 2
else:
    ans += lLen * (lLen+1) // 2
    ans += gLen * (gLen-1) // 2
print(ans)

Solution (Python):
input = raw_input
S = input()
n = len(S) 
s = S


up_count = 0
down_count = 0

start_i = None
for i, c in enumerate(S):
    if c == "<":
        start_i = i
        break
    down_count += 1

if start_i is None:
    start_i = n

nums = []
for num in reversed(range(down_count+1)):
    nums.append(num)

up = True
for c in S[start_i:]: 
    if c == "<":
        if not up: 
            if up_count >= down_count:
                for num in range(1, up_count+1):
                    nums.append(num)
                for num in reversed(range(0, down_count)):
                    nums.append(num)
            else:
                for num in range(0, up_count):
                    nums.append(num)
                for num in reversed(range(1, down_count+1)):
                    nums.append(num)
                 
            down_count = 0
            up_count = 1
            up = True
        else:
            up_count += 1
    else:
        if up:
            up = False
            down_count = 1
        else:
            down_count += 1
        

if up:
    for num in range(up_count+1):
        nums.append(num)
else:
    if up_count >= down_count:
        for num in range(1, up_count+1):
            nums.append(num)
        for num in reversed(range(0, down_count)):
            nums.append(num)
    else:
        for num in range(0, up_count):
            nums.append(num)
        for num in reversed(range(1, down_count+1)):
            nums.append(num)

out = sum(nums)
print(out)

Solution (Python):
s=list(input())
n=len(s)+1
f=1
s.append('!')
y=[]
for i in range(n-1):
    if s[i]==s[i+1]:
        f+=1
    else:
        y.append(f)
        f=1
if len(y)==1:
    print(y[0]*(y[0]+1)//2)
    exit()
if s[0]=='<':
    ans=0
    for i in range(len(y)-1):
        ans+=y[i]*(y[i]-1)//2
        if i%2==0:
            ans+=max(y[i],y[i+1])
    if len(y)%2==0:
        ans+=y[-1]*(y[-1]-1)//2
    else:
        ans+=y[-1]*(y[-1]+1)//2
    print(ans)
else:
    ans=0
    for i in range(len(y)-1):
        if i==0:
            ans+=y[i]*(y[i]+1)//2
            continue
        ans+=y[i]*(y[i]-1)//2
        if i%2==1:
            ans+=max(y[i],y[i+1])
    if len(y)%2==0:
        ans+=y[-1]*(y[-1]+1)//2
    else:
        ans+=y[-1]*(y[-1]-1)//2
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
def input():
    return sys.stdin.readline().rstrip()

def main():
    S=list(input())
    pre=0
    ans=0
    m=0
    p=0
    for a in S:
        if a =='<':
            p+=1
            if m != 0:
                ans-=min(pre,m)
                pre=m
                ans+=m*(m+1)//2
                m=0
        elif a=='>':
            m+=1
            if p!=0:
                pre=p
                ans+=p*(p+1)//2
                p=0
    if m != 0:
        ans-=min(pre,m)
        pre=m
        ans+=m*(m+1)//2
        m==0
    elif p!=0:
        pre=p
        ans+=p*(p+1)//2
        p==0
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def sigma(b):
    return (b+1)*b//2


S = input()[:-1]
N = len(S)
L = []
if S[0] == ">":
    L.append(("U", 0))
else:
    L.append(("D", 0))
for i, a, b in zip(range(1, N), S, S[1:]):
    if a == b:
        continue
    else:
        if a == "<" and b == ">":
            L.append(["U", i])
        else:
            L.append(["D", i])
if S[-1] == ">":
    L.append(["D", N])
else:
    L.append(["U", N])

ans = 0
if L[0][0] == "U":
    ans += sigma(L[1][1]-L[0][1])
if L[-1][0] == "U":
    ans += sigma(L[-1][1]-L[-2][1])
for i in range(1, len(L)-1):
    if L[i][0] == "U":
        dist_long = max(L[i][1]-L[i-1][1], L[i+1][1]-L[i][1])
        dist_small = min(L[i][1]-L[i-1][1], L[i+1][1]-L[i][1])
        ans += sigma(dist_long) + sigma(dist_small-1)
print(ans)

Solution (Python):
S = list(input())
N = len(S) + 1
INF = 10 ** 8

l = [INF] * N

if S[0] == "<":
    l[0] = 0

for i in range(1,N-1):
    if S[i-1] == "<" and S[i] == "<":
        l[i] = l[i-1] + 1
    elif S[i-1] == ">" and S[i] == "<":
        l[i] = 0
    

if S[N-2] == "<":
    l[N-1] = l[N-2] + 1
else:
    l[N-1] = 0

for i in range(1,N-1):
    if S[-i] == ">" and S[-i-1] == ">":
        l[-i-1] = l[-i] + 1
    elif S[-i] == ">" and S[-i-1] == "<":
        l[-i-1] = 1 + max(l[-i],l[-i-2])
    

if S[0] == ">":
    l[0] = l[1] + 1

print(sum(l))

Solution (Python):
def calc(l):
    if l == "":
        return 0

    left = l.count("<")
    right = l.count(">")

    if left == 0:
        return right * (right + 1) // 2

    if right == 0:
        return left * (left + 1) // 2

    return left * (left - 1) // 2 + right * (right - 1) // 2 + max(right, left)

def f():
    S = input()
    res = 0
    l = ""
    for s in S:
        if l == "":
            l += s
            continue

        if l[-1] == ">" and s == "<":
            res += calc(l)
            l = "<"
        else:
            l += s
    res += calc(l)
    print(res)


if __name__ == '__main__':
    f()

Solution (Python):
s = input()
n = len(s) + 1

lst_0 = []
a = [-1 for i in range(n)]
if s[0] == '<':
    a[0] = 0
    lst_0.append(0)
if s[-1] == '>':
    a[-1] = 0
    lst_0.append(n-1)

mae = s[0]
for i in range(1, n-1):
    now = s[i]
    if mae == '>' and now == '<':
        a[i] = 0
        lst_0.append(i)
    mae = now

for i in lst_0:
    now = i - 1
    kazu = 1
    while now >= 0 and s[now] == '>':
        a[now] = max(a[now], kazu)
        now -= 1
        kazu += 1
    now = i + 1
    kazu = 1
    while now <= n-1 and s[now-1] == '<':
        a[now] = max(a[now], kazu)
        now += 1
        kazu += 1

print(sum(a))

Solution (Python):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue


Mod = 1000000007
 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): 
    S = input()
    N = len(S)+1
    ll = []
    j = 0
    ans = 0
    for i in range(N-1):
        if S[i:i+2] == "><":
            ll.append(S[j:i+1])
            j = i+1
    ll.append(S[j:i+1])
    for i in ll:
        p = i.count("<")
        q = i.count(">")
        if p > q:
            ans += (p*(p+1)//2+q*(q-1)//2)
        else:
            ans += (p*(p-1)//2+q*(q+1)//2)
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
s = str(input())
ans = 0
cnt = 1
lastright = 0
lis = []
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        lis.append(cnt)
        cnt = 1
lis.append(cnt)
if s[0] == "<":
    lis.insert(0,0)
if s[-1] == ">":
    lis.append(0)
for i in range(len(lis)//2-1):
    if lis[2 * i + 1] <= lis[2 * (i + 1)]:
        lis[2 * i + 1] -= 1
    else:
        lis[2 * (i + 1)] -= 1
for num in lis:
    ans += (num * (num + 1))//2
print(ans)

Solution (Python):
s=input()+" "
count=[]
left,right=0,0
for i in range(len(s)):
    if s[i]==">" and s[i+1]=="<":
        count.append(s[left:i+1])
        left=i+1
count.append(s[left:])

ans=0
for i in range(len(count)):
    left,right=0,0
    for j in count[i]:
        if j==">":right +=1
        elif j=="<":left +=1
    big_one=max(left,right)
    small_one=min(left,right)
    for k in range(big_one+1):
        ans +=k
    for l in range(small_one):
        ans +=l

print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
ass = []
tmp = ''
cnt = 0
for i in range(n):
    if i == 0:
        tmp = s[i]
        cnt = 1
        continue
    if tmp == s[i]:
        cnt += 1
    else:
        tmp = s[i]
        ass.append(cnt)
        cnt = 1
ass.append(cnt)
l = len(ass)
res = 0
if s[0] == '<':
    for i in range(l)[::2]:
        if i == l-1:
            res += (ass[i]+1)*ass[i]//2
            continue
        mm = max(ass[i],ass[i+1])
        nn = min(ass[i],ass[i+1])
        
        res += (mm+1)*mm//2 + (nn-1)*nn//2
else:
    res += (ass[0]+1)*ass[0]//2
    for i in range(1,l)[::2]:
        if i == l-1:
            res += (ass[i]+1)*ass[i]//2
            continue
        mm = max(ass[i],ass[i+1])
        nn = min(ass[i],ass[i+1])
        res += (mm+1)*mm//2 + (nn-1)*nn//2
print(res)

Solution (Python):
s=input()
n=len(s)+1
t=[-1]*n
if s[0]=='<':
    t[0]=0
if s[-1]=='>':
    t[-1]=0
for i in range(n-2):
    if s[i:i+2]=='><':
        t[i+1]=0
for i in range(n-1):
    if s[i]=='<':
        if t[i]>=0:
            t[i+1]=t[i]+1
for i in range(n-2,-1,-1):
    if s[i]=='>':
        if t[i+1]>=0:
            t[i]=max(t[i],t[i+1]+1)
print(sum(t))

Solution (Python):
import sys
input = sys.stdin.readline


def runLength(s):
    a = []
    for i, c in enumerate(s):
        if len(a) == 0 or a[-1][0] != c:
            a.append([c, 1, i])  
        else:
            a[-1][1] += 1
    return a


def main():
    s = input().strip()
    ans = [-1]*(len(s)+1)
    a = runLength(s)
    a.sort(key=lambda x: x[1], reverse=True)
    for c, n, i in a:
        if c == "<":
            x = (ans[i] if ans[i] != -1 else 0)
            for j in range(i, i+n+1):
                if ans[j] == -1:
                    ans[j] = x
                x += 1
        else:
            x = (ans[i+n] if ans[i+n] != -1 else 0)
            for j in range(i+n, i-1, -1):
                if ans[j] == -1:
                    ans[j] = x
                x += 1
    print(sum(ans))


if __name__ == '__main__':
    main()

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    before = " "
    RLE = []
    string = []
    for s in S:
        if s==before:
            RLE[-1] += 1
        else:
            RLE.append(1)
            string.append(s)
        before = s
    
    

    A = [-1]*(len(S)+1) 
    if string[0]=="<":
        A[0]=0
    now = 0
    for i,x in enumerate(RLE):
        s = string[i]
        now += x
        if s=="<":
            pass
        else: 
            A[now] = 0
    i=0
    while i<(len(S)+1):
        if A[i]==0:
            j=i-1
            while j>=0:
                if S[j]==">":
                    A[j]=max(A[j],A[j+1]+1)
                    j-=1
                else:
                    break

            j=i
            while j<len(S):
                if S[j]=="<":
                    A[j+1]=max(A[j+1],A[j]+1)
                    j+=1
                else:
                    break
        i+=1


    
    print(sum(A))

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
N = len(S)+1
a = [-1]*N
l = []
for i in range(1,N-1):
    if S[i-1] == '>' and S[i] == '<':
        a[i] = 0
        l.append(i)

if S[0] == '<':
    a[0] = 0
    l.append(0)
if S[-1] == '>':
    a[N-1] = 0
    l.append(N-1)

for i in l:
    j = i-1
    while 0<=j<=N-2 and S[j]=='>' and S[j-1]=='>':
        a[j] = a[j+1]+1
        j -= 1
    j = i+1
    while 0<=j<=N-3 and S[j]=='<' and S[j+1]=='<':
        a[j] = a[j-1]+1
        j += 1

for i in range(1,N-1):
    if a[i] == -1:
        a[i] = max(a[i-1],a[i+1])+1


if S[0] == '>':
    a[0] = a[1]+1

if S[-1] == '<':
    a[N-1] = a[N-2]+1

print(sum(a))

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
n = len(s)
f = [i for i in range(n+1)]
f = list(accumulate(f))
lst = []
lst2 = []
i = 0
flg = False
while i < n:
    cnt = 0
    tmp = s[i]
    while i < n:
        if tmp == s[i]:
            i += 1
            cnt += 1
        else:
            break
    if s[i-1] == '<':
        t = cnt
        flg = True
    elif flg:
        lst2.append(min(t,cnt))
        flg = False
    lst.append(f[cnt])
cnt = 0
print(sum(lst) - sum(lst2))

Solution (Python):
s=input()
ans=0
l=[]
c=1
now=s[0]
for i in s[1:]:
    if i==now:
        c+=1
    else:
        l.append(c)
        ans+=c*(c+1)//2
        now=i
        c=1
l.append(c)
ans+=c*(c+1)//2
if s[0]=="<":
    for i in range(len(l)//2):
       ans-=min(l[2*i],l[2*i+1])
else:
    for i in range((len(l)-1)//2):
       ans-=min(l[2*i+1],l[2*i+2])
print(ans)

Solution (Python):










def solve(s):
    n = len(s)
    t = [-1] * n

    if s[0] == '<':
        t[0] = 0
    if s[-1] == '>':
        t[-1] == 0

    for ind, (prev, after) in enumerate(zip(s, s[1:])):
        if prev + after == '><':
            t[ind + 1] = 0
    ku = 0
    hu = 0
    ans = 0
    for ind, c in enumerate(s):
        if t[ind] == 0:
            max_ = max(ku, hu)
            min_ = min(ku, hu)
            ans += (max_ + 1) * max_ // 2
            ans += (min_ - 1) * min_ // 2
            ku = 0
            hu = 0
        if c == '<':
            ku += 1
        else:
            hu += 1
    max_ = max(ku, hu)
    min_ = min(ku, hu)
    ans += (max_ + 1) * max_ // 2
    ans += (min_ - 1) * min_ // 2
    return ans


if __name__ == '__main__':
    s = input()
    print(solve(s))

Solution (Python):
S = input()
N = len(S) + 1
a = [0] * N

for i in range(-1, N-1):
    if (i == -1 and S[i+1] == '<') or (i == N - 2 and S[i] == '>') or (S[i] == '>' and S[i+1] == '<'):
        j = 0
        while i - j >= 0 and S[i-j] == '>' and a[i-j] < j + 1:
            a[i-j] = j + 1
            j += 1
        j = 0
        while i + 2 + j < N and S[i+1+j] == '<' and a[i+2+j] < j + 1:
            a[i+2+j] = j + 1
            j += 1
print(sum(a))