Problem:


Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3



Sample input:
4
WWRR


Sample output:
2

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = [s for s in input()]
    ans = 0
    right = N-1
    for left in range(N):
        if S[left] == "W" and S[right] == "R":
            S[left], S[right] = S[right], S[left]
            ans += 1
        while left < right and S[left] == "W" and S[right] != "R":
            right -= 1

        if left >= right:
            break

    add = 0
    T = S[:]
    for i in range(N-1):
        if S[i] == "W" and S[i+1] == "R":
            S[i+1] = "W"
            add += 1
    add2 = 0
    for i in range(N-1)[::-1]:
        if T[i] == "W" and T[i+1] == "R":
            T[i] = "R"
            add2 += 1
    print(ans + min(add, add2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()

ans = 0
for v in c[: n // 2]:
    if v == "W":
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
red=s.count('r')
print(red-s[:red].count('r'))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    a = input()

    if not 'WR' in a:
        print(0)
    else:
        rightmostW = a.find('WR')
        wnum = a[rightmostW+2:].count('R')
        print(wnum)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
nw = len(s.replace('R',''))
nr = n - nw

tmp_w = 0
ret = n
w_left = 0
for i, c in enumerate(s):
    if c == 'W':
        w_left += 1
    r_right = n - 2*i - 2 +  w_left
    ret = min(ret, max(r_right, w_left))
print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
flg = 0
cnt=0
rcnt=0
wcnt=0
ans = n
for i in range(n):
    if c[i]=="W" and flg==0:
        cnt+=1
        wcnt+=1
    elif c[i]=="R":
        flg=1
        rcnt+=1
    elif c[i]=="W":
        wcnt+=1
if rcnt>cnt:
    ans=rcnt-1
if rcnt>=wcnt:
    nans=wcnt
else:
    nans=rcnt
ans = min(ans,nans)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=list(input())
ans=0
for i in range(n//2):
    if c[i]=="W" and c[-i-1]=="R":
        ans+=1
    elif c[i]==c[-i+1]=="R":
        continue
    else:
        ans+=c[i:-i-1].count("R")
        break
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
L = 0
r = len(s)

cnt = 0
while True:
    L = s.find('W')
    if L == -1:
        break
    r = s.rfind('R')
    if r == -1:
        break

    if (L > r):
        break

    s = s[L + 1:r].lstrip('R').rstrip('W')

    cnt += 1

if L != -1 and L > r:
    cnt += L + 1

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
w,r=0,0
c=0
if s[0]=='R':r+=1
else:w+=1
for i  in range(1,len(s)):
  if s[i]=='R' and s[i-1]=='W':
    c+=1
  if s[i]=='R':r+=1
  if s[i]=='W':w+=1
f=0
Z=0
for i in range(len(s)-1):
  if s[i]=='R' and s[i+1]=='W':
    Z=1
    t1,t2=i,i+1
    break
if Z==1 and set(s[0:t1+1])=={'R'} and set(s[t2:])=={'W'} :
  print(min(r,w))
  f=1
z=0
for i in range(len(s)-1):
  if s[i]=='W' and s[i+1]=='R':
    z=1
    t1,t2=i,i+1
    break
if z==1 and set(s[0:t1+1])=={'W'} and set(s[t2:])=={'R'} and f==0 and z==1:
  print(min(r,w))
  f=1
if not f:print(c)

Submission status: Wrong Answer

Solution code:
#n,d=map(int,input().split())
#xy = [list(map(int,input().split())) for i in range(n)]
n=int(input())
c=list(input())
left=0
right=0
for i in range(0,n//2+1):
  if c[i]=="W":
    left+=1
  
for i in range(n//2+1,n):
  if c[i]=="R":
    right+=1
ans1=min(left,right)
print(left+right-ans1)

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
c = list(str(input()))

left = None  # 一番左の W　を記録
right = None  # 一番右の R を記録
count = 0

for i in range(len(c)):
    if c[i] == 'W':
        left = i
    if c[len(c) - 1 - i] == 'R':
        right = len(c) - 1 - i

    if (left is not None) and (right is not None) and (left < right):
        # print(f'left{left}, right {right}')
        count += 1
        left = None
        right = None

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
c = 0

for i in range(1, len(s)):
  if s[i - 1] == 'W' and s[i] == 'R':
    c += 1
    s[i - 1] = 'R'
print(c)

Submission status: Wrong Answer

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,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
C = input()
l = 0
for i in range(n-1):
    if C[i] == 'W' and C[i+1] == 'R':
        l += 1
r = C.count('R')
# if w == 0:
#     print(0)
#     exit()
nya = 0
#print(r)
for i in range(n-1-r,-1,-1):
    if C[i] == 'W':
        nya += 1
print(max(nya,l))

Submission status: Wrong Answer

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
from collections import Counter

n = ii()
s = input()

d = Counter(s)
num = d['R']
ans = num
wcnt = 0
rcnt = 0
for i in s:
    if i == 'W':
        wcnt += 1
    else:
        rcnt += 1
        if num-rcnt > wcnt or wcnt == 0:
            ans -= 1
            num -= wcnt
            wcnt = 0
        else:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(input())
print(arr)
head = 0
tail = n-1
count = 0
while True:
    while arr[head]=='R' and head<tail:
        head+=1
    while arr[tail]=='W' and head<tail:
        tail-=1
    if arr[head]=='W' and arr[tail]=='R':
        count+=1
    head+=1
    tail-=1
    if head>=tail:
        break
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
c_l = list(input())

l = 0
r = n-1
w_n = 0
r_n = 0

while l < r:
    if c_l[l] == 'W':
        w_n += 1
    if c_l[r] == 'R':
        r_n += 1
    l += 1
    r -= 1
print(max(w_n, r_n))

Submission status: Wrong Answer

Solution code:
N=int(input())
C=list(input())
ans=0

w=0
r=0
for i in range(N//2):
    print(i,C[i])
    if C[i]=='W':
        w+=1
    if C[i-2]=='R':
        r+=1

if w==0 or r==0:
    ans=0
else:
    ans=max(w,r)

print(ans)

Submission status: Wrong Answer

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

wn = [0] * n
if s[0] == 'W':
    wn[0] = 1
for i in range(1, n):
    wn[i] = wn[i-1]
    if s[i] == 'W':
        wn[i] += 1
rn = [0] * n
if s[-1] == 'R':
    rn[-1] = 1
for i in range(n-2, -1, -1):
    rn[i] = rn[i+1]
    if s[i] == 'R':
        rn[i] += 1
rn.append(0)
min_val = n
for i in range(n):
    m = max(wn[i], rn[i+1])
    if min_val > m:
        min_val = m
print(min_val)

Submission status: Wrong Answer

Solution code:
n = int(input())
line = list(input())
if 'W' not in line or 'R' not in line:
    print(0)
else:
    a = line.index('R')
    w = line[:a].count('W')
    r = line[a+1:].count('R')
    plan1 = abs(w-r)+min(w,r)
    plan2 = min(line.count('W'),line.count('R'))
    print(min(plan1,plan2))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(input())
 
rev = 0
 
# for i in range(1, len(l)-1):
#   if l[i-1] == 'W' and l[i] == 'R':
#    l.insert(0, l.pop(i))
 
rl = l.count('R')
wl = l.count('W')
 
print(rl if rl < wl else wl)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=input()

if n%2==0:
  c1=c[:n//2]
  c2=c[n//2:]
else:
  c1=c[:n//2]
  c2=c[n//2+1:]

ans1=c.count('W')
ans2=max(c1.count('W'),c2.count('R'))

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def _run(N, colors):
    double_c = 0
    single_c = 0
    right_exists_red = False
    for i in range(0, N - 1)[::-1]:
        if i == 0:
            current = colors[i:i + 2]
            if current == "WR":
                double_c += 1
            elif right_exists_red and current == "WW":
                single_c += 1
        else:
            current = colors[i - 1:i + 2]
            if current == "RWR":
                double_c += 1
                right_exists_red = False
            elif current == "WWR":
                single_c += 1
                right_exists_red = True
            elif right_exists_red and current[1:] == "WW":
                single_c += 1
            else:

                right_exists_red = False

    print(double_c // 2 + double_c % 2 + single_c)


def _main():
    N = int(input())
    colors = str(input())
    _run(N, colors)


if __name__ == "__main__":
    _main()

Submission status: Wrong Answer

Solution code:
n=int(input(""))
lis=input("")
rt=0
for i in lis:
    if(i=="R"):
        rt+=1
ht=n-rt
r=0
h=0
s=n
for i in lis:
    if(i=="R"):
        r+=1
    else:
        h+=1
    on=rt-r
    m=max(on,h)
    if(s>m):
        s=m

print(s)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()

    s = in_s()

    red = deque()
    white = deque()

    for i in range(N):
        if s[i] == 'R':
            red.append(i)
        else:
            white.append(i)

    if len(red) == 0:
        print(1)
        exit()
    if len(white) == 0:
        print(0)
        exit()

    cnt = 0
    while True:
        if len(red) == 0 or len(white) == 0:
            break
        if white[0] > red[-1]:
            break

        white.popleft()
        red.pop()
        cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
C=np.array([x for x in input()])

if np.count_nonzero(C[:]=='W')==N:
  print(0)
elif N%2==0:
  if np.count_nonzero(C[N//2:]=='R')>0:
    print(min(np.count_nonzero(C[:N//2]=='W'),np.count_nonzero(C[N//2:]=='R')))
  else:
    print(np.count_nonzero(C[:N//2]=='W'))
else:
  if np.count_nonzero(C[N//2:]=='R')>0:
    print(min(np.count_nonzero(C[:N//2+1]=='W'),np.count_nonzero(C[N//2:]=='R')))
  else:
    print(np.count_nonzero(C[:N//2]=='W'))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from collections import  Counter
def resolve():
    N,=pin()
    C=input()
    X=Counter(C)
    a=(min(X["R"],X["W"]))#すべての色を変えにしてしまったらいい時
    c=list(C)


    rightlimit_R=-1
    for i in range(N):
        if c[-i]=="R":
            rightlimit_R=N-i
            break
    cnt=0
    for i in range(N):
        if c[i]=="W":
            for j in range(rightlimit_R-1,0,-1):
                if C[j]=="R":
                    rightlimit_R=j
                    break
            cnt+=1
        if rightlimit_R<=i:
            
            break
    print(min(a,cnt))
#%%submit!
resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()

l = 0
r = N-1
ans = 0
while(l < r):
    while(l < N and c[l] == 'R'):
        l += 1
    while(r >= 0 and c[r] == 'W'):
        r -= 1
    if l == N:
        break
        if r >= 0:
            ans += 1
            r -= 1
        else:
            break
    else:
        if r < 0:
            break
            ans += 1
            l += 1
        else:
            ans += 1
            l += 1
            r -= 1

print(ans)

Submission status: Wrong Answer

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

N=int(input().rstrip('\n'))
S=list(input().rstrip('\n'))
alltow = S.count('R')
alltor = S.count('W')
torw = 0
sta = 'R'
sto = 0
change = 0
for i in range(N):
  if sta != S[i]:
    change+=1
    sto = 1
  else:
    sto += 1
if change % 2 ==0 :
  change += sto-1
print(min(alltow,alltor,round(change/2)))

Submission status: Wrong Answer

Solution code:
n = int( input() )
c = list( input() )

l = 0
r = n - 1
ans = 0
while l < r:
    while c[ l ] == "R" and l < n - 1:
        l += 1
    while c[ r ] == "W" and r > 1:
        r -= 1
    if l < r:
        c[ l ] = "R"
        c[ r ] = "W"
        ans += 1

print( ans )

Submission status: Wrong Answer

Solution code:
n = int(input())
clist = input()

rs = clist.count("R")
ws = clist.count("W")

sort_rs = rs - clist[0:rs-1].count("R")

ans = 10**7

if rs < ans:
    ans = rs
if ws < ans:
    ans = ws
if sort_rs < ans:
    ans = sort_rs
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = input()

k = -1
for i in range(N):
    if c[i] == 'W' and k == -1:
        k = i 

if k == -1 or k == N-1:
    print(0)
else:
    w = [0] * N
    r = [0] * N

    if c[0] == 'W':
        w[0] = 1
    if c[N-1] == 'R':
        r[N-1] = 1
    for i in range(1,N):
        if c[i] == 'W':
            w[i] = w[i-1] + 1
        else:
            w[i] = w[i-1]

        if c[N-i-1] == 'R':
            r[N-i-1] = r[N-i] + 1
        else:
            r[N-i-1] = r[N-i]

    for i in range(N-1):
        if w[i] == r[i+1]:
            print(w[i])
            break

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
L = 0
r = len(s)

cnt = 0
while True:
    L = s.find('W')
    if L == -1:
        break
    r = s.rfind('R')
    if r == -1:
        break

    if (L > r):
        break

    s = s[L + 1:r].lstrip('R').rstrip('L')

    cnt += 1

if L != -1 and L > r:
    cnt += L + 1

print(cnt)

Submission status: Wrong Answer

Solution code:

N = int(input())
S = input()

ans = 0
cnt = [0, 0]
for i in range(N):
    if S[i] == "W":
        if i > 0 and S[i - 1] == "R":
            ans += min(cnt)
            cnt = [0, 0]
        cnt[0] += 1
    else:
        cnt[1] += 1
ans += min(cnt)
print(min(ans, S.count("W"), S.count("R")))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
s = rr()
if 'W' not in s:
    print(0)
else:
    print(max(s[0:n//2].count('W'), s[n//2:].count('R')))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math

# import copy
from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret


def solve():
    n = getN()
    S = getS()

    # wc = Counter(S)["W"]
    # wcnt = 0 # 左から連続するW
    # for c in S:
    #     if c == "R":
    #         break
    #     wcnt += 1
    #
    # ans1 = wc - wcnt
    # # print(ans1)
    #
    # s2 = "".join(list(reversed(list(S))))
    # print(s2)
    # rc = Counter(S)["R"]
    # rcnt = 0  # 左から連続するR
    # for c in s2:
    #     if c == "W":
    #         break
    #     rcnt += 1
    l = 0
    r = n - 1
    # print(n, S, l, r)
    ansl, ansr = 0,0
    while True:
        while True:
            if S[l] == "W":
                l += 1
                ansl += 1
                # print(ansl, l, r)
                while S[l] == "R":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    l += 1
                break
            l += 1
            if l == r:
                break
            # print(l, r?)
        if l == r:
            print(max(ansl, ansr))
            return

        while True:
            if S[r] == "R":
                r -= 1
                ansr += 1
                while S[r] == "W":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    r -= 1
                break
            r -= 1
            if r == l:
                break
            # print(l, r)
        if l == r:
            print(max(ansl, ansr))
            return
    # ans2 = rc - rcnt
    # print(min(ans1,ans2))


def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import collections

def solve():
    l = len(c)
    left  = collections.Counter(c[0:l//2])
    right = collections.Counter(c[l//2:N])
        
    if left["W"] > right["R"]:
        print(right["R"])
    else:
        print(left["W"])


if __name__ == "__main__":
    N = int(input())
    C = input()
    c = [str(l) for l in C]
#    print(c)
    solve()

Submission status: Wrong Answer

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


N=int(input())

T=input()

S=[]
Wcount=0
Rcount=0
for i in range(N):
    S.append(T[i])
    if T[i]=="W":
        Wcount+=1
    else:
        Rcount+=1

search_count=min(Wcount,Rcount)
ans=search_count



for i in range(search_count):
    if T[i]=="R":
        ans-=1
if (Wcount+Rcount)%2!=0:
    ans+=1

if Rcount==0 or Wcount== 0:
    print(0)
    exit()


print(ans)
#print(Wcount,Rcount)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=n//2
c=input()
b=c[:a].count("W")
print(b)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    a = input()

    if not 'WR' in a:
        print(0)
    else:
        rightmostW = a.rfind('RW')
        wnum = a[:rightmostW+1].count('W')
        print(wnum)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
C = input()
A = []
for c in C:
    if c=='W':
        A.append(0)
    else:
        A.append(1)
l = 0
r = N - 1
ans = 0
while l < r:
    if not A[l] and A[r]:
        ans+=1
        l +=1
        r-=1
    elif not C[l]:
        r-=1
    elif C[r]:
        l+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

c = input()

count = 0
check = 0
for i in range(n):
    if c[i] == "W":
        if check == 0:
            check = 1
            wCount = 1
        elif check == 1:
            wCount += 1
        elif check == 2:
            count += min(wCount,rCount)
            check = 1
            wCount = 1
    else:
        if check == 1:
            check = 2
            rCount = 1
        elif check == 2:
            rCount += 1
            if i == n-1:
                count += min(wCount,rCount)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
lc = input()
lc = lc.lstrip("R").rstrip("W")

ans = 0
lin = 0
rin = len(lc)-1
while lin < rin:
    while True:
        if lc[lin] == "W":
            break
        lin += 1
    while True:
        if lc[rin] == "R":
            break
        rin -= 1
    lin += 1
    rin -= 1
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = list(input())
print(c)
red=0
for i in c:
    if i=="R":
        red+=1
count = 0
for i in range(red):
    if c[i]=="W":
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
c=input()
count=0
for i in range(N//2):
    if c[i]=="W":
        count+=1
if c.count("R")==0 or c.count("W")==0:
    print(0)
else:
    print(min(N//2,count))

Submission status: Wrong Answer

Solution code:
input()
s = input()
pref = 0
total = s.count('R')
res = int(1e9)
for i in range(total):
    pref += s[i] == 'R'
    res = min(res, total - pref)
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
ans = 0
i = 0
while True:
    r = 0
    w = 0
    if s[i] == 'W':
        w += 1
    else:
        r += 1
    i += 1
    while i < n and not (s[i] == 'W' and s[i - 1] == 'R'):
        if s[i] == 'W':
            w += 1
        else:
            r += 1
        i += 1
    ans += min(r, w)
    if i == n:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
C = list(input())
c = 0
pi = 0
pj = n-1
while True:
    for i in range(pi, n):
        if C[i] == 'W':
            break
    else:
        break
    for j in range(pj, 0, -1):
        if C[j] == 'R':
            break
    if i < j:
        C[i] = 'R'
        C[j] = 'W'
        c += 1
    else:
        break
    pi = i
    pj = j
print(c)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N = int(input())
    c = input()
    countlist = []
    count = 0
    for i in c:
        if i == 'R':
            count += 1
            countlist.append(count)
        else:
            countlist.append(count)
    ans = 10**6
    for i in range(N):
        lnum = (i+1) - countlist[i]
        rnum = N - (i+1) - countlist[i]
        difnum = min(lnum, rnum)
        lnum -= difnum
        rnum -= difnum
        tmp = difnum
        tmp += lnum + rnum
        if tmp < ans:
            ans = tmp
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
c = list(input())
ans = 0
if c.count('W') == n or c.count('W') == 0:
    ans =0 
else:
    if n % 2 == 0:
        a = n //2
        c_for = c[:a]
        c_back = c[a:]
        ans = max(c_for.count('W'),c_back.count('R'))
        
    else:
        a = n//2
        a = n //2
        c_for = c[:a]
        c_back = c[a+1:]
        ans = max(c_for.count('W'),c_back.count('R'))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = list(input())

ans = 0
x = 0
for i in range(n):
    if c[i] == "W":
        if i >= 1 and c[i-1] == "R":
            x = 0
        x += 1
    elif c[i] == "R":
        if x >= 1:
            ans += 1
            x -= 1
        else:
            pass
        
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

s=list(input())

count_w = 0

w_list = []

r_list = []
for i in range(n):
    if s[i] == 'W':
        count_w += 1
        w_list.append(i)
    else:
        r_list.append(i)
w_list.reverse()

chage_count = 0
while len(w_list) > 0 and len(r_list) > 0 and r_list[-1] > w_list[-1]:
    r_list.pop()
    w_list.pop()
    chage_count += 1

if len(w_list) > 0 and len(r_list) == 0:
    chage_count += len(w_list)

print(min(chage_count, count_w))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = input()
arr = []
for i in range(N):
    arr.append(l[i])

c = 0
left = 0
right = N-1

T = False
for i in arr:
    if i == 'R':
        T = True
if T == False:
    print(0)
else:
    T = False
    for i in arr:
        if i == 'W':
            T = True
    if T == False:
        print(0)
    else:
        while True:
            while True:
                if arr[left] == 'R':
                    left += 1
                else:
                    break
            while True:
                if arr[right] == 'W':
                    right -= 1
                else:
                    break
            arr[left] = 'R'
            arr[right] = 'W'
            if left >= right:
                break
            else:
                c += 1

Submission status: Wrong Answer

Solution code:

N = int(input())
S = input()

ans = 0
cnt = [0, 0]
for i in range(N):
    if S[i] == "W":
        if i > 0 and S[i - 1] == "R":
            ans += min(cnt)
            cnt = [0, 0]
        cnt[0] += 1
    else:
        cnt[1] += 1
ans += min(cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
l,r=0,n-1
ans=0
while l<r:
  cnt=0
  while r>=0 and s[r]=='W':r-=1
  while l<n and s[l]=='R':l+=1
  if l>=r:break
  while s[r]=='R':
    cnt+=1
    r-=1
  cm=cnt
  while cnt:
    if s[l]=='W':cnt-=1
    l+=1
    if l>=r:break
  if cnt>0:ans+=cnt
  else:ans+=abs(cm-cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = list(input())

if c.count('W') == 0:
	print(0)
else:
	if len(c)%2 == 0:
		print(min(c[0:len(c)//2].count('W'), c[len(c)//2:].count('R')))
	else:
		print(min(c[0:len(c)//2].count('W'), c[len(c)//2+1:].count('R')))

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
L = 0
r = len(s)

cnt = 0
while True:
    L = s.find('W')
    if L == -1:
        break
    r = s.rfind('R')
    if r == -1:
        break

    s = s[L + 1:r]

    cnt += 1

if L != -1:
    cnt += L + 1

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
line = list(input())
if 'W' not in line or 'R' not in line:
    print(0)
else:
    a = line.index('R')
    w = line[:a].count('W')
    r = line[a+1:].count('R')
    print(abs(w-r)+min(w,r))

Submission status: Wrong Answer

Solution code:
import math

#import sympy

#n,d=map(int,input().split())
n=int(input())
c=input()
rnum=c[0:int(len(c)/2)].count("W")
wnum=c[int(len(c)/2):-1].count("R")
lennum=int(len(c)/2)
print(max(rnum,wnum))

Submission status: Wrong Answer

Solution code:
import sys
import heapq

def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    C = list(input())

    Wcount=0
    Rcount =0
    rr =C.count("R")
    ww =C.count("W")


    for j in range(ww):
        if C[j]=="W":
            Rcount+=1

    for i in range(rr):
        if C[-1-i]=="R":
            Wcount+=1

    print(min(Rcount,Wcount))






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
cur = [0, 0]
pref = []
for i in range(n):
	if s[i] == 'W':
		cur[0] += 1
	else:
		cur[1] += 1
	pref += [cur[:]]

# print(pref)

res = pref[-1][0]
for i in range(1, n):
	# print(i, res)
	rem = i - pref[i-1][1]
	ans = rem
	# print('a', ans, rem)

	rightred = pref[-1][1] - pref[i-1][1]

	if rightred >= rem:
		ans += rightred-rem
	else:
		ans += rem-rightred

	res = min(res, ans)

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = input()
    R = S[::-1]
    cnt = 0
    jdex=0
    for i in range(N):
        if S[i]== 'W':
            for j in range(jdex,N):
                if S[j]=='R':
                    cnt+=1
                    jdex=j+1
                    break
            if j+i+1>=N:
                    print(cnt)
                    return
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
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 [LIST() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]


INF = float("inf")
MOD = 10**9 + 7


def main():
    N = INT()
    C = input()

    count = 0
    head = 0
    tail = N-1
    while head < tail:
        while head < N and C[head] == "R":
            head += 1
        while 0 < tail and C[tail] == "W":
            tail -= 1

        if head > tail:
            break
        count += 1
        head += 1
        tail -= 1
    print(count)

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
c=list(input())
lw_cnt=0
rr_cnt=0
flg=-1
for i in range(n):
  if flg<0 and c[i]=='W':
    lw_cnt+=1
  elif flg<0 and c[i]=='R':
    flg=i
  elif flg>=0 and c[i]=='R':
    rr_cnt+=1
  else:
    rw_cnt=1
if lw_cnt==0 or rr_cnt==0:
  print(0)
else:
  print(max(lw_cnt,rr_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = str(input())
wnum = 0
for i in range(N):
  if S[i] == "W":
    wnum += 1
right = 0
Flag = False
wright = 0
for i in range(N):
  #print(i,Flag,S[-1-i])
  if S[-1-i] == "R" and not Flag:
    right += 1
  elif S[-1-i] == "W":
      wright += 1
      Flag = True
  else:
    break


#print(wnum,right,wright)

if right > wnum-wright:
  ans = wnum
else:
  ans = wnum-wright
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=input()
rcnt=[0]*(n+1)
wcnt=[0]*(n+1)
for i in range(n):
    if c[i]=="W":
        wcnt[i+1]=wcnt[i]+1
        rcnt[i+1]=rcnt[i]
    else:
        rcnt[i+1]=rcnt[i]+1
        wcnt[i+1]=wcnt[i]
ans=n+100
for i in range(n):
    cw=wcnt[i+1]
    cr=rcnt[-1]-rcnt[i+1]
    ans=min(ans,cw+cr-min(cr,cw))
ans=min(ans,wcnt[-1])
print(ans)

Submission status: Wrong Answer

Solution code:
widx = []
ridx = []
s = input()
for i, char in enumerate(s):
  if char == "W":
    widx.append(i)
  else:
    ridx.append(i)
count = 0
i = 0
j = len(ridx) - 1
while i < len(widx) and j >= 0 and widx[i] < ridx[j]:
  i += 1
  j -= 1
  count += 1
print(count)

Submission status: Wrong Answer

Solution code:
k = int(input())
s = input()

res = 0
j = k - 1
for i in range(k//2):
    if s[i] == 'W' and s[j] == 'R':
        res += 1
        j -= 1
    elif s[i] == 'W' and s[j] == 'W':    
        res += 1
        j -= 1

if k % 2 and s[k//2]:res += 1
print(res)