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:
import sys


def main():
    s = sys.stdin.readline().rstrip()

    black = 0
    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def main():
    s = sys.stdin.readline().rstrip()

    black = 0
    ans = 0

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

    print(ans)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

s = [x for x in sys.stdin.readline().rstrip()]

black = float("inf")
ans = 0

for i in range(len(s)):
    if s[i] == "B" and black > i:
        black = i
    elif s[i] == "W" and black != float("inf"):
        ans += i - black
        black += 1

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
l = [0 for i in range(n)]
cou = 0
for i in range(n):
  k = s[i]
  if k=="B":
    l[i] += 1+l[i-1]
  else:
    l[i] += l[i-1]
    cou += l[i]
#  print(l)
print(cou)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy

# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
S = str(input())

count = 0

for i in range (0, len(S)):
	if S[i] == 'W':
		count+=1

choma = 0

for i in range (0, len(S)):
	if S[i] == 'W':
		choma+=(i+1)

if count*(count+1)/2 < choma:
	print(int(choma-count*(count+1)/2))
else:
	print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
	S = input()
	n = len(S)
	ans = 0

	black = 0
	f_pos = 0
	l_pos = 0

	for i, c in enumerate(S):
		if(c == 'B'):			
			f_pos += i
			black += 1

	l_pos = sum(list(range(n-1, n-1-black, -1)))
	

	ans = l_pos - f_pos

		
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

s = ns()
n = len(s)

lastw = 0
ans = 0
for i in range(n - 1, -1, -1):
    if s[i] == 'W':
        lastw = max(lastw, i)
    else:
        ans += max(0, lastw - i)
        lastw = max(0, lastw - 1)

print(ans)

Submission status: Accepted

Solution code:
def resolve():
  S = input().strip()
  
  ans = 0
  n_black = 0
  for index, s in enumerate(S):
    if s == "B":
      ans += len(S) - index - 1 - n_black
      n_black += 1
  
  print(ans)
resolve()

Submission status: Accepted

Solution code:
s = input()
ans = 0
b = 0
for c in s:
    if c == 'W':
        ans += b
    elif c == 'B':
        b += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()

#W,Bの総数は変わらない
#WWWWBBBBのような状態になるまでのWの転移回数を求める
wp=s.count('W')

now=0
count=0
for i in range(len(s)):
    if s[i]=='W':
        now+=i
for i in range(wp):
    count+=i
