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())
c=list(input())
i=0
j=len(c)-1
ans=0
while i<j:
    while i<len(c) and c[i]=='R':
        i+=1
    while j>=0 and c[j]=='W':
        j-=1
    if i<j:
        ans+=1
        i+=1
        j-=1
print(ans)

Solution (Python):
import math
import itertools
import sys
n = int(input())
c = input()
wcnt = 0
for i in range(n):
    if(c[i] == 'W'):
        wcnt += 1
sw = 0
for i in range(len(c)-wcnt):
    if(c[i] == 'W'):
        sw += 1
print(min(min(wcnt, len(c)-wcnt), sw))

Solution (Python):
n=int(input())
s=input()
w,r=0,0
for i in range(n):
    if s[i]=='W': w+=1
    else : r+=1
c=0
for i in range(r):
    if s[i]=='R': c+=1
print(r-c)

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


import collections
a = collections.Counter(c)
t = a["R"]

m = min(a["W"],a["R"])


co = 0

for i,j in enumerate(c):
    if co >=m or i>=t:
        break
    else:

        if j == "W":
            co +=1
        
        

print(min(m,co))

Solution (Python):


N = int(input())
C = list(input())

judged = True
red_flg = True
for c in C:
    if (red_flg == True) and (c == 'W'): red_flg = False
    if (red_flg == False) and (c == 'R'): judged = False

red_cnt = C.count('R')

if (judged):
    print(0)
else:
    print(C[red_cnt:].count('R'))

Solution (Python):
import heapq
N = int(input())
c = input()
w = []
r = []
for i, s in enumerate(c):
    if s == 'R':
        r.append(-i)
    else:
        w.append(i)
minrw = min(len(r), len(w))
        
heapq.heapify(r)
heapq.heapify(w)
ans = 0
while r and w:
    rq = -heapq.heappop(r)
    wq = heapq.heappop(w)
    if rq < wq: break
    else:
        heapq.heappush(r, -wq)
        heapq.heappush(w, rq)
        ans += 1
        if ans >= minrw:
            ans = minrw
            break

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
n = int(input())
c = str(input()).replace("\n","")
cl = list(c)

rnum = cl.count("R")

if rnum == 0:
  print(0)
  sys.exit()

rnum_f = cl[:rnum].count("R")
print(min(rnum,rnum-rnum_f))

Solution (Python):
N = int(input())
c = list(input())
Rcount = 0
for i in range(N):
    if c[i] == 'R':
        Rcount += 1
ans = 0
for i in range(Rcount):
    if c[i] == 'W':
        ans += 1
print(ans)

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

rc=l.count('R')

ltmp=l[:(rc)]
rctmp=ltmp.count('R')
print(rc-rctmp)

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

w = s.count('W')
r = s.count('R')
sw = s.count('W',0-w)

rw = 2*10**5
if r >=1:
  rw = w-sw
  
print(min(w,r,rw))

Solution (Python):
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=0

pos=0
Rpos=N-1



while ans<search_count and pos!=Rpos:
    if S[pos]=="W":
        while pos!=Rpos:
            if S[Rpos]=="R":
                
                S[pos]="R"
                S[Rpos]="W"
                ans+=1
                pos+=1
                break
            else:
                Rpos-=1
    else:
        pos+=1






print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

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

rc = c.count('R')
wc = c.count('W')

if wc == 0 or rc == 0:
    print(0)
else:
    rstr = c[0:rc]
    wstr = c[rc:]
    print(max(rstr.count('W'), wstr.count('R')))

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

count = 0
left = 0
right = N-1
while True:
    while left <= N-1 and c[left] != 'W':
        left += 1
    while 0 <= right and c[right] != 'R':
        right -= 1
    if not left < right:
        break
    c[left] = 'R'
    c[right] = 'W'
    count += 1

print(count)

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

idxl=-1
idxr=N
cnt=0


while(1):
  while(1):
    idxl=idxl+1
    if(idxl >= N):
      break
    if(c[idxl]=="W"):
      break
  if(idxl >= N):
    break
  while(1):
    idxr=idxr-1
    if(idxr <= 0):
      break
    if(c[idxr]=="R"):
      break
  if(idxr <= 0):
    break


  if(idxl>=idxr):
    break;
  cnt = cnt+1
print(cnt)

Solution (Python):
n = int(input())
string = input()
Rcount = string.count('R')
fixRcount = string.count('R', Rcount, n)
print(fixRcount)

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

c = list(input())

w_count = c.count("W")
r_count = c.count("R")

right_center = c[r_count:]
right_w_count = right_center.count("W")

print(w_count - right_w_count)

Solution (Python):
    

def main():
    n = int(input())
    cs = input()

    c = 0
    c_r = 0
    c_w = 0
    for i, c in enumerate(cs):
        if c == "R":
            c_r += 1
        else:
            c_w += 1
    if (c_r == 0) or (c_w == 0):
        print(0)
        return
    op = 0
    for i, c in enumerate(cs):
        if c_r == 0:
            break
        if c == "W":
            op += 1
        c_r -= 1
    print(op)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
C=input()
from collections import Counter
count=Counter(C)

countR=count["R"]
countW=count["W"]
missR=0
missW=0
for i in range(N):
    now=C[i]
    if now=="W" and i<N-countW:
        missW+=1
    if now=="R" and i>countR:
        missR+=1

X=min(missW, missR)

ans=min(missW, missR)+abs(missR-missW)

print(ans)

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

ans = 0
W = -1
for i in range(N):
    if S[i] == "W":
        W = i
        break
if W == -1:
    print(ans)
    sys.exit()

R = N
for i in range(N-1,-1,-1):
    if S[i] == "R":
        R = i
        break
if R == N:
    print(ans)
    sys.exit()

if(R<=W):
    print(ans)
    sys.exit()

ans += 1

while True:
    flagW = False
    flagR = False

    for i in range(W+1,R):
        if S[i] == "W":
            W = i
            flagW = True
            break
    
    for j in range(R-1, W, -1):
        if S[j] == "R":
            R = j
            flagR = True
            break
    
    if flagW and flagR:
        ans += 1
        continue
    else:
        print(ans)
        sys.exit()

Solution (Python):




N=int(input())
List=input()
AList=list(List)


RCNT=AList.count("R")
BList=sorted(List)




ANUM=0
BNUM=0
ans=0
i=0
while i<N:
    if AList[i]=="R":
        ANUM+=1
    if BList[i]=="R":
        BNUM+=1
        
    
    if AList[i]!=BList[i]:
        ans+=1
    if BNUM==RCNT or ANUM==RCNT:
        print(ans)
        quit()
    
    i+=1
    
print(ans)

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

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

if r == N or r == 0:
  print(0)
  exit()

a = S[:r].count('W')

print(a)

Solution (Python):
from collections import Counter

n = int(input())
c = list(input())
c_s = sorted(c)
count = 0
W = 0
R = 0

for i in range(n):
    if c_s[i] == 'R' and c[i] == 'W':
        W += 1
    if c_s[i] == 'W' and c[i] == 'R':
        R += 1
print(max(R,W))

Solution (Python):
N = int(input())
c = input()
lr_dict = dict()
lr_dict["W"] = {"R": 0,
                "W": 0
                }
lr_dict["R"] = {"R": 0,
                "W": 0
                }
n_r = 0
n_w = 0
for i in range(N):
    if c[i] == "W":
        n_w += 1
    else:
        n_r += 1
l_w = 0
r_r = 0
for i in range(n_r):
    if c[i] == "W":
        l_w += 1
for i in range(n_r, N):
    if c[i] == "R":
        r_r += 1
print(max(l_w, r_r))

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

ans = 0
w = 0
r = 0
j = N-1
for i in range(N):
    if c[i] == "W":
        while i < j:
            if c[j] == "R":
                ans += 1
                j -= 1
                break
            j -= 1
    if j - i == 1:
        break
print(ans)

Solution (Python):
def main():
    n = int(input())
    cs = input()

    white_l = n
    for i in range(n):
        if cs[i]=='W':
            white_l = i
            break
    red_r = -1
    for i in range(n-1, white_l, -1):
        if cs[i]=='R':
            red_r = i
            break

    manu = 0
    while white_l < red_r:
        manu += 1

        for i in range(white_l+1, n):
            if cs[i]=='W':
                white_l = i
                break
        else:
            break

        for i in range(red_r-1, white_l, -1):
            if cs[i]=='R':
                red_r = i
                break
        else:
            break

    print(manu)

main()

Solution (Python):
n = int(input())
stones = list(input())
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
goal = 'R' * r_num + 'W' * w_num

index = 0
poplist = list()
for i in stones:
    if i == goal[index]:
        poplist.append(index)
    index += 1
for i in sorted(poplist, reverse=True):
    stones.pop(i)
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
print(min(w_num, r_num))

Solution (Python):
N=int(input())
d=input()
r=d.count('R')
w=N-r
ans=r
leftr,leftw,rightr,rightw=0,0,r,N-r
for i in range(N):
  if d[i]=='R':
    leftr+=1
    rightr-=1
  else:
    leftw+=1
    rightw-=1
  if leftw==rightr:
    f=rightr
  elif leftw<rightr:
    f=rightr
  else:
    f=leftw
  ans=min(ans,f)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
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 TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



N = INT()
c = input()

n = c.count("R")
ans = 0
for i in range(n):
	if c[i] != "R":
		ans += 1

print(ans)

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

arr =[]
for i in S:
    arr.append(i)


Rnum =arr.count('R')
Wnum =arr.count('W')

ans =0
for i in range(Rnum):
    if arr[i] =='W':
        ans +=1

print(ans)

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

rw_after=sorted(rw)

count=0
for i in range(N):
    if rw[i] != rw_after[i]:
        count+=1
print(int(count/2))

Solution (Python):
N = int(input())
stones = list(input())
left = -1
right = N
ans = 0
while left < right:
    l = left
    r = right
    for i in range(l+1,N):
        if stones[i] == "W":
            left = i
            break
    else:
        left = N
    for i in reversed(range(right)):
        if stones[i] == "R":
            right = i
            break
    else:
        right = -1
    if left < right:
        ans += 1
print(ans)

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

if "R" not in l or "W" not in l:
    print(0)
    exit()

cnt = 0

i = 0
k = n-1

while i < k:
    if l[i] == "W" and l[k] == "R":
        l[i], l[k] = l[k], l[i]
        cnt += 1
    if l[i] == "R":
        i += 1
    if l[k] == "W":
        k -= 1
print(cnt)

Solution (Python):
from collections import deque
n = int(input())
c = input()
d = deque()
a = 0; b = 0
for i in range(n):
    d.append(c[i])
for _ in range(len(d)):
    if d[0] == "R":
        d.popleft()
    else:
        break
for _ in range(len(d)):
    if d[-1] == "W":
        d.pop()
    else:
        break
for i in range(len(d)):
    if d[i] == "R": a += 1
for i in range(a):
    if d[i] == "W": b += 1
print(b)

Solution (Python):
N = int(input())
C = input()
count_R = C.count('R')
print(C[:count_R].count('W'))

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



rn=c.count("R")
print(c[:rn].count("W"))

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    c = input()

    r = N - 1
    l = 0
    r_ch, l_ch = True, True
    ans = 0
    while True:
        if l_ch:
            if c[l] == "W":
                l_ch = False
            l += 1
        if r_ch:
            if c[r] == "R":
                r_ch = False
            r -= 1
        if l_ch is False and r_ch is False:
            ans += 1
            r_ch, l_ch = True, True
        if l-1 >= r+1:
            break

    print(ans)

Solution (Python):
from collections import Counter

n=int(input())
cli = list(input())

cli_count = Counter(cli)

white = cli_count['W']
red = cli_count['R']

count = 0
for c in cli:
    if red == 0:
        break

    if c == 'W':
        count += 1
        red -= 1
    else:
        red -= 1

