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: Runtime Error

Solution code:
N = list(input())
count = 0

def con(N,count, i):
    # print("con = " + str(i))
    if i < 1:
        return count

    elif N[i-1] == "B" and N[i] == "W":
        N[i-1] = "W"
        N[i] = "B"
        count += 1
        # print("2 = " + str(N))

        return con(N,count,i-1)
    
    return count


if len(N) == 1:
    print(0)

else:
    for i in range(len(N)-1):
        if N[i] == "B" and N[i+1] == "W":
            N[i] = "W"
            N[i+1] = "B"
            count += 1
            # print("1 = " + str(N))
            count = con(N, count,i)
            # print("3 = " + str(N))

    print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)-1
count = 0

def a(i):
    global count
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        count += 1
        if(i>0) and s[i-1] == "B" and s[i] == "W":
            a(i-1)

for i in range(n):
    a(i)

print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)-1
count = 0

def a(i):
    global count
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        count += 1
        if(i>0):
            a(i-1)

for i in range(n):
    a(i)

print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
t = input()
s = input()
s = s.split()
list=[]
for i in range(len(s)):
  list.append(int(s[i]))

kind = 1
counter=0

for i in range(len(list)-1):
  if(list[i]>=list[i+1]):
    if(kind==1):
      kind = kind + 1
    else:
      if(counter==kind-1):
        kind = kind + 1
        counter=0    
      else:
        counter = counter + 1
      
print(kind)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)-1
count = 0
i=0

def a(i):
    global count
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        count += 1
        if(i>0):
            a(i-1)

for i in range(n):
    a(i)
    i+=1

print(count)

Submission status: Runtime Error

Solution code:
N = list(input())
count = 0


def con(N,count, i):
    # print("con = " + str(i))
    if i < 1:
        return count

    elif N[i-1] == "B" and N[i] == "W":
        N[i-1] = "W"
        N[i] = "B"
        count += 1
        # print("2 = " + str(N))

        return con(N,count,i-1)
    
    return count


if len(N) == 1:
    count = 1

else:
    for i in range(len(N)-1):
        if N[i] == "B" and N[i+1] == "W":
            N[i] = "W"
            N[i+1] = "B"
            count += 1
            # print("1 = " + str(N))
            count = con(N, count,i)
            # print("3 = " + str(N))

print(count)

Submission status: Runtime Error

Solution code:
S = str(input())
count = 0;
for i in range(100000)
  count=count+S.count('BW');
  S = S.replace('BW','WB');
  if 'BW' not in S:break;
print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)-1
count = 0
def a(i):
    global count
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        count += 1
        a(i-1)
        print(i,s)

for i in range(n):
    a(i)
print(count)

Submission status: Runtime Error

Solution code:
import re
src = input()
count = 0
 
for  i in xrange(200000):
    if 'BW' in src :
       count += len(re.findall('BW', src))
       src = src.replace('BW', 'WB')
    else:
        break
print( count )

Submission status: Runtime Error

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

for i in range(len(S)):
  if S[i] == "B":
    black += 1

def f(N, BW, black):
  if N == "B" or N =="W":
    return 0
  elif BW == "B":
    return f(N[1:], N[1], black - 1) + len(N) - black
  elif BW == "W":
    return f(N[1:], N[1], black)

print(f(S, S[0], black))

Submission status: Runtime Error

Solution code:
S = input()
black = 0
count = 0
a = []

for i in range(len(S)):
  a.append(S[i])

for i in range(len(S)):
  if S[i] == "B":
    black += 1

def f(N, BW, black):
  if N == "B" or N =="W":
    return 0
  elif BW == "B":
    return f(N[1:], N[1], black - 1) + len(N) - black
  elif BW == "W":
    return f(N[1:], N[1], black)

print(f(S, S[0], black))

Submission status: Runtime Error

Solution code:
src = input()
count = 0

for  i in range(9999999):
    if 'BW' in src :
       count += len(src.findAll('BW'))
       src = src.replace('BW', 'WB')
    else:
        break
print( count )

Submission status: Runtime Error

Solution code:
stone_list = list(input())
num = 0


def count_changed_num(stone_list):
    for index,stone in enumerate(stone_list):
        if index != (len(stone_list) - 1):
            if stone == "B" and stone_list[index + 1] == "W" :
                stone_list[index] = "W"
                stone_list[index + 1] = "B"
                global num
                num += 1
                count_changed_num(stone_list)
            else:
                pass
        else:
            pass
        
count_changed_num(stone_list)
print(num)

Submission status: Runtime Error

Solution code:
s=list(input())
count=0

indexes = [i for i, x in enumerate(s) if x == "B"]
if len(s)%2==1:
  ideal=[i for i in range(len(s)//2+1,len(s))]
else:
  ideal=[i for i in range(len(s)//2,len(s))]

for i in range(len(s)//2):
  count+= ideal[i]-indexes[i]
print(count)

Submission status: Runtime Error

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


def main():
    """Function."""
    s = str(input())
    num = len(s)

    total = 0
    s_list = list(s)
    for _ in range(num):
        if s_list == []:
            break

        b_c, w_c = 0, -1
        while s_list[b_c] == "W":
            b_c += 1
        while s_list[w_c] == "B":
            w_c -= 1
        if b_c == 0:
            pass
        else:
            del s_list[:b_c]
        if w_c == -1:
            pass
        else:
            del s_list[w_c+1:]

        if s_list == []:
            break

        for i in range(num-1):
            end = num - i
            start = end - 2
            if s_list[start:end] == ["B", "W"]:
                total += 1
                s_list[start:end] = ["W", "B"]
    print(total)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
s=input()
t=input()
list=[]
list = t.split()
print(list)
ans=0

def even(i,k):
  for j in range(k+1,len(list)):
    if(int(list[j])%2==0):
      s = i + int(list[j])
      if(power(s)):
        return 1
    else:
      continue
      
  return 0
  
def odd(i,k):
  for j in range(k+1,len(list)):
    if(int(list[j])%2==0):
      continue
    else:
      s = i + int(list[j])
      if(power(s)):
        return 1
      
  return 0
      

def power(s):
  while(True):
    s = int(s / 2)
    if(s==1):
       return 1

    if(s % 2 != 0):
      break      
      return 0

for i in range(len(list)-1):
  if(int(list[i]) % 2 == 0):
    if(even(int(list[i]),i)):
      ans = ans+1
  else:
    if(odd(int(list[i]),i)):
      ans = ans+1

print(ans)

Submission status: Runtime Error

Solution code:
s = str(input())

if s.count('B')!=len(s):
  if s.count('W')!=len(s):
    if s.count('BW')!=0:
      a = s.replace('B', '1')
      a = a.replace('W', '0')
      ans = int( int(a, 2) / int('1'*a.count('1'), 2))
      print(ans)
    else:
      print('0')
  else:
    print('0')
else:
  print('0')

Submission status: Runtime Error

Solution code:
s = str(input())

if s.count('B')!=len(s):
  if s.count('W')!=len(s):
    if s.count('BW')!=0:
      print('OK')
      a = s.replace('B', '1')
      a = a.replace('W', '0')
      ans = int( int(a, 2) / int('1'*a.count('1'), 2))
      print(ans)
    else:
      print('0')
  else:
    print('0')
else:
  print('0')

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
S = list(input())
src = list('BW')
dst = list('WB')
count = 0
def reverse(i):
    global count
    if S[i:i+2] == src:
        S[i:i+2] = dst
        count += 1
        reverse(i-1)
for i in range(len(S)-1):
    reverse(i)
print(count)

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():
    bw=stdin.readline().replace('B', '0').replace('W', '1')
    A=list(map(int,bw))
    #A = list(map(int,stdin.readline().replace('B', '0').replace('W', '1')))

    A=[0,0,1]
    wc = A.count(1)
    indices = [i for i, x in enumerate(A) if x == 1]
    if wc > 0:
      print(sum(indices) - 1*wc*(wc-1) //2 )
    else:
      print(sum(indices))

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

Solution code:
s = input()
s = s.replace("B","1")
s = s.replace("W","0")
#print(s)

a = "0"*s.count("0")+"1"*s.count("1")
print(int(int(s,2)/int(a,2)))

Submission status: Runtime Error

Solution code:
S = list(input())
src = list('BW')
dst = list('WB')
count = 0
def reverse(i):
    global count
    if S[i:i+2] == src:
        S[i:i+2] = dst
        count += 1
        reverse(i-1)
for i in range(len(S)-1):
    reverse(i)
print(count)

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():
    A = list(map(int,stdin.readline().replace('B', '0').replace('W', '1')))

    A=[0,0,1]
    wc = A.count(1)
    indices = [i for i, x in enumerate(A) if x == 1]
    if wc > 0:
      print(sum(indices) - 1*wc*(wc-1) //2 )
    else:
      print(sum(indices))

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

Solution code:
temp=input()
S=list(temp)
counter1=0
counter2=0
counter3=0 
i=len(S)-1

while True:
    if S[i-1]=='B' and S[i]=='W':
        S[i-1]='W'
        S[i]='B'
        counter1 +=1
    i -=1
    if i ==0:
        if counter1==counter2:
            break
        counter2=counter1
        for j in range(len(S)):
            if S[j]=='W':
                counter3 =j
            j -=1
        i= counter3

print(counter1)

Submission status: Runtime Error

Solution code:
s = str(input())

if s.count('B')!=len(s):
  if s.count('W')!=len(s):
    if s.count('BW')!=0:
      a = s.replace('B', '1')
      a = a.replace('W', '0')
      ans = int( int(a, 2) / int('1'*a.count('1'), 2))
      print(ans)   
else:
  print('0')

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():
    A = list(map(int,stdin.readline().replace('B', '0').replace('W', '1')))

    wc = A.count(1)
    indices = [i for i, x in enumerate(A) if x == 1]
    if wc > 0:
      print(sum(indices) - 1*wc*(wc-1) //2 )
    else:
      print(sum(indices))

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

Solution code:
s = str(input())

if s.count('B')!=len(s) & s.count('W')!=len(s) & s.count('BW')!=0:
      a = s.replace('B', '1')
      a = a.replace('W', '0')
      ans = int( int(a, 2) / int('1'*a.count('1'), 2))
      print(ans)
      
else:
  print('0')

Submission status: Runtime Error

Solution code:
from sys import stdin
def main():
    A = list(map(int,stdin.readline().replace('B', '0').replace('W', '1')))

    wc = A.count(1)
    indices = [i for i, x in enumerate(A) if x == 1]
    if wc > 0:
      print(sum(indices) - 1*wc*(wc-1) //2 )
    else:
      print(sum(indices))

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
def main():
    A = list(map(int,stdin.readline().replace('B', '0').replace('W', '1')))

    wc = A.count(1)
    indices = [i for i, x in enumerate(A) if x == 1]
    print(sum(indices) - 1*wc*(wc-1) //2 )

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

Solution code:
s = str(input())

if s.count('B')!=len(s):
  if s.count('W')!=len(s):
    if s.count('BW')>0:

      a = s.replace('B', '1')
      a = a.replace('W', '0')

      ans = int( int(a, 2) / int('1'*a.count('1'), 2))
      print(ans)
else:
  print('0')

Submission status: Runtime Error

Solution code:


ball_num = int(input())
num_data = list(map(int, input().split()))

ans = 0

def check_beki(num1, num2):
    data = num1 + num2
    while 1:
        data1 = data % 2
        data = int(data / 2)
        if data1 == 1:
            return 0
        elif num1 + num2 >= 1 and data == 1:
            return 1

def check_ans(ans_list, tasu_flg):
    global ans
    ans_kari = 0
    for i in range(len(ans_list)):
        if check_beki(ans_list[i], 0) and tasu_flg[i] == 1:
            ans_kari += 1
    if ans_kari > ans:
        ans = ans_kari

def check(index, ans_list_ori, tasu_flg_ori):
    global ball_num, num_data
    ans_list = ans_list_ori.copy()
    tasu_flg = tasu_flg_ori.copy()

    # if len(ans_list) >= tree_num:
    #     return

    # print(check_beki(3, 5))

    if index < ball_num:
        for j in range(len(ans_list)):
            # print(ans_list[j], num_data[index])
            if check_beki(ans_list[j], num_data[index]) and tasu_flg[j] == 0:
                # print('yyy')
                ans_list[j] += num_data[index]
                tasu_flg[j] = 1
                check(index+1, ans_list, tasu_flg)
                tasu_flg[j] = 0
                ans_list[j] -= num_data[index]
            # 新しく木を使う場合
            ans_list.append(num_data[index])
            tasu_flg.append(0)
            check(index+1, ans_list, tasu_flg)
            tasu_flg.pop(-1)
            ans_list.pop(-1)
    else:
        # 以前よりも最少の気で作れたら最小値の更新
        check_ans(ans_list, tasu_flg)


def main():
    global num_data
    check(1, [num_data[0]], [0])

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
S = input()
length = len(S)
for c in S:
    if c == 'B':
        countB += 1
soko = 1
ans = 0
for i in range(length):
    if S[-i-1] == 'B':
        ans += i + 1  - soko
        soko += 1
print(ans)

Submission status: Runtime Error

Solution code:
S=input()
count=S.count("BW")
end=0
def sigma(i):
    a=[b for b in range(i+1)]
    return sum(a)
place=[]
i=0
while "BW" in S:
    i=S[i:].find("BW") + 1 + i
    place.append(i)
    S=S[:i-1]+S[i-1:].replace("BW","BB",1)
    print(S)

count=sum(place)-sigma(count-1)
print(count)

Submission status: Runtime Error

Solution code:
s = input()
c = 0

 while True:
  l = s.index('W') - s.index('B')
  if l < 0:
    break
  c += l
  s = s.replace('W', 'B', 1)
  s = s[1:]

print(c)

Submission status: Runtime Error

Solution code:
function Main(input) {
  input = input.split("\n");
  var S= input[0];
  var ans=0;
  var flag=1;
  while(flag==1){
    var counter = function(str,seq){
      return str.split(seq).length - 1;
    }
    ans+=counter(S,"BW");
    S=S.replace(/BW/g,"WB");
    if(counter(S,"BW")==0){
      flag=0;
    }
  }

  console.log('%s', ans);
}
  
// "実行する"ボタンを押した時に実行される関数 (デバッグ用)
function debug(){
  var input = document.getElementById("input").value;
  Main(input);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Submission status: Runtime Error

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

def search(t,ans):
    if t==n-1:
        print(ans)
    elif S[t]=='B' and S[t+1]=='W':
        S[t],S[t+1]='W','B'
        if t: search(t-1,ans+1)
        else: search(1,ans+1)
    else:
        search(t+1,ans)
    
search(0,0)

Submission status: Runtime Error

Solution code:
ans=0
for i in range(1000000):
    cnt=N.count("BW")
    ans=ans+cnt
    N=N.replace("BW","WB")
    N=N.lstrip("W")
    N=N.rstrip("B")
    if cnt==0:
        break
print(ans)

Submission status: Runtime Error

Solution code:
# your code goes here
import numpy as np

def a(s, w, ans):
	if(s == ""):
		return ans
	elif(s[-1] == 'B'):
		if(w == 0):
			return a(s[:-1], 0, ans)
		else:
			return a(s[:-1], w, ans+w)
	else:
		return a(s[:-1], w+1, ans)

def main():
	s = input()
	print(a(s, 0, 0))
	
main()

Submission status: Runtime Error

Solution code:
def run(S):
    s = 0
    f = len(S) - 1
    cnt = 0
    while s < f:
        while S[s] == 'W' or S[f] == 'B':
            if S[s] == 'W':
                s += 1
            elif S[f] == 'B':
                f -= 1
        for i in range(s, f):
            if S[i] == 'B' and S[i+1] == 'W':
                cnt += 1
                S = S[:i] + 'WB' + S[i+2:]
    return cnt


def main():
    S = input()
    print(run(S))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = str(input())
a = s.replace('B', '1')
a = a.replace('W', '0')

ans = int( int(a, 2) / int('1'*a.count('1'), 2))
print(ans)

Submission status: Runtime Error

Solution code:
# your code goes here
import numpy as np

def a(s, w):
	if(s == ""):
		return 0
	elif(s[-1] == 'B'):
		if(w == 0):
			return a(s[:-1], 0)
		else:
			return a(s[:-1], w)+w
	else:
		return a(s[:-1], w+1)

def main():
	s = input()
	print(a(s, 0))
	
	
	
	
	
main()

Submission status: Runtime Error

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

def search(t):
    for i in range(t,n-1):
        if S[i]=='B' and S[i+1]=='W':
            S[i],S[i+1]='W','B'
            if i>0:
                return 1+search(i-1)
            else:
                return 1+search(1)
    return 0

print(search(0))

Submission status: Runtime Error

Solution code:
S = input()
N = 0
while True:
    S, M = re.subn('BW', 'WB', S)
    if M == 0:
        break
    N += M
print(N)

Submission status: Runtime Error

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

def search(t):
    for i in range(t,n-1):
        if S[i]=='B' and S[i+1]=='W':
            S[i],S[i+1]='W','B'
            if t>0:
                return 1+search(t-1)
            else:
                return 1+search(1)
    return 0

print(search(0))

Submission status: Runtime Error

Solution code:
 = [i for i in input()]
f = True
i = 0
cnt = 0
while True:
    if s[i] == 'B' and s[i+1] == 'W':
        cnt += 1
        s[i] = 'W'
        s[i+1] = 'B'
        i += 1
        f = False
    else:
        i += 1
    if i >= len(s) - 1:
        if f:
            break
        i = 0
        f = True
print(cnt)

Submission status: Runtime Error

Solution code:
s=list(input())

ima=0
wNum=0
for i in range(len(s)):
  if s[i]=="W":
    ima += i
    wNum += 1

def kasan(n):
  if n==0:
    return 0
  else:
    return kasan(n-1) + n
if wNum==0:
  print(0)
else:
  saigo = kasan(wNum-1)
  print(ima-saigo)

Submission status: Runtime Error

Solution code:
#S = input().strip()
Sb = [c == "B" for c in S]

def reverse(Sb,i):
    _list = Sb.copy()
    _list[i] = not _list[i]
    return _list

def operate_onestep(Sb):
    _list = Sb.copy()
    cnt = 0
    flg = True
    while flg:
        flg = False
        for i in range(len(_list)-1):
            if _list[i] and not _list[i+1]:
                cnt += 1
                _list = reverse(_list,i)
                _list = reverse(_list,i+1)
                flg = True
    return cnt

print(operate_onestep(Sb))

Submission status: Runtime Error

Solution code:
S = array(input())
print(S)

Submission status: Runtime Error

Solution code:
# AtCoder Grand Contest 029
# December 15, 2018
#

S = input()
count = 0

while True:
  if S.find('BW') >= 0:
    S = S.replace('BW', 'WB', 1)
    count += 1
    print(S)
  else:
    break

print(count)

Submission status: Runtime Error

Solution code:
list = list(s)
count=0
b=0

for i in range(len(list)):
  if(list[i] == "W"):
    count+=b
    continue
  else:
    if(i == len(list)-1):
      break
    b += 1
      
print(count)

Submission status: Runtime Error

Solution code:
S = input()

l = len(S)-1
count = 0
for i in range(0..l):
    if S[i] == 'W': continue
    if S[i+1] == 'B': continue
    S[i] = 'W'
    S[i+1] = 'B'
    count = count+1

print(count)

Submission status: Runtime Error

Solution code:
S = list(input())
def rec(list1,t):
    for i in range(len(list1)-1):
        if list1[i] == "B" and list1[i+1] == "W":
            list1[i] = "W"
            list1[i+1] = "B"
            return rec(list1,t+1)
    return t
result = rec(S,0)
print(result)