Problem:


Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6



Sample input:
BBW


Sample output:
2

Submission status: Accepted

Solution code:
s = list(input().rstrip())
r=0
bm=0
while r<len(s)-1:
    if s[r]=="B" and s[r+1]=="W":
        bm+=1
        break
    elif s[r]=="B":
        bm+=1
    r+=1
ans=0
for i in range(r+1, len(s)):
    if s[i]=="W":
        ans+=bm
    if s[i]=="B":
        bm+=1

print(ans)

Submission status: Accepted

Solution code:
s=input()
a=0#ans
p=0#白の数
for i in range(len(s)):
    if s[i]=='W':
        a+=i-p
        p+=1
print(a)

Submission status: Accepted

Solution code:
S = input()

n_left_white = 0
ans = 0

for i in range(len(S)):
    if S[i] == "W":
        ans += i - n_left_white
        n_left_white += 1

print(ans)

Submission status: Accepted

Solution code:
s = input()
a = b = 0

for x in s:
    a += b*(x=="W")
    b += x=="B"
print(a)

Submission status: Accepted

Solution code:
S = input()

W_index_num = [n for n,v in enumerate(S) if v == 'W']
ans = 0
cnt = 0

for i in range(len(S)):
    if S[i] == "W":
        ans += i - cnt
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()
count = 0
ans = 0
for i in range(len(S)):
  if S[i] == 'W':
    ans += i - count
    count += 1
print(ans)

Submission status: Accepted

Solution code:
s=input()
lst=[]
countw=0

for i in  reversed(range(len(s))):
    if s[i]=='W':
        countw+=1
    lst.append(countw)
lst=lst[::-1]
for j in range(len(s)):
    if s[j]=='W':
        lst[j]=0
print(sum(lst))

Submission status: Accepted

Solution code:
s=input()

cnt=0
ans=0
for i in range(len(s)):
    if s[i]=="W":
        ans+=i-cnt
        cnt+=1
print(ans)

Submission status: Accepted

Solution code:
S = input()
if len(set(S)) == 1:
    print(0)
    exit()
ans = 0
num = 0
for i in range(len(S)):
    if S[i] == "W":
        ans += i - num
        num += 1
print(ans)

Submission status: Accepted

Solution code:
c = b = 0
for s in input():
  c += b*(s=="W")
  b += s=="B"
print(c)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
#my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())


#your code here!
def resolve():
    S=input()
    ans=0
    
    hidari=0
    for i,c in enumerate(S):
        if c=="W":
            ans+=i-hidari
            hidari+=1
    print(ans)

import sys
from io import StringIO
import unittest


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 = """BBW"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """BWBWBW"""
        output = """6"""
        self.assertIO(input, output)


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


    resolve()#and submit 2 atcoder!

Submission status: Accepted

Solution code:
S = input()
if len(set(S)) == 1:
    print(0)
    exit()
ans = 0
num = 0
for i in range(len(S)):
    if S[i] == "W":
        ans += i - num
        num += 1
print(ans)

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
S = input()
ans = 0
word = ""
Slist = list(S.split("W"))
for s in Slist[:-1]:
    ans += len(word + s)
    word = word + s
print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
def myAnswer(S:list) -> int:
   N = 0
   counter = 0
   for n,s in enumerate(S):
      if(s == "W"):
         counter += n - N
         N += 1
         

   return counter

def modelAnswer():
   tmp=1
def main():
   S = list(input())
   print(myAnswer(S[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
#AGC029-A
s = input()
tmp = 0
cnt = 0
for idx,j in enumerate(s):
    if j =="W":
        cnt += idx - tmp
        tmp += 1
print(cnt)

Submission status: Accepted

Solution code:
S = list(input())
L = len(S)

to = -1
ans = 0
for i in range(L-1,-1,-1):
  if S[i] == 'W':
    to = max(to,i)
  else:
    if to == -1:
      continue
    else:
      ans += to - i
      to -= 1
      
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = - int((1 + S.count('W')) * S.count('W')/2)
num = 0

for i in range(len(S)):
    if S[i] == 'W':
        ans += i + 1
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = input()
N = len(S)
tmp = 0
ans = 0
A = 0
B = 0
b_num = 0
for i in range(1, N+1):
    if S[i-1] == "B":
        b_num += 1
        A += i

all_wa = N * (N + 1) // 2
B = all_wa - (N - b_num) * (N - b_num + 1) // 2

print(B - A)

Submission status: Accepted

Solution code:
S = input()
tmp = 0
N = 0
for i in range(len(S)):
    if S[i]=="W":
        N += (i-tmp)
        tmp+=1
print(N)

Submission status: Accepted

Solution code:
import sys
from sys import stdin

s = stdin.readline().rstrip()
ans = 0
count = 0
for i in s:
    if i == 'B':
        count += 1
    else:
        ans += count
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 0
b_count = 0
w_count = 0
#B, W の転倒数を数える
for i in range(len(s)):
    if s[i] == 'B':
        b_count += 1
    else:
        w_count += 1
        ans += b_count
print(ans)

Submission status: Accepted

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

s=input()

cnt=0
cum=0

for i in range(len(s)):
    if s[i]=='W':
        cnt+=1
        cum+=i
print(cum-int((cnt-1)*cnt/2))

Submission status: Accepted

Solution code:
S=input()
black = 0
ans = 0

for s in S:
    if s=="B":
        black+=1
    else:
        ans+=black

print(ans)

Submission status: Accepted

Solution code:
S=input()

a=0
ans=0
for i in range(len(S)):
  if S[i]=='W':
    ans+=i-a
    a+=1
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc029/tasks/agc029_a
s = input()

ans = 0
t = 0
for i in range(len(s)):
    if s[i] == 'B':
        t += 1
    elif s[i] == 'W':
        ans += t
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
j = 0
k = 0
for i in range(len(S)):
    if S[i] == "W":
        j += i - k
        k += 1
print(j)

Submission status: Accepted

Solution code:
S = list(input())

blacks = []
for i in range(len(S)):
    if S[i] == "B":
        blacks.append(i)

# print(blacks)

for i in range(len(blacks)):
    blacks[len(blacks) - i - 1] = len(S) - i - 1 - blacks[len(blacks) - i - 1]

print(sum(blacks))

Submission status: Accepted

Solution code:
# agc029_a.py
# W の後ろに何個Bがあるかをカウントすれば行けそう
def swap(s,i,j):
    return s[:i] + s[j] + s[i] + s[j+1:]
cnt = 0
S = input()
bcnt = 0
ans = 0
for i in S:
    if i =="B":
        bcnt +=1
    else:
        ans += bcnt
print(ans)

Submission status: Accepted

Solution code:
s_l = list(input())
count_tot = 0
count = 0

for i in range(len(s_l)):
    if s_l[i] == 'W':
        count_tot += i - count
        count += 1

print(count_tot)

Submission status: Accepted

Solution code:
s = input()
w = 0
for i in s:
    if i == 'W':
        w += 1

ans = 0
ind = 0
for i in range(len(s)):
    if s[i] == 'B':
        ans += w+ind-i
        ind += 1

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
min sec
"""
# とりあえず愚直に書いてみる。→TLEした。
# Bは0、Wは1にする。→必要ない。
# ひっくり返す順番は関係なさそうなので考えない。
"""
BWのn個の連続（長さ2n）は、n+(n-1)+(n-2)+...+1 = n(n+1)//2回の操作で、
n個のBとn個のWの連続に置き換えられる。
"""
"""
ようするにバブルソートをしている。
k番目のWが文字列Sのi文字目ならば、
その移動距離は、(i-k)回になる。
これを足せばよい。
"""

def main():
    S = input()
    N = len(S)
    counter = 0
    k = 0
    for i in range(N):
        if S[i] == "W":
            counter += (i-k)
            k += 1
    print(counter)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    cunt_b=0
    ans=0
    for ss in s:
        if ss=='B':
            cunt_b+=1
        else:
            ans+=cunt_b
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    n=len(s)
    flag=-1
    for i in range(n):
        if s[i]=='W':
            flag=i
        else:
            break
    cunt_b=0
    cunt_w=0
    ans=0
    for i in range(n):
        if s[i]=='B':
            cunt_b+=1
        elif cunt_b>0:
            ans+=i-flag-1-cunt_w
            cunt_w+=1
    print(ans)



if __name__=='__main__':
    main()

Submission status: Accepted

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


def read():
    S = list(input().strip())
    return S,

def solve(S):
    N = len(S)
    count = 0
    l = 0
    r = N-1
    while r >= 0 and S[r] != 'W':
        r-=1
    s = list()
    c = 0
    for i in range(r, -1, -1):
        d = r - i
        if S[i] == 'B':
            s.append(d - c)
            c += 1
    return sum(s)

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Accepted

Solution code:
s=input()
a=len(s)
cnt=0
ans=0
for i in range(a):
  if s[i]=='W':
    ans+=i-cnt
    cnt+=1
print(ans)

Submission status: Accepted

Solution code:
s = input()

b = 0
a = 0
for i in range(len(s)):
    if s[i] == 'B':
        b += 1
        a += i + 1
ans = 0
for i in range(len(s) - b, len(s)):
    ans += i + 1

print(ans - a)

Submission status: Accepted

Solution code:
S = input()

ans = 0
bcount = 0
for i, s in enumerate(S[::-1]):
    if s=='B':
        ans += i-bcount
        bcount+=1
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
wNum = 0
ans = 0
for i in range(len(s))[::-1]:
    if s[i] == "W":
        wNum += 1
    else:
        ans += wNum
print(ans)

Submission status: Accepted

Solution code:
S = input()
cnt_b = 0
ans = 0
for s in S:
    if s == "B":
        cnt_b += 1
    if s == "W":
        ans += cnt_b
print(ans)

Submission status: Accepted

Solution code:
N = input()
ans = 0
ct = 0
for i in N:
    if i == 'B':
        ct += 1
    if i == 'W':
        ans = ans + ct
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    S = [item for item in input()]

    res = 0
    w_cnt = 0
    for i, v in enumerate(S):
        if v == 'W':
            res += i+1
            w_cnt += 1

    res -= w_cnt * (w_cnt + 1)//2
    print(res)
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
st=input()
B=[1 if st[0]=='B' else 0]
tot=0
for i in range(1,len(st)):
    if st[i]=='B':
        B.append(B[i-1]+1)
    else:
        B.append(B[i-1])
        tot+=B[i-1]
print(tot)

Submission status: Accepted

Solution code:
s = input()
b = s.count("B")
n = len(s)
cou = 0
ans = 0
for i in reversed(range(n)):
    if s[i] == "B":
        ans += n - 1 - cou - i
        cou += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():

    S=str(input())
    cnt=0
    b=0
    for i in S:
        if i=='B':
            b+=1
        else:
            cnt+=b
    print(cnt)

resolve()

Submission status: Accepted

Solution code:
S = input()

first_b = -1
for i in range(0, len(S)):
    if S[i] == 'B':
        first_b = i
        break

last_w = len(S)
for i in range(len(S) - 1, -1, -1):
    if S[i] == 'W':
        last_w = i
        break

# all B or all W
if first_b == -1 or last_w == len(S):
    print(0)
    exit(0)

S = S[first_b:last_w + 1]
cnt_b = 0
ans = 0
for s in S:
    if s == 'B':
        cnt_b += 1
    else:
        ans += cnt_b
print(ans)

Submission status: Accepted

Solution code:
s=str(input())
count_w=0
ans=0
for i in range(len(s)):
    if s[i:i+1]=='W':
        ans+=i-count_w
        count_w+=1

print(ans)

Submission status: Accepted

Solution code:
s = input()
n  =len(s)
res = 0
ans = 0
for i in range(n):
    if s[i] == 'B':
        res+=1
    else:
        ans+=res


print(ans)

Submission status: Accepted

Solution code:
S = input()
answer = 0
white_counter = 0
for i, s in enumerate(S):
    if s =='W':
        answer += (i-white_counter)
        white_counter += 1

print(answer)

Submission status: Accepted

Solution code:
s=input()

ans=0
b_count=0
for i in range(len(s)):
    if(s[i]=='W'):
        ans+=b_count
    else:
        b_count+=1
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 0
count = 0
for i in range(len(s)):
    if s[i]=='W':
        ans += (i-count)
        count += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
t = 0
ans = 0

for i in range(len(s)):
    if s[i] == 'W':
        ans += (i-t)
        t += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
t = 0
ans = 0
for i in range(len(s)):
    if s[i] == "W":
        ans += (i-t)
        t += 1
print(ans)

Submission status: Accepted

Solution code:
s=input()
n_s=s.count('W')
a=0
for i in range(len(s)):
    if s[i]=='W':
        a+=i+1
print(a-sum(range(n_s+1)))

Submission status: Accepted

Solution code:
s=input()
t=0
ans=0
for i in range(len(s)):
  if s[i]=="W":
    ans+=(i-t)
    t+=1
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
ans=0
w=0
for index,value in enumerate(s):
    if value=="W":
        ans+=index-w
        w+=1
