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 in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Schema;

namespace AtCoder
{
    public class Solver
    {
        private readonly IInputReader _inputReader;
        private readonly IOutputWriter _outputWriter;

        public Solver(IInputReader inputReader, IOutputWriter outputWriter)
        {
            _inputReader = inputReader;
            _outputWriter = outputWriter;
        }

        public void Solve()
        {
            var N = _inputReader.ReadLine().ToInt();
            var chars = _inputReader.ReadLine().ToCharArray();

            int leftSearchedIndex = -1;
            int rightSearchedIndex = chars.Length;
            int count = 0;
            while (true)
            {
                int firstWhiteIndex = -1;
                int lastRedIndex = -1;

                //左からWを探す
                for (int i = leftSearchedIndex + 1; i < chars.Length; i++)
                {
                    if (i >= rightSearchedIndex) break;
                   
                    if (chars[i] == 'W')
                    {
                        firstWhiteIndex = i;
                        leftSearchedIndex = i;
                        break;
                    }
                }

                //右からRを探す
                for (int i = rightSearchedIndex - 1; i >= 0; i--)
                {
                    if (i <= leftSearchedIndex) break;
                    if (chars[i] == 'R')
                    {
                        lastRedIndex = i;
                        rightSearchedIndex = i;
                        break;
                    }
                }

                if (firstWhiteIndex != -1 && lastRedIndex != -1 && firstWhiteIndex < lastRedIndex)
                {
                    chars[firstWhiteIndex] = 'R';
                    chars[lastRedIndex] = 'W';
                    count++;
                }
                else
                {
                    break;
                }
            }

            _outputWriter.WriteLine(count.ToString());
        }
    }

    public static class Combination
    {
        public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items, int k, bool withRepetition)
        {
            if (k == 1)
            {
                foreach (var item in items)
                    yield return new T[] {item};
                yield break;
            }

            foreach (var item in items)
            {
                var leftside = new T[] {item};

                // item よりも前のものを除く （順列と組み合わせの違い)
                // 重複を許さないので、unusedから item そのものも取り除く
                var unused = withRepetition ? items : items.SkipWhile(e => !e.Equals(item)).Skip(1).ToList();

                foreach (var rightside in Enumerate(unused, k - 1, withRepetition))
                {
                    yield return leftside.Concat(rightside).ToArray();
                }
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            var reader = new ConsoleInputReader();
            var writer = new ConsoleOutputWriter();
            new Solver(reader, writer).Solve();
        }
    }

    public interface IInputReader
    {
        string ReadLine();
    }

    public interface IOutputWriter
    {
        void WriteLine(string output);
    }

    internal class ConsoleInputReader : IInputReader
    {
        public string ReadLine() => Console.ReadLine();
    }

    internal class ConsoleOutputWriter : IOutputWriter
    {
        public void WriteLine(string output) => Console.WriteLine(output);
    }

    public static class StringExtensions
    {
        public static char ToChar(this string text) => text[0];
        public static string[] ToStringArray(this string text) => text.Split(' ').ToArray();
        public static int ToInt(this string text) => int.Parse(text);
        public static int[] ToIntArray(this string text) => text.Split(' ').Select(txt => txt.ToInt()).ToArray();

        public static (string, string) ToString2(this string text)
        {
            var arr = text.ToStringArray();
            return (arr[0], arr[1]);
        }

        public static (string, string, string) ToString3(this string text)
        {
            var arr = text.ToStringArray();
            return (arr[0], arr[1], arr[2]);
        }

        public static (int, int) ToInt2(this string text)
        {
            var arr = text.ToIntArray();
            return (arr[0], arr[1]);
        }

        public static (int, int, int) ToInt3(this string text)
        {
            var arr = text.ToIntArray();
            return (arr[0], arr[1], arr[2]);
        }

        public static long ToLong(this string text) => long.Parse(text);
        public static long[] ToLongArray(this string text) => text.Split(' ').Select(txt => txt.ToLong()).ToArray();

        public static string ToJoinedString(this string[] texts, string separator = "") =>
            string.Join(separator, texts);

        public static string ToJoinedString(this char[] chars) => string.Join("", chars);
        public static string ToYESNO(this bool b) => b ? "YES" : "NO";
        public static string ToYesNo(this bool b) => b ? "Yes" : "No";
    }

    public struct ModLong : IEquatable<ModLong>
    {
        private const long Mod = 1000000007;
        private readonly long _value;

        public ModLong(long value)
        {
            _value = (value % Mod + Mod) % Mod;
        }

        public bool Equals(ModLong other)
        {
            return _value == other._value;
        }

        public override bool Equals(object obj)
        {
            return obj is ModLong other && Equals(other);
        }

        public override int GetHashCode()
        {
            return _value.GetHashCode();
        }

        public static bool operator ==(ModLong left, ModLong right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(ModLong left, ModLong right)
        {
            return !left.Equals(right);
        }

        public static ModLong operator +(ModLong left, ModLong right)
        {
            return (ModLong) (left._value + right._value);
        }

        public static ModLong operator -(ModLong left, ModLong right)
        {
            return (ModLong) (left._value - right._value);
        }

        public static ModLong operator *(ModLong left, ModLong right)
        {
            return (ModLong) (left._value * right._value);
        }

        public static ModLong operator /(ModLong left, ModLong right)
        {
            return (ModLong) (left._value * Util.ModInv(right._value, Mod));
        }

        public static explicit operator long(ModLong num)
        {
            return num._value;
        }

        public static explicit operator ModLong(long num)
        {
            return new ModLong(num);
        }

        public override string ToString() => _value.ToString();
    }

    public class ModCombination
    {
        private const int Max = 510000;
        private const long Mod = 1000000007;

        private long[] fac = new long[Max];
        private long[] finv = new long[Max];
        private long[] inv = new long[Max];

        public ModCombination()
        {
            fac[0] = 1;
            fac[1] = 1;
            finv[0] = 1;
            finv[1] = 1;
            inv[1] = 1;
            for (var i = 2; i < Max; i++)
            {
                fac[i] = fac[i - 1] * i % Mod;
                inv[i] = Mod - inv[Mod % i] * (Mod / i) % Mod;
                finv[i] = finv[i - 1] * inv[i] % Mod;
            }
        }

        /// <summary>
        /// mCn 組み合わせ
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public long Combination(long m, long n)
        {
            if (m < n) return 0;
            if (m < 0 || n < 0) return 0;
            return fac[m] * (finv[n] * finv[m - n] % Mod) % Mod;
        }
    }

    public class Util
    {
        /// <summary>
        /// a^n mod を計算する
        /// </summary>
        /// <param name="a"></param>
        /// <param name="n"></param>
        /// <param name="mod"></param>
        /// <returns></returns>
        public static long ModPow(long a, long n, long mod)
        {
            long res = 1;
            while (n > 0)
            {
                if ((n & 1) == 1)
                {
                    res = res * a % mod;
                }

                a = a * a % mod;
                n >>= 1;
            }

            return res;
        }

        /// <summary>
        /// a^{-1} mod を計算する
        /// </summary>
        /// <param name="a"></param>
        /// <param name="mod"></param>
        /// <returns></returns>
        public static long ModInv(long a, long mod)
        {
            return ModPow(a, mod - 2, mod);
        }

        /// <summary>
        /// 階乗
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static long ModFactional(long n)
        {
            if (n < 0) throw new ArgumentOutOfRangeException(nameof(n));
            ModLong result = (ModLong) 1;
            for (var i = 1; i <= n; i++)
            {
                result *= (ModLong) i;
            }

            return (long) result;
        }
    }
}