Buggy python submission (verdict: Runtime Error):
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1


ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Fixed submission:
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1

ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if r >= 0 and cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
C = [c for c in input()]

ans = 0
cnt = 0
w = 0
r = n-1
while True:
    cnt = w
    while cnt < n and C[cnt] != 'W': cnt += 1
    if cnt >= n:
        print(ans)
        exit()
    else:
        w = cnt
        while cnt+1 < n and C[cnt+1] != 'R': cnt += 1
        if cnt+1 >= n:
            print(ans)
            exit()
        else:
            while cnt != r and C[r] != 'R': r -= 1
            if cnt == r:
                print(ans)
                exit()
            else:
                ans += 1
                C[w], C[r] = C[r], C[w]

Fixed submission:
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1

ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if r >= 0 and cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
C = [c for c in input()]

ans = 0
cnt = 0
w = 0
r = n-1
while True:
    cnt = w
    while cnt < n and C[cnt] != 'W': cnt += 1
    if cnt >= n:
        print(ans)
        exit()
    else:
        w = cnt
        while cnt+1 < n and C[cnt+1] != 'R': cnt += 1
        if cnt+1 >= n:
            print(ans)
            exit()
        else:
            while cnt != r and C[r] != 'R': r -= 1
            if cnt == r:
                print(ans)
                exit()
            else:
                ans += 1
                C[w], C[r] = C[r], C[w]

Fixed submission:
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1

ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if r >= 0 and cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()