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



Sample input:
<>>


Sample output:
3

Submission status: Accepted

Solution code:
S = input()

results = []

l_delete_flag = False
r_delete_flag = False


if S[0] == ">":
    S = S + "S"
    cnt = 0
    i = 1
    while S[i] == ">":
        results.append(i + 1)
        i += 1
    results.append(1)
    S = S.rstrip("S")
    l_delete_flag = True


if S[-1] == "<":
    S = "S" + S
    i = len(S) - 1
    r = 1
    while S[i] == "<":
        results.append(r)
        r += 1
        i -= 1
    S = S.lstrip("S")
    r_delete_flag = True

if l_delete_flag:
    S = S.lstrip(">")


if r_delete_flag:
    S = S.rstrip("<")
    


if S:
    end_flag = False
    l_cnt = 0
    m_cnt = 0
    for i, s in enumerate(S):
        if end_flag:
            l_cnt = 0
            m_cnt = 0
            end_flag = False
        
        if s == "<":
            l_cnt += 1
        else:
            m_cnt += 1
        
        if (i == len(S) - 1) or ((s == ">") and (S[i + 1] == "<")):
            if l_cnt > m_cnt:
                results.append(int((l_cnt * (l_cnt + 1)) / 2))
                results.append(int((m_cnt * (m_cnt - 1)) / 2))
            else:
                results.append(int((m_cnt * (m_cnt + 1)) / 2))
                results.append(int((l_cnt * (l_cnt - 1)) / 2))
            end_flag = True

print(sum(results))

Submission status: Accepted

Solution code:
n = list(input())
counter = 1
directions = []
list = []
def add_up(x):
    added = (1/2) * x * (x + 1)
    return added
for i in range(len(n)-1):
    if n[i] == n[i+1]:
        counter += 1
    else:
        directions.append(counter)
        counter = 1
directions.append(counter)
if n[0] == "<":
    for i in range(0,len(directions)-1,2):
        if directions[i] >= directions[i+1]:
            directions[i+1] -= 1
        else:
            directions[i] -= 1
else:
    for i in range(1,len(directions)-1,2):
        if directions[i] >= directions[i+1]:
            directions[i+1] -= 1
        elif directions[i] < directions[i+1]:
            directions[i] -= 1
for direction in directions:
    list.append(add_up(direction))
print(round(sum(list)))

Submission status: Accepted

Solution code:
s=input()
a=[]
b=1
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        b+=1
    elif s[i]!=s[i+1]:
        a.append(b)
        b=1
    if i==len(s)-2:
        a.append(b)

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

else:
    if s[0]=="<":
        for i in range(0,len(a)-1,2):
            if a[i]>=a[i+1]:
                a[i+1]-=1
            else:
                a[i]-=1
    else :
        for i in range(1,len(a),2):
            if a[i]>=a[i+1]:
                a[i+1]-=1
            else:
                a[i]-=1   
#print(a)
def wa(n):
    return (n+1)*n//2
d=[]
if a==[]:
  print(1)
else:
  for i in range(len(a)):
    d.append(wa(a[i]))
  print(sum(d))

Submission status: Accepted

Solution code:
S = list(str(input()))

count = 0
count_bef = 0
count_list=[]
count_list2=[]
minus = []
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    count += 1
  elif not S[i] ==S[i+1]:
    count_list.append(int((count+1)*(count+2)/2))
    count_list2.append(count+1)
    count = 0
count_list.append(int((count+1)*(count+2)/2))
count_list2.append(count+1)
if S[0] == "<":
  for i in range(len(count_list2)//2):
    minus.append(min(count_list2[2*i], count_list2[2*i+1]))
elif S[0] == ">":
  try:
    for i in range(len(count_list2)//2):
      minus.append(min(count_list2[2*i+1], count_list2[2*i+2]))
  except IndexError:
    pass
#print(count_list)
#print(count_list2)
#print(minus)
print(sum(count_list)-sum(minus))

Submission status: Accepted

Solution code:
s = input()
s0 = s[0]
count = 1
nodes = []
ans = 0
for i in range(1, len(s)):
  if s0 == s[i]:
    count += 1
  elif s0 == '>':
    s0 = s[i]
    nodes.append(-count)
    count = 1
  else:
    s0 = s[i]
    nodes.append(count)
    count = 1
if s0 == '>':
  nodes.append(-count)
else:
  nodes.append(count)

for i in range(1, len(nodes)):
  if nodes[i] < 0:
    if abs(nodes[i]) > abs(nodes[i - 1]):
      nodes[i] = abs(nodes[i])
      nodes[i - 1] -= 1
    else:
      nodes[i] = abs(nodes[i])
      nodes[i] -= 1
nodes[0] = abs(nodes[0])
for i in nodes:
  ans += (i * (i + 1)) // 2
print(ans)

Submission status: Accepted

Solution code:
S = str(input())
l = 0
r = 0
ans = 0
tmp = S[0]
if tmp == ">":
    r = 1
else:
    l = 1
for i in range(1,len(S)):
    if tmp == ">" and S[i] == "<":
        ans += max(l, r) * (max(l, r) + 1) / 2 + min(l, r) * (min(l, r) - 1) / 2
        l = 1
        r = 0
        tmp = S[i]
    else:
        if S[i] == ">":
            r += 1
        else:
            l += 1
        tmp = S[i]
ans += max(l, r) * (max(l, r) + 1) / 2 + min(l, r) * (min(l, r) - 1) / 2
print(int(ans))

Submission status: Accepted

Solution code:
import sys

iter_num = 5 * (10**5) + 10
sys.setrecursionlimit(iter_num)

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

a = [0 for _ in range(n)]
next_num=0

def solve(num):
	global next_num
	if num == 0:
		if s[0] == '<':
			a[num] = 0
			next_num = 1
			return 0
		else:
			next_num = 1
			a[num] = solve(num+1) + 1
	if num == n-1:
		if s[n-2] == '>':
			next_num = -1
			a[num] = 0
			return 0
		else:
			a[num] = solve(num-1) + 1
			next_num = -1
			return a[num]

	if s[num-1] == '>' and s[num] == '<':
		next_num = num+1
		a[num] = 0
		return 0
	elif s[num-1] == '>' and s[num] == '>':
		a[num] = solve(num+1) + 1
		return a[num]
	elif s[num-1] == '<' and s[num] == '>':
		next_num = num+1
		a[num] = max(solve(num-1),solve(num+1)) + 1
		return a[num]
	else:
		#a[num] = solve(num-1) + 1
		a[num] = a[num-1] + 1
		next_num = num+1
		return a[num]

while next_num != -1:
	solve(next_num)

#print(a)
print(sum(a))

Submission status: Accepted

Solution code:
from sys import stdin

s = stdin.readline().rstrip()
li = [0]*(len(s)+1)
point = 0
for i in range(len(s)):
    if s[i] == "<":
        point += 1
        li[i+1] = point
    else:
        point = 0

li = li[::-1]
s = s[::-1]
ns = ""

for i in s:
    if i == "<":
        ns += ">"
    else:
        ns += "<"

point = 0

for i in range(len(ns)):
    if ns[i] == "<" and li[i+1] <= point:
        point += 1
        li[i+1] = point
    elif i == 0 or ns[i] == "<" and li[i+1] > point:
        point = 0
    else:
        point = 0

print(sum(li))

Submission status: Accepted

Solution code:
S = str(input())
a = []
ans = 0

def sumn(x):
    y = x * (x+1)//2
    return y
    
cnt = 1

if len(S) == 1:
    ans = 1
    print(ans)
else:
    for i in range(len(S)-1):
        if S[i+1] == S[i]:
            cnt += 1
            if i+1 == len(S)-1:
                a.append(cnt)
        
        else:
            a.append(cnt)
            cnt = 1
            if i+1 == len(S)-1:
                a.append(cnt)
            
    if S[0] == '<':
        for i in range(len(a)):
            if i % 2 == 0:
                ans += sumn(a[i])
            else:
                if a[i] > a[i-1]:
                    ans += (sumn(a[i]) - a[i-1])
                else:
                    ans += sumn(a[i]-1)
    else:
        for i in range(len(a)):
            if i == 0:
                ans += sumn(a[i])
            elif i % 2 == 1:
                ans += sumn(a[i])
            else:
                if a[i] > a[i-1]:
                    ans += (sumn(a[i]) - a[i-1])
                else:
                    ans += sumn(a[i]-1)
    print(ans)

Submission status: Accepted

Solution code:
S=input()
N=len(S)
ren=[]
a=1
if N==1:
  print(1)
  exit()
for i in range(N-1):
  if S[i]==S[i+1]:
    a+=1
  else:
    ren.append(a)
    a=1
  if i==N-2:
    ren.append(a)
      
M=len(ren)
ma=max(ren)
su=0
if S[0]=="<":
  if M%2==0:
    for k in range(M//2):
      if ren[2*k]<ren[2*k+1]:
        su=su+ren[2*k]*(ren[2*k]-1)//2+ren[2*k+1]*(ren[2*k+1]+1)//2
      else:
        su=su+ren[2*k]*(ren[2*k]+1)//2+ren[2*k+1]*(ren[2*k+1]-1)//2
  else:
    for k in range(M//2):
      if ren[2*k]<ren[2*k+1]:
        su=su+((ren[2*k]*(ren[2*k]-1))//2)+(ren[2*k+1]*(ren[2*k+1]+1)//2)
      else:
        su=su+ren[2*k]*(ren[2*k]+1)//2+ren[2*k+1]*(ren[2*k+1]-1)//2
    su+=ren[-1]*(ren[-1]+1)//2
if S[0]==">":
  su+=ren[0]*(ren[0]+1)//2  
  if (M-1)%2==0:
    for k in range((M-1)//2):
      if ren[1+2*k]<ren[1+2*k+1]:
        su=su+ren[1+2*k]*(ren[1+2*k]-1)//2+ren[1+2*k+1]*(ren[1+2*k+1]+1)//2
      else:
        su=su+ren[1+2*k]*(ren[1+2*k]+1)//2+ren[1+2*k+1]*(ren[1+2*k+1]-1)//2
  else:
    for k in range((M-1)//2):
      if ren[1+2*k]<ren[1+2*k+1]:
        su=su+ren[1+2*k]*(ren[1+2*k]-1)//2+ren[1+2*k+1]*(ren[1+2*k+1]+1)//2
      else:
        su=su+ren[1+2*k]*(ren[1+2*k]+1)//2+ren[1+2*k+1]*(ren[1+2*k+1]-1)//2
    su+=ren[-1]*(ren[-1]+1)//2
print(su)

Submission status: Accepted

Solution code:
def main():
    s = input()
    f1 = False
    f2 = False
    l = [1]
    if s[0] == "<":
        f1 = True
        l.insert(0,0)
    if s[-1] == ">":
        f2 = True
    sm = [i for i in range(0,len(s)+1)]
    for i in range(1,len(sm)):
        sm[i] = sm[i-1]+sm[i]
    lap = l.append
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            l[-1] += 1
        else:
            lap(1)
    dlist = []
    plist = []
    dap = dlist.append
    pap = plist.append
    if f2:
        l.append(0)
    for i in range(len(l)//2):
        dap(l[2*i])
        pap(l[2*i+1])   
    for i in range(1,len(dlist)):
        x = dlist[i]
        y = plist[i-1]
        if y <= x:
            dlist[i] = sm[x]
            plist[i-1] = sm[max([0,y-1])]
        else:
          dlist[i] = sm[max([0,x-1])]
          plist[i-1] = sm[y]
    dlist[0] = sm[dlist[0]]
    plist[-1] = sm[plist[-1]]
    print(sum(dlist)+sum(plist))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=list(input())
a=[0]*(len(s)+1)
if s[0]=='<':
    a[0]=0
    l=0
    while s[l]=='<':
        a[l+1]=a[l]+1
        l+=1
        if l>len(s)-1:
          break
if s[len(s)-1]=='>':
    a[len(s)]=0
    m=len(s)-1
    while s[m]=='>':
        a[m]=a[m+1]+1
        m-=1
        if m<0:
          break
for i in range(len(s)-1):
    if s[i]=='>' and s[i+1]=='<':
        a[i+1]=0
        k=i
        t=i+1
        while s[k]=='>':
            a[k]=a[k+1]+1
            k-=1
            if k<0 or k>len(s)-1:
                break
        while s[t]=='<':
            a[t+1]=a[t]+1
            t+=1
            if t<0 or t>len(s)-1:
                break
for i in range(len(s)-1):
    if s[i]=='<' and s[i+1]=='>':
        a[i+1]=max(a[i],a[i+2])+1
print(sum(a))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

S = input().strip()
a = 0
sum_a = 0
c = 0
for s in S:
    if s == '<':
        if c > a:
            sum_a += int((1 + c) * c / 2 - a)
            a = 1
            sum_a += 1
            c = 0
        elif c > 0:
            sum_a += int((c) * (c - 1) / 2)
            a = 1
            sum_a += 1
            c = 0
        else:
            a += 1
            sum_a += a
            c = 0
    else:
        c += 1
else:
    if c > a:
        sum_a += int((1 + c) * c / 2  - a)
        a = 1
        c = 0
    elif c > 0:
        sum_a += int((c) * (c - 1) / 2)
        a = 1
        c = 0

print(sum_a)

Submission status: Accepted

Solution code:
s=input()
n=len(s)+1
list=[]
stock1 = 0
stock2 = 0
for i in range(0,n-1):
    if s[i] == "<":
        if i==0 or (i>0 and s[i-1]==">"):
            for j in range(0,stock1+1):
                if j == 0:
                    list.append(max(stock1,stock2)-j)
                else:
                    list.append(stock1-j)
            stock1 = 0
            stock2 = 1
        else:
            list.append(list[i-1]+1)
            stock2 += 1
    else:
        stock1 += 1

if s[-1] == ">":
    for j in range(0,stock1+1):
        if j == 0:
            list.append(max(stock1,stock2)-j)
        else:
            list.append(stock1-j)
else:
    list.append(list[-1]+1)

ans=0
for i in list:
    ans += i

print(ans)

Submission status: Accepted

Solution code:
s = input()
b = s[0]
cou = 0


lis = []
for i in s:
    if b ==i:
        if i=='<':
            cou-=1
        else:
            cou+=1
    else:
        lis.append(cou)
        b = i
        if b=='<':
            cou =-1
        else:
            cou = 1
lis.append(cou)
l = len(lis)
sum = 0
if s[0]=='<':
    for i in range(0,l,2):
        if i+1 == l and (i+1)%2==1:
            a = abs(lis[i])
            sum += a*(a+1)//2
            break
        a = abs(lis[i])
        b = abs(lis[i+1])

        if a < b:
            sum += b*(b+1)//2
            sum += (a-1)*a//2
        else:
            sum += a*(a+1)//2
            sum += (b-1)*b//2
else:
    a = abs(lis[0])
    sum += a*(a+1)//2
    for i in range(1,l,2):
        if i+1 == l and (i+1)%2==0:
            a = abs(lis[i])
            sum += a*(a+1)//2
            break
        a = abs(lis[i])
        b = abs(lis[i+1])

        if a < b:
            sum += b*(b+1)//2
            sum += (a-1)*a//2
        else:
            sum += a*(a+1)//2
            sum += (b-1)*b//2

print(sum)

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)+1
a=[0]*n
x=[0]*n
y=[0]*n

for i in range(1,n):
  if s[i-1]=="<":
    x[i]=x[i-1]+1
  else:
    x[i]=0
    
for j in range(n-2,-1,-1):
  if s[j]==">":
    y[j]=y[j+1]+1
  else:
    y[j]=0

for k in range(0,n):
  a[k]=max(x[k],y[k])
  
print(sum(a))

Submission status: Accepted

Solution code:
s=list(input())

i=1
ans=0
mode=2
con=1
anslist=[]
if s[0]==">":
  mode=0
else:
  mode=1


while i!=len(s):

  if s[i]==">":
    if mode==0:
      con+=1
    else:
      anslist.append([con,"<"])



      mode=0
      con=1
  else:
    if mode==1:
      con+=1
    else:
      anslist.append([con,">"])

      mode=1
      con=1
  i += 1

if s[-1]=="<":
  anslist.append([con, "<"])

else:
  anslist.append([con, ">"])

i=0
while i!=len(anslist):
  try:
    if anslist[i][1]=="<":
      if anslist[i][0]<=anslist[i+1][0]:
        anslist[i][0]=anslist[i][0]-1
    else:
      if anslist[i][0]<=anslist[i-1][0] and i!=0:
        anslist[i][0]=anslist[i][0]-1

  except:
    pass
  i+=1



i=0
ans=0
while i!=len(anslist):
  ans+=anslist[i][0]*(anslist[i][0]+1)/2
  i+=1

print(int(ans))

Submission status: Accepted

Solution code:
from itertools import groupby
def sum_until_n(n): return (n+1)*n//2

S = groupby(input())
ans = 0
prv_r_val = 0
for key, value in S:
  value = list(value)
  len_value = len(value)
  if key == '<':
    ans += sum_until_n(prv_r_val+len_value) - sum_until_n(prv_r_val)
    prv_r_val += len_value
  else:
    if prv_r_val-len_value < 0:
      ans +=  abs(prv_r_val-len_value)     
      
    ans += sum_until_n(len_value-1)
    prv_r_val = 0      
print(ans)

Submission status: Accepted

Solution code:
s = input()


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

n = len(s)+1


def rui(n):
  return n*(n+1)//2
  

tani = []
yama = []

if s[0] =='<':
  tani.append(0)
elif s[0] =='>':
  yama.append(0)


for i in range(n-1):
  if s[i:i+2] =='><':
    tani.append(i+1)
  elif s[i:i+2] =='<>':
    yama.append(i+1)
    
if s[-1] =='>':
  tani.append(n-1)
elif s[-1] =='<':
  yama.append(n-1) 


count = 0
for i in range(len(yama)):
  d1 = yama[i]-tani[i]
  d2 = tani[i+1]- yama[i]
  count += rui(max(d1,d2)) + rui(min(d1,d2)-1)
  
print(count)

Submission status: Accepted

Solution code:
S = input()
#print(len(S))

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

s_list = []
head_num = 10**6 if S[0] == ">" else 0
s_list.append(head_num)
for i in range(len(S)-1):
    if S[i] == ">" and S[i+1] == "<":
        s_list.append(0)
    elif S[i] == "<":
        s_list.append(s_list[i]+1)
    else:
        s_list.append(s_list[i]-1)
if S[len(S)-1] == "<":
    s_list.append(s_list[len(S)-1] +1)
else:
    s_list.append(s_list[len(S)-1] -1)

#print(s_list)
count = 1
s_list[len(s_list)-1] = 0 if s_list[len(s_list)-1] < 0 else s_list[len(s_list)-1] 
#print(s_list)
for i in range(len(S)-1,0,-1):
    if S[i] == ">" and S[i-1] == ">":
    #if S[i] == ">":
        s_list[i] = count
        count += 1
    elif S[i] == ">" and S[i-1] == "<":
        s_list[i] = max(s_list[i-1], s_list[i+1]) + 1
    else:
        count = 1

if S[0] == ">":
    s_list[0] = s_list[1] + 1
if S[len(S)-1] == ">":
    s_list[len(s_list)-1] =  0

#print(len(s_list))
#print(s_list)

sum = 0
for i in range(len(s_list)):
    sum += s_list[i]

print(sum)

Submission status: Accepted

Solution code:
S = input()

N = len(S) + 1
L = []
counter = 0

for n in range(N-2):
    counter += 1
    if S[n]!=S[n+1]:
        L.append(counter)
        counter = 0
            
L.append(counter+1)

if S[0]=='<':
    K = len(L)//2
    for k in range(K):
        if L[2*k] <= L[2*k+1]:
            L[2*k] -= 1
        else:
            L[2*k+1] -= 1

else:
    K = (len(L)-1)//2
    for k in range(K):
        if L[2*k+1] <= L[2*k+2]:
            L[2*k+1] -= 1
        else:
            L[2*k+2] -= 1
        
print(sum([l*(l+1)//2 for l in L]))

Submission status: Accepted

Solution code:
S = list(input())
ans = 0
now = 0
count = 0
a = 0
for i in S:
  if i == "<":
    if count > 0:
      if count == 1:
        now = 0
        count = 0
      else:
        if count > now:
          ans = ans +  sum(range(1, count+1)) - now
          now = 0
          count = 0
        else:
          ans = ans + sum(range(1, count))
          now = 0
          count = 0
    now += 1
    a = 1
  else:
    if a == 1:
      ans += sum(range(1, now+1))
      a = 0
    count += 1
if a == 1:
  ans += sum(range(1, now+1))
elif count > 0:
  if count == 1:
    if now == 0:
      ans += 1
      now = 0
      count = 0
    else:
      now = 0
      count = 0
  else:
    if count > now:
      ans = ans +  sum(range(1, count+1)) - now
      now = 0
      count = 0
    else:
      ans = ans + sum(range(1, count))
      now = 0
      count = 0
  
print(ans)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
lst=[]
cnt=1

if n==2:
  print(1)
  
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
  else:
    lst.append(cnt)
    cnt-=(cnt-1)

if s[n-2]!=s[n-1]:
  lst.append(1)
else:
  lst.append(cnt)
    
totl=0
l=len(lst)
if s[0]=='<':
  for j in range(l):
    if j%2==0:
      totl+=(lst[j]*(lst[j]-1))//2
      totl+=max(lst[j],lst[min(j+1,l-1)])
    else:
      totl+=(lst[j]*(lst[j]-1))//2
  print(totl)
else:
  totl+=lst[0]
  for j in range(len(lst)):
    if j%2==1:
      totl+=(lst[j]*(lst[j]-1))//2
      totl+=max(lst[j],lst[min(j+1,l-1)])
    else:
      totl+=(lst[j]*(lst[j]-1))//2
  print(totl)

Submission status: Accepted

Solution code:
s = input()
t = s.split(">")
t2 = s.split("<")
a = []
b = []
for i in t:
    if i != '':
        a.append(i)

for i in t2:
    if i != '':
        b.append(i)
n = len(a)
n2 = len(b)
if len(b) == 1 and len(a) == 1:
    if s[0] == '>':
        y = len(b[0])
        x = len(a[0])
        sums = 0
        sums += (x * (x + 1)) / 2
        sums += (y * (y + 1)) / 2
        print(int(sums))
        exit()

tmp = min(n, n2)
sums = 0
if s[0] == '>' and s[len(s)-1] == '<':
    x = len(a[n-1])
    sums += (x * (x + 1)) / 2
    x = len(b[0])
    sums += (x * (x + 1)) / 2
    for i in range(tmp-1):
        x = len(a[i])
        y = len(b[i+1])
        if x > y:
            sums += (x * (x + 1)) / 2
            sums += (y * (y - 1)) / 2
        else:
            sums += (x * (x - 1)) / 2
            sums += (y * (y + 1)) / 2
    print(int(sums))
    exit()

if n2 > n:
    for i in range(tmp):
        x = len(a[i])
        y = len(b[i+1])
        if x > y:
            sums += (x * (x + 1)) / 2
            sums += (y * (y - 1)) / 2
        else:
            sums += (x * (x - 1)) / 2
            sums += (y * (y + 1)) / 2
elif n > n2:
    for i in range(tmp):
        x = len(a[i])
        y = len(b[i])
        if x > y:
            sums += (x * (x + 1)) / 2
            sums += (y * (y - 1)) / 2
        else:
            sums += (x * (x - 1)) / 2
            sums += (y * (y + 1)) / 2
elif n == n2:
    for i in range(tmp):
        x = len(a[i])
        y = len(b[i])
        if x > y:
            sums += (x * (x + 1)) / 2
            sums += (y * (y - 1)) / 2
        else:
            sums += (x * (x - 1)) / 2
            sums += (y * (y + 1)) / 2
if n > n2:
    x = len(a[n-1])
    sums += (x * (x + 1)) / 2
elif n2 > n:
    x = len(b[0])
    sums += (x * (x + 1)) / 2

print(int(sums))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# agc040/agc040_a
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    S = i2s()
    sum_r = 0
    ls_max = 0
    i = 0
    while i < len(S):
        if S[i] == '<':
            next_lg = S.find('>', i + 1)
            if next_lg == -1:
                next_lg = len(S)
            ls_len = next_lg - i
            ls_max = ls_len - 1
            sum_r += ls_len * (ls_len - 1) / 2
            i = next_lg
        else:
            next_ls = S.find('<', i + 1)
            if next_ls == -1:
                next_ls = len(S)
            lg_len = next_ls - i
            lg_max = lg_len
            sum_r += lg_len * (lg_len + 1) / 2
            if ls_max >= lg_max:
                sum_r += ls_max + 1 - lg_max
            i = next_ls
    if S[-1] == '<':
        sum_r += ls_max + 1
    print(int(sum_r))
    return

main()

Submission status: Accepted

Solution code:
S = str(input())
N = len(S) + 1
ans = 0

if S[0] == '<':
    pm = 1
else:
    pm = 0

inlist = [1]
for i in range(N - 2):
    if S[i] != S[i+1]:
        inlist.append(1)
    else:
        inlist[-1] += 1
if pm == 0:
    inlist.insert(0,1)
if len(inlist)%2 == 1:
    inlist.append(1)

for j in range(int(len(inlist)/2)):
    p = max(inlist[2*j], inlist[2*j+1])
    ans += ((p + 1) * p / 2)
    p = min(inlist[2*j], inlist[2*j+1]) - 1
    ans += ((p + 1) * p / 2)

print (int(ans))

Submission status: Accepted

Solution code:
S = input()
n = len(S)
S = S + " "
ans = 0
l = 0
r = 0
temp = 0

if S[0] == '<':
    temp = 0
else:
    for i in range(n):
        if S[i] == '>' and S[i + 1] == ('<'):
            temp = i+1
            break
        if i == n - 1:
            print((n + 1) * n // 2)
            exit()
ans += (temp) * (temp + 1) // 2
i = max(temp, 0)

while(i < n):
    while (i < n and S[i] == '<'):
        l += 1
        i += 1
    while (i < n and S[i] == '>'):
        r += 1
        i += 1
    l, r = min(l, r), max(l, r)
    ans += l * (l - 1) // 2 + r * (r + 1) // 2
    l = 0
    r = 0

l, r = min(l, r), max(l, r)
ans += l * (l - 1) // 2 + r * (r + 1) // 2
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

b=input()

i=0
left=0
right=0
previous=0
minleft=0
ans=0 

def process_peak(): 
    global left
    global minleft
    global right
    global ans
    if minleft > left:
        ans = ans + minleft
        ans = ans + left * (left -1) // 2
    else:
        ans = ans + left * (left +1) //2

    ans = ans + right * (right - 1 )//2
    # print("process",ans,left,right,minleft)
    minleft = right
    right = 0


for c in b:
    # print(c,previous)
    if c == ">": # Down 
        if previous == 0: # Start 
            left = 1
        elif previous == 1: # Beyond Peak
            # print("Beyond peak")
            process_peak()
            left = 1 
        else: # Keep down 
            left = left + 1
        previous = -1 
    else: # Up  
        if previous == 0: # Start 
            right  = 1
        elif previous == 1: # Keep up 
            right = right + 1 
        else: # Beyond Bottom 
            # print("Beyond Bottom")
            right = 1
        previous = 1
    # print(ans,left,right,minleft)


process_peak()
ans += minleft 
    
print(ans)

Submission status: Accepted

Solution code:
S = str(input())
N = len(S) + 1

ans = 0

con = 0
x = S[0]
i = 0
while x != "<":
  con += 1
  x = S[i]
  if x == ">":
    i += 1
  if i == N-1:
    x = "<"
ans += con * (con - 1) / 2

#print(i , N)
if i == N-1:
  ans += con
  print(int(ans))
  quit()
  
#print(i)
mae = 0
con = 1
x = "<"
i += 1
#print(i,N)
while i < N - 1:
  if S[i] == x:
    #print(i)
    con += 1
  else:
    ans += con * (con-1)/2
    if S[i] == ">":
      mae = con
    else:
      ans += max(con, mae)
      mae = 0
    con = 1
    x = S[i]
    #print(ans, i, con, mae)
  i += 1
#print(ans, con)
ans += con * (con-1)/2 
#print(ans)
ans += max(con, mae)

print(int(ans))

Submission status: Accepted

Solution code:
s = input()

i = 0
li = list()
while i < len(s):
    lc = 0
    while i < len(s) and s[i] == '<':
        lc += 1
        i += 1
    gc = 0
    while i < len(s) and s[i] == '>':
        gc += 1
        i += 1
    li.append((lc, gc))

ans = 0
for e in li:
    g = max(e)
    l = min(e)
    l = l - 1 if l > 0 else 0
    ans += int(g * (g + 1) / 2 + l * (l + 1) / 2)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
S = input()

p = [0] * (len(S) + 1)
m = [0] * (len(S) + 1)

for i in range(len(S)):
    if S[i] == '<':
        p[i + 1] = p[i] + 1

for i in range(len(S) - 1, -1, -1):
    if S[i] == '>':
        m[i] = m[i + 1] - 1

p = np.array(p, np.int64)
m = np.array(m, np.int64) * (-1)
answer = np.maximum(p, m)
print(answer.sum())

Submission status: Accepted

Solution code:
S = str(input())

tmp_list = [0]
bigger = True
for i in range(len(S)):
    if (bigger and S[i] == '<') or (not bigger and S[i] == '>'):
        tmp_list[-1] += 1
    else:
        tmp_list.append(1)
        bigger = not bigger

if len(tmp_list) % 2 == 1:
    tmp_list.append(0)

def func(x):
    return x * (x + 1) // 2

ans = 0
for i in range(len(tmp_list) // 2):
    a = tmp_list[i * 2]
    b = tmp_list[i * 2 + 1]
    if a > b:
        a, b = b, a
    ans += func(a - 1)
    ans += func(b)

print(ans)

Submission status: Accepted

Solution code:
import math
def till_sum(n):
    return((n) * (n - 1) / 2)
s = list(input())
count = 0
ls = []
for i in range(len(s)):
    count += 1
    if len(s) - 1 == i:
        break
    else:
        if s[i] != s[i+1]:
            ls.append(count)
            count = 0
ls.append(count)
result = sum(map(till_sum, ls))
if s[0] == "<":
    for i in range(math.ceil(len(ls)/2)):
        if len(ls) - 1 == 2*i:
            result += ls[2*i]
        else:
            if ls[2*i] < ls[2*i + 1]:
                result += ls[2*i + 1]
            if ls[2*i] >= ls[2*i + 1]:
                result += ls[2*i]
else:
    result += ls[0]
    ls = ls[1:]
    for i in range(math.ceil(len(ls)/2)):
        if len(ls) - 1 == 2*i:
            result += ls[2*i]
        else:
            if ls[2*i] < ls[2*i + 1]:
                result += ls[2*i + 1]
            if ls[2*i] >= ls[2*i + 1]:
                result += ls[2*i]
print(int(result))

Submission status: Accepted

Solution code:
import sys
from itertools import groupby
inputs = sys.stdin.readlines()
s = inputs[0].strip()

p = [len(list(t[1])) for t in groupby(s)]
p_ = []
if s[0] == '<':
    for i in range(0, len(p), 2):
        try:
            p_.append((p[i], p[i+1]))
        except IndexError:
            p_.append((p[i], 0))
else:
    p_.append((0, p[0]))
    for i in range(1, len(p), 2):
        try:
            p_.append((p[i], p[i+1]))
        except IndexError:
            p_.append((p[i], 0))

ans = 0
for r1, r2 in p_:
    if r1 > r2:
        ans += (r1+1)*r1 // 2 + r2*(r2-1) // 2
    else:
        ans += (r2+1)*r2 // 2 + r1*(r1-1) // 2
        
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = list(input().rstrip())

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

        if len(S2) == 1:
            print((S2[0][1] * (S2[0][1] + 1)) // 2)
            exit()

        ans = 0
        for i in range(len(S2)):
            ans += (S2[i][1] * (S2[i][1] + 1)) // 2

        for i in range(len(S2) - 1):
            if S2[i][0] == '>' and S2[i + 1][0] == '<':
                continue
            else:
                ans -= min(S2[i][1], S2[i + 1][1])
        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
s = input()
s += '!'

sn = 0

pvl = 0
psl = 1

ch = 0
if(s[0]=='>'):
    ch = 0
else:
    ch = 1

for i in range(len(s)):
    
    if(s[i]=='<'):
        if(s[i]==s[i+1]):
            psl += 1

        else:
            sn += psl*(psl-1)//2
            pvl = psl
            psl = 1
            ch = 1

    
    if(s[i]=='>'):
        if(s[i]==s[i+1]):
            psl += 1

        else:

            sn += psl*(psl+1)//2
            if(psl>=pvl):
                sn = sn
            else:
                sn += pvl-psl
            pvl = psl 
            psl = 1
            ch = 0
    
if(ch):
    sn += pvl

         
print(sn)

Submission status: Accepted

Solution code:
slist=list(input())
kosu=[]
n=len(slist)
counter=1
for i in range(n-1):
	if slist[i]==slist[i+1]:
		counter+=1
	else:
		kosu.append(counter)
		counter=1
kosu.append(counter)

nagasa=len(kosu)
kotae=0
if nagasa==1:
	kotae=kosu[0]*(kosu[0]+1)/2
else:
	if slist[0]=="<":
		if nagasa%2==0:
			for i in range(0,nagasa,2):
				kotae+=(kosu[i]*(kosu[i]+1)+kosu[i+1]*(kosu[i+1]+1))/2-min(kosu[i],kosu[i+1])
		else:
			for i in range(0,nagasa-1,2):
				kotae+=(kosu[i]*(kosu[i]+1)+kosu[i+1]*(kosu[i+1]+1))/2-min(kosu[i],kosu[i+1])
			kotae+=kosu[-1]*(kosu[-1]+1)/2
	else:
		if nagasa%2!=0:
			for i in range(1,nagasa,2):
				kotae+=(kosu[i]*(kosu[i]+1)+kosu[i+1]*(kosu[i+1]+1))/2-min(kosu[i],kosu[i+1])
			kotae+=kosu[0]*(kosu[0]+1)/2
		else:
			for i in range(1,nagasa-1,2):
				kotae+=(kosu[i]*(kosu[i]+1)+kosu[i+1]*(kosu[i+1]+1))/2-min(kosu[i],kosu[i+1])
			kotae+=kosu[0]*(kosu[0]+1)/2
			kotae+=kosu[-1]*(kosu[-1]+1)/2
kotae=int(kotae)
print(kotae)