print(now-count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

def main():
    S = list(readline()[:-1])
    cnt = 0
    ans = 0
    for i, s in enumerate(S):
        if s == 'W':
            ans += i - cnt
            cnt += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
b = "B"
w = "W"
con = 0
ans = 0
for i in range(len(S)):
  if S[i] == b:
    con += 1
  elif S[i] == w:
    ans += con
print(ans)

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    S = input()
    res = 0
    num = 0
    for i in range(len(S)):
        if S[i] == 'W':
            res += num
        else:
            num += 1
    print(res)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
s = list(input())
len_s = len(s)
len_b = s.count('B')

b_index_list = []

for i in range(len_s):
    if s[i] == 'B':
        b_index_list.append(i)

b_index_list.reverse()
l0 = len_s - 1
cnt = 0

for b in b_index_list:
    cnt += l0 - b
    l0 -= 1

print(cnt)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

cnt =  0
cnt_W = 0
for i in range(n):
	if s[i] == 'W':
		cnt += i
		cnt_W += 1

res = cnt - (cnt_W - 1) * cnt_W // 2
print(res)

Submission status: Accepted

Solution code:
s = input()

b = 0
ans = 0
for bw in s:
    if bw == "B":
        b += 1
    else:
        ans += b
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

b = 0
b_count = 0
for i in range(n):
    if s[i] == 'B':
        b += i
        b_count += 1

c = 0
for i in range(n-b_count, n):
    c += i

print(c-b)

Submission status: Accepted

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

cnt = 0
ans = 0

for i in s:
    if i == "B":
        cnt += 1
    else:
        ans += cnt
print(ans)

Submission status: Accepted

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

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()
b = 0
ans = 0
 
for s in S:
    if s == 'B':
        b += 1
    else:
        ans += b
 
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 0
cnt_w = 0
for i, ss in enumerate(s):
    if ss == 'W':
        ans += i - cnt_w
        cnt_w += 1

print(ans)

Submission status: Accepted

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

for s in S:
    if s == 'B':
        b += 1
    else:
        ans += b

print(ans)

Submission status: Accepted

Solution code:
#AGC029-A Irreversible operation
"""
問題：
一直線上にオセロがある
一回の操作で[黒白]を裏返す時[白黒]、最大何回の操作が行えるか
解法：
まずいちばん最初のBが出てくるところまでは無視できる
そこから、Wが出てくる度にそこまでに出現したBの数を足した合計が答え
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
flag = 0
black = 0
white = 0
ans = 0
for i in s:
    if flag:
        if i == "B":
            black += 1
        else:
            ans += black
    else:
        if i == "B":
            flag = 1
            black += 1
print(ans)

Submission status: Accepted

Solution code:
#AGC029-A Irreversible operation
"""
問題：
一直線上にオセロがある
一回の操作で[黒白]を裏返す時[白黒]、最大何回の操作が行えるか
解法：
まずいちばん最初のBが出てくるところまでは無視できる
そこから、Bの数*Wの数が答え
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
flag = 0
black = 0
white = 0
ans = 0
for i in s:
    if flag:
        if i == "B":
            black += 1
        else:
            ans += black
    else:
        if i == "B":
            flag = 1
            black += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
n = len(s)
b_count = 0
score = 0
for i in range(n-1, -1, -1):
  if s[i] == "B":
    score += n-1-i-b_count
    b_count += 1
print(score)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
ans = 0
black = 0
for c in s:
    if c=='B':
        black += 1
    else:
        ans += black
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = str(input())
    B_count = 0
    result = 0
    for i in range(0, len(N)):
        target = N[i]
        if target == 'W':
            result = result + B_count
        else:
            B_count = B_count + 1        
    print(result)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    s = IS()
    s_b = [1 if si == 'B' else 0 for si in s]
    cnt = 0
    ans = 0
    for b in s_b:
        if b == 1:
            cnt += 1
        else:
            ans += cnt
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s =list(str(input()))
s_replace = [1 if i == "B" else 0 for i in s]
 
count = 0
ans = 0
 
for i in range(len(s_replace)):
  if s_replace[i] == 1: #もし黒だったらカウントしてください
    count += 1
    continue
  else: #もし白だったら黒の数を足してください
    ans = ans + count
    continue
    
print(ans)

Submission status: Accepted

Solution code:
s =list(str(input()))
s_replace = [1 if i == "B" else 0 for i in s]

count = 0
ans = 0

for i in range(len(s_replace)):
  if s_replace[i] == 1: #もし黒だったらカウントしてください
    count += 1
    continue
  else: #もし白だったら黒の数を足してください
    ans = ans + count
    continue
    
print(ans)

Submission status: Accepted

Solution code:
# 2020/05/02
# AtCoder Grand Contest 029 - A

# Input
s = input()

sidx = len(s) - 1
bidx = -1
ans = 0
leftflg = True
while(sidx >= 0):
    # Set Init position of B
    if leftflg and s[sidx] == 'W':
        leftflg = False
        bidx = sidx
    # Put B
    elif s[sidx] == 'B':
        ans = ans + max(0, bidx - sidx) 
        bidx = bidx - 1
        
    sidx = sidx - 1

# Output
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

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

Submission status: Accepted

Solution code:
s = 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:
s = input()
res = 0
B = 0
for i in range(len(s)):
  if s[i] == 'B':
    B += 1
  else:
    res += B
print(res)

Submission status: Accepted

Solution code:
s = str(input())
n = len(s)
distance = 0
count = 0
max_W = 0

for i in range(n):
    if s[i] == 'W':
        distance += (i - count)
        count += 1
        max_W = i

if s.count('W') == 0 or s.count('W') == n:
    print(0)
    exit()

print(distance)

Submission status: Accepted

Solution code:
s = input()
r = 0
c = 0
for i in range(len(s)-1, -1, -1):
    if s[i] == "B":
        r += c
    else:
        c += 1
print(r)

Submission status: Accepted

Solution code:
S = input()

sumB = 0
count = 0
for x in S:
    if x == "B":
        count += 1
    if x == "W":
        sumB += count
print(sumB)

Submission status: Accepted

Solution code:
S=input()

B=[]
b=0
for i in range(len(S)):
    if S[i]=="B":
        b+=1
        B.append(b)
    else:
        B.append(b)

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

print(ans)

Submission status: Accepted

Solution code:
s=input()
c=0
f=0
ans=0
for i in range(len(s)):
  if f==0 and s[i]=='W':
    c+=1
  elif f==0 and s[i]=='B':
    f=1
  elif f==1 and s[i]=='W':
    ans+=i-c
    c+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input() #sys.stdin.readlineは最後が改行

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

print(ans)

Submission status: Accepted

Solution code:
S = str(input())
counter = 0
ans = 0
for i, v in enumerate(S):
    if v == 'B':
        counter += 1
    else:
        ans += counter

print(ans)

Submission status: Accepted

Solution code:
S=input()
Bind=[]
for i in range(len(S)):
    if S[i]=='B':
        Bind.append(i)
cnt=0
for i in reversed(range(len(Bind))):
    cnt+=len(S)-1-i-Bind[i]
print(cnt)

Submission status: Accepted

Solution code:
s = list(input())[::-1]

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

print(ans)

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:
S = list(input())
s =len(S)
p = 0
q = 0
for i in range (s):
    if S[i]=='W':
        q += (i - p)
        p += 1
print(q)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

w = 0
for c in s:
    if c == 'W':
        w += 1
ans = []

for c in s:
    if c == 'B':
        ans.append(w)
    else:
        w -= 1
print(sum(ans))

Submission status: Accepted

Solution code:
S = list(input())
black_ind = [i for i, s in enumerate(S) if s == 'B']
current_ind_sum = sum(black_ind)
import numpy as np
final_list = np.arange(len(S))[len(S)-len(black_ind):]
print(sum(final_list)-current_ind_sum)

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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    S = input()
    S = S.lstrip('W').rstrip('B')
    L = len(S)
    left = 0
    ans = 0
    for i in range(L):
        if S[i]=='W':
            ans += i-left
            left += 1
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
N = S.count('W')
B = sum([b for b in range(N) if S[b] == 'B'])
W = sum([w for w in range(N, len(S)) if S[w] == 'W'])
print(W-B)

Submission status: Accepted

Solution code:
S = input()
N = S.count('W')
B = sum([b for b in range(1, N+1) if S[b-1] == 'B'])
W = sum([w for w in range(N+1, len(S)+1) if S[w-1] == 'W'])
print(W-B)

Submission status: Accepted

Solution code:
S = input()
N = S.count('W')

B = [b for b in range(1, N + 1) if S[b-1] == 'B']
W = [w for w in range(N+1, len(S)+1) if S[w-1] == 'W']
# assert len(B) == len(W)

print(sum(W) - sum(B))

Submission status: Accepted

Solution code:
S = input()
N = S.count('W')

B = [b for b in range(1, N + 1) if S[b-1] == 'B']
W = [w for w in range(N+1, len(S)+1) if S[w-1] == 'W']
# assert len(B) == len(W)

ans = 0
for b, w in zip(B, W):
    ans += w - b

print(ans)

Submission status: Accepted

Solution code:
S = input()[::-1]

s = 0
countw = 0
for i, v in enumerate(S):
    if v == 'B' and i == 0:
        continue
    elif v == 'W':
        countw += 1
    elif v == 'B':
        s += countw

print(s)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
S=input()
ans = 0
w_cnt = 0
for n,i in enumerate(S):
    if i=="W":
        ans += n-w_cnt
        w_cnt += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AtCoder
S = list(input())
N = len(S)

cnt = 0
ans = 0

for i in range(N):
    if S[i] == 'W':
        ans += cnt
    else:
        cnt += 1

print(ans)

Submission status: Accepted

Solution code:
s=list(input())
count = 0
black = 0
for name in s:
    if name == 'B':
        black += 1
    else:
        count += black
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()

    cnt_B = 0
    res = 0
    for i in s:
        if i == "B":
            cnt_B += 1

        if i == "W":
            res += cnt_B

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = list(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    S = input().rstrip('\n')
    cc = 0
    itr = 0
    for i in range(len(S)):
        if S[i] == 'W':
            itr += 1
            cc += i - (itr - 1)
    S = S[::-1]
    itr = 0
    for i in range(len(S)):
        if S[i] == 'B':
            itr += 1
            cc += i - (itr - 1)
    print(cc//2)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
# If i-th stone is i_j-th white stone, add `i - i_j`
white_cnt = 0
res = 0
for index, stone in enumerate(s):
    i = index + 1
    if stone == 'W':
        white_cnt += 1
        res += i - white_cnt
print(res)

Submission status: Accepted

Solution code:
S = input()
w = S.count('W')
ans = 0
c = 0
for i, s in enumerate(S):
    if s == 'B':
        ans += w - i + c
        c += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
w = 0
b = 0
wa = 0
ba = 0
l = len(s)
 
for i in range(l):
  if s[i] == 'W':
    wa += i - w
    w += 1
  if s[-1-i] == 'B':
    ba += i - b
    b += 1
    
print(min(ba, wa))

Submission status: Accepted

Solution code:
s=input()
for i in range(len(s)):
    if s[i]=="B":
        break
new_s = s[i:]

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

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
s = input()
n = len(s)
ans = 0
# while True:
#     finish = True
#     for i in range(n - 1):
#         if s[i] == "B" and s[i + 1] == "W":
#             finish = False
#             s = s[:i] + "WB" + s[i + 2:]
#             ans += 1
#     if finish:
#         break
cnt = 0
for i in range(n):
    if s[i] == "W":
        cnt += 1
        ans += i
ans -= (cnt - 1) * cnt // 2
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = b = 0 
for i in s:
    if i=='W':ans+=b
    else:b+=1
print(ans)

Submission status: Accepted

Solution code:
S = input() # S_i = B(lack) or W(hite)
N = len(S)

"""
    最終的には W..W...B...B のような並びになる。
    元の状態から B がどれだけ右に進んだかの総和が答え。
"""

""" White version """
cnt_initial = 0
cnt_w = 0
for i in range(N):
    if S[i] == 'W':
        cnt_initial += i
        cnt_w += 1
ans = cnt_initial - sum([i for i in range(cnt_w)])


""" Black version """

# def sum_contin_int(l, r):
#     return (r * (r + 1) - l * (l - 1)) // 2

# cnt_initial = 0
# cnt_b = 0
# for i in range(N):
#     if S[i] == 'B':
#         cnt_initial += i
#         cnt_b += 1

# # cnt_final = sum_contin_int(N - cnt_b, N - 1)
# cnt_final = sum([i for i in range(N - cnt_b, N)])

# ans = cnt_final - cnt_initial
print(ans)

Submission status: Accepted

Solution code:
S = input() # S_i = B(lack) or W(hite)
N = len(S)

"""
    最終的には W..W...B...B のような並びになる。
    元の状態から B がどれだけ右に進んだかの総和が答え。
"""

# def sum_contin_int(l, r):
#     return (r * (r + 1) - l * (l - 1)) // 2

cnt_initial = 0
cnt_b = 0
for i in range(N):
    if S[i] == 'B':
        cnt_initial += i
        cnt_b += 1

# cnt_final = sum_contin_int(N - cnt_b, N - 1)
cnt_final = sum([i for i in range(N - cnt_b, N)])

ans = cnt_final - cnt_initial
print(ans)

Submission status: Accepted

Solution code:
S = input() # S_i = B(lack) or W(hite)
N = len(S)

"""
    最終的には W..W...B...B のような並びになる。
    元の状態から B がどれだけ右に進んだかの総和が答え。
"""

def sum_contin_int(l, r):
    return (r * (r + 1) - l * (l - 1)) // 2

cnt_initial = 0
cnt_b = 0
for i in range(N):
    if S[i] == 'B':
        cnt_initial += i
        cnt_b += 1

cnt_final = sum_contin_int(N - cnt_b, N - 1)

ans = cnt_final - cnt_initial
print(ans)

Submission status: Accepted

Solution code:
def solve():
    s = list(input())

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

    w = s.count('W')
    a = [int(i) for i in range(w)]
    cnt -= sum(a)

    return cnt

if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
S = [i for i in input()]
ans = 0
c = 0
'''while S[0] == 'W':
    S = S[1:]
    '''
for i,s in enumerate(S):
    if s == 'B':
        c += 1
    else:
        ans += c
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)

count=0
total=0
for i in range(n):
  if s[i]=="W":
    total+=i
    count+=1

print(int(total - (count-1)*count/2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

cnt = 0
ans = 0
for i in s:
    if i == 'B':
        cnt += 1
    else:
        ans += cnt

print(ans)

Submission status: Accepted

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):
    return map(type,input().split())

#%%code

def resolve():
    
    S=input()
    index=0
    ans=0
    for i in range(len(S)):
        if S[i]=="W":
            ans+=i-index
            index+=1
    print(ans)
#%%submit!
resolve()

Submission status: Accepted

Solution code:
S = input()[::-1]
w = 0
ans = 0
for e in S:
    if e == "W":
        w += 1
    else:
        ans += w
print(ans)

Submission status: Accepted

Solution code:
s=input()

s=list(s)

kazu = []

for i in range(len(s)):
    if s[i] == 'W':
        kazu.append(i)
ans = 0
for i in range(len(kazu)):
    ans += kazu[i]-i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
total_w = [0]*len(S)
cnt_w = S.count('W')

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

ans = 0
for i in range(len(S)):
    if S[i]=='B':
        ans += total_w[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    s = input()

    b = 0
    for c in s:
        if c == "B":
            b += 1
        else:
            yield b

print(sum(main()))

Submission status: Accepted

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

def main():
    s = input()
    total = 0

    for i in range(len(s)-1):
        if s[i] == "B":
            total += 1
            if s[i] == "B" and s[i+1] == "W":
                yield total
        else:
            if s[i] == "W" and s[i+1] == "W":
                yield total

print(sum(main()))