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 Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;
use std::collections::BinaryHeap;

#[allow(unused_macros)]
macro_rules! unwrap {
    ($arg:expr) => {
        {
            if $arg.is_none() {return;}
            $arg.unwrap()
        }
    };
}

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let mut k = scan.read::<usize>();
    let s = scan.chars();
    let cmp = compress(&s);
    let mut heap = BinaryHeap::new();
    let mut omit = vec![0;n];
    for c in cmp {
        heap.push(c);
    }
    while k > 0 {
        let item: (Rev<usize>, (usize, usize)) = unwrap!(heap.pop());
        omit[(item.1).0] = (item.1).1;
        k -= 1;
    }
    println!("{}", calc(&s, &omit));
}

fn calc(slice: &Vec<char>, omit: &Vec<usize>) -> usize {
    let mut ret = 0;
    let mut i = 0;
    let mut cnt = 0;
    let mut pre = None;
    loop {
        if omit[i] != 0 {
            cnt += omit[i] - i;
            i = omit[i];
            continue;
        }
        if pre == None {
            pre = Some(slice[i]);
        } else if pre != Some(slice[i]) {
            ret += cnt - 1;
            cnt = 0;
            pre = Some(slice[i]);
        }
        cnt += 1;
        i += 1;
        if i == slice.len() {
            break;
        }
    }
    ret += cnt - 1;
    ret
}

#[allow(dead_code)]
fn compress<T: Clone + PartialEq>(slice: &[T]) -> Vec<(Rev<usize>, (usize, usize))> {
    if slice.len() == 0 {
        return vec![];
    }
    let mut acc = vec![];
    let mut current = slice[0].clone();
    let mut cnt = 1usize;
    for i in 1..slice.len() {
        if current != slice[i] {
            acc.push((Rev(cnt), (i - cnt, i)));
            cnt = 0;
        }
        current = slice[i].clone();
        cnt += 1;
    }
    acc.push((Rev(cnt), (slice.len() - cnt, slice.len())));
    acc
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<std::cmp::Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> std::cmp::Ordering {
        other.0.cmp(&self.0)
    }
}


// region template
#[allow(dead_code)]
fn iu(i: isize) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> isize { i as isize }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion