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 resolve():
  N = int(input())
  C = [0 if x == "W" else 1 for x in list(input())]
  red_seacher_index = N - 1
  white_seacher_index = 0

  counter = 0
  while white_seacher_index <= red_seacher_index:
    if C[white_seacher_index] == 0:
      
      while white_seacher_index <= red_seacher_index:
        if C[red_seacher_index] == 1:
          C[white_seacher_index] = 1
          C[red_seacher_index] = 0
          counter += 1
          break
        red_seacher_index -= 1
    white_seacher_index += 1
  print(counter)

if __name__ == "__main__":
  resolve()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    c = SI()
    r = [0] * N
    w = [0] * N
    for i in range(N):
        r[i] = r[i-1] + (c[i] == 'R')
        w[i] = w[i-1] + (c[i] == 'W')

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



if __name__ == '__main__':
    main()

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

if "W" not in c or "R" not in c:
  print("0")
  sys.exit()

red = c.count("R")
stay_red = c.count("R", 0, red)
print(red - stay_red)

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

C = input()

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

if len(C) == r or len(C) == w or C[:r].count('R') == r:
    print(0)
else:
    print(C[:r].count('W'))

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    s = list(input())

    i = 0
    j = n
    ans = 0

    while i < n:
        if s[i] == "W":
            while j > i:
                j -= 1
                if s[j] == "R":
                    s[i], s[j] = s[j], s[i]
                    ans += 1
                    break
        i += 1


    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math
from collections import deque
import itertools
import heapq

from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint




def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    
    n=int(input())
    c=list(input())
    count=INFI
    
    count=c.count("R")

    
    temp=c.count("W")
    count=min(count,temp)

    
    top=0
    bot=n-1
    temp=0
    while top<bot:
        while top < bot:
            if c[top] == "R":
                top+=1
            else:
                temp += 1
                break
        while top<bot:
            if c[bot]=="W":
                bot-=1
            else:
                temp += 1
                break

        top+=1
        bot-=1

    temp=temp//2
    count=min(count,temp)
    print(count)



if __name__ == "__main__":

    main()

Solution (Python):
from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw and dr:
    w = dw.popleft()
    r = dr.popleft()
    if w < r:
        ans += 1

    else:
        break
print(ans)

Solution (Python):
import sys 
from collections import Counter
 
N = int(sys.stdin.readline().strip())
C = sys.stdin.readline().strip()

counter = Counter(C)
if len(counter) == 1:
    print(0)
    sys.exit()

limit = counter["R"]
ok = 0
for i in range(limit):
    if C[i] == "R":
        ok += 1

print(limit - ok)

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

rcount =0
wcount =0
for i in range(N):
    if c[i]=="R":
        rcount+=1
    else:
        wcount+=1

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

print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

r_cnt = c.count('R')
w_cnt = c.count('W')

ans = min(r_cnt, w_cnt)

from heapq import heappush, heappop

r_heapq = []
w_heapq = []

for i in range(1, n + 1):
    if c[i - 1] == "R":
        heappush(r_heapq, -i)
    else:
        heappush(w_heapq, i)
cnt = 0
while w_heapq and r_heapq:
    if heappop(w_heapq) < -heappop(r_heapq):
        cnt += 1
ans = min(ans, cnt)
print(max(ans, 0))

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

red=0
for i in c:
    if i=="R":
        red+=1
count = 0
for i in range(red):
    if c[i]=="W":
        count+=1
print(count)

Solution (Python):

n = int(input())
s = list(input())
if s.count("W") == 0:
    print(0)
    exit()
print(s[:s.count('R')].count("W"))

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

count = 0
i = 0
j = 1

while n>i+j:
    if c[i] == 'W' and c[-j] == 'R':
        count += 1
        i += 1
        j += 1
    elif c[i] == 'R':
        i += 1
    elif c[-j] == "W":
        j += 1

        
print(count)

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


sum_r, sum_w = [0], [0]
s1, s2 = 0, 0
for v in c:
    if v == 'R':
        s1 += 1
    else:
        s2 += 1
    sum_r.append(s1)
    sum_w.append(s2)

res = float('inf')
for i in range(n+1):
    res = min(res, max(sum_w[i], s1 - sum_r[i]))

print(res)

Solution (Python):













from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy



MOD = 1000000007
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): 
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): 
    return reduce(math.gcd, numbers)

def combination(m,n): 
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): 
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r = S.count("R")
    Sr = S[0:r]
    w_Sr = Sr.count("W")
    print(w_Sr)
    
main()

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

stones = list(input())

left = 0
right = N - 1

count = 0

while left < right:
    if stones[left] == 'R':
        left += 1
        continue
    while left < right:
        if stones[right] == 'R':
            stones[left], stones[right] = stones[right], stones[left]
            left += 1
            right -= 1
            count += 1
            break
        right -= 1

print(count)

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 

 










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

if S.count("R")==0 or S.count("W")==0:
    print(0)
    sys.exit()

tmp = S.count("R")
mae = S[0:tmp]
tmp2 = mae.count("R")

print(tmp-tmp2)

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



print(c.count("W", 0, c.count("R")))

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

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

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

print(ans)

Solution (Python):
"""
    こんにちは。これはクシャルです
    
    Author : thekushalghosh (クシャル)
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()


    
def solve():
    n = inp()
    s = insr()
    q = s.count("R")
    print(q - s[:q].count("R"))
    


 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
        sys.stdout.close()
    

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 

def counter(a):
    q = [0] * max(a)
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)

def counter_elements(a):
    q = dict()
    for i in range(len(a)):
        if a[i] not in q:
            q[a[i]] = 0
        q[a[i]] = q[a[i]] + 1
    return(q)

def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97],1
    return(q)
 
def factors(n):
    q = []
    for i in range(1,int(n ** 0.5) + 1):
        if n % i == 0: q.append(i); q.append(n // i)
    return(list(sorted(list(set(q)))))
 
def prime_factors(n):
    q = []
    while n % 2 == 0: q.append(2); n = n // 2
    for i in range(3,int(n ** 0.5) + 1,2):
        while n % i == 0: q.append(i); n = n // i
    if n > 2: q.append(n)
    return(list(sorted(q)))

def transpose(a):
    n,m = len(a),len(a[0])
    b = [[0] * n for i in range(m)]
    for i in range(m): 
        for j in range(n): 
            b[i][j] = a[j][i]
    return(b)


        
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
main()

Solution (Python):

import sys

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

ls = reader()
n = int(ls[0][0])
wr =ls[1][0]
nr = len(list(filter(lambda a:a=='R',wr)))
nleftr = len(list(filter(lambda a:a=='R',wr[:nr])))
print(nr-nleftr)

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

cnt_r = 0
for c in C:
  if c == "R":
    cnt_r += 1

cnt_r_in_lefternmost_cnt_r = 0
for c in C[:(cnt_r)]:
  if c == "R":
    cnt_r_in_lefternmost_cnt_r += 1

print(cnt_r - cnt_r_in_lefternmost_cnt_r)

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

stones=input()

variation={"R":0,"W":0}
for i in range(N):
    variation[stones[i]]+=1

num=0
for i in range(variation["R"]):
    if stones[i]=="R":
        num+=1

ans=variation["R"]-num

print(ans)

Solution (Python):

import sys
import math

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
c = input()

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

ans = min(R,W)
ans = min(ans,left.count('W'))

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    C = input().rstrip()

    l = 0
    r = N - 1
    ans = 0
    while True:
        if C[l] == "W" and C[r] == "R":
            ans += 1
            l += 1
            r -= 1
        elif C[l] == "R" and C[r] == "R":
            l += 1
        elif C[l] == "W" and C[r] == "W":
            r -= 1
        else:
            l += 1
            r -= 1

        if not l < r:
            break

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    N=int(sys.stdin.readline())
    C=[1 if c=='R' else 0 for c in sys.stdin.readline().strip()]
    R=C.count(1)
    print(C[:R].count(0))

main()

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

Solution (Python):
N = int(input())
c = list(input())
from collections import Counter
dic = Counter(c)
left = c[:dic["R"]]
print(left.count("W"))

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

Solution (Python):
N = int(input())
c = list(input())
counter = 0
left = 0
right = N-1

while left < right:
    if c[left] == 'W':
        flag = True
        while flag:
            if left >= right:
                flag = False
            if c[right] == 'R':
                c[right] = 'W'
                c[left] = 'R'
                counter += 1
                right -= 1
                flag = False
            else:
                right -= 1
    left += 1

print(counter)

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

l = 0
r = n - 1

ans = 0

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

Solution (Python):



def solve(cs):
    num_w = cs.count("W")
    num_r = cs.count("R")
    ref = "R" * num_r + "W" * num_w
    num_mismatches = sum(c != d for c, d in zip(cs, ref))
    res = num_mismatches // 2
    return res


def main():
    n = int(input())
    cs = input()
    res = solve(cs)
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = list(input())
R = [0]*N
W = [0]*N
for i in range(N):
    if S[i]=="R":
        R[i]+=1
    else:
        W[i]+=1

sumR = sum(R)

ans = sumR-sum(R[:sumR])

print(ans)

Solution (Python):
n=int(input()) 
s=input()
a=[]
r,w=0,0
ans=float("inf")
for i in range(n):
    if s[i]=="R":
        r+=1
    else:
        w+=1
    a.append([r,w])
if r==n or w==n:
    print(0)
    exit()
for i in range(n):
    r1,w1=a[i][0],a[i][1]
    r2,w2=r-r1,w-w1
    t=min(w1,r2)
    tmp=t+max(w1-t,0)+max(r2-t,0)
    if tmp<ans:
        ans=tmp
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
C = input()
c_c = Counter(C)
w = c_c['W']
r = c_c['R']
ans = 0
for i in range(r):
    if C[i] == 'W':
        ans += 1
print(ans)

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

W = [0 for _ in range(N)]
R = [0 for _ in range(N)]

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

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

for i, ch in enumerate(C):
    R[i] = r
    if ch == "W":
        w += 1
    else:
        r -= 1
    W[i] = w

ans = N
for i in range(N-1):
    ans = min(ans, max(W[i], R[i+1]))
print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

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

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

R_cnt = S.count("R")
W_cnt = S.count("W")

if W_cnt == 0:
    print(0)
    exit()

answer = 0
for s in S:
    if R_cnt == 0:
        print(answer)
        exit()
    if s == "W":
        answer += 1
    R_cnt -= 1

Solution (Python):
def resolve():
    n = int(input())
    c = input()
    ans = 0
    l = -1
    r = n
    while True:
        l += 1
        r -= 1
        while l < n and c[l] == "R":
            l += 1
        while r >= 0 and c[r] == "W":
            r -= 1
        if l > r:
            break
        ans += 1
    print(ans)


if __name__ == '__main__':
    resolve()

Solution (Python):
N = int(input())
string = input()
r2w = string.count("R")
w2r = 0
ans_list = [max(r2w, w2r)]
for c in string:
    if c == "W":
        w2r += 1
    else:
        r2w -= 1
    ans_list.append(max(r2w, w2r))
print(min(ans_list))

Solution (Python):
N, = map(int, input().split())
X = input().strip()
i,j = 0,N-1
R = 0
while i<=j:
    if X[i] == "R":
        i += 1
        continue
    if X[j] == "W":
        j -= 1
        continue
    R += 1
    i += 1
    j -= 1
print(R)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
c = input()
d = c.count('R')
ans = d
for i in range(d):
    if c[i] == 'R':
        ans -= 1
print(ans)

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



res = min(pref[-1][0], pref[-1][1])
for i in range(1, n):
	
	rem = i - pref[i-1][1]
	ans = rem
	

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

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

	res = min(res, ans)

print(res)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()



def main():
    N = int(input())
    S = input()

    table = [0] * (N + 1)

    if len(set(S)) == 1:
        print(0)
        return
    

    for i in range(1, N + 1):
        if S[i - 1] == "R":
            table[i] = table[i - 1] + 1
        else:
            table[i] = table[i - 1]

    ans = float("inf")

    for i in range(N):
        left_red = table[i]
        left_white = i - table[i]
        right_red = table[N] - table[i]
        right_white = N - i - right_red
        
        ans = min(ans, max(left_white, right_red))

    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):


n = int(input())
c_l = input()

ans = 0
l = 0
r = n
while True:
    _l = c_l.find("W", l, n-1)
    _r = c_l.rfind("R", 0, r)
    if _l >= _r or _l == -1 or _r == -1:
        break
    ans += 1
    l = _l + 1
    r = _r

print(ans)

Solution (Python):
N = int(input())
C = input()
cnt = 0
left = 0
right = N-1
while(left < right and left<=N-1):
  if(C[left] == 'W'):
    flag = 0
    while(left < right):
      if(C[right] == 'R'):
        cnt += 1
        right -= 1
        break
      right -= 1
  left += 1
print(cnt)

Solution (Python):
n = int(input())
c = input()
count = 0
for i in range(n):
    if c[i] == "R":
        count += 1
count2 = 0
for i in range(count):
    if c[i] == "R":
        count2 += 1
print(count - count2)

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

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

cntR = C.count('R')
cntW = C.count('W')
if cntR == 0 or cntW == 0:
    print(0)
else:
    T = 'R' * cntR + 'W' * cntW
    ans = sum([1 for a, b in zip(C, T) if a != b]) // 2
    print(ans)

Solution (Python):
def main():
    N = int(input())
    C = list(input())
    ans = 0
    red = C.count('R')
    white = C.count('W')
    
    cnt = 0
    for i in range(red):
        if C[i] == 'W':
            cnt += 1

    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
c = list(input())
r = c.count('R')
r2 = c[:r].count('R')
print(r - r2)

Solution (Python):
n=int(input())
C=input()
rc=C.count('R')
x=C.count('R',0,rc)
print(rc-x)

Solution (Python):
n = int(input())
c = list(input())
w, r = [0] * (n + 1), [0] * (n + 1)
for i in range(n):
    if c[i] == "W":
        w[i + 1] = w[i] + 1
        r[i + 1] = r[i]
    else:
        w[i + 1] = w[i]
        r[i + 1] = r[i] + 1
ans = min(w[n], r[n])
for i in range(1, n):
    x = w[i] - w[0]
    y = r[n] - r[i]
    ans = min(ans, x + abs(x - y))
print(ans)

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

left = 0
right = N-1

ans = 0

while left < right:
    while color[left] == "R":
        left += 1
        if left >= N:
            break

    while color[right] == "W":
        right -= 1
        if right < 0:
            break
    
    if left < right:
        ans += 1
        left += 1
        right -= 1

print(ans)

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

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

t = stones.count('R')

num_not_equal = t
red_num_diff = t
red_count = 0

ans = t
for s in stones:
    if s == 'R':
        num_not_equal -= 1
    else:
        num_not_equal += 1

    red_count += 1
    red_num_diff = abs(red_count - t)

    temp = red_num_diff + (num_not_equal - red_num_diff) // 2
    ans = min(ans, temp)

    

print(ans)

Solution (Python):
def main():
    n = int(input())
    c = input()
    w = c.count("W")
    ans = 0
    r = n - w
    for i in range(n):
        target = "R" if i < r else "W"
        if c[i] != target:
            ans += 1
    print(min(ans // 2, w, r) if 0 < w < n else 0)


if __name__ == '__main__':
    main()

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

count_r = 0
count_w = 0
R = []
W = []
for i in range(n):
    if c[i:i+1] == "R":
        count_r += 1
        R.append(i)
    else:
        count_w += 1
        W.append(i)

ans_count = 0
while count_r * count_w:
    if  ans_count+1 > count_r or ans_count+1 > count_w:
        break
    if R[-(1+ans_count)] < W[ans_count]:
        break
    ans_count += 1

print(min(ans_count,count_r,count_w))

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

Solution (Python):
n = int(input())
c = input()
r = c.count("R")
w = 0
a = r
for ci in c:
    if ci == "R":
        r -= 1
    elif ci == "W":
        w += 1
    a = min(max(w, r), a)
print(a)

Solution (Python):

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

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

W=0
R=0

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

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

print(leftW)

Solution (Python):
n=int(input())
lc=list(input())
r=lc.count("R")
w=lc.count("W")

l=["R" for i in range(r)]+["W" for i in range(w)]

p=0
q=0
for i in range(r):
    if l[i]!=lc[i]:
        p+=1
for i in range(w):
    if l[r+i]!=lc[r+i]:
        q+=1
m=max(p,q)
print(min(m,r,w))

Solution (Python):
def main():
    N = int(input())
    C = list(input())
    ans = 0
    w, r = 0, N - 1
    while True:
        while w < N and C[w] != 'W':
            w += 1
        if w == N:
            break
        while r >= 0 and C[r] != 'R':
            r -= 1
        if r == -1:
            break
        if r < w:
            break
        C[w] = 'R'
        C[r] = 'W'
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
C = input()
chk = 0
for i in range(n):
    if C[n-1-i] == 'R':
        chk = n-1-i
        break
cnt = 1
for i in range(chk):
    if C[i] == 'R':
        cnt += 1
ans = 0
for i in range(cnt):
    if C[i] == 'W':
        ans += 1
if 'R' not in C:
    print(0)
else:
    print(ans)

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

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

red = C.count('R')
white = C.count('W')

left = C[:red]
ans = left.count('W')

print(ans)

Solution (Python):
N = int(input())
S = input()
WPrefix = [0]
RSuffix = [0]
for elem in S:
    if elem == "R":
        RSuffix[0] += 1
for elem in S:
    if elem == "R":
        RSuffix.append(RSuffix[-1] - 1)
        WPrefix.append(WPrefix[-1])
    else:
        RSuffix.append(RSuffix[-1])
        WPrefix.append(WPrefix[-1] + 1)
minSwap = len(S)
for index in range(len(RSuffix)):
    if max(RSuffix[index],WPrefix[index]) < minSwap:
        minSwap = max(RSuffix[index],WPrefix[index])
print(minSwap)

Solution (Python):
from collections import Counter

(N,) = [int(x) for x in input().split()]
S = list(input())
freq = Counter(S)
count = 0
for i in range(freq["R"]):
    if S[i] != "R":
        count += 1
print(count)

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

r = 0
w = 0

for i in range(N):
    if C[i] == 'R':
         r += 1
    else:
         w += 1

s = 0

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

print(s)

Solution (Python):
def main():
  n = int(input())
  s =  str(input())
  num_r = 0
  for i in range(n):
    if s[i] == "R":
      num_r += 1
  ans = 0
  for i in range(num_r):
    if s[i] == "W":
      ans += 1
  print(ans)

if __name__ == "__main__":
  main()

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

iRs = []
iWs = []
for i in range(N):
    i_inv = N - 1 - i
    if c[i] == 'W':
        iWs.append(i)
    if c[i_inv] == 'R':
        iRs.append(i_inv)
iRs.append(-1)
iWs.append(N)

ans = 0
for i in range(min(len(iRs), len(iWs))):
    iW = iWs[i]
    iR = iRs[i]
    
    if iW >= iR:
        break
    else:
        ans += 1

print(ans)

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

cr = c.count('R')
cnt = 0
for i, ci in enumerate(c):
    if ci == 'R' and i >= cr:
        cnt += 1

print(min([cr, n-cr, cnt]))

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

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

Solution (Python):

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

import math
n = readint()
c = readstr()

r = c.count('R')
w = c.count('W')
ans = 0
for i in range(r):
    if c[i]=='W':
        ans += 1
for i in range(r+1,n):
    if c[i]=='R':
        ans += 1
print(math.ceil(ans/2))

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

Solution (Python):
n = int(input())
c = list(input())
schw = 0
schr = n-1
ans = 0
while schw < schr:
    while c[schw] != 'W' and schw < n-1:
        schw += 1
    while c[schr] != 'R' and schr > 0:
        schr -= 1
    ans += 1
    c[schw] = 'R'
    c[schr] = 'W'
print(ans - 1)

Solution (Python):
def next_white(s, w, n):
    w += 1
    while s[w] != 'W':
        w += 1
        if w > n - 1:
            return n
    return w


def next_red(s, r,n):
    r -= 1
    while s[r] != 'R':
        r -= 1
        if r < 0:
            return -1
    return r


n = int(input())
s = input()
white = -1
red = n
ans = -1
while red > white:
    ans += 1
    white = next_white(s, white, n)
    red = next_red(s, red, n)
print(ans)

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

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

Solution (Python):

n=int(input())
c=input()
w=0
ch=[0 for _ in range(n)]

for i in range(n):
    if c[i]=="W":
        ch[i]=1
        w+=1
print(sum(ch[:n-w]))

Solution (Python):
n = int(input())
n += 4
s = str(input())
s = "WR" + s + "WR"
lis = []
num = 1
for i in range(n-1):
    if s[i] != s[i+1]:
        lis.append(num)
        num = 1
    else:
        num += 1
lis.append(num)
  
W = 2
R = len(lis)-3

ans = 0
while W < R:
    if lis[W] > 0 and lis[R] > 0:
        lis[R] -= 1
        lis[W] -= 1
        ans += 1
    else:
        if lis[W] == 0:
            W += 2
        if lis[R] == 0:
            R -= 2
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]





N = ii()
S = input()

num_r = S.count('R')
ans = num_r

for i in range(num_r):
    if S[i] == 'R':
        ans -= 1

print(ans)