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:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    s = input()
    n = len(s)
    l = []
    a,b = 0,0
    for i in range(n):
        if s[i] == "<":
            if b > 0:
                l.append(b)
                b = 0
            a+=1
        else:
            if a > 0:
                l.append(a)
                a = 0
            b += 1
    if a > 0:
        l.append(a)
        a = 0
    if b > 0:
        l.append(b)
        b = 0
    ans = 0
    #print(l)
    if s[0] == "<":
        for i in range(0, len(l) - len(l) % 2, 2):
            if l[i] == l[i + 1]:
                ans += l[i]*l[i]
            elif l[i] < l[i + 1]:
                ans += l[i] * (l[i] - 1) // 2
                ans += l[i + 1] * (l[i + 1] + 1) // 2
            else:
                ans += l[i] * (l[i] + 1) // 2
                ans += l[i + 1] * (l[i + 1] - 1) // 2
        if len(l) % 2 == 1:
            ans += l[len(l) - 1] * (l[len(l) - 1] + 1) // 2
    else:
        ans += l[0]*(l[0]+1)//2
        for i in range(1, (len(l)-1) - (len(l)-1) % 2, 2):
            if l[i] == l[i + 1]:
                ans += l[i]*l[i]
            elif l[i] < l[i + 1]:
                ans += l[i] * (l[i] - 1) // 2
                ans += l[i + 1] * (l[i + 1] + 1) // 2
            else:
                ans += l[i] * (l[i] + 1) // 2
                ans += l[i + 1] * (l[i + 1] - 1) // 2
        if (len(l)-1) % 2 == 1:
            ans += l[len(l) - 1] * (l[len(l) - 1] + 1) // 2

                

    print(ans)
             
        
        
            

    
   


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
N = len(s)

ans = [-1]*(N+1)
ans[0] = 0
ans[-1] = 0
tmp = 0
for i in range(N-1):
  if s[i] == "<":
    tmp += 1
    ans[i + 1] = tmp
  elif s[i] == ">" and s[i + 1] == "<":
    tmp = 0
    ans[i + 1] = tmp
else:
  if s[-1] == "<":
    ans[-1] = ans[-2] + 1

tmp = 0
for i in range(N-1,0,-1):
  if s[i] == ">":
    tmp += 1
    if s[i - 1] == "<":
      ans[i] = max(ans[i], tmp)
    else:
      ans[i] = tmp
  elif s[i] == "<" and s[i - 1] == ">":
    tmp = 0
    ans[i] = tmp
  #print(ans)
else:
  if s[0] == ">":
    ans[0] = ans[1] + 1
#print(ans)
print(sum(ans))

Submission status: Accepted

Solution code:
s=str(input())
arr=[0]
sumx=0
i=0
while(i<len(s)):
	if(s[i]=='<'):
		arr.append(arr[-1]+1)
		i+=1
	else:
		countx=0
		j=i
		while(j<len(s) and s[j]=='>'):
			countx+=1
			j+=1
		if(arr[-1]<countx):
			arr[-1]=countx
		while(i<j):
			arr.append(countx-1)
			countx-=1
			i+=1

print(sum(arr))

Submission status: Accepted

Solution code:
S = input()
sep = [0]
i=0
for i in range(len(S)-1):
    if S[i]=='<' and S[i+1]=='>': sep.append(i+1)
sep.append(i+2)

S += 'X'
ans = 0
center = 0
for i in range(len(sep)-1):
    s = S[sep[i]: sep[i+1]]
    l_max = s.count('>')
    ans += max(center, l_max)
    ans += l_max*(l_max-1)//2

    r_max = s.count('<')
    ans += r_max*(r_max-1)//2
    center = r_max

ans += center
print(ans)

Submission status: Accepted

Solution code:
S=input()
V=[]
ans=0
num=[-1]*len(S)
for s in S:
	if len(V)==0 or V[-1][0]!=s:
		V.append([s,1])
	else:
		V[-1][1] += 1
if len(V)==1:print(V[0][1]*(V[0][1]+1)//2);exit()
c=len(V)
if V[0][0]==">":
  ans+=V[0][1]*(V[0][1]+1)//2
  del V[0]
  c-=1
if V[-1][0]=="<":
  ans+=V[-1][1]*(V[-1][1]+1)//2
  del V[-1]
  c-=1

for i in range(c//2):
  M=max(V[2*i][1],V[2*i+1][1])
  m=min(V[2*i][1],V[2*i+1][1])
  ans+=M*(M+1)//2+m*(m-1)//2
print(ans)

Submission status: Accepted

Solution code:
def answer():
    input_s = input()

    sign_list = [input_s[0]]
    n_list = [1]

    for sign in input_s[1:]:
        previous_sign = sign_list[-1]

        if sign == previous_sign:
            n_list[-1] += 1
        else:
            sign_list.append(sign)
            n_list.append(1)

    for i, sign in enumerate(sign_list[:-1]):
        if sign == '<':
            this_n = n_list[i]
            next_n = n_list[i+1]
            if this_n < next_n:
                n_list[i] -= 1
            else:
                n_list[i+1] -= 1

    total = 0
    for n in n_list:
        total += n*(n+1)//2

    print(total)


if __name__ == '__main__':
    answer()

Submission status: Accepted

Solution code:
import sys

s = list(input())
ans = 0
point = 0

if s[len(s)-1] == "<":
    s.append(">")

def walk(n):
    sum = 0
    i = n
    l = len(s)
    while True:
        sum += 1
        if i+1 < l:
            if s[i] != s[i+1]:
                break
        else:
            break
        i += 1
    return sum

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

if s[0] == ">":
    point = walk(0)
    ans += sigma(point)

if point == len(s):
    print(ans)
    sys.exit()

while True:
    d1 = walk(point)
    point += d1
    ans += sigma(d1-1)
    d2 = walk(point)
    point += d2
    ans += max(d1,d2)
    ans += sigma(d2-1)
    if point == len(s):
        break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Nov  3 14:57:33 2019

@author: Tomomasa Takahashi
"""

S=input()
tot = 0
lcont=0
scont=0
for i in range(len(S)):
    if S[i]=='<':
        lcont += 1
    else:
        scont += 1
    if i < len(S)-1:
        if S[i]==">" and S[i+1]=='<':
            t = max(lcont, scont)
            s = min(lcont, scont)-1
            tot += t*(t+1)//2
            tot += s*(s+1)//2
            lcont=0
            scont=0
t = max(lcont, scont)
s = min(lcont, scont)-1
tot += t*(t+1)//2
tot += s*(s+1)//2
print(tot)

Submission status: Accepted

Solution code:
def calc_count(token):
    num_left = token.count('<')
    num_right = token.count('>')
    sum_left = sum(range(num_left))
    sum_right = sum(range(num_right))
    midle = max(num_left, num_right)
    return sum_left + sum_right + midle


def main(n):
    strings = n.replace('><', '>|<').split('|')
    return sum([calc_count(s) for s in strings])


if __name__ == '__main__':
    N = input().strip()
    print(main(n=N))

Submission status: Accepted

Solution code:
s = input()

li = []

tmp = s[0]

if tmp == '<':
    flag = 0
else:
    flag = 1

now = 1

for i in range(len(s)-1):
    if tmp == s[i+1]:
        now += 1
    else:
        tmp = s[i+1]
        li.append(now)
        now = 1

li.append(now)
#print(li)
ans = 0
#ans_li = [0]

if flag == 0:
    ans_li = [0]
    for i in range(len(li)):
        if i % 2 == 0:
            for j in range(li[i]):
                ans_li.append(j+1)
        else:
            if li[i] > ans_li[-1]:
                ans_li[-1] = li[i]
            for k in range(li[i]):
                ans_li.append(li[i]-k-1)
else:
    ans_li = [li[0]]
    for i in range(len(li)):
        if i % 2 == 1:
            for j in range(li[i]):
                ans_li.append(j+1)
        else:
            if i != 0 and li[i] > ans_li[-1]:
                ans_li[-1] = li[i]
            for k in range(li[i]):
                ans_li.append(li[i]-k-1)

print(sum(ans_li))

Submission status: Accepted

Solution code:
import collections
S = input()

s = list(S.split("><"))
ans = 0
for i in range(len(s)):
    table = collections.defaultdict(int)
    for y in s[i]:
        table[y] += 1

    if i == 0 and len(table) != 0:
        table["<"] -= 1
    if i == len(s)-1 and len(table) != 0:
        table[">"] -= 1
    table = list(table.values())
    if len(table) == 2:
        table.sort()
        ans += max((table[1]+1)*(table[1]+2) // 2, 0)
        ans += max((table[0]+1)*table[0] // 2, 0)

    elif len(table) == 1:
        ans += max((table[0]+1)*(table[0]+2) // 2, 0)
    else:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
s=input()
if s[0]==">":s="<"+s
if s[-1]=="<":s=s+">"
tmps="<"
tmp1,tmp2=0,0
ans=0
for i in s:
    if tmps=="<":
        if i=="<":
            tmp1+=1
        else:
            tmps=i
            tmp2+=1
    else:
        if i==">":
            tmp2+=1
        else:
            ans+=tmp1*(tmp1+1)//2+tmp2*(tmp2+1)//2-min(tmp1,tmp2)
            tmp1,tmp2=1,0
            tmps=i
ans+=tmp1*(tmp1+1)//2+tmp2*(tmp2+1)//2-min(tmp1,tmp2)
print(ans)

Submission status: Accepted

Solution code:
s = input()

ans = 0
f = 0
be = 0
for k in range(len(s)):
    if s[k] == '<':
        if f != 0 and be != 0:
            ans -= be * f
            be = 0
        ans += be + 1
        f = 0
        be += 1
    elif be ==0:
        f += 1
        ans += be + f
    else:
        f += 1
        ans += be - 1
        be = be - 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


s = S()
ans = [0] * (len(s) + 1)
ret = 0
for j in range(len(s) - 1, -1, -1):
    if s[j] == '>':
        ret += 1
        ans[j] = ret
    else:
        ret = 0


ret = 0
for i in range(len(s)):
    if s[i] == '<':
        ret += 1
        if ans[i + 1] < ret:
            ans[i + 1] = ret
    else:
        ret = 0



print(sum(ans))

Submission status: Accepted

Solution code:
s = list(input())
pre_index = 0
split_s = []
for i in range(len(s) - 1):
    if s[i] == '>' and s[i + 1] == '<':
        split_s.append(s[pre_index:i+1])
        pre_index = i + 1
split_s.append(s[pre_index:])
ans = 0
for i in range(len(split_s)):
    l = split_s[i].count('<')
    r = split_s[i].count('>')
    if l > r:
        ans += sum(range(1,l+1))
        ans += sum(range(1,r))
    else:
        ans += sum(range(1,r+1))
        ans += sum(range(1,l))
print(ans)

Submission status: Accepted

Solution code:
S = input()

L = []
s = 0
u = 0

for i in range(len(S)-1):
  if S[i] != S[i+1]:
    L.append(i+1-u)
    u = i+1
L.append(len(S)-u)

if S[0] == '<':
  if len(L) % 2 == 1:
    L.append(0)
  for j in range(len(L) // 2):
    M = max(L[2*j], L[2*j+1])
    m = min(L[2*j], L[2*j+1])
    s += (M * (M + 1)) // 2 + (m * (m-1)) // 2
elif S[0] == '>':
  s += (L[0] * (L[0] + 1)) // 2
  del L[0]
  if len(L) % 2 == 1:
    L.append(0)
  for j in range(len(L) // 2):
    M = max(L[2*j], L[2*j+1])
    m = min(L[2*j], L[2*j+1])
    s += (M * (M + 1)) // 2 + (m * (m-1)) // 2
    
print(s)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
l=[0]*(n+1)
ans=0
c=1
ll=[]
for i in range(1,n):
    if s[i]==s[i-1]:
        c+=1
    else:
        ll.append((c,i,s[i-1]))
        c=1


for x,y,h in ll:
    if h=="<":
        l[y]=max(l[y],x)
    else:
        l[y-x]=max(l[y-x],x)


for i in range(n):
    if s[i]=="<":
        if l[i]>=l[i+1]:
            l[i+1]=l[i]+1

for i in range(n-1,-1,-1):
    if s[i]==">":
        if l[i]<=l[i+1]:
            l[i]=l[i+1]+1

print(sum(l))

Submission status: Accepted

Solution code:
s = list(input())
s.append(" ")
n = len(s)
i = 0
ans = []
j = 1
while i<n-1:
    if s[i]==s[i+1]:
        i+=1
        j+=1
    else:
        ans.append([s[i],j])
        i+=1
        j =1
m = len(ans)
answer = 0
i = 0
while i<m:
    if i==m-1:
        x = ans[i][1]*(ans[i][1]+1)
        x = x//2
        answer +=x
        break
    
    if ans[i][0]=="<" and ans[i][1]>=ans[i+1][1]:
        x = ans[i][1]*(ans[i][1]+1)
        x = x//2
        answer +=x 
        y = ans[i+1][1]*(ans[i+1][1]-1)
        y = y//2
        answer +=y
        i+=2
    elif ans[i][0]=="<" and ans[i][1]<ans[i+1][1]:
        x = ans[i][1]*(ans[i][1]-1)
        x = x//2
        answer+=x
        y = ans[i+1][1]*(ans[i+1][1]+1)
        y = y//2
        answer +=y
        i +=2
    else:
        x = ans[i][1]*(ans[i][1]+1)
        x = x//2
        answer +=x
        i+=1
print(answer)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
s = input()
lft, rgt = 0, 0
cnt = 0
tmp = ""
for cur in s:
    if tmp == ">" and cur == "<":
        lar = max(lft, rgt)+1
        sma = min(lft, rgt)
        cnt += sum(range(lar)) + sum(range(sma))
        lft, rgt = 1, 0
    elif cur == ">":
        rgt += 1
    elif cur == "<":
        lft += 1
    tmp = cur

lar = max(lft, rgt)+1
sma = min(lft, rgt)
cnt += sum(range(lar)) + sum(range(sma))
print(cnt)

Submission status: Accepted

Solution code:
s = input()
n = len(s)+1
num = 0
ans = 0
con = 0


for i in range(n-1):
    if s[i] == '>':
        if con == 0:
            con = 1
        else:
            con += 1

        num -= 1
        if num < 0:
            num = 0
            ans += con
    else:
        if not con == 0:
            ans = ans - num*(con)
            num = 0
        num += 1
        con = 0
    ans += num
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)+1
a = [-1]*n
flag = False
for i in range(n-1):
    if s[i] == "<":
        if not flag: a[i] = 0
        a[i+1] = a[i]+1
        flag = True
    else: flag = False
flag = False
for i in range(n-1):
    j = n-2-i
    if s[j] == ">":
        if not flag: a[j+1] = 0
        a[j] = max(a[j], a[j+1]+1)
        flag = True
    else: flag = False
print(sum(a))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

S = input().strip()

# a = [0]*30
a = [0]*(5*10**5)

i = 0
while i<=len(S)-1:
	si = S[i]
	if si=='<':
		a[i+1] = a[i]+1 
		i += 1
	else:
		dec_num = 0
		j = i
		while S[j]=='>':
			dec_num += 1
			j += 1
			if j==len(S): break

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

		i = j

ans = sum(a)

print(ans)

Submission status: Accepted

Solution code:
s = list(input())

n = len(s) + 1
sl = [0] * n

mins = 0
sums = 0
starti = 0
inc = False
if s[0] == '<':
    inc = True
for i in range(1, n):
    if inc == True:
        sl[i] = sl[i-1]+1
        if i == n-1 or s[i] == '<':
            sums += sl[i]
        else:
            starti = i
            sl[i] = 0
            inc = False
    else:
        if i == n-1 or s[i] == '<':
            sl[i] = sl[i-1]-1
            nums = i - starti -1
            if starti != 0 and sl[starti-1]+1 > -1* sl[i]:
                sums += sl[starti-1]+1
                sl[starti-1] = sl[starti-1]+1
            else:
                nums += 1
            sums += -1 * sl[i] * nums
            sl[i] = 0
            inc = True
        else:
            sl[i] = sl[i-1] -1
            sums += sl[i]


print(sums)

Submission status: Accepted

Solution code:
s = input()
value = [-1] * (len(s) + 1)

count = 0

for i in range(len(s)):
    if s[i] == '<':
        value[i] = count
        value[i + 1] = count + 1
        count += 1
    else:
        count = 0

count = 0
for j in range(len(s) - 1, -1, -1):
    if s[j] == '>':
        if value[j + 1] < count:
            value[j + 1] = count
        if value[j] < count + 1:
            value[j] = count + 1
        count += 1
    else:
        count = 0
print(sum(value))

Submission status: Accepted

Solution code:
s = input()
s += "a"
n = len(s)
k = 1
lis = []
for i in range(n-1):
  if s[i] != s[i+1]:
    lis.append(k)
    k = 1
  else:
    k += 1
def fac(i):
  return i*(i+1)//2
lislis = []
num = len(lis)
for i in range(num):
  lislis.append(fac(lis[i]))
ans = sum(lislis)
if s[0] == "<":
  for i in range(0, num-1, 2):
    l = min(lis[i], lis[i+1])
    ans -= l
else:
  for i in range(1, num-1, 2):
    l = min(lis[i], lis[i+1])
    ans -= l
print(ans)

Submission status: Accepted

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


S = input()

if S[0] == '>':
    S = '<' + S
    # print(S)

if S[-1] == '<':
    # print('this?')
    S = S + '>'


# 連長圧縮する
s_pre = '<'
sum_s_tmp = 0
rencho = []  # 偶数番に<の個数、奇数番に>の個数が入る
for s in S:
    if s_pre == s:
        sum_s_tmp += 1
    else:
        rencho.append(sum_s_tmp)
        sum_s_tmp = 1
        s_pre = s
rencho.append(sum_s_tmp)

# print(rencho)
ans = 0
for a_big, a_small in zip(rencho[0::2], rencho[1::2]):
    if a_big < a_small:
        a_big, a_small = a_small, a_big
    ans += sum(range(a_big + 1))
    ans += sum(range(a_small))


print(ans)

# A_sum = 0
# A_min = 0
# a_now = 0
# for s in S:
#     if s == '<':
#         a_now += 1
#         A_sum += a_now
#     if s == '>':
#         a_now -= 1
#         A_sum += a_now
#         A_min = min(a_now, A_min)

# print(A_sum - A_min * len(S))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])
#print(code)
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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 == '<':
        # always 0
        ans += stack * (stack - 1) // 2
        ans += max(height, stack)
        stack = 1

    elif last_c == '<' and c == '>':  # < a >
        # bigger
        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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)
#print(res)

if a[0]=="<":
    if len(res)%2==0:
        c=wa_arr(res)
    else:
        c=wa_arr(res[:-1])+wa(res[-1])
        #print(res[:-1],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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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 s != pre_s and l_strk_count =! 0 and r_strk_count == 0:
    #     ans += l_strk_count * (l_strk_count + 1) // 2
    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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

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()