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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	_ "container/heap"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	_ "strings"
)

var L byte = 76
var R byte = 82

func printBytes(S *[]byte) {
	println(string(*S))
}

func test(S *[]byte, N int) (*[]byte, *[]int) {
	data := make([]byte, 0)
	NS := make([]byte, 0)
	NSC := make([]int, 0)

	for i := 0; i < N; i++ {
		if len(data) == 0 {
			data = append(data, (*S)[i])
		} else if i == N-1 && (*S)[i] == data[len(data)-1] {
			data = append(data, (*S)[i])
			NS = append(NS, data[0])
			NSC = append(NSC, len(data))
		} else if i == N-1 && (*S)[i] != data[len(data)-1] {
			NS = append(NS, data[0])
			NSC = append(NSC, len(data))

			data = []byte{(*S)[i]}
			NS = append(NS, data[0])
			NSC = append(NSC, len(data))
		} else if (*S)[i] == data[len(data)-1] {
			data = append(data, (*S)[i])
		} else if (*S)[i] != data[len(data)-1] {
			NS = append(NS, data[0])
			NSC = append(NSC, len(data))
			data = []byte{(*S)[i]}

			if i == N-1 {
				NS = append(NS, data[0])
				NSC = append(NSC, len(data))
			}
		}
	}

	// printBytes(&NS)

	return &NS, &NSC
}

func run() {
	N := nextInt()
	K := nextInt()
	S := []byte(next())
	init := S[0]

	// printBytes(&S)

	for i, k := 0, 0; i < N && k < K; i++ {
		if init != S[i] {
			for true {
				if i >= N || S[i] == init {
					break
				}
				// println(i, S[i], init)
				S[i] = init
				i++
			}
			k++
		}
	}

	var ans = 0
	for i := 0; i < N-1; i++ {
		if S[i] == S[i+1] {
			ans += 1
		}
	}

	// printBytes(&S)
	println(ans)
}

// --- template ---
// Thanks https://gist.github.com/halllllll/853ab587fd82ee3ffe6f7fb7fb499695

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

const bufferSize = 64 * 1024 * 1024

func print(a ...interface{}) (n int, err error) {
	return fmt.Fprint(out, a...)
}

func printf(format string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, format, a...)
}

func println(a ...interface{}) (n int, err error) {
	return fmt.Fprintln(out, a...)
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, err := strconv.Atoi(next())
	if err != nil {
		log.Fatal(err)
	}
	return a
}

func nextFloat() float64 {
	a, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		log.Fatal(err)
	}
	return a
}

func min(nums []int) int {
	if len(nums) == 0 {
		panic("funciton min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Min(float64(res), float64(nums[i])))
	}
	return res
}

func max(nums []int) int {
	if len(nums) == 0 {
		panic("funciton max() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Max(float64(res), float64(nums[i])))
	}
	return res
}

func abs(a int) int {
	return int(math.Abs(float64(a)))
}

func pow(p, q int) int {
	return int(math.Pow(float64(p), float64(q)))
}

// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

// Push to heap
func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

// Pop from heap
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func main() {
	sc.Buffer(make([]byte, 0, bufferSize), bufferSize)
	sc.Split(bufio.ScanWords)
	run()
	out.Flush()
}