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):





def modinv(n,p):
    return pow(n,p-2,p)

def main():
    
    

    n = int(input())
    s = input()
    c1 = s.count('W')
    c2 = s.count('R')

    c3 = s[:c2].count('W')
    print(min(c1, c3))


py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')

if __name__ == '__main__':
   main()

Solution (Python):
n = int(input())
s = input()
nr = s.count('R')
nw = s.count('W')
s_ans = 'R' * nr + 'W' * nw
ans = 0
for i in range(n):
    if s[i] != s_ans[i]:
        ans += 1
print(ans // 2)

Solution (Python):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    C = kwargs["C"]
    left = 0
    right = C.count("R")
    res = max([left, right])

    for i in range(n):
        if C[i] == "W":
            left += 1
        if C[i] == "R":
            right -= 1
        tmp = max([left, right])
        res = min(res, tmp)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]
    cin = input().split()[0]
    kwargs["C"] = [i for i in cin]

    cout = main(kwargs)
    print(cout)

Solution (Python):
import sys
import numpy as np
import math  
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate 
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache 

def main():
    n = int(input())
    C = list(input())
    
    print('{:}'.format(C[:C.count('R')].count('W')))
    

    return 0

if __name__ == "__main__":
    main()

Solution (Python):



def input_int():
    return int(input())



def int1(x):
    return int(x) - 1



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



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = input_int()
    c_list = input()

    red_cnt = c_list.count("R")
    white_cnt = n - red_cnt

    pre = "R"
    is_out = False
    for c in c_list:
        if c != pre:
            is_out = True
            break
        pre = c

    if is_out:
        print(min(red_cnt, white_cnt, c_list[:red_cnt].count("W")))
    else:
        print(0)


if __name__ == "__main__":
    main()

Solution (Python):

"""
Created on Sun Aug  2 17:58:28 2020

@author: Paras
"""

n = int(input())
s = input()

i = 0
j = n-1
count = 0
while i<j:
    while i<n and s[i]=='R':
        i+=1
    while j>=0 and s[j]=='W':
        j-=1
    if i>=j:
        break
    count+=1
    i+=1
    j-=1
    
print(count)

Solution (Python):
import heapq
import sys
N = int(input())
K = list(input())
W = []
R = []
for i in range(N):
    if K[i] == "W":
        W.append(i)
    else:
        R.append(-i)

if len(W) == 0 or len(R) == 0:
    print(0)
    sys.exit()
heapq.heapify(W)
heapq.heapify(R)
c = 0
while(True):
    a = heapq.heappop(W)
    b = heapq.heappop(R)
    b *= -1
    if a > b:
        break
    heapq.heappush(W, b)
    heapq.heappush(R, -a)
    c += 1
print(c)

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

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

print(ans)

Solution (Python):

from collections import deque

N = int(input())
c = input()
l = deque()
for i in range(N):
    l.append(c[i])



t = 0

def shori():
    stat = True
    while stat:
        if(len(l) > 0):
            if(l[0] == 'R'):
                l.popleft()
            else:
                stat = False
        else:
            stat = False

    stat = True
    while stat:
        if(len(l) > 0):
            if(l[-1] == 'W'):
                l.pop()
            else:
                stat = False
        else:
            stat = False

    

shori()

while len(l) > 0:
    l.popleft()
    l.pop()
    t += 1
    shori()

print(t)

Solution (Python):

N = int(input())
C = list(input())
l = 0
r = N - 1
count = 0
while l < r:
  if C[l] == 'W' and C[r] == 'R':
    C[l], C[r] = C[r], C[l]
    l += 1
    r -= 1
    count += 1
  else:
    if C[l] == 'R':
      l += 1
    if C[r] == 'W':
      r -= 1
print(count)

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

c=input()

C=[a for a in c]

number=0
answer=0
red=C.count('R')

if red==0 or red==N:
    print(0)
else:
    words=C[red:]
    print(words.count('R'))

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

Solution (Python):
n=int(input())
c=input()
l,r=0,n-1
a=0
while r>l:
  while c[l]=='R' and l<n-1: l+=1
  while c[r]=='W' and r>=0: r-=1
  if r>l: a+=1
  l+=1
  r-=1
print(a)

Solution (Python):

n = int(input())
s=input()
r=s.count("R")
w=s.count("W")    
print(s[0:r].count("W"))

Solution (Python):
"""According to a fortune-teller, a white stone placed to the immediate left of a red stone 
WR leads to nok
"""
n = int(raw_input())
s = list(raw_input())
import collections


def f(s):
	cc = collections.Counter(s)
	if cc['R'] == 0 or cc['W'] == 0:
		return 0
	i = 0
	j = len(s) - 1
	
	
	
	c = 0
	while(i < j):
		if s[i] == 'W' and s[j] == 'R':
			s[i],s[j] = s[j],s[i]
			i += 1
			j -= 1
			c += 1
		while(i < j and s[i] == 'R'): i+=1
		while(i < j and s[j] == 'W'): j-=1
	return min(c, cc['R'], cc['W'])

print f(s)

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

Solution (Python):
N=int(input())
C=list(input())
D=sorted(C)
P=0
for i in range(N):
  if C[i]!=D[i]:
    P+=1
print(P>>1)

Solution (Python):
n=int(input(""))
lis=input("")
rt=0
for i in lis:
    if(i=="R"):
        rt+=1
ht=n-rt
r=0
h=0

s=max(h,rt)

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)

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

s=input()
import heapq
windex=[]
rindex=[]

for i,ss in enumerate(s):
  if ss=="W":
    heapq.heappush(windex,i)
  else:
    heapq.heappush(rindex,i*(-1))
 
cnt=0
while 1:
  if len(windex)==0 or len(rindex)==0:
    print(cnt)
    exit()
  else:
    tmpw=windex[0]
    tmpr=rindex[0]*(-1)
    
    if tmpw>tmpr:
      print(cnt)
      exit()
    else:
      cnt+=1
      heapq.heappop(windex)
      heapq.heappop(rindex)

Solution (Python):
N = int(input())
stone = str(input())
num_red = stone.count('R')
cnt = 0
for i in range(num_red):
    if stone[i] == 'W':
        cnt += 1
print(cnt)

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

count=0;
lp=0
rp=N-1
swap=0
for i in range(N):
    while c[lp]!='W' and lp<rp:
        lp+=1
    while c[rp]!='R' and lp<rp:
        rp-=1
    if lp<rp:
        swap+=1
        lp+=1
        rp-=1
    else:
        break
print(swap)

Solution (Python):
n = int(input())
c = list(input())
r = c.count("R")
ans = 0
for i in range(r):
    if c[i] == "W":
        ans += 1
print(ans)

Solution (Python):
n=int(input())
from collections import deque
queue=deque(input())


s=0
while len(queue)>1:
    a=queue.popleft()
    b=queue.pop()
    if a=='W' and b=='R':
        s+=1
        
    elif a=='W' and b=='W':
        while queue and b=='W':
            b=queue.pop()
        if b=='R':
            s+=1
    elif a=='R' and b=='R':
        while queue and a=='R':
            a=queue.popleft()
        if a=='W':
            s+=1                
print(s)

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

l=[]
for c in cs:
    l.append(1 if c=='R' else 0)

iW=0
iR=N-1
ans=0
while True:
    
    while l[iW]!=0:
        iW+=1
        if iW > N-1:
            break
    
    while l[iR]!=1:
        iR-=1
        if iR<0:
            break
    if iW<iR:
        l[iW],l[iR]=l[iR],l[iW]
        ans+=1
    else:
        break
print(ans)

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

n=int(input())
s=list(input().strip())
count = 0
cw = s.count('W')
cr = n-cw

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

print(count)

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

Solution (Python):
N = int(input())
C = input()
a = C.count("R")
b = C[:a].count("R")
print(a-b)

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

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

r_cnt = 0
w_cnt = 0
for i in c:
    if i =='R':
        r_cnt += 1
    else:
        w_cnt += 1

r_cnt2 = 0
for i in c[:-w_cnt-1:-1]:
    if i == 'R':
        r_cnt2 += 1
w_cnt2 = 0
for i in c[:r_cnt]:
    if i == 'W':
        w_cnt2 += 1

ans = min(w_cnt2, r_cnt2)
print(ans)

Solution (Python):
N = int(input())
c = input()
red = 0
for i in c:
    if i  == "R": red += 1
ans = 0
for i in c[red:]:
    if i == "R": ans += 1
print(ans)

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

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

print( ans )

Solution (Python):

N = int(input())
C = input()
W = []
R = []
for i in range(N):
    if C[i] == 'W':
        W.append(i)
    else:
        R.append(i)
R.reverse()
ans = 0
for i in range(min(len(W), len(R))):
    if W[i] > R[i]:
        break
    ans += 1
print(ans)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    N=int(input())
    c=input().strip("\n")
    cntR=0
    cntW=0
    for i in range(N):
        if c[i]=="R":
            cntR+=1
        else:
            cntW+=1
    ans=N
    
    ans1=cntR
    for i in range(cntR):
        if c[i]=="R":
            ans1-=1
    ans=min(ans,ans1)
    
    print(ans)
    
    
    
if __name__=="__main__":
    main()

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

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    c = list(input())
    r_cnt = 0
    for i in range(n):
        if c[i] == "R":
            r_cnt+=1
    w_cnt = n-r_cnt
    change = 0
    for i in range(r_cnt):
        if c[i] != "R":
            change+=1
    ans = change
    print(ans)

Solution (Python):

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()

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

r = c.count('R')
print(c[:r].count('W'))

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

a = k.count("R")
b = k[:a].count("W")
print(b)

Solution (Python):
n = int(input())
c = input()
f = 0
ans = 0
if 'W' not in c:
    f = 1

r_count = c.count("R")
for i in range(r_count):
    if c[i] == "R":
        ans -= 1

if f == 1:
    print("0")
else:
    print(ans+r_count)

Solution (Python):
def main():
    n=int(input())
    c=input()
    cc=sorted(c)
    count = 0
    for i in range(n):
        if c[i] != cc[i]:
            count += 1
    print(count//2)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    _ = int(rl())
    c = rl().rstrip()
    
    counter = Counter(c)
    red = counter['R']
    white = counter['W']
    
    red -= c[:red].count('R')
    white -= c[-white:].count('W')
    print(min(red, white))


if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque
def resolve():
    N = int(input())
    C = input()
    deq = deque(C)
    count = 0
    while deq:
        if deq[-1]=='W':
            deq.pop()
        if len(deq)==0:
            break

        if deq[0]=='R':
            deq.popleft()
        if len(deq)==0:
            break
        
        if deq[0]=="W" and deq[-1]=='R':
            deq.pop()
            deq.popleft()
            count +=1
    print(count)
resolve()

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

count = 0
if "W" in c and "R" in c:
  left_w = c.find("W")
  right_r = c.rfind("R")
  while left_w < right_r:
    c = c[:left_w] + "R" + c[left_w+1:right_r] + "W" + c[right_r+1:]
    count += 1
    left_w = c.find("W")
    right_r = c.rfind("R")
print(count)

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

rCount=0
for i in range(N):
  if c[i]=='R':
    rCount+=1
wCount=0

for i in range(rCount):
  if c[i]=='W':
    wCount+=1
print(wCount)

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

count_r = 0
for i in range(n):
  if s[i] == "R":
    count_r += 1

ans = 0
for i in range(count_r):
  if s[i] == "W":
    ans += 1
    
print(ans)

Solution (Python):
def main():
    n = int(input())
    c = input()
    l = 0
    r = n - 1
    cnt = 0
    while True:
        ok = True
        while c[l] != "W":
            l += 1
            if l == r:
                ok = False
                break
        if not ok:
            break
        while c[r] != "R":
            r -= 1
            if l == r:
                ok = False
                break
        if not ok:
            break
        cnt += 1
        l += 1
        r -= 1
        if r <= l:
            break
    print(cnt)


if __name__ == "__main__":
    main()

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

d = list(c)

i = 0
j = N - 1
result = 0
while True:
    while i < N and d[i] != 'W':
        i += 1
    while j > 0 and d[j] != 'R':
        j -= 1
    if i == N or j == -1 or i >= j:
        break
    d[i] = 'R'
    d[j] = 'W'
    result += 1
print(result)

Solution (Python):


import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = [int(i) for i in readline().split()]
s = input()


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

ans = 1<<30
for i in range(n+1):
    """
    W..WR..R (Wがi個)
    不純なWがw個
    不純なRがr個
    """
    v = max(w,r)
    ans = min(ans,v)
    
    if i!=n:
        if s[i] == "W":
            w += 1
        else:
            r -= 1
    
    

print(ans)

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

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

print(ans)

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

if not 'R' in s:
    print(0)
    quit()

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)

Solution (Python):





def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

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

def int_list():
    return list(map(int,input().split()))

def print_space(l):
    return print(" ".join([str(x) for x in l]))


import numpy as np




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

cnt = 0
for i in range(0,N):
    if c[i] == "R":
        cnt += 1

cnt2 = 0
for i in range(0,cnt):
    if c[i] == "R":
        cnt2 += 1

print(cnt - cnt2)

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

r = A.count("R")

ans = A[:r].count("W")

print(ans)

Solution (Python):
N=int(input())
s=list(input())
white=s.count("W")
red=s.count("R")
llist=[]
wl=0
wr=white
bl=0
br=red
swap=0
for i in range(len(s)):
    if(s[i]=="W"):
        wr-=1
        if(wl<=br):
            llist.append(br)
        else:
            llist.append(wl)
        wl+=1
    else:
        br-=1
        if(wl<=br):
            llist.append(br)
        else:
            llist.append(wl)
        bl+=1
print(min(llist))

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

ans = 0
lp = 0
rp = n-1
while True:
    if lp >= rp:
        break
    while lp < n and c[lp] != 'W':
        lp += 1
    while rp >= 0 and c[rp] != 'R':
        rp -= 1
    if lp >= rp:
        break
    lp += 1
    rp -= 1
    ans += 1
print(ans)

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

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

cnt = 0
for i in range(r):
  if c[i] == "W":
    cnt += 1

print(cnt)

Solution (Python):
import math

n = int(input())
cL = input()

ans = 0
s = 0
e = n - 1
while True:
    if s >= e:
        break
    if cL[s] == "R":
        s += 1
        continue
    else:
        if cL[e] == "W":
            e -= 1
            continue
        else:
            ans += 1
            s += 1
            e -= 1

print(ans)

Solution (Python):
N = int(input())
S = input()
r_count = S.count('R')
first_w = S.find('W')

result = min(S[first_w:].count('R'), S[:r_count].count('W'))

print(result)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

N = I()
c = LS2()

w = [0]*N
r = [0]*N
for i in range(N):
    if i == 0:
        if c[i] == 'W':
            w[i] = 1
        else:
            r[i] = 1
    else:
        if c[i] == 'W':
            w[i] = w[i-1]+1
            r[i] = r[i-1]
        else:
            w[i] = w[i-1]
            r[i] = r[i-1]+1
a = w[-1]
b = r[-1]
if b == 0:
    print(0)
    exit()
for i in range(N):
    if w[i] == b-r[i]:
        print(w[i])
        exit()

