Problem:
Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 各種変数の読み込み
            var its = Readints();
            int n = its[0];
            int k = its[1];
            var muki = ReadLR();

            int hap = 0;
            if (muki[0] & muki[1])
            {
                hap++;
            }
            if (!muki[muki.Length - 1] & !muki[muki.Length - 2])
            {
                hap++;
            }
            for (int i = 1; i < muki.Length - 1; i++)
            {
                bool isok = muki[i] ? muki[i + 1] : !muki[i - 1];
                if (isok)
                {
                    hap++;
                }
            }


            int lrcount = 0;
            int rlcount = 0;
            for (int i = 1; i < muki.Length; i++)
            {
                if (muki[i] & !muki[i - 1])
                {
                    lrcount++;
                }
                else if (!muki[i] & muki[i - 1])
                {
                    rlcount++;
                }
            }
            int fixnum = Math.Min(lrcount, rlcount);
            if (k > fixnum && lrcount != rlcount)
            {
                hap += fixnum * 2 + 1;
            }
            else
            {
                hap += Math.Min(k, fixnum) * 2;
            }
            /*
            for (int i = 0; i < k; i++)
            {
                var lr = SearchLR(muki);
                var rl = SearchRL(muki);
                if (lr < muki.Length | rl < muki.Length)
                {
                    if (lr < muki.Length && rl < muki.Length)
                    {
                        hap += 2;
                    }
                    else
                    {
                        lr = Math.Min(lr, rl);
                        rl = muki.Length;
                        hap += 1;
                    }
                    int a = Math.Min(lr, rl);
                    int b = Math.Max(lr, rl);
                    int apbm1 = a + b - 1;
                    for (int j = a; j < b; j++)
                    {
                        muki[apbm1 - j] = !muki[j];
                    }
                }
                else
                {
                    break;
                }
            }
            */
            Console.WriteLine(hap);

#if DEBUG
            Console.ReadKey();
#endif
        }

        private static bool[] ReadLR()
        {
            var s = Console.ReadLine().ToCharArray();
            var output = new bool[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                output[i] = s[i] == 'R';
            }
            return output;
        }

        private static int SearchLR(bool[] lr)
        {
            // LRとなっている箇所を見つけてRのインデックスを返す
            int i = 0;
            while (lr[i])
            {
                i++;
                if (i == lr.Length)
                {
                    return i;
                }
            }
            while (!lr[i])
            {
                i++;
                if (i == lr.Length)
                {
                    return i;
                }
            }
            return i;
        }


        private static int SearchRL(bool[] lr)
        {
            // RLとなっている箇所を見つけてLのインデックスを返す
            int i = 0;
            while (!lr[i])
            {
                i++;
                if (i == lr.Length)
                {
                    return i;
                }
            }
            while (lr[i])
            {
                i++;
                if (i == lr.Length)
                {
                    return i;
                }
            }
            return i;
        }

        private static int Readint()
        {
            // 1行に1つのintが入力されたのを読む
            return int.Parse(Console.ReadLine());
        }

        private static int[] Readints()
        {
            // 1行に複数のintが入力されたのを読む
            var s = Console.ReadLine().Split(' ');
            var output = new int[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                output[i] = int.Parse(s[i]);
            }
            return output;
        }

        private static int[][] Readints(int n, bool transpose=false)
        {
            // n行それぞれにintが入力されたのを読む
            // output[行番号][列番号]で返す。
            // transposeがtrueのときは、output[列番号][行番号]で返す。
            var s = Console.ReadLine().Split(' ');
            int rownum = s.Length;
            if (transpose)
            {
                var outputs = new int[rownum][];
                for (int j = 0; j < rownum; j++)
                {
                    outputs[j] = new int[n];
                }
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[i][0] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[i][l] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
            else
            {
                var outputs = new int[n][];
                outputs[0] = new int[rownum];
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[0][i] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    outputs[l] = new int[rownum];
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[l][i] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
        }

        private int[] Minusone(int[] x)
        {
            var output = new int[x.Length];
            for (int i = 0; i < x.Length; i++)
            {
                output[i] = x[i] - 1;
            }
            return output;
        }

        // 元の配列をソートして、ソート後の各要素の、元のインデックスを返す
        private static int[] SortwithIndex(double[] data)
        {
            var index = new int[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                index[i] = i;
            }
            QSort(data, index, 0, data.Length - 1);
            return index;
        }

        private static void QSort(double[] data, int[] index, int low, int high)
        {
            // 要素数が1以下の領域ができた場合、その領域は確定とする。
            if (high - low < 1)
                return;

            // 1. ピボットとして一つ選びそれをPとする。
            var P = data[(low + high + 1) / 2];

            // 2. 左から順に値を調べ、P以上のものを見つけたらその位置をiとする。
            // 3. 右から順に値を調べ、P以下のものを見つけたらその位置をjとする。
            // 4. iがjより左にあるのならばその二つの位置を入れ替え、2に戻る。ただし、次の2での探索はiの一つ右、次の3での探索はjの一つ左から行う。
            int i = low - 1;
            int j = high + 1;
            while (Scan(data, P, ref i, ref j))
            {
                Swap(data, index, i, j);
            }

            // 5. 2に戻らなかった場合、iの左側を境界に分割を行って2つの領域に分け、そのそれぞれに対して再帰的に1からの手順を行う。
            QSort(data, index, low, i - 1);
            QSort(data, index, i, high);
        }

        private static bool Scan(double[] data, double P, ref int i, ref int j)
        {
            while (data[++i] < P) ;
            while (data[--j] > P) ;
            return i < j;
        }
        

        private static void Swap(double[] data, int[] index, int i, int j)
        {
            var temp = data[i];
            data[i] = data[j];
            data[j] = temp;
            var temp2 = index[i];
            index[i] = index[j];
            index[j] = temp2;
        }
    }
}