/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE PROHIBITED   **********************************
*/
 
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define pb          emplace_back
#define pf          emplace_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define hell        998244353 
#define PI          3.141592653589
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MAX         2000000000000000000
#define M           1000000007
using namespace std;
 
signed main()
{
    tezz

    int n, k;
    cin>>n>>k;

    string s;
    cin>>s;

    vector<pair<int, pii>> a;
    int fl=0, ll=0, fr=0, lr=0;
    char c = 'L';
    int curr = -1;

    for(int i=0;i<n;i++){
        if(s[i] == c && curr>=0){
            if(i - curr - 1) a.push_back({i - curr - 1, {curr+1, i}});
            curr = i;
        }
        else if(s[i] == c) {curr = i; fl = i;}
    }
    ll = n-curr-1;

    vector<pair<int, pii>> b;
    c = 'R';
    curr = -1;

    for(int i=0;i<n;i++){
        if(s[i] == c && curr >= 0){
            if(i - curr - 1) b.push_back({i - curr - 1, {curr+1, i}});
            curr = i;
        }
        else if(s[i] == c) {curr = i; fr = i;}
    }
    lr = n-curr-1;

    sort(rall(a));
    sort(rall(b));

    int tempk = k;
    string str = s;

    for(int i=0;i<min(k, (int)a.size());i++){
        int x = a[i].y.x, y = a[i].y.y;
        for(int j=x;j<y;j++){
            str[j] = 'L';
        }
        tempk--;
    }
    
    if(tempk){
        if(fl >= ll){
            for(int i=0;i<n;i++){
                if(str[i] == 'L') break;
                str[i] = 'L';
            }
            fl = 0;
        }
        else{
            for(int i=n-1;i>=0;i--){
                if(str[i] == 'L') break;
                str[i] = 'L';
            }
            ll = 0;
        }
        tempk--;
    }
    if(tempk){
        if(fl >= ll){
            for(int i=0;i<n;i++){
                if(str[i] == 'L') break;
                str[i] = 'L';
            }
            fl = 0;
        }
        else{
            for(int i=n-1;i>=0;i--){
                if(str[i] == 'L') break;
                str[i] = 'L';
            }
            ll = 0;
        }
    }

    int ans = 0;
    tempk = k;

    for(int i=0;i<n-1;i++){
        if(str[i] == str[i+1]) ans++;
    }
    // cout<<str<<endl;

    str = s;
    int mx = ans;
    ans = 0;
    for(int i=0;i<min(k, (int)b.size());i++){
        int x = b[i].y.x, y = b[i].y.y;
        for(int j=x;j<y;j++){
            str[j] = 'R';
        }
        tempk--;
    }

    if(tempk){
        if(fl >= ll){
            for(int i=0;i<n;i++){
                if(str[i] == 'R') break;
                str[i] = 'R';
            }
            fl = 0;
        }
        else{
            for(int i=n-1;i>=0;i--){
                if(str[i] == 'R') break;
                str[i] = 'R';
            }
            ll = 0;
        }
        tempk--;
    }
    if(tempk){
        if(fl >= ll){
            for(int i=0;i<n;i++){
                if(str[i] == 'R') break;
                str[i] = 'R';
            }
            fl = 0;
        }
        else{
            for(int i=n-1;i>=0;i--){
                if(str[i] == 'R') break;
                str[i] = 'R';
            }
            ll = 0;
        }
    }

    for(int i=0;i<n-1;i++){
        if(str[i] == str[i+1]) ans++;
    }
    // cout<<str<<endl;

    mx = max(mx, ans);
    cout<<mx;
} 

Measured memory_bytes: 3780