Solution (Python):
def main():
    import collections

    N = int(input())
    stones = input()

    N_red = stones.count('R')
    N_whi = N - N_red

    ans = stones[:N_red].count('W')

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
c=input()
i,j=0,n-1
ans=0
while True:
    if c[i]=="W":
        while True:
            if c[j]=="R":
                ans+=1
                j-=1
                break
            j-=1
            if i>=j:
                break
    i+=1
    if i>=j: break
print(ans)

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

Solution (Python):
n = int(input())
arr = list(input())
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)

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

print(min(chage_count, count_w))

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

cnt = 0
C_sort = sorted(C)
for i in range(N):
    if C[i] != C_sort[i]:
        cnt += 1

print(cnt // 2)

Solution (Python):

n = int(input())
l = list(input()) 

l_r = sorted(l)
r = l.count('R')
w = l.count('W')



dif = 0
for i in range(n):
    if l[i] != l_r[i]:
        dif += 1

print(dif //2 + dif%2)

Solution (Python):
from collections import Counter


N = int(input())
C = input()
C_cntr = Counter(C)

all_R_cnt = 0
all_W_cnt = 0
R_W_cnt = 0
for i in range(N):
    if C[i] == "W":
        all_R_cnt += 1
    elif C[i] == "R":
        all_W_cnt += 1
    if i < C_cntr.get("R", 0) and C[i] == "W":
        R_W_cnt += 1

print(min(all_R_cnt, all_W_cnt, R_W_cnt))

Solution (Python):
n=int(input())
c=input()
c1=c.count("R")
c2=c[:c1].count("W")
print(c2)

Solution (Python):
N = int(input())
ln = input()
n = ln.count('R')
m = ln[:n]
print(m.count('W'))

Solution (Python):
from heapq import heappop,heappush
def main():
    N = int(input())
    S = input()
    W = []
    R = []
    ans = 0
    for i,s in enumerate(S):
        if s == 'W':
            heappush(W,i)
        else:
            heappush(R,-i)
    while (W and R):
        l = heappop(W)
        r = -heappop(R)
        if l < r:
            ans += 1
            heappush(W,r)
            heappush(R,-l)

        else:
            break
    print(ans)



if __name__ == "__main__":
    main()

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

x = c.count('R')
y = c.count('R', 0, x)
print(x-y)

Solution (Python):
import heapq
wl = []
rl = []
n = int(input())
c = input()
for i in range(n):
    if c[i] == "W":
        heapq.heappush(wl,i)
    else:
        heapq.heappush(rl,-i)
cnt = 0

if wl == [] or rl == []:
    print(0)
else:
    while wl[0] < -rl[0]:
        cnt += 1
        heapq.heappop(rl)
        heapq.heappop(wl)
        if wl == [] or rl == []:
            print(cnt)
            exit()
    print(cnt)

Solution (Python):
N = int(input())
c = [None] * N
r_cnt = 0
for n,s in enumerate(input()):
    c[n] = s
    if s == "R":
        r_cnt += 1
w_cnt = 0
for i in range(r_cnt):
    if c[i] == "W":
        w_cnt += 1
print(w_cnt)

Solution (Python):
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]
    
    if w[N-1] == N:
        print(0)
    else:
        for i in range(N-1):
            if w[i] == r[i+1]:
                print(w[i])
                break

Solution (Python):
def main():
    N = int(input())
    c = input()
    cnt = 0
    left = 0
    right = len(c) - 1
    while True:
        while c[right] == "W" and left < right:
            right -= 1
        while c[left] == "R" and left < right:
            left += 1
        if left >= right:
            return cnt
        cnt += 1
        right -= 1
        left += 1
    return cnt

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

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

r = c.count("R")
w = n - r
cnt = 0
ans = 0
for i in c:
    if cnt == r:
        break
    if i == "W":
        ans += 1
    cnt += 1
print(min(r, w, ans))

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


def main():
    s = 0
    l = list(C)
    i, j = 0, N - 1
    while i != j:
        if l[i] == "R":
            i += 1
        else:
            if l[j] == "W":
                j -= 1
            else:
                l[i] = "R"
                l[j] = "W"
                i += 1
                s += 1
    
    ans = min(C.count("R"), s)
    print(ans)
                

if __name__ == "__main__":
    main()

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

ans = 0
i = 0
j = n-1
while j>i:
    if c[i] == 'W' and c[j] == 'R':
        ans+=1
        i+=1
        j-=1
    elif c[i] == 'W' and c[j] == 'W':
        j-=1
    elif c[i] == 'R' and c[j] == 'R':
        i+=1
    else: 
        i+=1
        j-=1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    N = int(input())
    C = list(input().strip())
    last = N
    ans = 0
    for i in range(N):
        if C[i] == 'W':
            has_r = False
            for j in reversed(range(i+1, last)):
                if C[j] == 'R':
                    ans += 1
                    last = j
                    has_r = True
                    break
            if not has_r:
                break
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Solution (Python):
_ = input()
s = input()
s2 = sorted(s)
ans = 0
for i, j in zip(s, s2):
    if i != j: ans += 1

print(ans // 2)

Solution (Python):
def main():
    import collections

    N = int(input())
    stones = input()

    N_red = stones.count('R')
    N_whi = N - N_red

    ans = stones[:N_red].count('W')

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
num_r=s.count('R')
if num_r==n or num_r==0:
    print('0')
    exit()

red=[]
for i in range(n):
    if s[i]=='R':
        red.append(i)
ans = num_r
for j in red:
    if j+1 <=num_r:
        ans-=1
print(ans)

Solution (Python):
n=int(input())
c=list(input())
i,j=0,n-1
ans=0
while 1:
    if i>=j:
        break
    if c[i]=="W" and c[j]=="R":
        c[i]="R"
        c[j]="W"
        ans+=1
    if c[i]!="W":
        i+=1
    if c[j]!="R":
        j-=1
for i in range(n-1):
    if c[i]=="W" and c[i+1]=="R":
        c[i]="R"
        ans+=1
print(ans)

Solution (Python):
n = int(input())
c_str = input()
c_list = []
for i in range(n):
    c_list.append(c_str[i])

count = c_list.count("W")
count -= c_list[-count:].count("W")
print(count)

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

red_cnt = c.count("R")
ret = max( c[:red_cnt].count("W"), c[red_cnt:].count("R"))
print( ret )

Solution (Python):



import sys


def std_in():
    return sys.stdin.readline().strip()


def main():
    k = int(std_in())
    c = std_in()
    counter = 0
    while 1:
        l = c.find("W")
        r = c.rfind("R")
        if l == -1 or r == -1:
            break
        elif l < r:
            c = c[:l] + c[r] + c[l+1: r] + c[l] + c[r+1:]
            counter += 1
        else:
            break
    print(counter)


if __name__ == "__main__":
    main()

Solution (Python):
l = int(input())-1
n = input()+" "
s = 0
ans = 0
a = n[0]
while(s <= l):
  
  while(n[s]=="R" and s <= l):
    s+=1
  while(n[l]=="W" and s <= l):
    l-=1
  if(s-1!=l):ans+=1
  s+=1
  l-=1
print(ans)

Solution (Python):
N=int(input())
c=input()
left=0
right=N-1
ans=0
if all(c[i]=='R' for i in range(N)):
    print('0')
    exit()
if all(c[i]=='W'for i in range(N)):
    print('0')
    exit()
while left<right:
    if c[left]=='R':
        left+=1
    if c[right]=='W':
        right-=1
    if c[left]=='W' and c[right]=='R':
        left+=1
        right-=1
        ans+=1
print(ans)

Solution (Python):
N = int(input())
S = input()
l = 0
r = N - 1
ans = 0
while 1:
    while l < N and S[l] == "R":
        l += 1
    if l >= N: break
    
    while r >= 0 and S[r] == "W":
        r -= 1
    if r < 0: break
    if l > r:
        break
    ans += 1
    l += 1
    r -= 1
print(ans)