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()
s_len = len(S)

s_kigo_map = [] # [[0,1],[1,2]] 0 = < , 1 = >
init_s = S[0]
right_count = 0
left_count = 0
max_len = 0
if S[0] == '<':
    s_num = 0
else:
    s_num = 1
s_count = 1
for i in range(1,s_len):
    if S[0] == '<':
        right_count += 1
    else:
        left_count += 1
    if init_s == S[i]:
        s_count += 1
    else:
        if max_len < s_count:
            max_len = s_count
        s_kigo_map.append([s_num,s_count])
        if S[i] == '<':
            s_num = 0
        else:
            s_num = 1
        init_s = S[i]
        s_count = 1
s_kigo_map.append([s_num,s_count])

next_num = 0
sum_num = 0

i = 0
if len(s_kigo_map) == 1:
    sum_num += (0 +s_kigo_map[i][1] )*(s_kigo_map[i][1]+1)//2
    print(sum_num)
else:
    if s_kigo_map[i][0] == 0: # <の時
        if s_kigo_map[i][1] >= s_kigo_map[i+1][1]:
            sum_num += (0 +s_kigo_map[i][1] )*(s_kigo_map[i][1]+1)//2
            #next_num = next_num + s_kigo_map[i][0]
        else: # `差分を増やす必要がある`
            sum_num += ( (0 +s_kigo_map[i][1] )*(s_kigo_map[i][1]+1)//2 )+ ( s_kigo_map[i+1][1] - s_kigo_map[i][1])
            #next_num = next_num + s_kigo_map[i+1][0] 
    else:  # >の時
        sum_num += (0 +s_kigo_map[i][1] )*(s_kigo_map[i][1]+1)//2
        #next_num = 0

    for i in range(1,len(s_kigo_map)):
        if s_kigo_map[i][0] == 0: # <の時
            if i == len(s_kigo_map) -1:
                sum_num += (1 +s_kigo_map[i][1] )*s_kigo_map[i][1]//2
                break
            if s_kigo_map[i][1] >= s_kigo_map[i+1][1]:
                sum_num += (1 +s_kigo_map[i][1] )*s_kigo_map[i][1]//2
                #next_num = next_num + s_kigo_map[i][0]
            else: # `差分を増やす必要がある`
                sum_num += ( (1 +s_kigo_map[i][1])*s_kigo_map[i][1]//2 ) + ( s_kigo_map[i+1][1] - s_kigo_map[i][1])
                #next_num = next_num + s_kigo_map[i+1][0] 
        else:  # >の時
            sum_num += (s_kigo_map[i][1] - 1 )*(s_kigo_map[i][1])//2
            #next_num = 0
    print(sum_num)

Submission status: Accepted

Solution code:
S = input()
inc = 0
dec = 0
sum = 0

def solve(i, d):
	ans = 0
	if i >= d : 
		i += 1
	elif i < d:
		d += 1
	for c in range(i):
		ans += c
	for c in range(d):
		ans += c
	return ans

for i in range(len(S)):
	if S[i] == '<':
		if dec > 0:
			sum += solve(inc, dec)
			inc = 0
			dec = 0
		inc += 1
	if S[i] == '>':
		dec += 1
#	print(i, inc, dec, sum)
sum += solve(inc, dec)

print(sum)

Submission status: Accepted

Solution code:
s = input()

def pa(ai, an, n):
    #print(ai,an,n)
    s = (ai+an)*n//2
    #print('pa',s) 
    return s


c = []
ant = s[0]
cont = 1
for i in s[1:]:
    if i != ant:    
        c.append((cont, ant))
        ant = i
        cont = 1
    else:   
        cont += 1
        
c.append((cont, ant))

soma = 0

for x in range(len(c)):
    i = c[x]
    if i[1] == '<':
        soma += pa(0, i[0]-1, i[0])
    else:
        soma += pa(i[0], 1, i[0])
        if x>0:
            off = max(0, c[x-1][0] - i[0])
            #print('off',off)
            soma += off

if c[-1][1] == '<':
    soma += c[-1][0]
    
print(soma)

Submission status: Accepted

Solution code:

s = input()

small_mode = s[0] == '<'
initial_small_mode = small_mode
idx = 0
nums = []
while idx < len(s):
    if small_mode:
        num = 0
        while idx < len(s) and s[idx] == '<':
            num += 1
            idx += 1
        nums.append(num)
        if idx >= len(s):
            break
        else:
            small_mode = not small_mode
    elif not small_mode:
        num = 0
        while idx < len(s) and s[idx] == '>':
            num += 1
            idx += 1
        nums.append(num)
        if idx >= len(s):
            break
        else:
            small_mode = not small_mode

seq = []


if initial_small_mode:
    seq.append(0)
else:
    seq.append(nums[0])

for idx, run in enumerate(nums):
    if initial_small_mode:
        for i in range(1, run + 1):

            if i == run and idx < len(nums) -1 and nums[idx + 1] > run:
                seq.append(nums[idx + 1])
            else:
                seq.append(i)
    else:
        for i in range(run - 1, -1, -1):
            seq.append(i)
    initial_small_mode = not initial_small_mode

# print(nums)
# print(sum(nums))
# print(seq)
# print(len(seq))
print(sum(seq))

# 0<@3>2>1>@0<1<@2>0<1<2<3<4<5>2>1>0<1

Submission status: Accepted

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

tmp = S[0]
cnt = 0
cnt2 = 0
l = []
for i in range(N-1):
    if S[i] == tmp:
        cnt += 1
    else:
        tmp = S[i]
        l.append(cnt)
        cnt = 1

else:
    l.append(cnt)


if S[0] == '>':
    l = [1] + l    
if len(l) % 2 == 1:
    l.append(1)

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

print(ans)

Submission status: Accepted

Solution code:
S = input().strip()
count=1
a=[]
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        count=count+1
    else:
        a.append(count)
        count=1
    
if S[len(S)-1]!=S[len(S)-2]:
    a.append(1)
else:
    a.append(count)
sa=0
for i in range(len(a)-1):
    if S[0]=='<':
        if i%2==0:
            sa=sa+min(a[i],a[i+1])
    else:
        if i%2==1:
            sa=sa+min(a[i],a[i+1])
wa=0
for i in range(len(a)):
    wa=wa+((a[i]+1)*a[i]/2)
print(int(wa-sa))

Submission status: Accepted

Solution code:
from itertools import groupby
s=[[k, len(list(g))] for k, g in groupby(list(input()))]
a=[0]
for i in s:
    if i[0]=="<":
        n=0
        if a[-1] == 0:
            a.pop(-1)
        for j in range(i[1]+1):
            a.append(n)
            n+=1
    else:
        n=i[1]
        if a[-1] <= n:
            a.pop(-1)
        else:
            n-=1
        for j in range(i[1]+1):
            a.append(n)
            if n == 0:
                break
            n-=1
print(sum(a))

Submission status: Accepted

Solution code:
S = input()
n = len(S)
ans = 0
p = 0
q = 0
m = 1
s = ['>']
for i in range(n):
    s.append(S[i])
s.append('<')
for i in range(1,n+2):
    if s[i] == '<':
        if s[i-1] == '>':
            if p > 0:
                ans += (1-m)*(q-1)
            else:
                ans += (1-m)*q
            p = 0
            q = 0
            m = 1
        else:
            p += 1
            ans += p
    else:
        if s[i-1] == '<':
            p += 1
            q += 1
        else:
            p += -1
            q += 1
        m = p
        ans += p
    
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)+1
ans = 0
ls = [0]*n

tmp = 0

for i in range(n-1):
    if s[i] == "<":
        if i < n-2:
            if s[i+1] == "<":
                    tmp += 1
                    ans += tmp
            else:
                tmp += 1
                ans += tmp
                ls[i+1] = tmp
                tmp = 0
        else:
            tmp += 1
            ans += tmp
    else:
        tmp = 0
tmp = 0

for i in range(n-1):
    if s[n-i-2] == ">":
        if n-i-2 > 0:
            if s[n-i-3] == ">":
                tmp += 1
                ans += tmp
            else:
                tmp += 1
                if ls[n-i-2] < tmp:
                    ans += tmp - ls[n-i-2]
                tmp = 0
        else:
            tmp += 1
            ans += tmp

print(ans)

Submission status: Accepted

Solution code:
S = input()
from itertools import groupby
S_length = len(S)
k = 1
s_l=[k*len(list(g)) for k, g in groupby(S)]
  
        
        
def n_sum(n):
  return n*(n+1)//2
        

s_l_length = len(s_l)
answer=0
if s_l[0][0]=='<':
  if s_l_length%2==0:
    for i in range(0,s_l_length,2):
      s_1=s_l[i]
      s_2=s_l[i+1]
      s_1_l=len(s_1)
      s_2_l=len(s_2)
      if s_1_l<=s_2_l:
        answer+=n_sum(s_1_l-1)+n_sum(s_2_l)
      else:
        answer+=n_sum(s_1_l)+n_sum(s_2_l-1)
  else:
    for i in range(0,s_l_length,2):
      if i==s_l_length-1:
        answer+=n_sum(len(s_l[i]))
      else:
        s_1=s_l[i]
        s_2=s_l[i+1]
        s_1_l=len(s_1)
        s_2_l=len(s_2)
        if s_1_l<=s_2_l:
          answer+=n_sum(s_1_l-1)+n_sum(s_2_l)
        else:
          answer+=n_sum(s_1_l)+n_sum(s_2_l-1)
          
else:
  answer+=n_sum(len(s_l.pop(0)))
  s_l_length-=1
  if s_l_length%2==0:
    for i in range(0,s_l_length,2):
      s_1=s_l[i]
      s_2=s_l[i+1]
      s_1_l=len(s_1)
      s_2_l=len(s_2)
      if s_1_l<=s_2_l:
        answer+=n_sum(s_1_l-1)+n_sum(s_2_l)
      else:
        answer+=n_sum(s_1_l)+n_sum(s_2_l-1)
  else:
    for i in range(0,s_l_length,2):
      if i==s_l_length-1:
        answer+=n_sum(len(s_l[i]))
      else:
        s_1=s_l[i]
        s_2=s_l[i+1]
        s_1_l=len(s_1)
        s_2_l=len(s_2)
        if s_1_l<=s_2_l:
          answer+=n_sum(s_1_l-1)+n_sum(s_2_l)
        else:
          answer+=n_sum(s_1_l)+n_sum(s_2_l-1)
  
      
print(answer)

Submission status: Accepted

Solution code:
s = list(input())
sc = [0]*(len(s)+1)
key = 0
pre = ''
n = len(s)
        
if s[0] == '<':
    sc[0] = 0
    sc[1] = 1
    pre = 'l'
else:
    key += 1
    pre = 'r'
    
for i in range(1,n):
    if pre == 'l':
        if s[i] == '<':# <a<
            sc[i] = sc[i-1] + 1
            pre = 'l'
        else:#<a>
            sc[i] = sc[i-1] + 1
            key += 1
            pre = 'r'
    else:
        if s[i] == '<':# > a <
            while key > 0:
                sc[i-key] = max(key,sc[i-key])
                key = key -1
            sc[i] = 0
            sc[i+1] = 1
            pre = 'l'
        else:# > a >
            key += 1
            pre = 'r' 
    
if pre == 'l':
    sc[n] = sc[n-1] + 1
else:
    while key > 0:
        sc[n-key] = max(key,sc[n-key])
        sc[n] = 0
        key = key -1
        
print(sum(sc))

Submission status: Accepted

Solution code:
moji = list(input())
listlist=[]
inlist=[]
result=0
for i in range(len(moji)-1):
    if moji[i] == '>' and moji[i+1]=='<':
        listlist.append(inlist)
        result += 1 + max([inlist.count(1),inlist.count(2)])
        for m in range(inlist.count(1)+1):
            result += m
        for n in range(inlist.count(2)+1):
            result += n
        inlist=[]
    elif moji[i] == '>' and moji[i+1]=='>':
            inlist.append(1)
    elif moji[i] == '<' and moji[i+1]=='<':
            inlist.append(2)
    elif moji[i] == '<' and moji[i+1]=='>':
            inlist.append(3)
if moji[len(moji)-2] == '>' and moji[len(moji)-1]=='<':
    result +=1
elif moji[len(moji)-2] == '>' and moji[len(moji)-1]=='>':
    result += 1 + max([inlist.count(1),inlist.count(2)])
    for m in range(inlist.count(1)+1):
        result += m
    for n in range(inlist.count(2)+1):
        result += n
elif moji[len(moji)-2] == '<' and moji[len(moji)-1]=='<':
        result += 1 + max([inlist.count(1),inlist.count(2)])
        for m in range(inlist.count(1)+1):
            result += m
        for n in range(inlist.count(2)+1):
            result += n
elif moji[len(moji)-2] == '<' and moji[len(moji)-1]=='>':
        result += 1 + max([inlist.count(1),inlist.count(2)])
        for m in range(inlist.count(1)+1):
            result += m
        for n in range(inlist.count(2)+1):
            result += n
print(result)

Submission status: Accepted

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

from collections import deque

ans = []
_x = s[0]
l= 1
for x in s[1:]:
    if x == _x:
        l += 1
    else:
        ans.append(l)
        _x = x
        l = 1
ans.append(l)

ans = deque(ans)
# print(ans)
# import math

_ans = 0
if s[0] == '<':
    while len(ans)>1:
        a = ans.popleft()
        b = ans.popleft()
        # print(a,b)
        _ans += (max(a,b)*(max(a,b)+1))//2
        # print(_ans)
        _ans += (min(a,b)-1)*(min(a,b))//2
        # print(_ans)
    if len(ans)==1:
        c = ans.popleft()
        _ans += c*(c+1)//2
        # print(_ans)

else:
    d = ans.popleft()
    _ans += d*(d+1)//2
    # print(_ans)

    while len(ans)>1:
        a = ans.popleft()
        b = ans.popleft()
        # print(a,b)
        _ans += (max(a,b)*(max(a,b)+1))//2
        # print(_ans)
        _ans += (min(a,b)-1)*(min(a,b))//2
        # print(_ans)

    if len(ans)==1:
        c = ans.popleft()
        _ans += c*(c+1)//2
        # print(_ans)

print(_ans)

Submission status: Accepted

Solution code:
s = str(input())
s_length = len(s)
answer = 0
cnt = 0
t = 0
_t = 0
for i in range(s_length):
  if s[i] == '<':
    t += 1
    cnt = 0
    answer += t
  else:
    if t == 0:
      if _t <= cnt:
        t = 0
        cnt += 1
        answer += cnt 
      else:
        t = 0
        cnt += 1
        answer += cnt - 1
    else:
      _t = t
      t = 0
      answer += cnt * 1
      cnt += 1
print(answer)

Submission status: Accepted

Solution code:
s = input()
lr = []
if s[len(s)-1] == '>':
    s_add = s + '<'
else:
    s_add = s + '>'
cnt = 1
for i in range(1,len(s_add)):
    if s_add[i] == s_add[i-1]:
        cnt += 1
    else:
        lr.append(cnt)
        cnt = 1
# print(lr)
if s[0] == '<':
    num = 0
    for i,val in enumerate(lr):
        if val != 1:
            for j in range(1,val):
                num += j
        if i%2 == 0:
            if i+1 > len(lr)-1:
                num += lr[i]
            else:
                num += max(lr[i],lr[i+1])
else:
    num = 0
    for i,val in enumerate(lr):
        if val != 1:
            for j in range(1,val):
                num += j
        if i%2 == 1:
            if i+1 > len(lr)-1:
                num += lr[i]
            else:
                num += max(lr[i],lr[i+1])
        if i == 0:
            num += lr[i]
    

print(num)

Submission status: Accepted

Solution code:

S = input()
N = len(S) + 1
result = [ -1 for i in range(N) ]

def countContinueChar(index):
    startChar = S[index]
    count = 0
    for i in range(index, N-1):
        if S[i] != startChar:
            break
        count += 1
    return count

def getValueOfIndex(index, current, prev = '', preprev = ''):
    if S[index] == '<':
        if prev == '>':
            return 0
        else:
            return current + 1
    else:
        if prev == '<':
            v = countContinueChar(index)
            if v < current + 1:
                return current + 1
            else:
                return v
        else:
            if preprev == '<' and prev == '>':
                return countContinueChar(index)
            else:
                if prev != '>':
                    return countContinueChar(index)
                else:
                    return current - 1

current = -1
prev = ''
preprev = ''
sum = 0
for i in range(N):
    if i == N-1:
        if S[-1] == '<':
            result[i] = current + 1
            sum += current + 1
        #else:
            #result[i] = 0
        break
    if i > 0:
        prev = S[i - 1]
    if i > 1:
        preprev = S[i - 2]
    current = getValueOfIndex(i, current, prev, preprev)
    #result[i] = current
    sum += current

#print(result)
print(sum)

Submission status: Accepted

Solution code:
s = list(input())
N = len(s)
a = [0] * (10+N)
if(s[0] == '<'): p = 0
else: p = 1
count = 0
count2 = 0
A = 0
start = 0
end = 0

for i in range(N):
    if ((s[i] == '>')& (p == 0)):
        #print('a')
        count2 += 1
        p = 1
        max_ = i
    elif(p == 0):
        #print('b')
        count += 1
    elif(s[i] == '<'):
        #print('c')
        end = i
        if(count > count2):
            count2 -=1
            A += count*(count+1)/2
            A += count2*(count2+1)/2
        else:
            count -=1
            A += count*(count+1)/2
            A += count2*(count2+1)/2
        p = 0
        start = i
        count = 1
        count2 = 0
        max_ = 0
    else:
        #print('d')
        count2 += 1
if(count > count2):
    count2 -=1
    A += count*(count+1)/2
    A += count2*(count2+1)/2
else:
    count -=1
    A += count*(count+1)/2
    A += count2*(count2+1)/2
print(int(A))

Submission status: Accepted

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    S = args[0]
    A = [0 for _ in range(len(S)+1)]

    h = []

    cnt = 0

    # for n in range(len(S)):

    #     if 0 < n and S[n-1] != S[n]:
    #         h.append(cnt)
    #     else:
    #         cnt += 1 if S[n] == '<' else -1

    # for n in range(1, len[h]):
    #     if abs(h[n-1]) < abs(h[n]):
    #         for i in range(h[n-1]):
    #         h.append(cnt)
    #     else:

    m = 0
    for n in range(1, len(S)+1):
        if 0 < m:
            A[n] = m
            m -= 1
        elif S[n-1] == '<':
            A[n] = A[n-1]+1
        else:
            m = 0
            while n+m < len(S) and S[n+m] == '>':
                m += 1

            A[n-1] = max(m+1, A[n-1])
            A[n] = m
            m -= 1

    # for n in range(0, len(S)):
    #     print('{}{}'.format(A[n], S[n]), end='')
    # print(A[-1])

    return str(sum(A))


if __name__ == "__main__":
    print(solve(*[input() for _ in range(1)]))

Submission status: Accepted

Solution code:
S = list(input())
A = []
i = 0
if S[0] == "<":
    temp = [0,1]
else:
    temp = [0,-1]
for i in range(1,len(S)):
    if S[i-1] == "<" and S[i] == ">":
        A.append(temp)
        temp = [0]   
        if S[i] == ">":
            temp.append(temp[-1]-1)
        else:
            temp.append(temp[-1]+1)
    else:
        if S[i] == ">":
            temp.append(temp[-1]-1)
        else:
            temp.append(temp[-1]+1)
A.append(temp)

sumj = 0
for j in A:
    minj = min(j)
    for k in range(len(j)):
        j[k] += -minj
    sumj += sum(j)
m = []
for l in range(len(A)-1):
    m.append(min(A[l][-1],A[l+1][0]))

print(sumj-sum(m))

Submission status: Accepted

Solution code:
s = input()

total_sum = 0
minus = 0
plus = 0
sum = 0

for i in s:
    if i == '<':
        if minus > 0:
            if plus < minus:
                sum = (int)((minus * (minus + 1) + plus * (plus - 1)) / 2)
            else:
                sum = (int)((minus * (minus - 1) + plus * (plus + 1)) / 2)
            minus = 0
            plus = 0
            total_sum = total_sum + sum
        plus = plus + 1
    else:
        minus = minus + 1

if plus < minus:
    sum = (int)((minus * (minus + 1) + plus * (plus - 1)) / 2)
else:
    sum = (int)((minus * (minus - 1) + plus * (plus + 1)) / 2)

print(total_sum + sum)

Submission status: Accepted

Solution code:
S = input()

ans = [0] * (len(S) + 1)


prev = ""
for i in range(len(S)):
    # print(S[i])
    if S[i] == "<":
        ans[i + 1] = ans[i] + 1
        prev = "<"

    # print(ans)
ans.reverse()
for i in range(len(S)):
    # if S[len(S) - 1 - i] == ">":
    #     print("<")
    # else:
    #     print(">")
    if S[len(S)-1-i] == ">" and not ans[i] < ans[i+1]:
        ans[i + 1] = ans[i] + 1
        prev = ">"
    # print(ans)
# print()
print(sum(ans))

Submission status: Accepted

Solution code:
s = str(input())
 
s_length = len(s)
 
answer = 0
cnt = 0
t = 0
_t = 0
for i in range(s_length):
  if s[i] == '<':
    t += 1
    cnt = 0
    answer += t
  else:
    if t == 0:
      if _t <= cnt:
        t = 0
        cnt += 1
        answer += cnt 
      else:
        t = 0
        cnt += 1
        answer += cnt - 1
    else:
      _t = t
      t = 0
      answer += cnt * 1
      cnt += 1
      
print(answer)

Submission status: Accepted

Solution code:
S = input()
if S[0] == ">":
  S = "<" + S
if S[-1] == ">":
  S = S + "<"
elif S[-1] == "<":
  S = S + "><"
L = []
T = []

now = S[0]
for i in range(len(S)):
  if now == ">" and S[i] == "<":
    now = S[i]
    L.append(2)
  elif now == "<" and S[i] == ">":
    now = S[i]
    L.append(1)
  else:
    L.append(0)    
if L.count(2) == 0:
  L.append(2)
  
countl, countr = 0, 0
flag = 0
for i in range(len(L)):
  if L[i] == 0 and flag == 0:
    countl += 1
  elif L[i] == 0 and flag == 1:
    countr += 1
  elif L[i] == 1:
    flag = 1
    countr += 1
  else: # L[i] == 2
    if countl > countr:
      T.append((countl)*(countl+1)//2)
      T.append((countr)*(countr-1)//2)
    else:
      T.append((countl)*(countl-1)//2)
      T.append((countr)*(countr+1)//2)
    flag = 0
    countl, countr = 1, 0

print(sum(T))

Submission status: Accepted

Solution code:
s=input()
if s[-1]=='<':
  s+='>e'
else:
  s+='ee'

result=0
count=0
count2=0
pre='s'
for i in range(len(s)-1):
  count+=1
  if s[i]!=s[i+1]:
    if s[i]=='>':
      count-=1
      result+=count*count/2+count/2
      count+=1
      result+=max(count,count2)
    else:
      count-=1
      result+=count*count/2+count/2
      count2=count+1
    count=0
print(int(result))

Submission status: Accepted

Solution code:
# coding:utf-8

S = input()

# "<" then flag=0
# ">" then flag=1

if(S[0] == "<"):
    flag=0
else:
    flag=1

sum_count = 0
count=1
tmp_count = 1
for i in range(1, len(S)):
    if(S[i-1] == "<" and S[i] == "<"):
        count+=1
        tmp_count += 1
        
    elif(S[i-1] == "<" and S[i] == ">"):
        sum_count += int(((count-1)*count)/2)
        #reset
        count=1
        
    elif(S[i-1] == ">" and S[i] == "<"):
        if(tmp_count > count):
            sum_count += int(((count+1)*count)/2 + (tmp_count - count))
        else:
            sum_count += int(((count+1)*count)/2)
        #reset
        count=1
        tmp_count = 1
        
    elif(S[i-1] == ">" and S[i] == ">"):
        count+=1

if(S[-1] == ">"):
    if(tmp_count > count):
        sum_count += int(((count+1)*count)/2 + (tmp_count - count))
    else:
        sum_count += int(((count+1)*count)/2)
else:
    sum_count += int(((count+1)*count)/2)
  
  
print(sum_count)

Submission status: Accepted

Solution code:
def f(x):
    return x*(x+1)//2


s = input()
ans = 0
if len(set(s)) == 1:
    ss = len(s)
    ans = ss*(ss+1)//2
    print(ans)
    exit()

ss = s.replace('><' , '> <')
lst = ss.split()

for l in lst:
    a = l.count('<')
    b = l.count('>')
    
    if a == 0:
        ans += f(b)
    elif b == 0:
        ans += f(a)
    else:
        mn = min(a,b)
        mx = max(a,b)
        ans += f(mx) + f(mn-1)
print(ans)

Submission status: Accepted

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

i = 0
total = 0

def suma(n):
    if n <= 1:
        return 0
    return (n-1)*n//2

while True:
    j = S.find('><', i)
    if j == 0:
        total += 1
        i = j+1
    elif j > 0:
        t = S[i:j+1]
        tl = len(t)
        tc_s = t.count('<')
        tc_b = t.count('>')
        if t == tc_s or tl == tc_b:
            total += suma(tl+1)
        elif tc_s >= tc_b:
            total += suma(tc_s+1) + suma(tc_b)
        elif tc_s < tc_b:
            total += suma(tc_s) + suma(tc_b+1)
        i = j+1
    else:
        t = S[i:]
        tl = len(t)
        tc_s = t.count('<')
        tc_b = t.count('>')
        if t == tc_s or tl == tc_b:
            total += suma(tl+1)
        elif tc_s > tc_b:
            total += suma(tc_s+1) + suma(tc_b-1)
        elif tc_s < tc_b:
            total += suma(tc_s-1) + suma(tc_b+1)
        else:
            total += suma(tc_s+1) + suma(tc_b)
        break
print(total)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import math


def resolve():
    s = input()

    if s == '<' or s == '>':
        print(1)
        return

    upDownList = []
    count = 1
    before = s[0]
    c = None
    for i in range(1, len(s)):
        c = s[i]

        if c != before:
            if c == '>':
                upDownList.append(count)
            elif c == '<':
                upDownList.append(-1 * count)
            else:
                raise Exception()
            count = 0

        count += 1
        before = c

    if c == '<':
        upDownList.append(count)
    elif c == '>':
        upDownList.append(-count)
    else:
        raise Exception()
    # print(upDownList)

    beforeIsUp = False
    before = 0
    count = 0
    for i in range(len(upDownList)):
        upDown = upDownList[i]

        for i in range(abs(upDown)+1):
            count += i

        if beforeIsUp:
            if before > abs(upDown):
                count -= abs(upDown)
            else:
                count -= before

        if upDown > 0:
            beforeIsUp = True
            before = upDown
        else:
            beforeIsUp = False
        # print(count)

    print(count)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """<>>"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """<>>><<><<<<<>>><"""
        output = """28"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
s = list(input())

lens = len(s)
if lens == 1:
  print(1)
  exit()
  
li = [0] * (len(s) + 1)
for itr, val in enumerate(s):
  if itr == 0:
    if val == "<":
      li[0] = 0
  else:
    if (s[itr - 1] == "<" and s[itr] == "<"):
      li[itr] = li[itr - 1] + 1
#    elif s[itr - 1] == ">" and s[itr] == "<":
#      li[itr] = 0
    if s[itr] == "<" and itr == lens - 1:
      li[itr + 1] = li[itr] + 1

for itr, val in enumerate(reversed(s)):
  if itr == 0:
    if val == ">":
      li[lens] = 0
  else:
    if s[lens - itr - 1] == ">" and s[lens - itr] == ">":
      li[lens - itr] = li[lens - itr + 1] + 1
    if s[0] == ">":
      li[0] = li[1] + 1
      
for itr, val in enumerate(s):
  if itr > 0:
    if s[itr - 1] == "<" and s[itr] == ">":
      li[itr] = max(li[itr - 1],li[itr + 1]) + 1 

print(sum(li))

Submission status: Accepted

Solution code:
def souwa(n):
  return n * (n + 1) // 2

arr = input().replace('><', "> <").split()
res = 0
for s in arr:
  a = s.count("<")
  b = len(s) - a
  if(a < b):
    res += souwa(a - 1)
    res += souwa(b)    
  else:
    res += souwa(a)
    res += souwa(b - 1)
print(res)

Submission status: Accepted

Solution code:
s = input()
n = len(s) + 1
s = s[0] + s + s[-1]

A = [0]
for i in range(n-1):
    if s[i] == '>' and s[i+1] =='>':
        A[-1] -= 1
    elif s[i] == '<' and s[i+1] =='<':
        A[-1] += 1
    else:
        A.append(0)
        if s[i+1] == '<':
            A[-1] += 1
        else:
            A[-1] -= 1

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

m = len(A)
ans = 0

for j in range(m):
    A[j] = abs(A[j])
for i in range(m//2):
    a = max(A[2*i], A[2*i+1])
    b = min(A[2*i], A[2*i+1])
    ans += (a*(a+1)+b*(b-1))//2
            
        

print(ans)

Submission status: Accepted

Solution code:
s=str(input())
n=len(s)
i=0
ans=0
c=0
d=0
j=0
while i < n:
	if s[i] == "<":
		c=c+1
		ans=ans+c
		j=0
	else:
		j=j+1
		if j > 1:
			if d >= j:
				ans=ans+j-1
			else:
				ans=ans+j
		elif i == 0:
			ans=1
		else:
			d=c
		c=0
	i=i+1
print(ans)

Submission status: Accepted

Solution code:
a = list(input())
count=1
answer=0
newlist=[]
masklist=[]
def Souwa(n):
    s=(1/2)*int(n)*int((n+1))
    return int(s)

for i in range(len(a)):
    if i==len(a)-1:
        newlist.append(count)
    elif a[i] =="<" and a[i+1]==">":
        newlist.append(count)
        masklist.append(1)
        count=1
    elif a[i]==">" and a[i+1]=="<":
        newlist.append(count)
        masklist.append(0)
        count=1
    else:
        count+=1

for i in range(len(masklist)):
    if masklist[i]==1:
        if newlist[i]<newlist[i+1]:
            newlist[i]-=1
        else:
            newlist[i+1]-=1

for i in range(len(newlist)):
    answer+=Souwa(newlist[i])
print(answer)

Submission status: Accepted

Solution code:
import numpy as np

S = input()
L = []
N = len(S)

if S[-1] == '<':
    S += '><'
else:
    S += '<>'

index = 0
for n in range(N+1):
    if S[n: n + 2] == '><':
        L.append(S[index: n + 1])
        index = n + 1


def calc(string):
    K = len(string)
    c = K
    for k in range(K - 1):
        if string[k: k + 2] == '<>':
            c = k + 1
            break
    a = min(c, K - c)
    b = max(c, K - c)
    return a * (a - 1) // 2 + b * (b + 1) // 2


ans = 0
for string in L:
    ans += calc(string)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

# N,M = list(map(int, input().split()))

S = input()
set_array = []
prev_set_len = 0
total = 0

prev_check_point = 0
mountains = []
valleys = [0]
for i in range(len(S)-1):
    if S[i] == "<" and S[i+1] == ">":
        mountains.append(i+1)
    elif S[i] == ">" and S[i+1] == "<":
        valleys.append(i+1)

valleys.append(len(S))

if len(mountains) == 0 and len(valleys) == 2:
    print((len(S)+1)*len(S)//2)
    exit()
if len(mountains) == 0 and len(valleys) == 3:
    left = valleys[1]
    right = len(S) - valleys[1]
    total += (left+1)*left//2
    total += (right+1)*right//2
    print(total)
    exit()

if valleys[1] < mountains[0]:
    left = valleys[1]
    total += (left+1)*left//2
    valleys.remove(0)

for i, mountain in enumerate(mountains):
    left = mountain - valleys[i]
    right = valleys[i+1] - mountain

    if left > right:
        total += (left+1)*left//2
        total += (right-1+1)*(right-1)//2
    else:
        total += (right+1)*right//2
        total += (left-1+1)*(left-1)//2
# print(total)
if valleys[-2] > mountains[-1]:
    right = len(S) - valleys[-2]
    total += (right+1)*right//2


print(total)

Submission status: Accepted

Solution code:
from sys import stdin 
s = stdin.readline().rstrip()
a = [0] * (len(s) + 1)
t = 1
for i in range(len(s)):
    if i == 0:
        if s[i] == '<':
            k = 0
        else:
            k = 1
    if s[i] == '<' and k == 0:
        a[i+1] = a[i] + 1
    elif s[i] == '<' and k == 1:
        if a[i] < 0:
            min = (-1) * a[i]
            a[i] = 0
            for j in range(t, i):
                a[j] += min
            if t > 1:
                if a[t-2] > a[t]:
                    a[t-1] = a[t-2] + 1
                else:
                    a[t-1] = a[t] + 1
            else:
                a[t-1] = a[t] + 1
        else:
            a[i] = 0
        a[i+1] = a[i] + 1
        k = 0
    elif s[i] == '>' and k == 1:
        a[i+1] = a[i] - 1
    elif s[i] == '>' and k == 0:
        a[i+1] = 0
        t = i+1
        k = 1
    if i == len(s)-1 and s[i] == '>':
        if a[i+1] < 0:
            min = (-1) * a[i+1]
            a[i+1] = 0
            for j in range(t, i+1):
                a[j] += min
        if t > 1:
            if a[t-2] > a[t]:
                a[t-1] = a[t-2] + 1
            else:
                a[t-1] = a[t] + 1
        else:
            a[t-1] = a[t] + 1
print(sum(a))