Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

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

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

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

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

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

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3



Sample input:
4
WWRR


Sample output:
2

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
s1 = [0]
for i in range(n):
    if c[i] == 'W':
        s1.append(s1[-1] + 1)
    else:
        s1.append(s1[-1])

s2 = [0]
for i in range(n):
    if c[i] == 'R':
        s2.append(s2[-1] + 1)
    else:
        s2.append(s2[-1])
s2.reverse()
ans = 1001001001
for i in range(n+1):
    ans = min(ans, s1[i] + s2[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    B = sorted(C)
    for c in B:
        if c == 'R':
            red_cnt += 1
        else:
            break
    for i in range(N):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in sorted(C):
        if c == 'R':
            red_cnt += 1
        else:
            break
    for i in range(N):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in C:
        if c == 'R':
            red_cnt += 1
    for i in range(N):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in C:
        if c == 'R':
            red_cnt += 1
    for i in range(N):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# ABC174 D
import sys
sys.setrecursionlimit(3*10**5)

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

def search(s):
    if s=='':
        return 0
    if s[0]=='R':
        return search(s[1:])
    if s[-1]=='W':
        return search(s[:-1])
    return search(s[1:-1])+1
print(search(S))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
A=[]
for i in range(n):
  A.append(s[i])
ans=float('inf')
for i in range(n+1):
  w=A[:i].count('W')
  r=A[i:].count('R')
  ans=min(max(w,r),ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
count = 0
for i in range(N):
    if C[N-i-1] == 'R':
        for j in range(N-i-1):
            if C[j] == 'W':
                C[N-i-1] = 'W'
                C[j] = 'R'
                count += 1
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
C = list(input())
ans = 0
for l in range(n):
    if C[l] == "W":
        for r in range(n-1,0,-1):
            if r<=l:
                print(ans)
                exit()
            if C[r] == "R":
                #print(C)
                C[l] = "R"
                C[r] = "W"
                #print(C)
                #print("-----------------")
                ans += 1
                break
print(ans)

Submission status: Time Limit Exceeded

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

tmp = n
for i in range(n + 1):
    W = c[0 : i].count("W")
    R = c[i + 1: n].count("R")
    if tmp >= max([W ,R]):
        tmp = max([W, R])
print(tmp)

Submission status: Time Limit Exceeded

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

while c.find('WR') != -1:
    c = c[c.find('W')+1:c.rfind('R')]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

while c.find('WR') != -1:
    leftW = c.find('W')
    rightR = c.rfind('R')
    c = c[leftW+1:rightR]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

while c.count('WR') >= 1:
    leftW = c.find('W')
    rightR = c.rfind('R')
    if leftW != -1 and rightR != -1:
        c = c[leftW+1:rightR]
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = input()
cc = c
ans = 0

while cc.count('WR') >= 1:
    leftW = cc.find('W')
    rightR = cc.rfind('R')
    if leftW != -1 and rightR != -1:
        cc = cc[:leftW] + 'R' + cc[leftW+1:rightR] + 'W' + cc[rightR+1:]
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

i=0
listNum=[]

for i in range(N):
    w=c.count("W",0,i)
    s=c.count("R",i+1,N)
    listNum.append(max(w,s))
    i+=1
print(min(listNum))

Submission status: Time Limit Exceeded

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

i=0
listNum=[]

while i<=N:
    w=c.count("W",0,i)
    s=c.count("R",i+1,N)
    listNum.append(max(w,s))
    i+=1
print(min(listNum))

Submission status: Time Limit Exceeded

Solution code:
import re

N = input()
stones = input()
pattern = 'W.*R'
#stoneslist =list(stones)
#result = re.search(pattern,stones)
#if result:
 # print (result.start())
  #print (result.end())
#print(stones[result.start()])
#stoneslist[result.start()]='R'
#stoneslist[result.end()-1]='W'


count =0
while 'WR' in stones:
  if stones.count('WR')>1:
    stoneslist =list(stones)
    result = re.search(pattern,stones)
    stoneslist[result.start()]='R'
    stoneslist[result.end()-1]='W'
    #stones =str(stoneslist)
    stones = "".join(stoneslist)
  else:
    stones = stones.replace('WR','RR',1)
  count+=1
  #print(stones)

#print (stones)
print (count)

Submission status: Time Limit Exceeded

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

cnt = 0
while 'WR' in c:
    w = c.find('W')
    r = c.rfind('R')
    c = c[:w]+'R'+c[w+1:r]+'W'+c[r+1:]
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = float("inf")
for i in range(N+1):
  w = C[:i].count("W")
  r = C[i:].count("R")
  ans = min(ans, max(w,r))
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# sys.stdin.readline()
import sys
input = sys.stdin.readline
import numpy

n=int(input())
list=[]
ant=[]
str=input()
wnum=0
rnum=0
for i in range(n):
  list.append(str[i])
  if str[i]=="W":
    wnum+=1
  else:
    rnum+=1
n1=-5
n2=-5
k=0
ant=list.copy()
ant.reverse()
list2=numpy.asarray(list)
ant2=numpy.asarray(ant)

if wnum!=0 and rnum!=0:
  for i in range(8000):
    n1=numpy.where(list2=="W")[0][0]
    n2=n-1-numpy.where(ant2=="R")[0][0]
    if n1<n2:
      list2[n2],list2[n1]=list2[n1],list2[n2]
      ant2[n-1-n2],ant2[n-1-n1]=ant2[n-1-n1],ant2[n-1-n2]   
    else:
      print(i)
      break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
# sys.stdin.readline()
import sys
input = sys.stdin.readline

n=int(input())
list=[]
ant=[]
str=input()
wnum=0
rnum=0
for i in range(n):
  list.append(str[i])
  if str[i]=="W":
    wnum+=1
  else:
    rnum+=1
n1=-5
n2=-5
k=0
ant=list.copy()
ant.reverse()
if wnum!=0 and rnum!=0:
  for i in range(8000):
    n1=list.index("W")
    n2=n-1-ant.index("R")
    if n1<n2:
      list[n2],list[n1]=list[n1],list[n2]
#      ant[n-1-n2],ant[n-1-n1]=ant[n-1-n1],ant[n-1-n2]
      ant=list.copy()
      ant.reverse()
    else:
      print(i)
      break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
list=[]
ant=[]
str=input()
wnum=0
rnum=0
for i in range(n):
  list.append(str[i])
  if str[i]=="W":
    wnum+=1
  else:
    rnum+=1
ant=list.copy()
ant.reverse()
n1=-5
n2=-5
k=0
if wnum!=0 and rnum!=0:
  for i in range(200000):
    n1=list.index("W")
    n2=n-1-ant.index("R")
    if n1<n2:
      list[n2],list[n1]=list[n1],list[n2]
      ant[n-1-n2],ant[n-1-n1]=ant[n-1-n1],ant[n-1-n2]
    else:
      print(i)
      break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
list=[]
ant=[]
str=input()
wnum=0
rnum=0
for i in range(n):
  list.append(str[i])
  ant.append(str[n-1-i])
  if str[i]=="W":
    wnum+=1
  else:
    rnum+=1
n1=-5
n2=-5
k=0
if wnum!=0 and rnum!=0:
  for i in range(800000):
    n1=list.index("W")
    n2=n-1-ant.index("R")
#    print(list)
#    print(ant)
#    print(n1,n2)
    if n1<n2:
      list[n2],list[n1]=list[n1],list[n2]
      ant[n-1-n2],ant[n-1-n1]=ant[n-1-n1],ant[n-1-n2]
#      print(list)
#      print(ant)
    else:
      print(i)
      break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
n=input()
k=list(input())
wc=k.count('W')
rc=k.count('R')
count=0
if wc==0 or rc==0:
  print(0)
  exit()
while True:
  wi=k.index('W')
  l=[i for i, x in enumerate(k) if x == 'R']
  if max(l)>wi:
    k[max(l)],k[wi]=k[wi],k[max(l)]
    count+=1
  else:
    print(count)
    exit()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
sl=0
sr=N
z=0
while(sl<sr):
    if S[sl]=='W':
        for j in reversed(range(sl,sr)):
            if S[j]=='R':
                sr=j
                z+=1
                break
    sl+=1
print(z)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    pedras = input()

    r = 0
    st1 = ''
    for p in pedras:
        if p == 'R':
            r += 1
            st1 += '1'
        else:
            st1 += '0'

    st2 = '1'*r + '0'*(n - r)

    resp = 0

    for i in range(n):
        if st1[i] == '1' and st2[i] == '0':
            resp += 1

    print(resp)
main()

Submission status: Time Limit Exceeded

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

ans = N
for i in range(N + 1):
    w = s[0:i].count("W")
    r = s[i:N].count("R")
    ans = min(ans, max(w, r))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
count = 0

while 'WR' in s:
    w_i = s.find('W')
    r_i = s.rfind('R')
    s = s[:w_i] + 'R' + s[w_i + 1:r_i] + 'W' + s[r_i + 1:]
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
count = 0

while 'WR' in s:
    w_i = s.find('W')
    r_i = s.rfind('R')
    s = s[:w_i] + 'R' + s[w_i + 1:]
    s = s[:r_i] + 'W' + s[r_i + 1:]
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

def checkFunction(c):
    # print(c)
    c = re.sub('W+$','',c)
    c = re.sub('^R+','',c)
    if (not 'WR' in c):
        return 0
    if c.index('R') + 1 != len(c):
        c2 = c.replace('WR', 'RR',1)[:-1]
        return checkFunction(c2) + 1
    return 1


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

print(checkFunction(c))

Submission status: Time Limit Exceeded

Solution code:
import re

def checkFunction(c):
    # print(c)
    c = re.sub('W+$','',c)
    c = re.sub('^R+','',c)
    if (not 'WR' in c):
        return 0
    if c.index('WR') + 2 != len(c):
        c2 = c.replace('WR', 'RR',1)[:-1]
        return checkFunction(c2) + 1
    return 1


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

print(checkFunction(c))

Submission status: Time Limit Exceeded

Solution code:
import re

def checkFunction(c):
    c = re.sub('W+$','',c)
    if (not 'WR' in c):
        return 0
    if c.index('WR') + 2 != len(c):
        c2 = c.replace('WR', 'RR',1)[:-1]
        return checkFunction(c2) + 1
    return 1

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

print(checkFunction(c))

Submission status: Time Limit Exceeded

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

C_l = []
ans = []
for _ in range(N):
    w = C_l.count("W")
    r = C.count("R")
    ans.append(max(w, r))
    C_l.append(C.pop(0))
w = C_l.count("W")
r = C.count("R")
ans.append(max(w, r))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
 
def a():
    N = int(input())
    c = list(str(input()))
    l = N-1
    s = 0
 
    if not ('W' in c) or not ('R' in c):
        print(0)
        return
 
 
    for i in range(int(N)):        
        if c[i] == 'W':
            for j in range(l,i,-1):
                if c[j] == 'R':
                    l = j-1
                    s = s+1
                    break
        if l < i:
            break
    print(s)
if __name__ == "__main__":
    a()

Submission status: Time Limit Exceeded

Solution code:
import re
N=int(input())
c = (input())
count=0
while len(re.findall("WR",c)) !=0:
    W_early= W_early= c.find('\WR') if c.find('\WR') >0 else 0
    R_last=c.rfind('WR')
    c=list(c)
    c[W_early]="R"
    c[R_last+1]="W"
    c="".join(c)
    #print(c)
    count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
N=int(input())
c = (input())
count=0
while len(re.findall("WR",c)) !=0:
    W_early= c.find('\WR') if c.find('\WR') >0 else 0
    R_last=c.rfind('WR')
    c=list(c)
    c[W_early]="R"
    c[R_last+1]="W"
    c="".join(c)
    #print(c)
    count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
N=int(input())
c = (input())
count=0
while len(re.findall("WR",c)) !=0:
    W_early=max(c.find('\WR'),0)
    R_last=c.rfind('WR')
    c=list(c)
    c[W_early]="R"
    c[R_last+1]="W"
    c="".join(c)
    #print(c)
    count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main() -> None:

    n = int(input())
    s = input()
    ans = n
    for i in range(n + 1):
        W = s[:i].count('W')
        R = s[i:].count('R')
        tmp = max(W, R)
        ans = min(ans, tmp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def exchange(str_list: list, n: int) -> None:
    exc_index_1, exc_index_2 = 0, 0
    for i in range(n):
        if str_list[i] == 'W':
            exc_index_1 = i
            break
    for j in range(n):
        if list(reversed(str_list))[j] == 'R':
            exc_index_2 = n - j - 1
            break
    str_list[exc_index_1] = 'R'
    str_list[exc_index_2] = 'W'

def main() -> None:

    n = int(input())
    s = input()
    str_list = [x for x in s]
    ans = 0
    while 'WR' in s:
        ans += 1
        exchange(str_list, n)
        s = ''.join(str_list)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = eval(input())
code = input()
count = 0

for i in range(N):
    if code[i] == 'W':
        for j in range(N-1, i, -1):
            if code[j] == 'R':
                code = code[:i] + 'R' + code[i+1:j] + 'W' + code[j+1:]
                count += 1
                break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
c=input().replace('W','1').replace('R','0')
c=[int(x) for x in c]
count=0

if sum(c)!=0 and sum(c)!=N:
    while True:
        w=c.index(1)
        r=N-c[::-1].index(0)-1
        if w>r:
            break
        count+=1
        c[w]=0 ; c[r]=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
#print(c)
num=0
for i in range(N):
    if c[N-1-i]=='R':
        #print('1',i+1)
        for u in range(N):
            if c[u]=='W' and u<N-1-i:
                #print('2',u+1)
                c[N-1-i]='W'
                c[u]='R'
                num+=1
                break
print(num)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

c=list(input())

def split(c,i):
    left=c[:i]
    right=c[i:]
    return left,right

minimum=len(c)

for i in range(0,len(c)+1):
    left,right=split(c,i)
    W=left.count('W')
    R=right.count('R')
    min_i=max(W,R)
    if min_i < minimum:
        minimum=min_i

print(minimum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list = list(input())
lisr_r = list[::-1]
list_len = len(list)
ans = 0
count = 0

while count < n:
  if 'W' not in list or 'R' not in list:
    break

  w = list.index('W')
  r = lisr_r.index('R')

  if w > list_len - 1 - r:
    break

  list[w] = 'R'
  lisr_r[r] = 'W'

  ans += 1
  count += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list = list(input())
lisr_r = list[::-1]
list_len = len(list)
ans = 0

while True:
  if 'W' not in list or 'R' not in list:
    break

  w = list.index('W')
  r = lisr_r.index('R')

  if w > list_len - 1 - r:
    break

  list[w] = 'R'
  lisr_r[r] = 'W'

  ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list = list(input())
list_len = len(list)
ans = 0

while True:
  if 'W' not in list or 'R' not in list:
    break

  w = list.index('W')
  r = list_len - 1 - list[::-1].index('R')

  if w > r:
    break

  list[w] = 'R'
  list[r] = 'W'

  ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
string = input()
count = []
for i in range(n+1):
    left = string[:i]
    right = string[i:]
    tmp_max = max(left.count("W"), right.count("R"))
    count.append(tmp_max)
print(min(count))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
c=list(input())
le=len(c)
count=0

while 'WR' in ''.join(c):
    count+=1
    w=c.index('W')
    r=le-c[::-1].index('R')-1
    c[w]='R' ; c[r]='W'

print(count)

Submission status: Time Limit Exceeded

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

if "W" not in c:
  print(0)
  exit()

elif "R" not in c:
  print(0)
  exit()

w = c.index("W")
r = n-1 - c[::-1].index("R")

cnt = 0
while w < r:
  c[w],c[r] = c[r],c[w]
  w = c.index("W")
  r = n-1 - c[::-1].index("R")
  cnt += 1
else:
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n  = int(input())

stone = input()

count = 0
w = n-1
wnum = 0
r = 0
rnum = 0

for k in range(n):
    for i in range(wnum,n):
        if stone[i] == "W":
            w = i
            wnum = i + 1
            break

    for i in range(rnum,n):
        if stone[n-1-i] == "R":
            r = n-1-i
            rnum = i + 1
            break
    #print (w,r,count)
    if w < r:
        stone=stone[:w] + "R" +stone[w+1:r]+ "W" +stone[r+1:]  
        count += 1
        #print(stone)
        
    else:
        break
print (count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = input()
cnt =0
maxlen = len(C)
while( "WR" in C ):
    cnt+=1
    tmp = C.index('W')
    tmp2 = C.rindex('R')
    tmp3 = ""
    tmp4 = ""
    tmp5 = ""

    if tmp != 0:
      tmp3 = C[0:tmp-1]
    if tmp2 != maxlen-1:    
      tmp5 = C[tmp2+1:maxlen]
    if tmp2-tmp > 1:
      tmp4 = C[tmp+1:tmp2]
      
    C = tmp3+"R"+ tmp4 +"W"+ tmp5

    
print(cnt)

Submission status: Time Limit Exceeded

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

def check(O):
    idx = [i for i, s in enumerate(O) if s == 'R' and i != 0]
    for i in idx:
        if O[i-1] == 'W':
            return False
    return True

def swap(O, t1, t2):
    L = [x for x in O]
    tmp = L[t1]
    L[t1] = L[t2]
    L[t2] = tmp
    return ''.join(L)

count = 0
while not check(O):
    ridx = [i for i, s in enumerate(O) if s == 'R']
    wrdx = [i for i, s in enumerate(O) if s == 'W']
    O = swap(O, max(ridx), min(wrdx))
    count += 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C=list(input())
X=[0]*(N+1)
for i in range(N+1):
  W=(C[:i].count('W'))
  R=(C[i:].count('R'))
  X[i]=max(W,R)
print(min(X))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      if L==N-1-R:
        break
      if C[i]=='W':
        type=set(C[i+1:N-R])
        if 'R' in type:
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              R+=ii+1
              break
            if L==N-1-R:
              break
        else:
          break
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      if L==N-1-R:
        break
      if C[i]=='W':
        type=set(C[i+1:])
        if 'R' in type:
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              R+=ii+1
              break
            if L==N-1-R:
              break
        else:
          break
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      if L==N-1-R:
        break
      if C[i]=='W':
        for ii in range(N-L-R):
          if C[N-1-R-ii]=='R':
            C[L]='R'
            C[N-1-R-ii]='W'
            X+=1
            R+=ii+1
            break
          if L==N-1-R:
            break
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C=list(input())
L=0
R=0
X=0
for i in range(N):
  L=i
  if L==N-1-R:
    break
  if C[i]=='W':
    for ii in range(N-L-R):
      if C[N-1-R-ii]=='R':
        C[L]='R'
        C[N-1-R-ii]='W'
        X+=1
        R+=ii+1
        if L==N-1-R:
          break
        break
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = list(input())
es = [0,N,0]

def p(X):
    print(X)

def list_change(X):
    for i in range(N):
        if X[i] == 'W':
            es[0] = i
            for j in reversed(range(es[1])):
                if X[j] == 'R':
                    es[1] = j
                    if i<j :
                        X[i] = 'R'
                        X[j] = 'W'
                        es[2] +=1
                        break
                elif j == 0:
                    break


list_change(X)
p(es[2])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = list(input())
es = [0,N,0]

def p(X):
    print(X)

def list_change(X):
    for i in range(N):
        if X[i] == 'W':
            es[0] = i
            for j in reversed(range(es[1])):
                if X[j] == 'R':
                    es[1] = j
                    if i<j :
                        X[i] = 'R'
                        X[j] = 'W'
                        es[2] +=1
                        break
                   


list_change(X)
p(es[2])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = list(input())
es = [0,N,0]

def p(X):
    print(X)

def list_change(X):
    for i in range(N):
        if X[i] == 'W':
            es[0] = i
            for j in reversed(range(N)):
                if X[j] == 'R':
                    es[1] = j
                    if i<j :
                        X[i] = 'R'
                        X[j] = 'W'
                        es[2] +=1
                        break
                   


list_change(X)
p(es[2])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=list(input())
m=float('inf')
for i in range(n+1):
  x=max(c[:i].count('W'),c[i:].count('R'))
  if m>x:
    m=x
print(m)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=list(input())
l=[]
for i in range(n+1):
  l.append(max(c[:i].count('W'),c[i:].count('R')))
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
c = input()
ans = [-1] * (N+1)

for i in range(0, N+1):
  l = c[:i]
  r = c[i:]
  R = l.count("W")
  W = r.count("R")
  ans[i] = max(R, W)

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
c = input()
ans = []

for i in range(0, N+1):
  p = i  #仕切り
  l = c[:p]
  r = c[p:]
  R = l.count("W")
  W = r.count("R")
  ans.append(max(R,W))

print(min(ans))

Submission status: Time Limit Exceeded

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

cnt = 0
lst = list(s)

for i in range(N-2):
    if s[i:i+2] == 'WR':
        for j in range(i+2,N-1):
            if s[j:j+2] == 'RW':
                lst[i], lst[j] = lst[j], lst[i]
                s = ''.join(lst)
                cnt += 1
                continue

for i in range(N-1):
    if s[i:i+2] == 'WR':
        lst[i+1] = 'W'
        s = ''.join(lst)
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
row = input()
L = [""]*N
for i in range(N):
  L[i] = row[i]

count = 0
while(1):
  while(1):
    if len(L) == 0:
      break
    if L[0] == "R":
      del L[0]
    elif L[-1] == "W":
      del L[-1]
    else:
      break
      
  if len(L) == 0:
    break
  else:
    left = L[0]
    right = L[-1]
    L[0] = right
    L[-1] = left
    count = count + 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N= int(input())
state = input()

newlist = sorted(state)
goalstate = "".join(newlist)

uni = 0
for l in range(len(goalstate)):
    if goalstate[l] == "W":
        uni = l
        break

def bfs(state,goalstate):
    cur_state = deque([state])
    cou = 0
    while N + 1:
        cur = cur_state.popleft()
        if cur == goalstate:
            return cou
            break
        new= list(cur)
        temp = 0
        for i in range(len(goalstate)):
            if new[i] == "W":
                temp = i
                break
        neow= 0
        for j in range(uni-1,len(goalstate)):
            if new[j] == "R":
                neow = j
                break
        tempstr = new.pop(temp)
        temp1str = new.pop(neow-1)
        new.insert(neow,tempstr)
        new.insert(temp,temp1str)
        nowstr = "".join(new)
        cur_state.append(nowstr)
        cou += 1


goal = bfs(state, goalstate)
print(goal)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N= int(input())
state = input()

newlist = sorted(state)
goalstate = "".join(newlist)

uni = 0
for l in range(len(goalstate)):
    if goalstate[l] == "W":
        uni = l
        break

def bfs(state,goalstate):
    cur_state = deque([state])
    cou = 0
    while cur_state:
        cur = cur_state.popleft()
        if cur == goalstate:
            return cou
            break
        new= list(cur)
        temp = 0
        for i in range(len(goalstate)):
            if new[i] == "W":
                temp = i
                break
        neow= 0
        for j in range(uni-1,len(goalstate)):
            if new[j] == "R":
                neow = j
                break
        tempstr = new.pop(temp)
        temp1str = new.pop(neow-1)
        new.insert(neow,tempstr)
        new.insert(temp,temp1str)
        nowstr = "".join(new)
        cur_state.append(nowstr)
        cou += 1


goal = bfs(state, goalstate)
print(goal)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
cn = input()
sto = list(cn)
count = 0

r = sto.count("R")
for i in range(r):
    if sto[i] == "W":
        sto[i] = "R"
        for j in range(r,N):
            if sto[j] == "R":
                sto[j] = "W"
                count += 1
print(count)

Submission status: Time Limit Exceeded

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


def calculate(n, s):
    arr = list(s)
    result = []


    for i in range(n):
        left = arr[:i]
        right = arr[i + 1:]
        res = max(left.count('W'),right.count('R'))
        result.append(res)

    if len(result) == 0:
        print(-1)
    else:
        print(min(result))

calculate(N, S)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
stn = input()
m = stn.count('R')

for i in range(N+1):
    white_l = stn.count('W', 0, i)
    red_r = stn.count('R', i, N)
    if red_r >= white_l:
        m = min(m, red_r)
    else:
        m = min(m, white_l)

print(m)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if c[i] == 'W':
        for j in range(n-1,0,-1 ):
            if i > j:
                break
            if c[j] == 'R':
                if i < j:
                    c[i] = 'R'
                    c[j] = 'W'
                    cnt += 1
                break
print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if c[i] == 'W':
        for j in range(n-1,0,-1 ):
            if c[j] == 'R':
                if i < j:
                    c[i] = 'R'
                    c[j] = 'W'
                    cnt += 1
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
cs = input()
r_num = 0
w_num = 0
# RRR|WWWW にする

for c in cs:
    if c == 'R':
        r_num += 1
    elif c == 'W':
        w_num += 1

if r_num == 0 or w_num == 0:
    print(0)


else:
    temp_W = 0
    temp_R = 0
    ans = []
    for i in range(len(cs)+1):
        #print('i:', i, cs[i:])
        #print('i_r:', i, cs[:i])
        W = w_num - cs[i:].count("W")
        R = r_num - cs[:i].count("R")
        ans.append(max(W, R))

    print(min(ans))

Submission status: Time Limit Exceeded

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

ans = 0

if c.find("W") == -1:
    print(ans)
    exit()

while True:
    ri = c.rfind("R")
    wi = c.find("W")

    if ri < wi:
        print(ans)
        exit()

    c = c.replace("W", "R", 1)
    c = c[::-1].replace("R", "W", 1)[::-1]

    ans = ans + 1

Submission status: Time Limit Exceeded

Solution code:

def main():
    N = int(input())
    C = list(input())
    min_max = N
    #方針:borderの左のWと右側のRの個数を求める
    for border in range(N+1):
        R_red_cnt = 0 
        L_white_cnt = 0
        for j in range(border):#仕切りの左側のWの個数を求める. 
            if C[j] == "W":
                L_white_cnt +=1
        for j in range(border,N):#仕切りの右側のRの個数を求める.
            if C[j] == "R":
                R_red_cnt +=1
        if L_white_cnt > R_red_cnt:#左側のWと右側のRの個数の大きいほうを保存(最小交換回数)
            max = L_white_cnt
        else:
            max = R_red_cnt
        if max < min_max:#今までの仕切りのパターンの中で最も小さい最小回数を保存
            min_max = max
        
    print(min_max)

if __name__ == "__main__":
    main()