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: Wrong Answer)

use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        k: usize,
        s: chars
    }

    let mut first_R = false;
    if s[0] == 'R' {
        first_R = true;
    }

    let mut is_R = first_R;

    let mut L_vec: Vec<(usize, bool, bool)> = Vec::new();
    let mut R_vec: Vec<(usize, bool, bool)> = Vec::new();

    let mut first = true;
    let mut count = 0;
    let mut l_sum = 0;
    let mut r_sum = 0;
    for i in 0..n {
        if is_R {
            if s[i] == 'R' {
                count += 1;
            } else {
                r_sum += count;
                R_vec.push((count, first, false));
                count = 1;
                is_R = false;
                first = false;
            }
        } else {
            if s[i] == 'L' {
                count += 1;
            } else {
                l_sum += count;
                L_vec.push((count, first, false));
                count = 1;
                is_R = true;
                first = false;
            }
        }
    }

    if is_R {
        r_sum += count;
        R_vec.push((count, false, true));
    } else {
        l_sum += count;
        L_vec.push((count, false, true));
    }
    L_vec.sort_by(|x, y| {
        if y.0 > x.0 {
            cmp::Ordering::Greater
        } else if y.0 < x.0 {
            cmp::Ordering::Less
        } else {
            if !x.2 {
                cmp::Ordering::Less
            } else {
                cmp::Ordering::Greater
            }
        }
    });
    R_vec.sort_by(|x, y| {
        if y.0 > x.0 {
            cmp::Ordering::Greater
        } else if y.0 < x.0 {
            cmp::Ordering::Less
        } else {
            if !x.1 {
                cmp::Ordering::Less
            } else {
                cmp::Ordering::Greater
            }
        }
    });

    // 向きを変える対象を判定
    if R_vec.len() > L_vec.len() {
        is_R = false;
    } else if R_vec.len() < L_vec.len() {
        is_R = true;
    } else {
        if r_sum > l_sum {
            is_R = false;
        } else {
            is_R = true;
        }
    };

    //println!("{:?} {:?} {:?} {:?} {}", L_vec, l_sum, R_vec, r_sum, is_R);

    if is_R {
        if R_vec.len() <= k {
            println!("{}", n - 1);
            return;
        }
    } else {
        if L_vec.len() <= k {
            println!("{}", n - 1);
            return;
        }
    }

    let mut ans = 0;

    if is_R {
        for i in &L_vec {
            ans += i.0 - 1;
        }

        for i in 0..k {
            if !R_vec[i].2 {
                ans += R_vec[i].0 + 1;
            } else {
                ans += R_vec[i].0;
            }
        }
    } else {
        for i in &R_vec {
            ans += i.0 - 1;
        }

        for i in 0..k {
            if !L_vec[i].1 {
                ans += L_vec[i].0 + 1;
            } else {
                ans += L_vec[i].0;
            }
        }
    }

    println!("{}", ans);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet