Problem: 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?

Submission attempts (in order):
import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N):
    if N-1-i >exchange:
        if c[-(i+1)] =="R":
            c = c[:exchange]+"R"+c[exchange+1:]
            c = c[:-(i+1)]+"W"+c[(i+1)+1:]
            ans += 1
            for k in range(exchange+1,N):
                if c[i] == "W":
                    exchange = k
                    break
            else:
                break
    
print(ans)

import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N):
    
    # print("exchange=",exchange)
    # print("c=",c)
    if N-i < exchange:
        if c[-(i+1)] =="R":
            # c = list(c)
            # c[exchange] = "R"
            # c[-(i+1)] = "W"
            # c = "".join(c)
            c = c[:exchange]+"R"+c[exchange+1:]
            print("newc=",c)
            c = c[:-(i+1)]+"W"+c[N-i:]
            # print("newc=",c)
            ans += 1
            for k in range(exchange+1,N):
                if c[k] == "W":
                    exchange = k
                    break
            else:
                break
    else:
        break
print(ans)

import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N//2):
    
    # print("exchange=",exchange)
    # print("c=",c)
    if c[-(i+1)] =="R":
        # c = list(c)
        # c[exchange] = "R"
        # c[-(i+1)] = "W"
        # c = "".join(c)
        c = c[:exchange]+"R"+c[exchange+1:]
        print("newc=",c)
        c = c[:-(i+1)]+"W"+c[N-i:]
        # print("newc=",c)
        ans += 1
        for k in range(exchange+1,N):
            if c[k] == "W":
                exchange = k
                break
        else:
            break
    
print(ans)

import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N):
    if N-i > exchange:
        if c[-(i+1)] =="R":
            # c = list(c)
            # c[exchange] = "R"
            # c[-(i+1)] = "W"
            # c = "".join(c)
            # c = c[:exchange]+"R"+c[exchange+1:]
            # print("newc=",c)
            # c = c[:-(i+1)]+"W"+c[N-i:]
            # print("newc=",c)
            ans += 1
            for k in range(exchange+1,N-(i+1)):
                if c[k] == "W":
                    exchange = k
                    break
            else:
                break
    else:
        break
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted