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

Solution (Python):
N = int(input())
stone = list(map(str,input()))
ind = [""]*2
ind[0] = list()
ind[1] = list()
R = 0
W = 0
for i in range(N):
    if(stone[i]=="W"):
        ind[0].append(i)
        W += 1
    else:
        ind[1].append(i)
        R += 1
newR = 0
for i in range(R):
    if(i != ind[1][i]):
        break
    newR+= 1


newind =list(reversed(ind[1]))
ans2 = 0



if(newR != R):
    ans1 = min(W,R-newR)
else:
    ans1 = min(W,R)
if(ans1 != 0):
    for i in range(N//2):
        if(ind[0][i]>newind[i]):
            break
        ans2+=1
        
print(min(ans1,ans2))

Solution (Python):
N = int(input())
C = list(input())
import copy
D = []
flag = 0
ans = 0
keisan = 0

for p in range(N):
    if C[p] == "R":
        D += ["0"]
    else:
        D += ["1"]



def change(x,y):
    global ans
    hoge = copy.deepcopy(D[x])
    D[x] = D[y]
    D[y] = hoge
    ans += 1

def color(x):
    global ans
    ans += 1
    if D[x] == "0":
        D[x] = "1"
    elif D[x] == "1":
        D[x] = "0"

zero = D.count("0")
ichi = D.count("1")

for p in range(zero):
    if D[p] == "1":
        ans += 1


"""
while flag == 0:

    for r in range(N):
        if int("".join(D),2) == 2**r-1:
            flag = 1

    if flag == 0:
        for q in range(N):
            if D[q] == "1":
                for s in range(N):
                    keisan += 1
                    if D[N-s-1] =="0" :
                        if q < N-s-1:
                            change(q,N-s-1)
                        break
            else:
                continue
            break
"""
print(ans)

Solution (Python):
import sys
import math
from collections import Counter
import heapq
for _ in range(1):
    n=int(input())
    s=list(input())
    ptr=0
    while(ptr<n and s[ptr]=="R"):ptr+=1
    if ptr==n:
        print(0)
        exit(0)
    k=ptr
    ptr=n-1
    while(ptr>=0 and s[ptr]=="W"):
        ptr-=1
    if ptr==-1:
        print(0)
        exit(0)
    l=ptr
    ans=0
    while(k<l):
        if s[k]=="W" and s[l]=="R":
            s[k],s[l]=s[l],s[k]
            k+=1
            l-=1
            ans+=1

        elif s[k]=="R":
            k+=1
        elif s[l]=="W":
            l-=1
    print(ans)

Solution (Python):
N=int(input())
S=input()
rn,wn=0,0
for i in range(len(S)):
    if S[i]=='R':
        rn+=1
    else:
        wn+=1
winr,rinw=0,0
for i in range(rn):
    if S[i]=='W':
        winr+=1
for i in range(rn,rn+wn):
    if S[i]=='R':
        rinw+=1

print(max(winr,rinw))

Solution (Python):
n = int(input())
c = list(input())
num_r = c.count("R")
right=num_r
ans = 0
b_num_r = c[:num_r].count("R")
"""
for i in range(num_r):
    if c[i] == "W":
        while right != n-1 or c[right] != "W" 
"""
print(num_r - b_num_r)

Solution (Python):
N = int(input())
C = input()

rCnt = C.count('R')
tmp = C[0:rCnt]
print(tmp.count('W'))

Solution (Python):
N = int(input())
C = input()

i= 0
j = N - 1
cnt = 0
while i < j:
    if C[i] == 'W':
        while i < j:
            if C[j] == 'R':
                cnt += 1
                j -= 1
                break
            j -= 1
    i += 1
print(cnt)

Solution (Python):
n = int(input())
c = input()
c_ls = [str(i) for i in c]

num_r = c_ls.count('R')
num_sub_r = c_ls[:num_r].count('R')

print(num_r-num_sub_r)

Solution (Python):


N=int(input())
c=input()
d=list(c)
w=0
ans=0
w_list=[0]*N
w_cnt=0
r_cnt=0

if c.count("W")==0 or c.count("W")==N:
    ans=0
else:
    for i,j in enumerate(d):
        if j=="W":
            w_cnt +=1
        else:
            w_list[i]=w_cnt
            r_cnt += 1
            
    
    

    for i in range(N-1,-1,-1):
        if d[i]=="R":
            r_cnt -= 1
            
            if w_cnt>0:
                ans += 1
                w_cnt -= 1 
            else:
                break
        else:
            w_cnt -= 1
            if w_cnt<=0:
                break
        
        
print(ans)

Solution (Python):
N = int(input())

c_list = [s for s in input()]
back = N - 1


def check(forward):
    global back
    for i in range(back, -1, -1):
        if i < forward:
            return -1
        if c_list[i] == "R":
            back = i - 1
            return i
    return -1


count = 0
for i, c in enumerate(c_list):
    if c_list[i] == "W":
        temp = check(i + 1)
        if temp != -1:
            c_list[i] = "R"
            c_list[temp] = "W"
            count += 1
        else:
            break
print(count)

Solution (Python):
import sys
N = int(input())
c = input()
n = int(N/2)
cnt_R = c.count('R')
c_1 = c[:cnt_R]
cnt = c_1.count('W')
print(cnt)

Solution (Python):
n = int(input())
s = input()
l_1 = []

cnt = 0
for i in s[::-1]:
    if i =='R':
        cnt += 1
    else:
        l_1.append(cnt)

l_1.reverse()

def get_res(l_1):
    for i,j in enumerate(l_1):
        if i+1 == j:
            return j
        elif i+1 > j:
            return i
    return len(l_1)

print(get_res(l_1))

Solution (Python):
from collections import deque
N = int(input())
c = deque(list(input()))
cnt = 0
def left(c):
  l = c.popleft()
  return l
def right(c):
  r = c.pop()
  return r
while c:
  l = left(c)
  if l == 'R':
    continue
  else:
    while True:
      if not c:
        break
      r = right(c)
      if r == 'W':
        continue
      else:
        cnt += 1
        break
print(cnt)

Solution (Python):
import sys

n = int(input())
c = [0 if x=='W' else 1 for x in input()]
counter = 0

white_index = [i for i,ele in enumerate(c) if ele == 0]
red_index = [i for i in range(len(c)-1, -1, -1) if c[i] == 1]
    
    
for i in range(min(len(white_index), len(red_index))):
    if white_index[i] < red_index[i]:
        counter += 1
print(counter)

Solution (Python):
N = int(input())
c = str(input())

head = 0
tail = -1
count = 0

while head < N:
    if c[head] == "W":
        while tail >= -N and c[tail] == "W":
            tail -= 1
        if head >= N + tail:
            print(count)
            exit()
        tail -= 1
        count += 1
    head += 1
print(count)

Solution (Python):
n = int(input())
c = input()
dv = c.count('R')
print(c[:dv].count('W'))

Solution (Python):
N = int(input())
c = [0 if s=='R' else 1 for s in input()]

l = 0
r = N - 1

ans = 0
while l < r:
    while l < N and c[l] == 0:
        l += 1
    while r > 0 and c[r] == 1:
        r -= 1
    if l < r:
        ans += 1
    l += 1
    r -= 1
print(ans)

Solution (Python):
n=int(input())
a=list(input())


RR=[-1]

WW=[]

for i in range(n):
    if a[i]=='R':
        RR.append(i)
    else:
        WW.append(i)
WW.append(float('inf'))

if a.count('R')==0 or a.count('R')==n:
    print(0)
    exit()
if a.count('W')==1 or a.count('R')==1:
    print(1)
    exit()

for i in range(len(RR)-1):
    if RR[-2-i]<WW[i+1]:
        ans=i+1
        break


wc = [0]

rc = [0]

import itertools

a = ['R'] + a

if a[-1] == 'R':
    a.append('W')
else:
    a.append('R')

cntr = 0
cntw = 0
flag1 = True
for i in range(n + 2):
    if a[i] == 'W':
        cntr += 1
        if flag1:
            flag1 = False
        else:
            if cntw != 0:
                wc.append(cntw)
                cntw = 0
    else:
        if flag1:

            continue
        else:
            cntw += 1
            if cntr != 0:
                rc.append(cntr)
                cntr = 0

wc = list(itertools.accumulate(wc))
rc = list(itertools.accumulate(rc))


for i in range(len(wc)):
    ans = min(ans, rc[i] + wc[-1] - wc[i])
print(ans)

Solution (Python):
n = int(input())
c = input()
_trigger = False
ans = [0 for i in range(2)]
_count = c.count('R')
for i in range(len(c)):
  if c[i] == 'W':
    _trigger = True
    if _count >= i + 1:
      ans[1] += 1
  else:
    if _trigger == True and i + 1 >= _count:
      ans[0] += 1
print (min(ans))

Solution (Python):
N = int(input())
c = input()
li = list(c)
count = li.count('R')
num = 0
for i in range(count):
    if li[i] == 'W':
        num += 1

print(num)

Solution (Python):
def main():
    N = int(input())
    c = input()
    num_of_w = 0
    for i in range(N):
        if c[i] == 'W':
            num_of_w += 1

    num_of_w2 = 0
    for j in range(N-1,N-1-num_of_w,-1):
        if c[j] == 'W':
            num_of_w2 += 1

    judge = num_of_w - num_of_w2
    if judge <= 0:
        return 0
    else:
        return judge

print(main())

Solution (Python):
n=int(input())
c=list(input())
w=c.count('W')
ans=0
for i in range(n-w):
    if c[i]=='W':
        ans += 1

print(ans)

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

cw = c.count("W")
cr = c.count("R")
cc = c[-cw:].count("W")
print(cw - cc)

Solution (Python):
n = int(input())
a = input()
b = list(a)
c = sorted(b)
d = 0
for i in range(n):
  if(c[i] != b[i]):
    d += 1
print(d//2)

Solution (Python):
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


mod=1000000007
def combinations_count_mod(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x,y: x*y%mod, range(n, n - r, -1), 1)
    denom = pow( reduce(lambda x,y: x*y%mod, range(1, r + 1), 1) , mod-2, mod)
    return numer * denom % mod


def solve(n, s):
    
    cnt=0
    doneflag=0
    l=0
    r=n-1    

    
    sameflag=0
    for x in s:
        sameflag *=( x=='R' )
    for x in s:
        sameflag *= ( x=='W')
    if sameflag!=0:
        return 0

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

    
    for r in range(0,n)[::-1]:
        
        if s[r] == 'R':
            for l in range(l,n):
                if s[l]=='W':
                    cnt+=1
                    w_cnt-=1
                    l+=1
                    eprint('r,l, w_cnt ',end=':\n')
                    eprint(r,l, w_cnt)
                    break
        else: 
            w_cnt-=1

        
        if l>=r or w_cnt==0:
            eprint('r,l, w_cnt ',end=':\n')
            eprint(r,l, w_cnt)
            return cnt       
    return cnt
    
    

def main():
    n = int(input().strip())
    S = input().strip()
    print(solve(n,S))


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)




def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
C_ = S()
C = [1 if c == 'R' else 0 for c in C_]

ans = sum(C)
ans = min(ans, N-ans)


ans_ = 0
i = N - 2
_C = copy.copy(C)
for _ in range(N-1):
    
    if (_C[i] == 0 and _C[i + 1] == 1):
        _C[i] = 1
        
        ans_ += 1
    i -= 1
ans = min(ans, ans_)




_C = copy.copy(C)

l = 0
r = N-1
ans_ = 0
while (l < r):
    while (_C[l] != 0):
        l += 1
        if (l >= r):
            break
    while (_C[r] != 1):
        r -= 1
        if (l >= r):
            break
    if (l >= r):
        break
    _C[l], _C[r] = _C[r], _C[l]
    
    ans_ += 1
ans = min(ans, ans_)


print(ans)

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


numW = c.count('W')
numR = c.count('R')


rindex = [i for i, x in enumerate(c) if x == 'R']
rindextobe = range(0, len(rindex))
swap = len(set(rindex) - set(rindextobe))

print(min(numW, numR, swap))

Solution (Python):







n = int(input())
c = input()

wcount = c.count("W")
rcount = c.count("R")
check = 0

num = min(wcount, rcount)

if rcount < wcount:
    for i in range(rcount):
        if c[i] == "R":
            check += 1

else:
    for i in range(n - wcount, n):
        if c[i] == "W":
            check += 1

print(num - check)

Solution (Python):
N = int(input())
C = input()

white_ruisekiwa = [0]
for i in range(N):
    if C[i] == "W":
        white_ruisekiwa.append(white_ruisekiwa[-1]+1)
    else:
        white_ruisekiwa.append(white_ruisekiwa[-1])

red_ruisekiwa = [0]
for i in range(N-1, -1, -1):
    if C[i] == "R":
        red_ruisekiwa.append(red_ruisekiwa[-1]+1)
    else:
        red_ruisekiwa.append(red_ruisekiwa[-1])




for i in range(N):
    if white_ruisekiwa[i] == red_ruisekiwa[N-i]:
        print(white_ruisekiwa[i])
        exit()
else:
    print(0)

Solution (Python):
N = int(input())
S = list(input())
i, j = 0, N - 1
res = 0
while i < j:
    while i < j and S[i] == 'R':
        i += 1
    while i < j and S[j] == 'W':
        j -= 1
    if i < j and  S[i] == 'W' and S[j] == 'R':
        S[i], S[j] = S[j], S[i]
        i += 1
        j -= 1
        res += 1
        continue

print(res)

Solution (Python):
n = int(input())
c=str(input())
if c.count("W") >= c.count("R"):
    C_W =c[:-c.count("W")]
    print(C_W.count("W"))
else:
    C_R =c[c.count("R"):]
    print(C_R.count("R"))

Solution (Python):
N=int(input())
num=list(input())
num=[0 if i=="W" else 1 for i in num ]
count=0
first=0
for i in num:
    if i==1:count+=1
jk=0
for i in range(count):
    if num[i]==1:
        jk+=1
print(count-jk)

Solution (Python):
N = int(input())
S = input()
n=0
if S.find("W")==-1 or S.find("R")==-1:
  print(0)
else:
  while S.find("W")<S.rfind("R") and S.find("W")>=0:
    S = S[S.find("W")+1:S.rfind("R")]
    n+=1
  print(n)

Solution (Python):
n = int(input())
c = list(input())
left = 0
cnt = 0
right = len(c) - 1
while left < n and right >=0 and left < right:
    while left < n and c[left] != 'W':
        left += 1
    while right >= 0 and c[right] != 'R':
        right -= 1
    if left< n and right >= 0 and left<right:
        c[left], c[right] = c[right], c[left]
        cnt += 1
        left += 1
        right -=1 
print(cnt)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
from bisect import bisect_left
from collections import deque

def main():
    n = II()
    cc = list(IS())
    cc_rev = cc[::-1]
    cnt = 0
    indexs = []
    for i, c in enumerate(cc):
        if c == 'W':
            indexs.append(i)
    indexs = deque(indexs)
    if cc.count('W') == 0:
        print(0)
        return 0
    
    for i, c in enumerate(cc_rev):
        if c == 'R':
            if indexs:
                idx = indexs.popleft()
                if idx < (n-i-1):
                    cc[idx] = 'R'
                    cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
a = S.count("R")
print(S[:a].count("W"))

Solution (Python):
N = int(input())
c = input()
li = list(c)
count = li.count('R')
num = 0


    
        
            
                
                
                


    
        
        
        


    
        

for i in range(count):
    if li[i] == 'W':
        num += 1

print(num)

Solution (Python):
N = int(input())
c = input()
from collections import  deque
cc = deque(c)
ans = 0
while len(cc)>1:
    
    c1 = cc.popleft()
    c2 = cc.pop()
    
    if c1 == "R":
        cc.append(c2)
        continue
    else:
        if c2 == "R":
            ans += 1
            continue
        else:
            cc.appendleft(c1)
print(ans)

Solution (Python):
N = int(input())
c = list(input())
rr = []
ww = []
for i in range(N):
  if (c[i] == "R"):
    rr.append(i)
  if (c[i] == "W"):
    ww.append(i)

wl = len(ww)
rl = len(rr)
l = min([wl,rl])
ans = 0
for i in range(l):
  if (ww[i] < rr[-i-1]):
    ans += 1

print(ans)

Solution (Python):
int(input())
s = input()

x=s.count('R')

l=s[:x].count('W')
r=s[x:].count('R')

print(min(l,r))

Solution (Python):
N = int(input())
Cs= input()
str_ans = "R"*Cs.count("R") + "W"*Cs.count("W")


diff = 0
for s_ans, s_inp in zip(str_ans, Cs):
    if s_ans != s_inp:
        diff+= 1
print(diff//2)

Solution (Python):
nn = int(input())
ss = str(input())
stones = list(map(str, str(ss)))



numW = sum(x == 'W' for x in stones)
numR = sum(x == 'R' for x in stones)
if numW == 0:
    print('0')
elif numR == 0:
    print('0')
else:
    addrW = [0]*numW
    addrR = [0]*numR
    countW = 0
    countR = 0

    for i in range(nn):
        if stones[i] == 'W':
            addrW[countW] = i
            countW += 1
        else:
            addrR[countR] = i
            countR +=1

    ind_leftmostW = 0
    ind_rightmostR = len(stones)

    ind = 0

    while ind < min(numR,numW):
        if addrW[ind] < addrR[numR-1-ind]:
    
    
    
            ind += 1
        else:
            break

    print(ind)

Solution (Python):
N = int(input())
mozi = input()
mozi2 = list(mozi)
num_w = mozi2.count("W"); num_r = mozi2.count("R")
list_w2 = mozi2[(N-num_w):]; list_w1 = mozi2[:(N-num_w)]
list_r2 = mozi2[num_r:]; list_r1 = mozi2[:num_r]
if num_w <= num_r:
    if "W" in list_w2:
        print(list_w1.count("W"))
    else:
        print(num_w)
else:
    if "R" in list_r1:
        print(list_r2.count("R"))
    else:
        print(num_r)

Solution (Python):
ans = 0
n = int(input())
c = input()

check  = c[0]
count = 0
flag=0
for i in c:
    if(i=="R"):
        count+=1
for i,k in enumerate(c):

    if check=="W" and k=="R":
        ans+=1
        check = "W"
        if i<count:
            flag+=1
    else:
        check  = k
print(ans-flag)

Solution (Python):

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

s = 0
e = N - 1

cnt = 0
while s <= e:
    if S[s] == 'W':
        while (S[e] != 'R'):
            if s >= e:
                print(cnt)
                exit()
            e -= 1
        S[s], S[e] = S[e], S[s]
        cnt += 1
    s += 1

print(cnt)

Solution (Python):


n = int(input())
mozi = input()
a_list = []
for i in range(n):
    if mozi[i] == 'W':
        a_list.append(0)
    else:
        a_list.append(1)

r_count = sum(a_list)
w_count = n - r_count
start = r_count
count = 0
for i in range(r_count):
    if a_list[i] == 0:
        count += 1
        while True:
            if a_list[start] == 1:
                start += 1
                break
            else:
                start += 1
print(count)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
w = s.count("W")
if w==0:
    print(0)
else:
    cnt = 0
    for i in range(r):
        if s[i] == "W":
            cnt+=1
    print(cnt)

Solution (Python):
N=int(input())
S=input()
c=0
d=0
for i in range(N):
    if S[i]=="R":
        c+=1

for j in range(c):
    if S[j]=="W":
        d+=1

for k in range(c,N):
    if S[k]=="R":
        d+=1

if d%2==0:
    print(int(d/2))
else:
    print(int((d+1)/2))

Solution (Python):
"""
AtCoder :: Beginner Contest 174 :: Alter Altar
https://atcoder.jp/contests/abc174/tasks/abc174_d
"""
import sys


def solve(altar):
    """Solve puzzle."""
    soln = 0
    left = 0
    right = len(altar) - 1
    while left < right:
        while left < len(altar) and altar[left] == 'R':
            left += 1
        while right >= 0 and altar[right] == 'W':
            right -= 1
        if left < right:
            altar[left], altar[right] = altar[right], altar[left]
            soln += 1
    return soln


def main():
    """Main program."""
    N = int(sys.stdin.readline())
    altar = [c for c in sys.stdin.readline().strip()]
    print(solve(altar))


if __name__ == '__main__':
    main()

Solution (Python):
input()
l =list(input())
cw = l.count('W')
cr = l.count('R')

if cr < cw:
  ans = l[:cr].count('W')
else:
  ans = l[cr:].count('R')
print(ans)

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

rcnt=s.count("R")
num=s[0:rcnt]
rnum=num.count("R")

print(rcnt-rnum)

Solution (Python):
n = int(input())
cs = input()
l = len(cs)

ans = 0

i = 0
j = 1
while i < l:
  if cs[i] == "W":
    while j <= l - i:
      if cs[-j] == "R":
        ans += 1
        j += 1
        break
      j += 1

  i += 1

print(ans)

Solution (Python):
n = int(input())
S = list(input().split())
str_list = []
for s in S:
  str_list.append(list(s))
a = str_list[0].count('W')
if a == 0:
  print(a)
  exit()
else:
  ans = str_list[0][-a:].count('R')
print(ans)

Solution (Python):
N = int(input())

S = input()

wcnt = 0
for i in range(N):
    if S[i] == 'W':
        wcnt += 1

cnt = 0

for i in range(1,wcnt+1):
    if S[-i] == 'W':
        cnt += 1

print(wcnt-cnt)

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






def is_ok(c):
  s = True
  
  for i, p in enumerate(c[:-1]):
    if p == 'W':
      if c[i+1] == 'R':
        s = False
        break
  return s

if is_ok(c):
  print(0)
  exit()






Wn = len([x for x in c if x == 'W'])
Rn = len([x for x in c if x == 'R'])

c_r = 'R'*Rn+'W'*Wn



w_cnt = 0
for i in range(len(c)):
  if c_r[i] != c[i]:
    w_cnt += 1




if w_cnt % 2 == 0:
  a_c = int(w_cnt/2)
else:
  a_c = int((w_cnt+1)/2)



c_rr = 'R' * len(c)
r_cnt = 0
for i in range(len(c)):
  if c_rr[i] != c[i]:
    r_cnt += 1
a_r = r_cnt


print(min([a_c, a_r]))

Solution (Python):
n = int(input())
al = list(input())
cr = al.count('R')
print(al[:cr].count('W'))

Solution (Python):
import copy
import collections

n = int(input())
stones = list(input())
c_stones = copy.copy(stones)
R_count = collections.Counter(c_stones)['R']
if R_count ==0:
  print(0)
else:
  c_stones.reverse()
  last_R = len(c_stones)-c_stones.index("R")
  R_left_count = stones[:R_count].count("R")
  print(R_count - R_left_count)

Solution (Python):
import sys
from typing import List


def main():
    def readline() -> str:
        s = sys.stdin.readline()
        return s.rstrip()

    n: int = int(readline())
    c: List[str] = list(readline())

    if "W" not in c or "R" not in c:
        print(0)
        return

    l_p: int = c.index("W")
    r_p: int = -1
    operations: int = 0

    while n > l_p - r_p:
        if c[l_p] == "W":
            if c[r_p] == "R":
                c[l_p] = "R"
                c[r_p] = "W"
                operations += 1
                l_p += 1
            r_p -= 1
        else:
            if c[r_p] == "W":
                r_p -= 1
            l_p += 1

    print(operations)
    


if __name__ == '__main__':
    main()

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

rcnt=s.count("R")
num=s[0:rcnt]
rnum=num.count("R")

print(rcnt-rnum)

Solution (Python):
def read():
    N = int(input())
    C = input()
    return N, C


def fun(N, C):
    rs = C.count('R')
    crs = C[:rs].count('R')
    return rs - crs


if __name__ == '__main__':
    N, C = read()
    print(fun(N, C))

Solution (Python):
import math

def minOPs():
    N = int(input())
    s = input()
    count = 0
    L = 0
    R = N-1
    while(L < R):
        if(s[L] == 'W' and s[R] == 'R'):
            L+=1
            R-=1
            count+=1
        elif(s[L] == 'R' and s[R] == 'W'):
            L+=1
            R-=1
        elif(s[L] == 'R' and s[R] == 'R'):
            L+=1
        else:
            R-=1
    return(count)

if __name__ == '__main__':
    print(minOPs())

Solution (Python):


N = int(input())
s = input()
R_count = s.count("R")

new_s = s[R_count:N+1]
R_new = new_s.count("R")
print(R_new)

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

w = s.count("W")
r = s.count("R")

a = ""     
b = ""     
cnt = 0

for t in s:
    if t == "W":
        b = b + "0"
    else:
        b = b + "1"

for i in range(r+w):
    if i < r:
        a = a + "1"
    else:
        a = a + "0"
    if a[i] != b[i]:
        cnt += 1

cnt = cnt // 2

print(cnt)

Solution (Python):
N=int(input())
String=input().replace("W","1").replace("R","2")
c=[0]*N
for i in range (N):
    c[i]=int(String[i])
s=sorted(c)
import bisect
a=bisect.bisect_right(s,1)
cnt=0
for j in range(N-a):
    if c[j]==1:
        cnt+=1
print(cnt)

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

if 'R' not in c or 'W' not in c:
    print(0)
    exit()

left, right = 0, len(c) - 1
cnt = 0

while left < right:

    if c[left] == c[right] == 'W':
        right -= 1
        continue

    elif c[right] == c[left] == 'R':
        left += 1
        continue

    elif c[left+1] == 'R' and c[left] == 'W':
        c[left] = 'R'
        left += 1

    elif  c[right-1] == 'W' and c[right] == 'R':
        c[right] = 'W'
        right -= 1

    elif c[left] is 'W' and c[right] is 'R':
        c[left] = 'R'
        c[right] = 'W'

    else:
        left += 1
        right -= 1
        continue

    left += 1
    right -= 1

    cnt += 1

print(cnt)

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

r=c.count('R')
w=c.count('W')

from_l=0
from_r=len(c)
rp=0
while 1:
    from_l=c.find('W',from_l)
    if from_l==-1:
        from_l=len(c)
    from_r=c.rfind('R',0,from_r)

    if from_l>=from_r:
        break
    rp+=1
    from_l+=1
    if from_l==len(c):
        break
    
print(min(r,w,rp))

Solution (Python):
from sys import exit
n = int(input())
stone = list(input())
w_num = stone.count("W")
r_num = stone.count("R")
if w_num == n or r_num == n:
    print(0)
    exit()





def w_serch(entry_point):
    for i in range(n):
        try:
            pointer_left = entry_point + i
            if stone[pointer_left] == "W":
                return pointer_left
        except:
            return n

def r_serch(entry_point):
    for i in range(n):
        try:
            pointer_right = entry_point - i
            if stone[pointer_right] == "R":
                return pointer_right
        except:
            return -1

counter = 0
pointer_l = 0
pointer_r = n-1
for i in range(n // 2):
    pointer_l = w_serch(pointer_l)
    pointer_r = r_serch(pointer_r)
    
    if pointer_l > pointer_r:
        break
    else:
        stone[pointer_l] = "R"
        stone[pointer_r] = "W"
    
    counter += 1
print(counter)

Solution (Python):


N = int(input())

C = str(input())

Lis = [0,0] 

for i in C:
    if i == "R":
        Lis[0] += 1
    else:
        Lis[1] += 1

D = "R" * Lis[0] + "W" * Lis[1]

Lis = [0,0]

for s,t in zip(C,D):
    if (s != t) and (s == "R"):
        Lis[0] += 1
    elif (s != t) and (s == "W"):
        Lis[1] += 1

print("{}".format(max(Lis)))

Solution (Python):
N = int(input())
c = list(map(str,input()))
w = []
r = []
for i in range(N):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)
k = 0
W = len(w)
R = len(r)
L = min([W,R])
for i in range(L):
  if w[i] < r[R - i - 1]:
    k += 1
print(k)

Solution (Python):
import datetime
import string
import re
import math

N=int(input())
input = input()
str = []
for i in range(N):
    str.append(input[i])
str.reverse()

count = 0
begin = 0
end = len(str) - 1
while True:
    if begin < len(str):
        while str[begin] == 'W':
            begin += 1
            if begin == len(str):
                break

    if end >= 0:
        while str[end] == 'R':
            end -= 1
            if end == -1:
                break

    if begin < end:
        str[begin] = 'R'
        str[end] = 'W'
        begin += 1
        end -= 1
        count += 1
    elif begin >= len(str) and end > -1:
        if end != len(str) - 1:
            if str[end + 1] == 'R':
                str[end] = 'W'
                end -= 1
                count += 1
            else:
                end -= 1
        else:
            end -= 1
    elif begin < len(str) and end <= -1:
        if begin != 0:
            if str[begin - 1] == 'W':
                str[begin] = 'R'
                begin += 1
                count += 1
            else:
                begin += 1
        else:
            begin += 1
    else:
        print(count)
        break

Solution (Python):
from collections import Counter

n = int(input())
c = input()

a = Counter(c)
b = c[::-1]

cnt = 0
for i in range(a['W']):
    if b[i] == "R":
        cnt += 1
        
print(cnt)

Solution (Python):
N = int(input())
C = input()
R = (C.count('R'))
C2 = C[0:R]
R2 = (C2.count('R'))

print(R-R2)

Solution (Python):
N = int(input())
C = list(input())

RC = C.count("R")
WC = C.count("W")

print(C[:RC].count("W"))

Solution (Python):
import sys
from collections import deque

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


def resolve():
    n = int(input())
    C = list(input())

    W = deque()
    R = deque()
    for i in range(n):
        if C[i] == "W":
            W.append(i)
        if C[i] == "R":
            R.appendleft(i)

    res = 0
    i = 0
    cnt = 0
    while R:
        if C[i] == "W":
            cnt += 1
        if cnt:
            r = R.popleft()
            w = W.popleft()
            C[r], C[w] = C[w], C[r]
            cnt -= 1
            res += 1
        else:
            R.pop()
        i += 1
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
N = int(input())
S = input()

ans = 0
while True:
    left_white_idx = S.find('W')
    right_red_idx  = S.rfind('R')

    if left_white_idx == -1 or right_red_idx == -1 or left_white_idx > right_red_idx:
        break

    
    S = S[left_white_idx+1:right_red_idx]

    ans = ans + 1

print(ans)

Solution (Python):
n = int(input())
s1 = input()
s2 = s1[::-1]
i1 = 0
i2 = 0
ans = 0
while i1 < n:
  if s1[i1]=='W':
    while i2 < n:
      if s2[i2]=='R':
        ans += 1
        i2 += 1
        break
      elif i1+i2 >= n-2:
        break
      else:
        i2 += 1
  if i1+i2 >= n-2:
    break
  i1 += 1
print(ans)

Solution (Python):
N = int(input())
C = list(input())

R_count = C.count('R')
W_count = C.count('W')


if R_count == 0 or W_count == 0:
    print(0)
    exit()



i, j = 0, -1
cnt = 0
while i < N + j:
    if C[i] == 'W':
        while i < N + j:
            if C[j] == 'R':
                C[i] = 'R'
                C[j] = 'W'
                cnt += 1
                
                i += 1
                j -= 1
                break
            else:
                j -= 1
    else:
        i += 1


print(cnt)