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:
s = input()

if s[0]=="W":
    l = [1]
else:
    l = [0,1]

for i in range(1,len(s)):
    if s[i]==s[i-1]:
        l[-1]+=1
    else:
        l.append(1)

rightw = sum(l[2::2])

ans = 0

for i in range(len(s)//2):
    ans += l[2*i+1]*rightw
    rightw -= l[2*i+2]

print(ans)

Submission status: Runtime Error

Solution code:

bws = input()

count = 0

def check(from, to):
  for i in range(from, to):
    if (bws[i] == 'B' && bws[i + 1] == "W"):
      count = count + 1
      bws[i] = reverse(bws[i])
      bws[i + 1] = reverse(bws[i + 1])
      check(i - 2, to)
      return
    
def reverse(c):
  return 'W' if c == 'B' else 'B' 

check(0, len(bws) - 2)
    
print(count)

Submission status: Runtime Error

Solution code:
osero = input();
len = len(osero);
con_b = 0;
con = 0;
for(i =0; i<len, i++) {
	if(osero[i]='B') {
		con_b++;
	}
	if(osero[i] ='W') {
		con = con + con_b;
	}
}
print(con)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))

count = 1;
s = [0] * int(max(A))
flag = True

for i in range(1,N):
    a = int(A[i])
    if a <= int(A[i-1]):
        for j in reversed(range(a)):
            if s[j] < count-1: 
                s[j] += 1
                flag  = False
                break;
        s[a:] = [0]*(len(s) - a)
    
        if flag:
            count += 1
            s[a-1] += 1
        flag = True
print (count)

Submission status: Runtime Error

Solution code:
#import numpy as np
s = np.array(list(input()))
#Bの場所
ind = np.array(np.where(s=='B'))
#文字列の長さ
len_ind=ind.shape[1]
#Bの個数
len=s.shape[0]
#Bの移動先
dist=np.arange(len-len_ind,len)
#答え
ans=np.sum(dist-ind)
print(ans)

Submission status: Runtime Error

Solution code:
n  =  int(input())
a  =  list(map(int, input().split()))
a.sort(reverse = True)
d  =  {}
count = 0
overlap = -1 

for x in a:
    if x not in d: d[x] = 0
    d[x] += 1
    
for i in range(len(a)):
    if a[i] == overlap:continue
        
    y = 2**a[i].bit_length() - a[i]
    
    if y == a[i]: 
        count += d[y] // 2
        overlap = y
        continue 
            
    if y in d and d[y] > 0:
        count += 1
        d[y]  -= 1
        
print(count)

Submission status: Runtime Error

Solution code:
A = input()

def bw(bwlist):
    if len(bwlist) == 1:
        return 1
    if bwlist[-2] is not bwlist[-1]:
        return 1 + bw(bwlist[0:-1])
    else:
        return bw(bwlist[0:-1])
print(bw(A))

Submission status: Runtime Error

Solution code:
A = input()

def bw(bwlist):
    if len(bwlist) == 1:
        return 1
    if bwlist[-2] is not bwlist[-1]:
        return 1 + bw(bwlist[0:-1])
    else:
        return bw(bwlist[0:-1])

bw(A)

Submission status: Runtime Error

Solution code:
import numpy as np
S = input()
b_num = S.count('B')
w_num = len(S) - b_num
end = 2**(w_num + 1) - 1
start = 0
for i in range(len(S)):
  if S[i] = 'W':
    start += np.power(2, i)
print(np.log2(start / end))

Submission status: Runtime Error

Solution code:
s = input()

def func(num, lst):
  if num == 0:
    return len(lst[num])
  else:
    return func(num - 1, lst) + len(lst[num])

s = s.lstrip("W")
s = s.rstrip("B")
if 'W' in s:
  lst = s.split('W')
  out = 0
  for i in range(0, len(lst)):
    if not lst[i] == '':
      out += func(i, lst) 
  print(out)
else:
  print(0)

Submission status: Runtime Error

Solution code:
s = input()

def func(num, lst):
  if num == 0:
    return len(lst[num])
  else:
    return func(num - 1, lst) + len(lst[num])

lst = s.split('W')
out = 0
for i in range(0, len(lst)):
  if not lst[i] == '':
  	out += func(i, lst)
  
print(out)

Submission status: Runtime Error

Solution code:
s = input()
ans = 0
while b:
  b = 0
  c = s.count("BW")
  if c:
    ans += c
    b = 1
    s = s.replace("BW", "WB")
print(ans)

Submission status: Runtime Error

Solution code:
s,t,a=input(),0,0
for c in s:
  t+=1 if c is 'B' else a+=t
print(a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = input()
 
arr = [0 if s == "W" else 1 for s in S]
sorted_arr = sorted(arr)
 
state = [arr[i] * i for i in range(len(S))]
final_state = [sorted_arr[i] * i for i in range(len(S))]
 
print(final_state - state)
S = input()

arr = [0 if s == "W" else 1 for s in S]

state = sum([arr[i] * i for i in range(len(S))])
final_state = sum([sorted(arr)[i] * i for i in range(len(S))])

print(final_state - state)

Submission status: Runtime Error

Solution code:
S = input()
 
arr = [0 if s == "W" else 1 for s in S]
 
state = [arr[i] * i for i in range(len(S))]
final_state = [sorted(arr)[i] * i for i in range(len(S))]
 
print(final_state - state)
S = input()

arr = [0 if s == "W" else 1 for s in S]

state = sum([arr[i] * i for i in range(len(S))])
final_state = sum([sorted(arr)[i] * i for i in range(len(S))])

print(final_state - state)

Submission status: Runtime Error

Solution code:
S = input()

arr = [0 if s == "W" else 1 for s in S]

state = [arr[i] * i for i in range(len(S))]
final_state = [sorted(arr)[i] * i for i in range(len(S))]

print(final_state - state)

Submission status: Runtime Error

Solution code:
S = [1 if(_ =="B") else 0 for _ in input()]
ans = 0
first = S.index(1)
count = first
start = first
for s in S[start:]:
    if(s == 0):
        ans += count -first
        first += 1
    count += 1
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
num = 0
while(True):
    num += S.count("BW")
    S = S.replace("BW", "WB")
    print(S)
    if not "BW" in S:
        break
print(num)

Submission status: Runtime Error

Solution code:
S = input()

count = 0
for i in range(len(S)):
    if S[i] == 'W':
        continue
    else:
        break
S = S[i:]

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

Submission status: Runtime Error

Solution code:
S=input()[::-1]
ans,tmp=0,0
for i in S:
  if i=='W'
    tmp+=1
  else:
    ans+=tmp
print(ans)

Submission status: Runtime Error

Solution code:
S=list(input())
a=0
for i in range(len(S):
  if S[i]=='W':
    a+=L[:i].count('B')
print(a)

Submission status: Runtime Error

Solution code:
N = input()
cnt1 = 0
for x in N:
  if x == 'B':
    cnt1 += 1
N = N[::-1]
ans =  0
cnt2 = 0
for i in range(N):
  if N[i] == 'B':
    ans += i - cnt2
    cnt2 += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
S=input()
l=len(S)
w_idx=0
cnt=0
for i in range(n):
    if S[l]=='W':
        cnt+=i-w_idx
        w_idx+=1
print(cnt)

Submission status: Runtime Error

Solution code:
S = list(input())
s = sorted(S)[::-1]
B = []
W = []

for i in range(len(S)):
    if S[i] == 'B':
        B.append(i)
    else:
        W.append(i)

W = W[::-1]
#print(B,W)
cnt = 0
while True:
    if B == [] :
        break
    tmpB = B.pop(0)
    tmpW = W.pop(0)
    if tmpB >= tmpW:
        break
    cnt += tmpW - tmpB
    W.append(tmpB)
print(cnt)

Submission status: Runtime Error

Solution code:
input_s = input()
s_lst = [1 if s == 'B' else 0 for s in input_s]

s_lst = np.array(s_lst)
count = 0

for i in range(len(s_lst)-1 , 0, -1):
    for j in range(i):
        if (s_lst[j] == 1) and (s_lst[j+1] == 0):
            s_lst[j] = 0
            s_lst[j+1] = 1
            count += 1
            
s = s_lst.tolist()
s = ['B' if s == 1 else 'W' for s in s_lst.tolist()]
s = ''.join(s)
print(count)

Submission status: Runtime Error

Solution code:
import sys

def main(l):
    u = set()
    l = list(sorted(l))
    for i, a in enumerate(reversed(l)):
        searching = (1 << a.bit_length()) - a
        for j, b in enumerate(l[:-i-1]):
            if b == searching and not j in u:
                u.add(j)
                break

    return len(u)


if __name__ == '__main__':
    input()
    main(map(int, input()))

Submission status: Runtime Error

Solution code:
import sys

nbs = list(sorted(map(int, list(sys.stdin)[1].strip().split())))
used = set()
count = 0

for k in reversed(range(1, 31)):
    p = 1 << k
    j = len(nbs) - 1
    for i, a in enumerate(nbs):
        if i not in used:
            while j > i and (nbs[j] > p - a or j in used):
                j -= 1
            if j <= i:
                break
            if nbs[j] == p - a:
                used.add(i)
                used.add(j)
                count += 1

print(count)

Submission status: Runtime Error

Solution code:
S = input()
L = []
for i in S[::-1]:
    if i == "W":
        L.append(0)
    if i == "B":
        L[-1] += len(L) 

print(sum(L))

Submission status: Runtime Error

Solution code:
S = input()
count_B = 0
right_W = 0
for i in S:
  if i == "B":
    count_B += 1
  else:
    right_W = i
rb = len(S) - right_W - 1
print(count_B - rb)

Submission status: Runtime Error

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
A=input().split()
for i in range(N):
    A[i]=int(A[i])
sorted(A)#Aを昇順にソート
for i in (reversed(beki)):
    inverseA=[]
    for j in range(len(A)):
        inverseA.append(i-A[j])#inverseA
    for j in range(len(A)):
        if inverseA[j] in A:
            A.remove(inverseA[j])
print((N-len(A))//2)

Submission status: Runtime Error

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
for i in range(N):
    A[i]=int(A[i])
sorted(A)#Aを昇順にソート
for i in (reversed(beki)):
    inverseA=[]
    for j in range(len(A)):
        inverseA.append(i-A[j])#inverseA
    for j in range(len(A)):
        if inverseA[j] in A:
            A.remove(inverseA[j])
print((N-len(A))%2)

Submission status: Runtime Error

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
N=int(input())
for i in range(N):
    A[i]=int(A[i])
sorted(A)#Aを昇順にソート
for i in (reversed(beki)):
    inverseA=[]
    for j in range(len(A)):
        inverseA.append(i-A[j])#inverseA
    for j in range(len(A)):
        A.remove(inverseA[j])
print((N-len(A))%2)

Submission status: Runtime Error

Solution code:
#AGC030 B - Powers of two
beki=[]
for i in range(1,32):
    beki.append(2**i)#bekiは２のべき乗の配列
def tasitekesu(data,n):#dataの中で足したらnになるやつを消し、消した結果とペア数を報告する
    result=[]
    kosu=0
    for i in range(len(data)):
        if i in result:
            continue
        for j in range(i):
            if j in result:
                continue
            if data[i]+data[j]==n:
                result.append(i)
                result.append(j)
                kosu=kosu+1
                break
    result.sort(reverse=True)
    for i in result:
        del data[i]
    return [data,kosu]

N=int(input())
A=input().split()
ans=0
for i in range(N):
    A[i]=int(A[i])
for i in (reversed(beki)):
    R=tasitekesu(A,i)
    A=R[0]
    ans=ans+R[1]
print(ans)

Submission status: Runtime Error

Solution code:
s=list(input().replace(""," ").split())
cnt=0
while exchange!=0:
  exchange=0
  for i in s:
    if i=="B" and i+1=="W":
      cnt+=1
      s[i],s[i+1]=s[i],s[i+1]
      exchange+=1
print(cnt)

Submission status: Runtime Error

Solution code:
s = input('')
bc = 0
c = 0

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

Submission status: Runtime Error

Solution code:
s = input() + "lllllll"
n = len(s)
tt = 0
ts = 0
for i in range(n-3):
    #print(s[i])
    if s[i] == "B" and s[i+1] == "W":
        #print("Hello")
        tt+=1
        #if s[i+2] == "B" and s[i+3] == "W":tt+=1
        for j in range(1,n-i,2):
            if s[i+j+1] == "B" and s[i+j+2] == "W":tt+=1
        for k in range(1,i+1):
            #print(k,i)
            if i - k >= 0 and s[i-k] == "B":tt+=1
            else:break
        for j in range(1,n-i):
            #print(j,i)
            if i + j <= n-2 and s[i+j+1] == "W":tt+=1
            else:break
        i = i+j+1
print(tt)

Submission status: Runtime Error

Solution code:
st = input()

def find_bw(s):
  k = 0
  temp = False
  while True:
    if s[k] == "B":
      while True:
        if s[k] == "W":
          temp = True
          break
        k+= 1
    if temp:
      break
    k+=1
    if k>len(s):
      break
  return k

print(find_bw(st))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

"""
https://atcoder.jp/contests/agc029/tasks/agc029_a
"""

import sys

def my_rep(old, new, st):
    st2 = st.replace(old, new, 1)

    if st2 == st:
        return 0

    return my_rep(old, new, st2) + 1



s = sys.stdin.readline().rstrip()
print(my_rep('BW','WB',s))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

"""
https://atcoder.jp/contests/agc029/tasks/agc029_a
"""

import sys
import re



def my_sub(_re,_rep,st):
    st2 = _re.sub(_rep, st, 1)

    if st2 == st:
        return 0

    return my_sub(_re, _rep, st2) + 1



s = sys.stdin.readline().rstrip()
print(my_sub(re.compile('BW'),'WB',s))

Submission status: Runtime Error

Solution code:
# coding: utf-8
def calc(state, print_state):
  times = 0
  while "BW" in print_state:
      for i in range(len(state)):
          if state[i] == "B":
              if i < len(state)-1 and state[i+1] == "W":
                  state[i] = "W"
                  state[i+1] = "B"
                  times += 1
                  print_state = "".join(state)            
  print(times)
    
if __name__ == "__main__":
  start = time.time()
  state = list(input())
  print_state = "".join(state)
  calc(state, print_state)

Submission status: Runtime Error

Solution code:
S = list(input())
S = [tmp == 'B' for tmp in S]
tmp = len(S)
def count(S,ctr,i):
  # print(i)
  if(i+1>=tmp):
    return ctr
  elif(i<0):
    ctr = count(S,ctr,i+1)
  elif(S[i]==True and S[i+1]==False):
    S[i]=False
    S[i+1]=True
    ctr = count(S,ctr+1,i-1)
  else:
    ctr = count(S,ctr,i+1)
  return ctr

print(count(S,0,0))

Submission status: Runtime Error

Solution code:
S = input()
count = 0
while(i!=0):
  for i in range(len(S)-1, -1, -1):
    if(S[i] == "W" and S[i-1] == "B"):
      S[i] = "B"
      S[i-1] = "W"
      count += 1
      break

print(count)

Submission status: Runtime Error

Solution code:
a = str(input())
aList = list(a)
isW = False
count = 0;
for i in (reversed(aList)):
  if i == B:
    if isW == True:
      count = count+1
    else:
      isW = False
  if i == W:
    isW = True

Submission status: Runtime Error

Solution code:
from bisect import bisect_right
import math

n=int(input())
lst=list(map(int,input().split()))
lst.sort()
use=[False]*n
ans=0
for i in range(n-1,0,-1):
  if use[i]: continue 
  x=lst[i]
  diff=2**x.bit_length()-x
  j=bisect_right(lst,diff,0,i)
  if j!=0 and lst[j-1]==diff and not use[j-1]:
    ans+=1
    use[j-1]=True
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left
import math

n=int(input())
lst=list(map(int,input().split()))
lst.sort()
use=[False]*n
ans=0
for i in range(n-1,0,-1):
  if use[i]: continue 
  x=lst[i]
  diff=2**x.bit_length()-x
  j=bisect_right(lst,diff,0,i)
  if j!=0 and lst[j-1]==diff and not use[j-1]:
    ans+=1
    use[j-1]=True
print(ans)

Submission status: Runtime Error

Solution code:
list1=list(input())
list1.reverse()
count=0
for i in range(len(list1)):
    if list1[i]=="W":
        a=i
        break
for i in range(a+1,len(list1)):
    if list1[i]=="B":
        count+=i-a
        a+=1
print(count)

Submission status: Runtime Error

Solution code:
# _*_ coding:utf-8 _*_
#  Atcoder_Grand_Contest029-B
#  https://agc029.contest.atcoder.jp/tasks/agc029_b
from collections import Counter

def  maxCountPair(n_ballCount,a_ballDatas):
	a_ballDatas.sort(reverse=True)
	ballDictBox = Counter(a_ballDatas)
	ballCountRange = range(0,n_ballCount)
	pairCount = 0
	for i in ballCountRange:
		getBallNumber = a_ballDatas[i]
		getBallNumberBin = getBallNumber.bit_length()
		nextTwoPower = 2**getBallNumberBin
		diffNumber = nextTwoPower - getBallNumber
		if 1 <= ballDictBox[diffNumber] :
			if getBallNumber != diffNumber:
				pairCount = pairCount + 1
				ballDictBox[getBallNumber]=ballDictBox[getBallNumber]-1
				ballDictBox[diffNumber] = ballDictBox[diffNumber]-1
			else:
				if 2 <= ballDictBox[diffNumber]:
					pairCount = pairCount + 1
					ballDictBox[diffNumber]=ballDictBox[diffNumber]-2
	answer = pairCount
	return answer


if __name__ == '__main__':
	n_ballCount=int(input())
	a_ballDatas=list((map(int,input().split(" "))))
	solution=maxCountPair(n_ballCount,a_ballDatas)
	print(solution)

Submission status: Runtime Error

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
two_list = [2 ** i for i in range(32)]
 
count = 0
items = {}
for a in A_list:
    if a in items:
        items[a] += 1
 
    else:
        items[a] = 1
A_list.sort(reverse=True)
 
for a in A_list:
    if items[a] >= 1:
 
        for t in two_list:
            m = t -a
            if m in items:
                if m == a and items[a] >= 2:
                    count += 1
                    items[a] -= 1
                    items[m] -= 1
                    break
 
                elif m != a and items[m] >= 1 and items[a] >=1:
                    count += 1
                    items[a] -= 1
                    items[m] -= 1
                    break
print(count)

Submission status: Runtime Error

Solution code:
import collections
N=int(input())
x=list(map(int,input().split()))
c=collections.Counter(x)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
x.sort()
for i in range(len(x))[::-1]:
    for j in range(len(t)):
        if t[j] < x[i]:
            break
        if t[j]-x[i] in c.keys():
            if t[j]-x[i] == x[i] and c[x[i]] >= 2:
                c[x[i]] -= 2
                count +=1
            elif c[t[j]-x[i]]>0 and c[x[i]]>0:
                c[t[j]-x[i]] -= 1
                c[x[i]] -= 1
                count+=1
print(count)

Submission status: Runtime Error

Solution code:
import collections
N=int(input())
x=list(map(int,input().split()))
c=collections.Counter(x)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
x.sort()
for i in range(len(x))[::-1]:
    for j in range(start,len(t)):
        if t[j] > 2*x[i]:
            start = j
        if t[j] < x[i]:
            break
        if t[j]-x[i] in c.keys():
            if t[j]-x[i] == x[i]:
                if c[x[i]] >= 2:
                    c[x[i]] -= 2
                    count +=1
            else:
                if c[t[j]-x[i]]>0 and c[x[i]]>0:
                    c[t[j]-x[i]] -= 1
                    c[x[i]] -= 1
                    count+=1
print(count)

Submission status: Runtime Error

Solution code:
import collections
N=int(input())
x=list(map(int,input().split()))
c=collections.Counter(x)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
#print(c)
#print("x=",x)
#print("t=",t)
for i in range(len(x))[::-1]:
    if x[i] != 0:
        for j in range(0,len(t)):
            if t[j] <= x[i]:
                break
            if t[j]-x[i] in c:
                if t[j]-x[i] == x[i]:
                    if c.get(x[i],-1) >= 2:
                        c[x[i]] -= 2
                        count +=1
                        break
                else:
                    if c.get(t[j]-x[i],-1)>0 and c.get(x[i],-1)>0:
                        c[t[j]-x[i]] -= 1
                        c[x[i]] -= 1
                        count+=1
                        break
#print(c)    
print(count)

Submission status: Runtime Error

Solution code:
l=input()
c=0

while("BW" in l):
  c += l.count("BW")
  l=l.replace("BW","WB")
  print(l)

print(c)

Submission status: Runtime Error

Solution code:
import collections
 
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
c=collections.Counter(a)
ans=0
for i in a:
  for j in range(31):
    if(2**j>i):
      k=2**j-i
      break
  if k in c.keys():
    if i==k and c[i]>1:
      c[i]-=2
      ans+=1
    elif i!=k and c[i]>0 and c[k]>0:
      c[i]-=1
      c[k]-=1
      ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

n = int(input())
nums = sorted(list(map(int, input().split())), reverse=True)

num_map = Counter(nums)

answer = 0
for num in nums:
    pair = (1 << (num.bit_length())) - num

    if num_map[num] > 0 and num_map[pair]:
        if num == pair and num_map[num] > 1:
            answer += 1
            num_map[num] -= 2
        else:
            answer += 1
            num_map[num] -= 1
            num_map[pair] -= 1

print(answer)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
Ae1=[]
Ao1=[]
for i in range(N):
    if A[i]%2==0:
        Ae1.append(A[i])
    else:
        Ao1.append(A[i])
if Ao1!=[]:
    s=0
    Ao=sorted(Ao1,reverse=True)
    i=0
    while i<=len(Ao)-1:
        a=int(math.log2(Ao[i]))
        if 2**(a+1)-Ao[i] in Ao[i:]:
            s+=1
            Ao.remove(2**(a+1)-Ao[i])
            i+=1
        else:
            i+=1
else:
    s=0
if Ae1!=[]:
    t=0
    Ae=sorted(Ae1,reverse=True)
    j=0
    while j<=len(Ae):
        b=int(math.log2(Ae[j]))
        if 2**(a+1)-Ae[j] in Ae[j:]:
            s+=1
            Ae.remove(2**(a+1)-Ae[j])
            j+=1
        else:
            j+=1
else:
    t=0
print(s+t)

Submission status: Runtime Error

Solution code:
import bisect

def main():
    _ = int(input())
    nums = list(map(int, input().split()))
    n = len(nums)
    nums.sort()
    used = [False] * n
    
    cnt = 0
    #print(num_bin_digits)

    for cur in reversed(range(n)):
        continue
        if used[cur]:
            continue
        num = nums[cur]
        power_of_2 = 1 << len("{:b}".format(num))

        pair = power_of_2 - num

        idx = bisect.bisect_left(nums, pair)
        if idx >= cur or nums[idx] != pair:
            #print('continue',pair, idx)
            continue

        while used[idx]:
            idx += 1

        if nums[idx] == pair:
            used[idx] = True
            cnt += 1

        #print(idx, num, power_of_2, pair)
        #print(cnt, nums, used)

    print(cnt)

    return

main()

Submission status: Runtime Error

Solution code:
osero = input()
count = 0

def exchange(osero):
    global count
    index =  osero.find('BW')
    if index >= 0:
        osero = osero.replace('BW','WB',1)
        count += 1
        exchange(osero)
    return count

count = exchange(osero)
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
b = 0
c = []
for i in a:
  if i > b:
    for j in range(i-b):
      c.append(0)
  else:
    c[i-1] += 1
    c = c[:i]
    if c[i-1] >= ans:
      ans += 1
  b = i    
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

A.sort()
m = 1 << 30
used = [False] * n
ans = 0

def search(ok, ng, n):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if A[mid] < n or (A[mid] == n and used[mid]):
        return search(mid, ng, n)
    else:
        return search(ok, mid, n)

for i in range(30):
    for j in range(n):
        if used[j] or A[j] >= m:
            continue
        a = search(-1, n, m - A[j]) + 1
        if a >= n:
            continue
        if A[a] == m - A[j]:
            if a == j:
                a += 1
                if a >= n or not A[a] == A[j]:
                    continue
            used[a] = True
            used[j] = True
            ans += 1
    m >>= 1

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
a=  stdin.readline().strip()
a= list(a)
a.reverse()
ans= 0
cur =0
for i in a:
 if i=='W':
  cur+=1
 else:
  ans+= 1
print ansa

Submission status: Runtime Error

Solution code:
letters = input()
L=len(letters)
flag = 0
j=0
while j<L :
	i=0
	while i<L :
		if letters[i]=="B" and letters[i+1]=="w" :
			letters.replace(letters[i],letters[i+1])
		i=i+1
		flag = flag + 1
	j=j+1
print(flag)

Submission status: Runtime Error

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

def a(i,count):
    # print("a",count)
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        count += 1
        if(i>0):
            if s[i - 1] == "B" and s[i] == "W":
                # print("b",count)
                return a(i-1,count)
            else:
                return count
        else:
            return count
    else:
        return count


# [a(i) for i in range(n)]
for i in range(n):
    tmp = count
    count = a(i, tmp)

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())

X = [[] for i in range(N)]
P = [-1] * N

for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)


def set_parent(i):
    # print(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            # print(a)
            X[a].remove(i)
            set_parent(a)
    
set_parent(0)

B = [-1] * N
def set_block(i):
    for a in X[i]:
        B[a] = max(i, B[i])
        set_block(a)

B_id = [-1] * N
B_id_cnt = [0] * N
def BID(i):
    for a in X[i]:
        if a > B[i]:
            B_id[a] = a
        else:
            B_id[a] = B_id[i]
            
        # print(a, B_id)
        B_id_cnt[B_id[a]] += 1
        BID(a)
        
Ans = [1] * N
def flow(i):
    for a in X[i]:
        if B_id[a] == B_id[i]:
            Ans[a] = Ans[i]
        else:
            Ans[a] = Ans[i] + B_id_cnt[a]
        flow(a)

set_block(0)


# print("X=", X) # 子リスト
# print("P=", P) # 親
# print("B=", B) # Block
# print(B_id) # BlockのID
# print(B_id_cnt) # BlockのID
# print(Ans)
print(" ".join(map(str, Ans[1:])))

Submission status: Runtime Error

Solution code:
N = int(input().rstrip())
A = [int(s) for s in input().rstrip().split()]
A.sort()

squared = [2]
tmp = 2
while tmp < 2 * 10**9:
    tmp *= 2
    squared.append(tmp)

pairs = []
for i in range(N):
    for j in range(i+1, N):
        if A[i] + A[j] in squared:
            pairs.append([A[i], A[j]])

adopt = [format(i, 'b').rjust(len(pairs), '0') for i in range(2**len(pairs))]
max_pair = 0

for i in range(len(adopt)):
    num = []
    for j in range(len(pairs)):
        if adopt[i][j] == '1':
            num.append(pairs[j][0])
            num.append(pairs[j][1])
    if len(num) == len(set(num)) and len(num) > max_pair:
        max_pair = len(num)

print(max_pair // 2)

Submission status: Runtime Error

Solution code:
N, As = int(input()), list(map(int, input().split()))
maxA, ans = max(As), 1
while True:
    if pow(ans, maxA) - 1 > N:
        break
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
letters = input()
L=len(letters)
flag = 0
j=0
while(j<L)
	i=0
	while(i<L)
		if letters[i]=="s" and letters[i+1]=="w" :
			letters.replace(letters[i],letters[i+1])
		i=i+1
		flag = flag + 1
	j=j+1
print(flag)

Submission status: Runtime Error

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

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

pairCounter=0
for i in range(N):
  if A[i] < 0:
    continue
  for b in range(9):
    pair=2**b - A[i]
    if pair < 1:
      continue
    #print(pair)
    if A.count(pair) >0:
      A[i] = -1
      A[A.index(pair)] = -1
      pairCounter+=1
      break

print(pairCounter)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)-1
count = 0
counts = []
def a(i):
    # global count
    if s[i] == "B" and s[i + 1] == "W":
        s[i] = "W"
        s[i + 1] = "B"
        # count += 1
        counts.append(1)
        if(i>0):
            if s[i - 1] == "B" and s[i] == "W":
                a(i-1)
        # if(i>0) and s[i-1] == "B" and s[i] == "W":
        #     a(i-1)

# [a(i) for i in range(n)]
for i in range(n):
    a(i)

print(len(counts))

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
A.sort()
s=0
i=1
while i<=len(A)-1:
    a=int(math.log2(A[-i]))
    if 2**(a+1)-A[-i] in A[:-i]:
        s+=1
        A.remove(2**(a+1)-A[-i])
        i+=1
    else:
        i+=1
print(s)

Submission status: Runtime Error

Solution code:
from sys import stdin
N=(stdin.readline().rstrip())
ans=0
for i in range(10000):
    index=N.find("W")
    if index==-1:
        break
    ans=ans+index
    N=str_del(N,index)
print(ans)

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):
            if s[i - 1] == "B" and s[i] == "W":
                a(i-1)
        # if(i>0) and s[i-1] == "B" and s[i] == "W":
        #     a(i-1)

[a(i) for i in range(n)]
# 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) and s[i-1] == "B" and s[i] == "W":
            a(i-1)

[a(i) for i in range(n)]
# for i in range(n):
#     a(i)

print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter as C
n = int(input())
l = sorted(map(int, input().split()), reverse = True)
c = C(l)
res = 0
for e in l:
    for p in range(32, 0, -1):
        e_ = (1 << p) - e
        if c.get(e_, 0) > 0:
            # print(e, e_, c)
            c[e_] -= 1
            c[e] -= 1
            res += 1
            break
print(res)

Submission status: Runtime Error

Solution code:
from sys import stdin

S = stdin.readline().rstrip()
#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:
N = list(input())
count = 0

def con(N, count, i):
    if i < 1:
        return count

    elif N[i-1] == "B" and N[i] == "W":
        N[i-1] = "W"
        N[i] = "B"
        count += 1
        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
            count = con(N, count,i)
    print(count)