print(ans)

Submission status: Accepted

Solution code:
s = input()
b_idx = list(reversed([i for i, x in enumerate(s) if x == "B"]))
ans = 0
for i in range(len(b_idx)):
    ans += (len(s)-(i+1)) - b_idx[i]
print(ans)

Submission status: Accepted

Solution code:
S = list(input())

cnt = 0
t = 0
for i in range(len(S)):
    if S[i] == 'W':
        cnt += t
    else:
        t += 1

print(cnt)

Submission status: Accepted

Solution code:
s=input()
count = 0
c_black = 0
for s0 in s:
    if s0 == 'W':
        count += c_black
    else:
        c_black += 1
print(count)

Submission status: Accepted

Solution code:
S=input()
ls=len(S)
bb=0
ans=0
for i in range(ls):
    if S[i]=="B":
        bb+=1
    else:
        ans+=bb
print(ans)

Submission status: Accepted

Solution code:
S = input()
wc = 0
w = S.count('W')
for i in range(len(S)):
    if S[i] == 'W':
        wc += i
print(wc - w*(w-1)//2)

Submission status: Accepted

Solution code:
s = input()
li = list(s)
sum_index = 0
count = 0
for i,l in enumerate(li):
    if l == 'W':
        count += 1
        sum_index += i+1
hidari = (1+count)*count//2
#print(sum_index,hidari)
print(sum_index-hidari)

Submission status: Accepted

Solution code:
S = input()

black = 0
ans = 0
for c in S:
    if c == 'B':
        black += 1
    else:
       ans += black
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = 0
nc = 0
for i in range(len(S)):
    if S[i] == 'W':
        ans += i
        nc += 1
print(ans - nc*(nc - 1)//2)

Submission status: Accepted

Solution code:
s = list(input())

ans = 0
count_b = 0
n = len(s)
for i in range(n):
    if s[i] == 'B':
        count_b += 1
    if s[i] == 'W':
        ans += count_b

print(ans)

Submission status: Accepted

Solution code:
def solve():
    S = input()
    N = len(S)
    ans = 0
    cnt = 0
    for i in range(N-1,-1,-1):
        if S[i] == 'B':
            ans += N - i - (1 + cnt)
            cnt += 1
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
S = input()
ans, cnt = 0, 0
for i in range(len(S)):
  if S[i] == "W":
    ans += i - cnt
    cnt += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()
x = len(S)
cout = S.count('W')
num = 0
for i in range(x):
    if S[i] == 'W':
        num += (i+1)
print(num - cout * (cout + 1) // 2)

Submission status: Accepted

Solution code:
S = list(input())
step = 0
ans = 0
for i, s in enumerate(reversed(S)):
    if s == 'W':
        step += 1
    if s == 'B':
        if step != 0:
            S[-(i-step)-1] = 'B'
            S[-i-1] = 'W'
            ans += step
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = 0
cnt = 0
for i,j in enumerate(s[::-1]):
  if j == 'B':
    ans += i - cnt
    cnt += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()

count_white = 0
ans = [0] * len(S)

for i in range(len(S)):
    if S[i] == "W":
        ans[i] = i -count_white
        count_white += 1
print(sum(ans))

Submission status: Accepted

Solution code:
s = input()

b_n = []
c = 0
for i in s:
    if i == 'B':
        c += 1
    
    else:
        b_n.append(c)

print(sum(b_n))

Submission status: Accepted

Solution code:
S = input()
s = S[::-1]
cnt_b = 0
cnt_w = 0
ans = 0
for i in range(len(s)):
    if(s[i] == "W"):
        cnt_w += 1
    else:
        cnt_b += 1
        ans += cnt_w
print(ans)

Submission status: Accepted

Solution code:
S = input()
 
ans = 0
b_count = 0

for i in S:
    if i == "B":
        b_count += 1
    else:
        ans += b_count

print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = 0
wcnt = 0
for i in range(len(S)):
    if S[i] == "W":
        wcnt += 1
        ans += i-wcnt+1
print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = 0
x = 0
for i in range(len(S)):
    if S[i] == "B":
        x += 1
    if S[i] == "W":
        ans += x

print(ans)

Submission status: Accepted

Solution code:
S = str(input())
ans = 0
cnt = 0
for i, s in enumerate(S):
  if s == 'W':
    cnt += 1
    ans += i + 1 - cnt
print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = 0
cnt = 0
for i in range(len(S)):
    if S[i] == 'W':
        ans += i-cnt
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = 0
cnt = 0
for i in range(len(S)):
    if S[i] == "W":
        ans += i - cnt
        cnt += 1

print(ans)

Submission status: Accepted

Solution code:
# 操作によって「同じ色の石同士の相対順序」は変わらない
# さらに全操作終了後はW...WB...Bとなる.
# よって各Wに対して、その左方にあるBの(場所は関係ない)数(だけ)を求めれば
# その総和が答えとなる。
S = input()
ans = 0
cnt_black = 0
for s in S:
    if s == 'B':
        cnt_black += 1
    else:
        ans += cnt_black
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
c = 0
ans = 0
for i in s:
    if i == "B":
        c += 1
    else:
        ans += c
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
    def sum(self, i):  # 区間 [1, i]の和
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):  # iにxを加算
        while i <= self.size:
            self.tree[i] += x
            i += i & -i  
    def pos(self, idx):  # idxの値を取得
        return self.sum(idx) - self.sum(idx-1)
    def lower_bound(self, w):  # w以上の値を持つ最小のidxを検索
        if w <= 0:
            return 0
        idx = 0
        k = 1
        while k*2 <= self.size:
            k *= 2
        while k > 0:
            if idx+k <= self.size and self.tree[idx+k] < w:
                w -= self.tree[idx+k]
                idx += k
            k //= 2
        return idx+1

S = input()
N = len(S)
b = Bit(N)
x = [1 if s == "W" else 2 for s in S]
ans = 0
for i in range(N):
	ans += i-b.sum(x[i])
	b.add(x[i], 1)
print(ans)

Submission status: Accepted

Solution code:
S = input()
Sum = 0
b_num = 0
for s in S:
    if s=="W":
        Sum+=b_num
    else:
        b_num+=1
print(Sum)

Submission status: Accepted

Solution code:
S=list(input())
N=len(S)
sumB=0
ans=0

for i in range(N):
  if S[i]=="B":
    sumB +=1
  else:
    ans +=sumB
    
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
f_list = [k for k in range(len(S)) if S[k] == "W"]    
S.sort(reverse=True)
s_list = [k for k in range(len(S)) if S[k] == "W"]
ans = 0
for i in range(len(f_list)):
    ans += f_list[i]-s_list[i]
print(ans)

Submission status: Accepted

Solution code:
S = input()
l1 = list(S)
l2 = sorted(l1,reverse=True)
m1 = [i for i, x in enumerate(l1) if x == 'W']
m2 = [i for i, x in enumerate(l2) if x == 'W']
count = 0
for i in range(len(m1)):
    count += m1[i] - m2[i]
print(count)

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

S = input()
S_length = len(S)
black_indices = []
for i in range(S_length):
    if S[i] == 'B': black_indices.append(i)

total_blacks_indices = sum(black_indices)

black_count = len(black_indices)
index = S_length-1
result_black_indices = 0
for _ in range(black_count):
    result_black_indices += index
    index -= 1

print(result_black_indices - total_blacks_indices)

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
from collections import Counter
#template
#issueから始める
S = input()
ans = 0
count = 0
for i in range(len(S)):
    if S[i] == 'W':
        ans += i - count
        count += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
b = 0
res = 0
for i in range(len(s)):
    if s[i]=='B':
        b += 1
    else:
        res += b
print(res)

Submission status: Accepted

Solution code:
S = input()
ans = 0
count = 0
for i in range(len(S)):
    if S[i] == 'W':
        ans += i - count
        count += 1
print(ans)

Submission status: Accepted

Solution code:
s  = (input())
dist = 0
count = 0
for i in range(len(s)):
         if s[i] == "W":
                  count += 1
                  dist += i
print(dist-((count * (count - 1))//2))

Submission status: Accepted

Solution code:
S = list(input())

b = S.count("B")
# print(S, b)

ans = 0
n = len(S)
for i, s in enumerate(S):
    # print(i, s)
    if s == "B":
        ans += n - b - i
        b -= 1
        # print(ans, b)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
b_count = 0
ret = 0
S = input()

for s in S:
    if s == 'B':
        b_count += 1
    else:
        ret += b_count

print(ret)

Submission status: Accepted

Solution code:
s = input()

ans = 0
b_count = 0
for i, c in enumerate(s):
    if c == "W":
        ans += b_count
    else:
        b_count += 1

print(ans)

Submission status: Accepted

Solution code:
S = list(input())
ans = 0
b = 0
for i in range(len(S)):
    if S[i] == 'B':
        b += 1
    else:
        ans += b
        
print(ans)

Submission status: Accepted

Solution code:
#coding utf-8
S = input()
N = len(S)
cnt = 0
ans = 0
for i in range(N):
    if S[i] == 'W':
       ans += i - cnt
       cnt += 1
       
print(ans)

Submission status: Accepted

Solution code:
def main():
    reversi = input()
    answer = 0
    count_black = [0] * len(reversi)
    if reversi[0] == "B":
        count_black[0] = 1
    for i in range(1, len(reversi)):
        if reversi[i] == "B":
            count_black[i] = count_black[i - 1] + 1
        else:
            count_black[i] = count_black[i - 1]
    for i in range(len(reversi)):
        if reversi[i] == "W":
            answer += count_black[i]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = str(input())
ans,move = 0,0
for i in range(len(s)):
  if(s[i] == 'B'):
    move += 1
  else:
    ans += move
print(ans)

Submission status: Accepted

Solution code:
s = input()

ans = 0
b_count = 0
for i,c in enumerate(s):
    if c == "W":
        ans += b_count
    else:
        b_count += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    b = []
    w = []
    for i, v in enumerate(S):
        if v == 'B':
            b.append(i)
        else:
            w.append(i)
    if len(b) == 0 or len(w) == 0:
        return 0
    return sum(w) - (len(w) * (len(w)-1))//2

print(main())

Submission status: Accepted

Solution code:
def main():
    S = input()
    w = 0
    r = 0
    for i, s in enumerate(S):
        if s == 'W':
            r += i - w
            w += 1
    return r

print(main())

Submission status: Accepted

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

S = input().strip()
#-----

sorted = 0
cnt=0
for i,word in enumerate(S):
    if word == "W":
        cnt += i - sorted
        sorted += 1

print(cnt)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
Ans = 0
B = S.count("B")
for i in range(N):
    if S[i] == "B":
        Ans += N - B - i
        B -= 1

print(Ans)

Submission status: Accepted

Solution code:
s=input()

b_count = 0
ans = 0

for s_i in s:
  if s_i=='W':
    ans += b_count
  elif s_i=='B':
    b_count += 1

print(ans)

Submission status: Accepted

Solution code:
s = input()

cnt = 0
l = 0

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

print(cnt)

Submission status: Accepted

Solution code:
S = list(input())
ans = 0
cnt = 0
for i in range(len(S)):
    if S[i]=="B":
        cnt += 1
    else:
        ans += cnt
print(ans)

Submission status: Accepted

Solution code:
#029_A
s=input()
n=s.count('W')
res=0
for i in range(len(s)):
    if s[i]=='W':
        res+=i
print(res-(n-1)*n//2)

Submission status: Accepted

Solution code:
s = input()
s = [i for i in s]
w_count = s.count('W')
ans = 0
if w_count == 0:
    print(0)
else:
    number = [i for i in range(w_count)]
    w_element = [i for i,j in enumerate(s) if j=='W']
    for i in range(w_count):
        ans += abs(number[i]-w_element[i])
    print(ans)

Submission status: Accepted

Solution code:
S=input()
b=0
for i in range(0,len(S)):
    if S[i]=='B':
        b+=1

ans=0
cnt=0
for i in range(0,len(S)):
    if S[i]=='B':
        cnt+=1
        ans+=len(S)-1-i-(b-cnt)

print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input().rstrip()
    begin, end = 0, 0
    ans = 0
    for i in range(len(s)):
        if s[i] == "B":
            end = i
        else:
            end = i
            ans += end - begin
            begin += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()

ans = 0
cnt = 0
for i in range(len(S)):
    if S[i] == "B":
        cnt += 1
    else:
        ans += cnt

print(ans)

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
ans=0
res=0
for i in range(n):
    if s[n-1-i]=="W":
        res+=1
    else:
        ans+=res
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
black = 0
ans = 0
for c in S:
    if c == "B":
        black += 1
    else:
        ans += black
print(ans)

Submission status: Accepted

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


before = 0
cnt = 0
for i, si in enumerate(S):
    if si == 'B':
        cnt += 1
        before += i

after = 0
for i in range(cnt):
    after += (N-1) - i

# print(before, after)
print(after - before)