print(count)

Solution (Python):
def main():
  n = int(input())
  li = list(input())
  i = 0
  j = n-1
  ans = 0
  while(i < j):
    while(li[i] == "R" and i < j):
      i += 1
    while(li[j] == "W" and i < j):
      j -= 1
    if li[i] == "W" and li[j] == "R":
      li[i] = "R"
      li[j] = "W"
      ans += 1
  print(ans)    

if __name__ == '__main__':
  main()

Solution (Python):
n=int(input())
s=input()
rcnt=s.count('R')
ans=0
for i in range(rcnt):
  if s[i]=='W':
    ans+=1
print(ans)

Solution (Python):
n = int(input())
s = list(input())
r = s.count("R")
w = s.count("W")
p, q = 0, 0
for i in range(r):
    if s[i] == "W":
        p += 1

for i in range(r, n):
    if s[i] == "R":
        q += 1

print(min(p, q))

Solution (Python):

import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n = I()
s = list(S())

w = 0
r = n - 1
ans = 0
while True:
    for i in range(w, n):
        if s[i] == "W":
            w = i
            break
    else:
        w = -1
    for i in range(r, 0, -1):
        if s[i] == "R":
            r = i
            break
        r += 1
    else:
        r = -1
    if w == -1 or r == -1:
        print(ans)
        exit()
    else:
        if r < w:
            print(ans)
            exit()
        ans += 1
        s[w] = "R"
        s[r] = "W"
        w += 1
        r -= 1

Solution (Python):
N=int(input())
c=list(map(str,input()))

W=0;R=0;w=0;r=0;p=0
for i in range(N):
  if c[i]=="W":
    W=W+1
R=len(c)-W

for i in range(R):
  if c[i]=="W":
    p=p+1
if W==0 or R==0:
  print(0)
else:
  print(p)

Solution (Python):
n = int(input())
s = [_ for _ in input()]

l, r = 0, n-1
if not('R' in s):
    print(0)
    exit()

if not('W' in s):
    print('0')
    exit()

ans = 0
while 1:
    while s[l] == "R":
        l += 1
    while s[r] == "W" and l != r:
        r -= 1

    if l != r:
        s[l], s[r] = s[r], s[l]
        ans += 1
        
    if l == r:
        break

print(ans)

Solution (Python):
N=int(input())
c=list(input())
m=c.count('R')
a=0
for i in range(m):
    if c[i]=='R':
        a+=1
print(m-a)

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

l = 0
r = n-1

cnt = 0

while r > l:
    if c[l] == "R":
        l = l + 1
    else:
        while r > l:
            if c[r] == "W":
                r = r - 1
            else:
                cnt = cnt + 1
                r = r - 1
                l = l + 1
                break

print(cnt)

Solution (Python):
import sys
readline = sys.stdin.readline

N = int(readline().strip())
C = readline().strip()
c = []
for i in range(len(C)):
  c.append(C[i])

ans = 0
s = 0
e = len(c)-1
d = True
while s < e:
  si = s
  ei = e
  for i in range(si, ei):
    if c[i] == "R":
      s += 1
    else:
      break
  for j in range(ei, si, -1):
    if c[j] == "W":
      e -= 1
    else:
      break
  if s < e:
    c[s] = "R"
    c[e] = "W"
    ans += 1
print(ans)

Solution (Python):
N = int(input())
c = list(input())
r_num = c.count("R")
ref = c[:r_num]
w_num = ref.count("R")
print(r_num - w_num)

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

r_count = 0
for i in range(N):
    if(c[i] == "R"):
        r_count += 1
w = 0
for i in range(r_count):
    if(c[i] == "W"):
        w += 1

print(w)

Solution (Python):







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

ans = 0
r = 0

for i in range(n):
    if c[i] == 'R':
        r+=1

if (r == n or r== 0):
    print(0)
    exit()

for i in range(n):
    if c[i] == 'W':
        r -= 1
        ans+=1
    else:
        r -= 1
    if (r == 0):
        break
print(ans)

Solution (Python):
def redc(arr):
    res=0
    for i in range(len(arr)):
        if arr[i]=='R':
            res+=1
    return res
        
n=int(input())
arr=list(input())
m=redc(arr)
ans=m-redc(arr[0:m])
print(ans)

Solution (Python):
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l ==[]:
  print(0)
elif l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))

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

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

sort_rs = rs - clist[0:rs].count("R")
ans = rs
if ws < ans:
    ans = ws

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

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

R_num=c.count('R')
c_ext=c[:R_num]
W_num=c_ext.count('W')
print(W_num)

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

    ans = 0
    lp = 0
    rp = N-1
    try:
        while lp < rp:
            while lp < N and c[lp] == "R":
                lp += 1
            while rp > -1 and c[rp] == "W":
                rp -= 1
            if lp < rp:
                ans += 1
                lp += 1
                rp -= 1
    except Exception as err:
        print(err)
        print(c, lp, rp)
    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
n=int(input())
s=input()
w=0
r=0
for i in s:
    if i=='W':
        w+=1
    else:
        r+=1
if r==0 or w==0:
    print(0)
else:
    t='R'*r
    t=t+'W'*w
    
    ans=0
    for i in range(len(s)):
        if s[i]!=t[i]:
            ans+=1
    ans=ans//2
    print(ans)

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

num_w = c.count('W')

ans = 'R' * (N-num_w) + 'W' * num_w
cnt = 0
for i in range(N-num_w):
    if c[i] != ans[i]:
        cnt+=1
print(cnt)

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

l = list(input())

begin = 0
end = n-1

cnt = 0
while(True):
    
    if(end==-1):
        break
    if(begin==n):
        break
    for i in range(begin,end+1):
        w_place = i
        if(l[w_place]=='W'):
            begin=w_place
            break
        if(i==end):
            begin=end
    for j in range(end,begin-1,-1):
        r_place = j
        if(l[r_place]=='R'):
            end=r_place
            break
        if(j==begin):
            end=begin
    if(begin>=end):
        break
    else:
        cnt += 1
        l[begin] = 'R'
        l[end] = 'W'
    begin += 1
    end -= 1

print(cnt)

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

ans = 0
rightNum = n-1
flag = 0
for i in range(n):
    if c[i] == "W":
        for j in range(n):
            if i >= rightNum-j:
                flag = 1
                break
            if c[rightNum-j] == "R":
                c[rightNum-j] = "W"
                c[i] = "R"
                rightNum -= j+1
                ans += 1
                break
    if flag == 1:
        break
print(ans)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
s = sr()
ans = inf
a = [0 for i in range(n)]
a1 = [0]
for i,c in enumerate(s):
    if c == 'W':
        a[i]=1
for num in a:
    a1.append(a1[-1]+num)
def sumArea(l, r):
    return a1[r]-a1[l]

for k in range(n+1):
    numW0k = sumArea(0,k)
    numRkn = n-k-sumArea(k,n)
    pre = 0
    if numW0k >= numRkn:
        pre = numRkn+(numW0k-numRkn)
    else:
        pre = numW0k+(numRkn-numW0k)
    ans = min(ans, pre)
print(ans)

Solution (Python):
n=int(input())
cnt=[0,0]
a=input()
for i in range(len(a)):
  if a[i]=='R':
    cnt[0]+=1
  else:
    cnt[1]+=1   
cnt_=[0,0]
for j in range(cnt[0]):
  if a[j]=='R':
    cnt_[0]+=1   
print(cnt[0]-cnt_[0])

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


cnt = 0
for i in range(n):
  w_idx = c.find("W")
  r_idx = c.rfind("R")
  if w_idx < r_idx and w_idx != -1:
    c = c[w_idx+1:r_idx]
    cnt += 1

print(cnt)

Solution (Python):
n = int(input())
c = input()
p,q = 0,n-1
x = 0
while(1):
    while(c[p] == 'R' and p < n-1):
        p += 1
    while(c[q] == 'W' and q > 0):
        q -= 1
    if(p < n-1 and q > 0 and p < q):
        p += 1
        q -= 1
        x += 1
    else:
        break
print(x)

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

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

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

print(ans)

Solution (Python):
N = int(input())
c_str = input()
R_cnt = c_str.count('R')
tmp_cnt = c_str[:R_cnt].count('R')
print(R_cnt - tmp_cnt)

Solution (Python):
n = int(input())
s = input()
cR = s.count("R")
cW = n - cR
print(s[cR:].count("R") )

Solution (Python):
n = int(input())
s = list(input())
ans = 0
right = n-1
for i in range(n):
    if s[i] == "W":
        while right >= i:
            if s[right] == "R":
                s[right] = "W"
                s[i] = "R"
                ans += 1
                right -= 1
                break
            else:
                right -= 1


print(ans)

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

nw = C.count('W')

ans = 0
for _ in range(N):
    if nw == 0:
        break
    tmp = C.pop()
    if tmp == 'W':
        nw -= 1
    else:
        ans += 1
        nw -= 1
print(ans)

Solution (Python):
def main():
    N = int(input())
    C = input()
    
    ANS = 0
    while 1 == 1:
        i = C.find('W')
        j = C.rfind('R')
        if i == -1 or j == -1:
            break
        if i >= j:
            break
        if i + 1 == j:
            ANS += 1
            break
        C = C[i+1:j]
        ANS += 1
    print(ANS)
if __name__ == '__main__':
    main()

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

r=c.count('R')
w=0
ans=r
for ch in c:
    if ch=='W':
        w+=1
    else:
        r-=1
    m=min(r,w)
    ans=min(r+w-m, ans)

print(ans)

Solution (Python):


import sys
input = sys.stdin.readline

N=int(input())
c=input().rstrip()
R =[0]*(N+1)
W =[0]*(N+1)

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

ans = float('inf')

for i in range(N+1):
    tmp = min(W[i],Rall-R[i])+abs(W[i]-(Rall-R[i]))
    ans = min(ans,tmp)

print(ans)

Solution (Python):
n=int(input())
c=list(input())
if len(set(c))==1:
    print('0')
else:
    r=c.count('R')
    w=c.count('W')
    rc=c[:r].count('R')
    wc=c[:r].count('W')
    if rc==0:
        if r<w:
            print(r)
        else:
            print(w)
    else:
        if r-rc<wc:
            print(r-rc)
        else:
            print(wc)

Solution (Python):


N = int(input())
C = input()
RL=[]
WL=[]
for i,c in enumerate(C):
    if c == 'R':
        RL.append(-i)
    else:
        WL.append(i)

import heapq
heapq.heapify(RL)
heapq.heapify(WL)
count = 0
if not RL or not WL:
    print(0)
    exit()

while True:
    r = heapq.heappop(RL)
    r = -r
    w = heapq.heappop(WL)
    if w>r:
        print(count)
        exit()
    heapq.heappush(RL,-w)
    heapq.heappush(WL,r)
    count+=1

Solution (Python):
N = int(input())
c = list(input())
cnt = c.count('R')
b = c[cnt:]
print(b.count('R'))

Solution (Python):
N = int(input())
S = str(input())
w = S.count('W')
r = S.count('R')
A = 'R'*r+'W'*w
count = 0
for i in range(len(S)):
    if S[i]!=A[i]:
        count += 1

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

Solution (Python):
import math

import sys

input = sys.stdin.readline
def inp():
	return(int(input()))
def inlt():
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(s[:len(s)-1])
def invr():
	return(map(int,input().split()))
def print_fract(p,q):
	g = math.gcd(p,q)
	p//=g
	q//=g
	print(str(p)+"/"+str(q))














N = inp()
S = insr()
print(S[:S.count("R")].count("W"))

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


bottom_w = N
top = 0
count = 0
while(bottom_w > top):
    for i in reversed(range(bottom_w)):
        if(line[i]=='R'):
            bottom_w = i
            break

    if(line[-1]=='W' and bottom_w == N):
        print(0)
        exit()
    if(bottom_w <= top):
        break
    for i in range(top,N):
        if(i == N-1):
            print(count)
            exit()
        if(line[i]=='W' and bottom_w > i):
            
            
            count += 1
            line[bottom_w] = 'W'
            top = i+1
            break
print(count)

Solution (Python):
from itertools import accumulate
N = int(input())
S = list(input())
W = [0]*N
R = [0]*N
ans = N

for i in range(N):
    if S[i] == "W":
        W[i] = 1
    else:
        R[i] = 1
R.reverse()
R = list(accumulate(R))
W = list(accumulate(W))
if R[N-1] == 0 or W[N-1] == 0:
    ans = 0
else:
    sakai = R[N-1]
    ans = max(W[sakai-1], R[N-sakai-1])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
C = input()

bad_w = 0
red = 0
ok = True
for i in range(N-1,-1,-1):
    if C[i] == 'R':
        red += 1
        ok = False
    if not ok and C[i] == 'W':
        bad_w += 1

good_red = 0
for i in range(red):
    if C[i] == 'R':
        good_red += 1


print( min(bad_w, N-bad_w, red-good_red) )

Solution (Python):
c = []

n = int(input())
c = input()
counterR = 0
counterW = 0
flag = 0

for i in range(n):
    if(c[i] == 'R'):
        counterR += 1 

if(counterR == n): 
    print('0')
    flag = 1

else:
    for i in range(counterR):
        if(c[i] == 'W'):
            counterW += 1

if(flag == 0):
    print(counterW)

Solution (Python):
from collections import Counter


N = int(input())
c = input()

counter = Counter(c)
R_cnt = counter['R']
W_cnt = counter['W']

ans = 0
for s in c[:R_cnt]:
    if s == 'W':
        ans += 1


print(ans)

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

r = N-1
l = 0
ans = 0

while C[r] == 'W':
    r -= 1
    if r < 0:
        print(0)
        exit()
while C[l] == 'R':
    l += 1
    if l >= N:
        print(0)
        exit()
while l < r:
    C[r], C[l] = C[l], C[r]
    ans += 1
    while C[r] == 'W':
        r -= 1
        if r < 0:
            break
    while C[l] == 'R':
        l += 1
        if l >= N:
            break
print(ans)

Solution (Python):
n = int(input())
c = list(input())
md = c.count('R')
rc, wc = c[md:].count('R'), c[:md].count('W')
if rc==wc: print(rc)
elif rc > wc: print(rc+(rc-wc))
else: print(wc+wc-rc)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
s = list(input())
r = s.count('R')
w = s.count('W')

all_paint = min(r, w)
all_convert_r = r - s[0:r].count('R')
all_convert_w = w - s[n-w:n].count('S')
print(min(all_paint, all_convert_r, all_convert_w))

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

w_count = c.count('W')

move_count = c[-w_count:].count('R')
if w_count == 0:
    print(0)
else:
    print(move_count)

Solution (Python):


N = int(input())
c_list = list(input())


red_count = 0
white_count = 0
swap_count = 0


for c in c_list:
    if c=="R":
        red_count += 1
    else:
        white_count += 1


w_i = 0
r_i = N-1
while r_i>w_i:
    if c_list[r_i]=="R":
        find_white = False
        
        while r_i>w_i and w_i<=N-1:
            if c_list[w_i]=="W":
                find_white = True
                break
            w_i += 1
        
        if find_white:
            tmp = c_list[r_i]
            c_list[r_i] = c_list[w_i]
            c_list[w_i] = tmp
            swap_count += 1
    else:
        r_i -= 1


ans = min(red_count, white_count, swap_count)
print(ans)

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

cnt = 0

ans = "".join(sorted(c))
ans_ = list(ans)
ans_[-1] = "R"
ans_ = "".join(ans_)


cnt = 0
for i in range(n-1):
    if c[i] != ans[i]:
        cnt += 1
    
        
print(cnt//2 + cnt%2)

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

Nr = C.count("R")
ans = C[:Nr].count("W")
print(ans)