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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll mod = 1e9+7;
typedef pair<int,int> P;
#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,m,N) for(int i=m;i<N;i++)
#define mm(n,m) memset(n,m,sizeof(n))

int main(){
    ll n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    ll sl = s.length();
    ll lc[n];
    ll rc[n];
    memset(lc,0,sizeof(lc));
    memset(rc,0,sizeof(rc));
    if(s[0]=='L')lc[0]=1;
    else rc[0]=1;
    string ss;
    ss = s;
    rep2(i,1,sl){
        if(s[i]=='L')lc[i]=lc[i-1]+1;
        else rc[i]=rc[i-1]+1;
    }
    ll ans = 0;
    ll cnt=0;
    pair<int, pair<int,int>> maxr[n+5];
    memset(maxr,0,sizeof(maxr));
    int c= 0;
    rep(i,sl){
        if(s[i]=='R'){
            cnt++;
        }else{
            if(cnt==0)continue;
            maxr[c].second.first=cnt;
            maxr[c].second.second=i-1;
            int t = i-1;
            int tt = i;
            while(s[tt]=='L')tt++;
            while(s[t]=='R')t--;
            if(t<0)t=0;
            if(tt>sl-1)tt=sl-1;
            maxr[c].first=lc[t];
            maxr[c].first+=lc[tt-1];
            c++;
            cnt=0;
        }
    }
    if(s[n-1]=='R'){
        maxr[c].second.first=cnt;
        maxr[c].second.second=sl-1;
        int t = sl-1;
        while(s[t]=='R')t--;
        if(t<0)t=0;
        maxr[c].first=lc[t];
    }

    sort(maxr,maxr+n,greater<pair<int,pair<int,int>>>());
    c=0;
    rep(i,n){
       // cout<<"lc:"<<maxr[i].first<<" r:"<<maxr[i].second.first<<" zahyou:"<<maxr[i].second.second<<endl;

    }
    for(int i=0;i<k;i++){
        int j=maxr[c].second.second;
        while(j<0){
            c++;
            j=maxr[c].second.second;}
        while(s[j]=='R'){
            s[j]='L';
            j--;
        }
        c++;
    }
    rep(i,sl-1){
        if(s[i]==s[i+1]){
            ans++;
        }
    }
//
    int ans2 = 0;
//    memset(maxr,0,sizeof(maxr));
rep(i,n){
    maxr[i].first=0;
    maxr[i].second.first=0;
    maxr[i].second.second=0;
}
    c= 0;
    cnt=0;
    rep(i,sl){
        if(ss[i]=='L'){
            cnt++;
        }else{
            if(cnt==0)continue;
            maxr[c].second.first=cnt;
            maxr[c].second.second=i-1;
            int t = i-1;
            int tt = i;
            while(ss[tt]=='R')tt++;
            while(ss[t]=='L')t--;
            if(t<0)t=0;
            if(tt>sl-1)tt=sl-1;
            maxr[c].first=rc[t];
            maxr[c].first+=rc[tt-1];
            c++;
            cnt=0;
        }
    }
    if(ss[n-1]=='L'){
        maxr[c].second.first=cnt;
        maxr[c].second.second=sl-1;
        int t = sl-1;
        while(ss[t]=='L')t--;
        if(t<0)t=0;
        maxr[c].first=rc[t];
    }

    sort(maxr,maxr+n,greater<pair<int,pair<int,int>>>());
    c=0;
    // rep(i,sl){
    //     cout<<"lc:"<<maxr[i].first<<" r:"<<maxr[i].second.first<<" zahyou:"<<maxr[i].second.second<<endl;
    // }
    for(int i=0;i<k;i++){
        int j=maxr[c].second.second;
        while(j<0){
            c++;
            j=maxr[c].second.second;}

        while(ss[j]=='L'){
            ss[j]='R';
            j--;
        }
        c++;
    }
    rep(i,sl-1){
       // cout<<ss[i]<<" ";
        if(ss[i]==ss[i+1]){
            ans2++;
        }
    }
    if(ans2>ans)ans=ans2;
    cout<<ans<<endl;
}