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: Runtime Error

Solution code:
def checkFunction(c):
    count = 0

    while True:
        print(count, c)
        if not ('WR' in c):
            return count
        while c[0] == 'R':
            c = c[1:]
        while c[-1] == 'W':
            c = c[:-1]

        if c.index('R') == len(c) - 1:
            return count + 1
        else:
            c = c.replace('WR', 'RR',1)[:-1]
            count += 1

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

print(checkFunction(c))

Submission status: Runtime Error

Solution code:
N = int(input())
stone = list(map(input().split()))
R = stone.count('R')
ans = stone.count('W', 0, R-1)
print(ans)

Submission status: Runtime Error

Solution code:
def checkFunction(c):
    
    if not ('WR' in c):
        return 0
    while c[0] == 'R':
        c = c[1:]
    while c[-1] == 'W':
        c = c[:-1]

    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: Runtime Error

Solution code:
s = input()
R_num = s.count("R")
print(s[:R_num].count("W")

Submission status: Runtime Error

Solution code:
def checkFunction(c):
    
    if (not 'WR' in c):
        return 0
    while True:
        if c[0] == 'R':
            c = c[1:]
        else:
            break
    while True:
        if c[-1] == 'W':
            c = c[:-1]
        else:
            break

    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: Runtime Error

Solution code:
def checkFunction(c):
    
    if (not 'WR' in c):
        return 0
    while True:
        if c[0] == 'R':
            c = c[1:]
        else:
            break
    while True:
        if c[-1] == 'W':
            c = c[:-1]
        else:
            break

    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: Runtime Error

Solution code:
def checkFunction(c):
    # print(c)
    while True:
        if c[0] == 'R':
            c = c[1:]
        else:
            break
    while True:
        if c[-1] == 'W':
            c = c[:-1]
        else:
            break
    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: Runtime Error

Solution code:
import re

def checkFunction(c):
    # print(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: Runtime Error

Solution code:
import re

def checkFunction(c):
    print(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: Runtime Error

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

result = -float('inf')
for i in range(N):
    p = P[i] - 1
    c = C[p]
    k = K - 1
    t = c
    while p != i and k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    if k == 0 or c <= 0:
        result = max(result, t)
        continue
    l = K - k
    c = c * (K // l - 1)
    k = K - (K // l - 1) * l
    t = c
    while k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    result = max(result, t)
print(result)

Submission status: Runtime Error

Solution code:
n = int(input())
storn = input()
m = 0
l = 0
for i in range(1, n+1):
	if storn(i) == W:
		m += 1

for i in range(1, m+1):
	if storn(-i) == R:
		l += 1

print(l)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
d = c.count(w)
e = c.count(w)[:d]
print(d-e)

Submission status: Runtime Error

Solution code:
n=int(input())
c=list(input())
numW=0
numR=c.count("R")
W=n-numR
a=[]
if numR==0 or W==0:
    print(0)
else:
    for i in range(n):
        if c[i]=="W":
            numW+=1
        else:
            numR-=1
        a.append(max(numR,numW))
print(min(a))

Submission status: Runtime Error

Solution code:
n=int(input())
c=list(input())
numW=0
numR=c.count("R")
a=[]
if numR==0:
    print(0)
else:
    for i in range(n):
        if c[i]=="W":
            numW+=1
        else:
            numR-=1
        a.append(max(numR,numW))
print(min(a))

Submission status: Runtime Error

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

cnt = 0
start = False

for i in range(1,n-1):
    if c[i] + c[i+1] == 'WR':
        cnt+=1
        start = True
    elif c[i] + c[i+1] == 'RR' and start:
        cnt+=1
    else:
        start = False

if c[0]+c[1] == 'WR' and cnt ==0:
    cnt++
    
print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
li=[]
red=0
white=0
count=0
li=list(input())
for i in li:
  if i == 'R':
    red+=1
  else:
    white+=1
while True:
  for i in range(red):
    checkw=-1
    if li[i]=='W':
      checkw=i
      break
  for i in range(white):
    checkr=-1
    if li[i+red]=='R':
      checkr=i
      break
  if checkw!=-1 and checkr!=-1:
    li[checkw]='R'
    li[red+checkr]='W'
    count+=1
  elif checkw!=-1:
    li[checkw]='R'
    count+=1
  elif checkr!=-1:
    li[red+checkr]='W'
    count+=1
  else:
    break
print(count)

Submission status: Runtime Error

Solution code:
'import zlib
print(eval(zlib.decompress(b\\'x\xda\xf3\xd3I\xb6\xcd\xcc+(-\xd1\xd0\xd4\x81\xd2\xd6\x05E\x99y%\x1a\xc9\xd1V\xc9z\xc9\xf9\xa5@\xa6z\x90\xbaf,\x8c\x1d\xae\xae\xa9\t\x00\xf0]\x11O\\')))'

Submission status: Runtime Error

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

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: Runtime Error

Solution code:
def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

n = int(input())
data = input()
c = 0
countR = 0
mask = 0
for i in range(len(data)):
  #print(i)
  #print(data[i])
  if i != 0:
    c = c << 1
  ci = data[i]
  if ci == 'R':
    countR += 1
  else:
    c += 1
for i in range(n - countR):
  mask += 1
  mask = mask << 1
mask = mask << countR - 1
#print(bin(c))
#print(bin(mask))
#print(countR)
print(popcount(c & mask))

Submission status: Runtime Error

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

i, j = 0, n - 1
if c.count('W') == 0 | c.count('R') == 0:
    count = 0
else:
    white_cnt = []
    red_cnt = []
    for i in range(n):
        if c[i] == 'W':
            white_cnt.append(i)
        else:
            red_cnt.append(i)
while 1:
    white_num = white_cnt.pop(0)
    red_num = red_cnt.pop()
    if white_num < red_num:
        count += 1
    if len(white_cnt) == 0:
        break
    if len(red_cnt) == 0:
        break
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
C=list(input())
L=0
R=0
RR=0
X=0
    for i in range(N):
      L=i
      R=RR
      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
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)

Submission status: Runtime Error

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):
      R+=ii 
      if C[N-1-R]=='R':
        C[L]='R'
        C[N-1-R]='W'
        X+=1
        R+=1
        if L==N-1-R:
          break
        break
print(X)

Submission status: Runtime Error

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):
      R+=ii 
      if C[N-1-R]=='R':
        C[L]='R'
        C[N-1-R]='W'
        X+=1
        R+=1
        if L==N-1-R:
          break
        break
print(X)

Submission status: Runtime Error

Solution code:
def solve(n, c_str):
    cum_sum_w_left = [0] * (n + 1)
    cum_sum_r_right = [0] * (n + 1)
    # cum_sum_w_left
    cum_sum_w = 0
    for i in range(n):
        if c_str[i] == "W":
            cum_sum_w += 1
        cum_sum_w_left[i + 1] = cum_sum_w
    # cum_sum_r_right
    cum_sum_r = 0
    for i in range(n - 1, -1, -1):
        if c_str[i] == "R":
            cum_sum_r += 1
        cum_sum_r_right[i] = cum_sum_r
    # print(cum_sum_w_left)
    # print(cum_sum_r_right)
    res = n + 1
    for i in range(n + 1):
        res = min(res, max(cum_sum_w_left[i], cum_sum_r_right[i]))
    # print(res)
    return res


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


def test():
    assert solve(4, "WWRR") == 2
    assert solve(2, "RR") == 0
    assert solve(8, "WRWWRWRR") == 3


if _name_ == "_main_":
    test()
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
ans = 0
for i in range(n - 1):
  idx = n - 1 - i
  if s[idx] == 'W' and s[idx + 1] == 'R':
    ans += 1
    s[idx] = 'R'
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(input())
print(C[:C.count('R')].count("W"))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input().split()
s2=sorted(s)
t=0
for i in range(n):
  if s[i]!=s2[i]:
    t+=1
print(t)

Submission status: Runtime Error

Solution code:
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: Runtime Error

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

newlist = sorted(state)
goalstate = "".join(newlist)
# print("goal is " + goalstate)

# action1 is flippingg R(W) 
# action2 is changing position of two ones
# and goal is no left W to all R
# Distinguish if W is left of R
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 cou < 4:
        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: Runtime Error

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

fixed = True
fixed_count = 0
red_count = 0
white_count = 0

print stones

for i in range(N):
    if fixed and stones[i] == 'R':
        fixed_count += 1
    else:
        fixed = False
        if stones[i] == 'R':
            red_count += 1
        else:
            white_count += 1 

target_min = min(red_count, white_count)

print target_min

Submission status: Runtime Error

Solution code:
N = input()
stones = input()

fixed = True
fixed_count = 0
red_count = 0
white_count = 0


for i in range(N):
    if fixed and stones[i] == 'R':
        fixed_count += 1
    else:
        fixed = False
        if stones[i] == 'R':
            red_count += 1
        else:
            white_count += 1 

target_min = min(red_count, white_count)

print target_min

Submission status: Runtime Error

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
                    print(i, j)
                    print(c)
                break
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
a = [str(c) for c in s]

def sea(a, z, n):
  while z[0] < n-1:
    if a[z[0]] == 'W':
      break
    z[0] += 1
    
  while z[1] > 0:
    if a[z[1]] == 'R':
      break
    z[1] += 1
  
  return(z)

z = [0, n-1]

z = a(a, z, n)
c = 0
while z[0] < z[1]:
  a[z[0]], a[z[1]] = a[z[1]], a[z[0]]
  c += 1
  z = a(a,z,n)
  
print(c)

Submission status: Runtime Error

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

l = 0
r = n - 1
ans = 0
while r > l:

    while r > l:
        if s[l] == 'W': break
        else: l += 1
    while r > l:
        if s[r] == 'R': break
        else: r += 1
    if r > l:
        ans += 1
        r -= 1
        l += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
C = []
for c in list(input()):
  a = 0
  if c == "R":
    a = 1
  C.append(a)
L = -1
R = N
cnt = 0
while L < R:
  L += 1
  if C[L] != 0:
    continue
  while L < R:
    R -= 1
    if C[R] != 1:
      continue
    cnt += 1
    break     
print(cnt)

Submission status: Runtime Error

Solution code:
from math import floor, ceil
N = int(input())
WHITE = 0
RED = 1
str = list(map(str, list(input())))
r = []
w = []

for i in range(N):
  w.append(i) if str[i] == "W" else r.append(i)
  str[i] = WHITE if str[i] == "W" else RED
r.reverse()

max_count = max(len(r), len(w))
count = 0
for i in range(max_count):
  if r[i] - w[i] < 0:
    count = i
    break

print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
c = ','.join(input()).split(',')

ans = 0

ri = -1

for i in range(n):
    if c[i] == 'W':
        while c[ri] != 'R':
            ri -= 1
        
        if n < i-ri:
            break
        
        c[i], c[ri] = 'R', 'W'
        ans += 1
    
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
#s = str(input())
s = list(input())

count = 0

d = False
c = False

while d == False:
  for i in range(n):
    if c == False:
      if s[i] =='W':
        firstw = i
        c = True
        d = True
    if c == True:
      if s[i] =='R':
        lastr = i
        d = False
  if d == False:
    c = False
    #swap
    s[firstw] = 'R'
    s[lastr] = 'W'
    count += 1
    
print(count)

Submission status: Runtime Error

Solution code:
 #https://atcoder.jp/contests/agc008/submissions/15248942
 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')
 left = c[:R]
 ans = left.count('W')
 
 print(ans)

Submission status: Runtime Error

Solution code:
input()
a=input()
a=a[a.index('W'):]
b=a.count('R')
print(b-a[:b].count('R'))

Submission status: Runtime Error

Solution code:
num = int(input())
wr = str(input())

i_counts = []
r_num = wr.count("R")

for i in range(r_num):
  w_counts = 0
  r_counts = 0
  for j in range(num):
    if i > j and wr[j] == "W":
      w_counts += 1
    elif i < j and wr[j] == "R":
      r_counts += 1
  i_count = max(w_counts, r_counts)
  i_counts.append(i_count)

ans = min(i_counts)
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int lw = 0, rr = count(begin(s), end(s), 'R'), res = rr;
    for (int i = 0; i < n; ++i)
    {
        if (s[i] == 'R')
            --rr;
        else
            ++lw;
        res = min(res, max(lw, rr));
    }
    cout << res;
}

Submission status: Runtime Error

Solution code:
N = int(input())
stone = [0]*N
stone = (list(input()))


WhiteList =[i for i in range(N) if stone[i]=='W']
RedList =[i for i in range(N) if stone[i]=='R']
count=0
tmp = N-1
for i in range(N):
    if i >= tmp:
        break
    if stone[i]=='W':
        while stone[tmp]=='W':
            tmp-=1
        count+=1
        tmp-=1

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
stone = [0]*N
stone = (list(input()))


WhiteList =[i for i in range(N) if stone[i]=='W']
RedList =[i for i in range(N) if stone[i]=='R']
count=0
tmp = N-1
for i in range(N):
    if i >= tmp:
        break
    if stone[i]=='W':
        while stone[tmp]=='W':
            tmp-=1
        count+=1
        print(i, tmp)
        tmp-=1

print(count)

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
S = input()
cnt = 0
while True:
    while i < N and S[i] == 'R':
        i += 1
    while 0 <= j and S[j] == 'W':
        j -= 1
    if j < i:
        break
    cnt += 1
    i += 1
    j -= 1
print(cnt)

Submission status: Runtime Error

Solution code:
N=int(input())
c=input()
W=[]
R=[]
pw=0
pr=0
for i in range(N):
  if c[i]=="W":
    W+=[i]
    pw+=1

  if c[N-1-i]=="R":
    R+=[i]
    pr+=1

for i in range(200000):
    if min(len(W),len(R))==0:
        result=0
        break
    if W[i]+R[i]>=N:
        result=i
        break
print(result)
result=i

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
c=input()
d=Counter(c)
MIN=min(Counter(c)["W"],Counter(c)["R"])
ans=[]
for i in range(MIN):
  if Counter(c[:i])["W"]<=Counter(c[i:])["R"]:
    ans.append(Counter(c[i:])["R"])
  else:
    ans.append(Counter(c[i:])["W"])
    
print(min(ans))

Submission status: Runtime Error

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def Ra():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()

########################################################################################################################################################################


def dijkstra(edge,st):
    # edge=[[(v_to,dist_to_v),...],[],...]
    # initialize: def: d=dist(st,i), prev=[previous vertex in minimum path], q[]
    n=len(edge)
    d=[(0 if st==i else inf) for i in range(n)]
    q=[(j,i) for i,j in enumerate(d)]
    heapify(q)
    v=[False]*n
    # calc
    while q:
        dist,cur=heappop(q)
        if v[cur]:
            continue
        v[cur]=True
        for dst,dist in edge[cur]:
            alt=d[cur]+c[cur]
            if alt<d[dst]:
                d[dst]=alt
                heappush(q,(alt,dst))
    return d

class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
 
    def _root(self, x):
        if self.table[x] < 0:
            return x
        else:
            # 経路の圧縮
            self.table[x] = self._root(self.table[x])
            return self.table[x]
 
    def find(self, x, y):
        return self._root(x) == self._root(y)
 
    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
    
    def __str__(self):
        rt=[i if j<0 else j for i,j in enumerate(self.table)]
        return str(rt)

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo



show_flg=False
show_flg=True

ans=0


n=I()
s=input()
r,w=[],[]
for i in range(n):
    if s[i]=='R':
        r+=i,
    else:
        w+=i,
w=w[::-1]

ans=0
while w:
    c=w.pop()
    if c>r[-1]:
        break
    r.pop()
    ans+=1
        
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter


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

ans = 0
for i in range(C_cntr["R"]):
　if C[i] == "W":
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:

import math

def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！

n,k = MI()
a = LI()

def cutting_isOK(x,li,k):
    ans = 0
    for i in range(n):
        ans += math.ceil(li[i]/x) - 1
    if ans <= k:
        return True
    else:
        return False
left = -1
right = 10**9+1
while right != left + 1:
    mid = (left+right)//2
    if cutting_isOK(mid,a,k):
        right = mid
    else:
        left = mid
print(right)

Submission status: Runtime Error

Solution code:
N = int(input())
C = list(map(int, input().split()))

R_cnt = C.count('R')
x = C[:R_cnt].count('R')

print(R_cnt-x)

Submission status: Runtime Error

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

ans=0
i,j=0,N-2
while i<j:
    if s[i]=='R':
        i+=1
    elif s[j]=='W':
        j-=1
    else:
        k+=1
        i+=1
        j-=1

print(k)

Submission status: Runtime Error

Solution code:
def calculate(n, s):
    arr = list(S)

    RNum = arr.count("R")

    leftW = 0
    rightR = RNum

    result = []
    for i in range(n):
        if arr[i] == "R":
            rightR -= 1
        if arr[i] == "W":
            leftW += 1

        result.append(max(leftW, rightR))

    print(min(result))


calculate(N, S)

Submission status: Runtime Error

Solution code:
n = int(input())
c = list(input())#ciが R のとき赤、W のとき白
a = c.count("R")
b = 0
count = max(a, b)
# a: R->W, b: W->Rの回数
for i in range(n+1):
  if c[i] == "R":
    a = a - 1
  else:
    b = b + 1
  ans = max(a, b)
  count = min(ans, count)
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
c = list(input())#ciが R のとき赤、W のとき白
count = n
a = c.count("R")
b = 0
ans = max(a, b)
count = min(ans, count)
# a: R->W, b: W->Rの回数
for i in range(n+1):
  if c[i] == "R":
    a = a - 1
  else:
    b = b + 1
  ans = max(a, b)
  count = min(ans, count)
print(count)

Submission status: Runtime Error

Solution code:
import sys
N = int(input())
c = [str(x) for x in list(input())]
R = sum([1 for i, x in enumerate(c) if(x=='R')])
W = 0
if(R==N)or(R==0):
  print(0)
  sys.exit()
ans = R
i = 0
while True:
  if(c[i] == 'W'):
    W += 1
  else:
    R -= 1
  n_ans = max(W,R)
  if(n_ans>ans):
    print(ans)
    sys.exit()
  ans = n_ans
  i += 1

Submission status: Runtime Error

Solution code:
n = int(input())
 
stone = input()
 
count = 0
w = n-1
r = 0
 
for k in range(n):
    for i in range(n):
        if stone[i] == "W":
            w = i
            break
 
    for i in range(n):
        if stone[n-1-i] == "R":
            r = n-1-i
            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)
n = int(input())

stone = input()

count = 0
w = n-1
r = 0

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

    for i in range(n):
        if stone[n-1-i] == "R":
            r = n-1-i
            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: Runtime Error

Solution code:
n = int(input())
 
stone = input()
 
count = 0
w = n-1
r = 0
 
for k in range(n):
    for i in range(n):
        if stone[i] == "W":
            w = i
            break
 
    for i in range(n):
        if stone[n-1-i] == "R":
            r = n-1-i
            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)
n = int(input())

stone = input()

count = 0
w = n-1
r = 0

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

    for i in range(n):
        if stone[n-1-i] == "R":
            r = n-1-i
            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: Runtime Error

Solution code:
n = int(input())

stone = input()

count = 0
w = n-1
r = 0

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

    for i in range(n):
        if stone[n-1-i] == "R":
            r = n-1-i
            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)