Problem: 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.

Submission attempts (in order):
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)

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)

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)

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)

S=input()
count=0
def sigma(i):
    a=[b for b in range(0,i+1)]
    return sum(a)
place=0
flag=0
start=0
for i in range(len(S)):
    if S[i]=="B":
        flag=1
    elif S[i]=="W":
        if flag == 1:
            count += 1
            place += i - start
        else:
            start+=1
ans=place-sigma(count-1)
print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted