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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	static final PrintStream out = System.out;
	static final Scanner sc = new Scanner(System.in);
	static final long Mod = 1000_000_007L;
	public static void main(String[] args) {
		Main main = new Main();
		main.solve();
		System.exit(0);
	}

	void solve() {
		int N = nextInt();
		int K = nextInt();
		char[] C = (next()+'-').toCharArray();
		List<Integer> nList = new ArrayList<>();
		List<Character> lrList = new ArrayList<>();
		PriorityQueue<Integer> pqLi = new PriorityQueue <Integer>((a1,a2)->MyComparator(nList.get(a1),nList.get(a2))); // index 降順
		PriorityQueue<Integer> pqRi = new PriorityQueue <Integer>((a1,a2)->MyComparator(nList.get(a1),nList.get(a2)));

		int cnt = 1;
		int sumL = 0;
		int sumR = 0;
		for(int i=1;i<=N;i++) {
			if(C[i-1] == C[i]) {
				cnt++;
			}else {
				nList.add(cnt);
				lrList.add(C[i-1]);
				if(C[i-1] == 'L') {
					pqLi.add(nList.size()-1);
				}
				else if(C[i-1] == 'R'){
					pqRi.add(nList.size()-1);
				}
				cnt = 1;
			}
		}
		int pqLlen = pqLi.size();
		int pqRlen = pqRi.size();
		List<Character> LKlrList = new ArrayList<>(lrList);
		List<Character> RKlrList = new ArrayList<>(lrList);
		int stockL = 0; // 変更可能箇所が端のLRのみの場合++
		int stockR = 0;

		for(int i=0;i<K;i++) {
			if(pqRi.size()>0) {
				int t = pqRi.poll();
				if(t == 0||t == pqRlen-1) {
					if(pqRi.size()>0)t = pqRi.poll();
				}
				LKlrList.set(t,'L');
			}else {
				stockL ++;
			}
			if(pqLi.size()>0) {
				int t = pqLi.poll();
				if(t == 0||t == pqLlen-1) {
					if(pqLi.size()>0)t = pqLi.poll();
				}
				RKlrList.set(t,'R');
			}else {
				stockR++;
			}

		}

		nList.add(0);
		LKlrList.add('-');
		RKlrList.add('-');

		sumL=0;
		sumR=0;
		cnt = nList.get(0);

		for(int i=0;i<LKlrList.size()-1;i++) {
			if(LKlrList.get(i)==LKlrList.get(i+1)) {
				cnt += nList.get(i+1);
			}else {
				sumL += Math.max(cnt-1,0);
				cnt = nList.get(i+1);
			}
		}
		cnt = nList.get(0);
		for(int i=0;i<RKlrList.size()-1;i++) {
			if(RKlrList.get(i)==RKlrList.get(i+1)) {
				cnt += nList.get(i+1);
			}else {
				sumR += Math.max(cnt-1,0);
				cnt = nList.get(i+1);
			}
		}
		if(stockL>0) {
			while(stockL>0&&sumL<N-1) {
				sumL++;
				stockL--;
			}
		}
		if(stockR>0) {
			while(stockR>0&&sumR<N-1) {
				sumR++;
				stockR--;
			}
		}

		out.println(Math.max(sumL, sumR));
	}
	private int MyComparator(int a1, int a2) {
		if(a1 < a2) return 1;
		else if(a1 > a2) return -1;
		return 0;
	}

	public int[] nextIntArray(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)a[i] = nextInt();
		return a;
	}
	public long[] nextLongArray(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)a[i] = nextLong();
		return a;
	}
	public int nextInt() {
		return Integer.parseInt(next());
	}
	public long nextLong() {
		return Long.parseLong(next());
	}
	public String next() {
		return sc.next();
	}

}