stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<algorithm>/*{{{*/

#include<cctype>

#include<cassert>

#include<cmath>

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<ctime>

#include<iostream>

#include<map>

#include<queue>

#include<set>

#include<vector>

using namespace std;

typedef long long lld;

typedef long double lf;

typedef unsigned long long uld;

typedef pair<int,int> pii;

#define fi first

#define se second

#define pb push_back

#define mk make_pair

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

#define ROF(i,a,b) for(int i=(a);i>=(b);--i)

namespace RA{

    int r(int p){return 1ll*rand()*rand()%p;}

    int r(int L,int R){return r(R-L+1)+L;}

}

namespace IO{

    char nc(){ static char bf[100000],*p1=bf,*p2=bf; return p1==p2&&(p2=(p1=bf)+ fread(bf,1,100000,stdin),p1==p2)?EOF:*p1++; }

    int rd(){ int res=0; char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))res=res*10+c-'0',c=getchar(); return res; }

}/*}}}*/

/******************heading******************/

const int N=1e5+5;



int n,k,ans;

char s[N];

int sig,c[2];



int main(){

    scanf("%d%d",&n,&k);

    scanf("%s",s+1);

    FOR(i,1,n){

        if(s[i]!=s[i-1])sig=!sig,c[sig]++;

        else ans++;

    }

    //printf("%d %d\n",c[0],c[1]);

    if(c[0]>c[1])swap(c[0],c[1]);

    if(k<c[0])ans+=k*2;

    else ans=n-1;

    printf("%d",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.212904891180581x):
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

using namespace std;

const int maxd = 1e5+10;

char s[maxd];

int main()

{

    // freopen("a.in","r",stdin);

    // freopen("k.out","w",stdout);

    int n,k,ans=0,v;

    scanf("%d%d%s",&n,&k,s);

    for(int i=1;s[i];i++)

    {

        if(s[i]==s[i-1]) ans++;

    }

    printf("%d",min(ans+2*k,n-1));

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

using namespace std;

int main(){

	string S;

	int N,K,out=0,count=0;

	cin>>N>>K>>S;

	int i=0,j=1;

	while(j<N){ 

		if(S[i]==S[j]){ 

		    j++;

		    out++;} 

		else 

		{i=j;

		j++;}}

	for(int t=K;t>0;t--)	

    out+=2;

    if(out>=N-1)

    out=N-1;

    cout<<out;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.229535025812812x):
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

using namespace std;

const int maxd = 1e5+10;

char s[maxd];

int main()

{

    // freopen("a.in","r",stdin);

    // freopen("k.out","w",stdout);

    int n,k,ans=0,v;

    scanf("%d%d%s",&n,&k,s);

    for(int i=1;s[i];i++)

    {

        if(s[i]==s[i-1]) ans++;

    }

    printf("%d",min(ans+2*k,n-1));

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<algorithm>



using namespace std;



int main()

{

	int n,k;

	int max=0;

	string s;

	cin>>n>>k>>s;

	for(int i=1;i<n;i++)

		if(s[i]==s[i-1]) max++;

	max=min(max+2*k,n-1);

	cout<<max;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.231334070777873x):
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

using namespace std;

const int maxd = 1e5+10;

char s[maxd];

int main()

{

    // freopen("a.in","r",stdin);

    // freopen("k.out","w",stdout);

    int n,k,ans=0,v;

    scanf("%d%d%s",&n,&k,s);

    for(int i=1;s[i];i++)

    {

        if(s[i]==s[i-1]) ans++;

    }

    printf("%d",min(ans+2*k,n-1));

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int INF = 1e9;



struct Block {

    bool dir = false; // 0 - L, 1 - R

    int cnt = 0;

};



int main() {

    int n, k;

    cin >> n >> k;

    string s;

    cin >> s;

    

    deque<Block> q;

    

    int cnt = 1;

    for (int i = 1; i <= n; ++i) {

        if (i == n || s[i] != s[i - 1]) {

            q.push_back({s[i - 1] == 'R', cnt});

            cnt = 1;

        } else {

            cnt++;

        }

    }

    

    // for (Block &b : q) {

    //     for (int i = 0; i < b.cnt; ++i)

    //         cout << (b.dir ? 'R' : 'L');

    // }

    // cout << '\n';

    

    for (int i = 0; i < k; ++i) {

        if (q.size() == 1) {

            q[0].dir = !q[0].dir;

        } else if (q.size() == 2) {

            q[1].cnt += q[0].cnt;

            q.pop_front();

        } else {

            q[2].cnt += q[0].cnt;

            q[2].cnt += q[1].cnt;

            q.pop_front();

            q.pop_front();

        }

    }

    

    int ans = 0;

    for (Block &b : q) {

        ans += b.cnt - 1;

        // for (int i = 0; i < b.cnt; ++i)

        //     cout << (b.dir ? 'R' : 'L');

    }

    // cout << '\n';

        

    cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 5.285749056012204x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <string>

using namespace std;

int main() {

	long long n, k;

	string s;

	cin >> n >> k >> s;

	long long p = 0, d = 0;

	for (int i = 0; i < n - 1; ++i)

		if (s[i] == s[i + 1]) ++p;

		else ++d;

	if (k >= d / 2) p += d / 2 * 2, p += d % 2 == 1 && k >= d / 2 + 1 ? 1 : 0;

	else p += k * 2;

	cout << p;

	return 0;

}






// Faster C++ solution for the same problem (measured speedup: 5.272035324280499x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n, k;

string s;



int main() {

    //freopen("sample.in", "r", stdin);

    cin >> n >> k;

    cin >> s;

    int ret = 0;

    for(int i = 1; i < n; i++)

        if (s[i] == s[i - 1]) ret++;

    cout << min(ret + 2 * k, n - 1) << endl; 

}

// Faster C++ solution for the same problem (measured speedup: 5.244805426932484x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<algorithm>/*{{{*/

#include<cctype>

#include<cassert>

#include<cmath>

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<ctime>

#include<iostream>

#include<map>

#include<queue>

#include<set>

#include<vector>

using namespace std;

typedef long long lld;

typedef long double lf;

typedef unsigned long long uld;

typedef pair<int,int> pii;

#define fi first

#define se second

#define pb push_back

#define mk make_pair

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

#define ROF(i,a,b) for(int i=(a);i>=(b);--i)

namespace RA{

    int r(int p){return 1ll*rand()*rand()%p;}

    int r(int L,int R){return r(R-L+1)+L;}

}

namespace IO{

    char nc(){ static char bf[100000],*p1=bf,*p2=bf; return p1==p2&&(p2=(p1=bf)+ fread(bf,1,100000,stdin),p1==p2)?EOF:*p1++; }

    int rd(){ int res=0; char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))res=res*10+c-'0',c=getchar(); return res; }

}/*}}}*/

/******************heading******************/

const int N=1e5+5;



int n,k,ans;

char s[N];

int sig,c[2];



int main(){

    scanf("%d%d",&n,&k);

    scanf("%s",s+1);

    FOR(i,1,n){

        if(s[i]!=s[i-1])sig=!sig,c[sig]++;

        else ans++;

    }

    //printf("%d %d\n",c[0],c[1]);

    if(c[0]>c[1])swap(c[0],c[1]);

    if(k<c[0])ans+=k*2;

    else ans=n-1;

    printf("%d",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.216189415979868x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

using namespace std;

int main(){

	string S;

	int N,K,out=0,count=0;

	cin>>N>>K>>S;

	int i=0,j=1;

	while(j<N){ 

		if(S[i]==S[j]){ 

		    j++;

		    out++;} 

		else 

		{i=j;

		j++;}}

	for(int t=K;t>0;t--)	

    out+=2;

    if(out>=N-1)

    out=N-1;

    cout<<out;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.2328300288561405x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

#include<algorithm>



using namespace std;



int main()

{

	int n,k;

	int max=0;

	string s;

	cin>>n>>k>>s;

	for(int i=1;i<n;i++)

		if(s[i]==s[i-1]) max++;

	max=min(max+2*k,n-1);

	cout<<max;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.234630207355753x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

using namespace std;

//参考郑伟杰 

int main(){

	string S;

	int N,K,h=0,count=0;

	cin>>N>>K>>S;

	int i=0,j=1;

	while(j<N)

	{ 

		if(S[i]==S[j])

		{ 

		    j++;

		    h++;} 

		else 

		{

		  i=j;

		  j++;

		}

	}

	for(int t=K;t>0;t--)	

        h+=2;

    if(h>=N-1)

        h=N-1;

    cout<<h;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.2328300288561405x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>



using namespace std;







int main()

{

    int N, K;

    string S;

    int i, j, k = 0;

	int happy = 0;

	cin>>N>>K>>S;

	i = 0;

	j = i + 1;

	while(j < N)

	{

		if(S[i] == S[j])

		{

			j++;

			happy++;

		}

		else

		{

			i = j;

			j++;

		}

	}

	for(k = K; k > 0; k--)

	{

		happy += 2;

	}

	if(happy >= N-1)

		happy = N-1;

	cout<<happy;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 5.2328300288561405x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string>

using namespace std;

int main(){

    int N,K;

    cin >> N >> K;

    string S;

	cin >> S; 

	int temp=0;

	for(size_t i=1;i<S.length();i++)

	    if(S[i]==S[i-1]) temp++;

	if(temp+2*K<N-1) 

	    cout << temp+2*K;

	else 

	    cout << N-1;

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.234301060618167x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;

char line[100000];

int min(int a, int b)

{

	if (a <= b)

		return a;

	else

		return b;

}

int main()

{

	long n,k;

	long i;

	long sum;

	cin >> n;

	cin >> k;

	for (i = 0; i < n; i++)

	{

		cin >> line[i];

	}

	int m = 0;

	for (i = 0; i < n - 1; i++)

	{

		if (line[i] != line[i + 1])

			m++;

	}

			sum = n - 1 - m + 2 * k;

		sum = min(sum, (n - 1));

	cout << sum;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.226903324225904x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

#define ll long long

#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

using namespace std;

const int N = 1e5 + 5, M = 3e5 + 5;



int n, k;

char s[N];

int a[N], sz;

int main(){

    scanf("%d%d%s", &n, &k, &s);

    int lst = '#';

    int cnt = 0;

    for(int i = 0 ; i < n ; ++i){

        if(s[i] != lst){

            if(cnt)

                a[sz++] = cnt;

            cnt = 0;

        }

        lst = s[i];

        ++cnt;

    }

    a[sz++] = cnt;

    if(sz == 1){

        printf("%d\n", n - 1);

        exit(0);

    }

    if(sz == 2){

        if(k)printf("%d\n", n - 1);

        else printf("%d", a[0] + a[1] - 2);

        exit(0);

    }

    int ans = 0;

    for(int i = 0 ; i < sz ; ++i)

        ans += (a[i] - 1);

    if(sz & 1)

        printf("%d\n", ans + min(k, sz / 2) * 2);

    else

        printf("%d\n", ans + min(k * 2, sz - 1));

}

// Faster C++ solution for the same problem (measured speedup: 5.175229544960665x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <fstream>

#include <sstream>

#include <iomanip>

#include <algorithm>

#include <numeric>

#include <vector>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <climits>

#include <cmath>

#include <cstring>

#include <unordered_set>

#include <unordered_map>

using namespace std;



#define pb push_back

#define fst first

#define snd second



typedef long long ll;

typedef pair<int,int> pii;

template<typename T> using min_queue=priority_queue<T,vector<T>,greater<T>>;



const ll MOD=1e9+7;



int N,K;

string s;



int main(){

  ios::sync_with_stdio(0);cin.tie(0);



  cin>>N>>K>>s;



  int cnt=0;

  char lst = -1;



  for(char c:s) {

    if (c != lst) cnt++;

    lst=c;

  }



  cnt = max(cnt-2*K, 1);

  cout<<N-cnt<<endl;



  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.313861957205013x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#include<algorithm>

#include<string>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/rope>

using namespace __gnu_cxx;

using namespace __gnu_pbds;

using namespace std;



#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#define pi acos(-1.0)



#define sf(a) scanf("%lld",&a)

#define sff(a,b) scanf("%lld %lld",&a,&b)

#define sfff(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)

#define pf(a) printf("%lld\n",a)



#define stll(a) stoll(a,nullptr,10)

#define all(a) a.begin(),a.end()

#define PII pair<ll,ll>

#define MP(a,b) make_pair(a,b)

#define pb(a) push_back(a)

#define EL printf("\n")

#define ff first

#define ss second

#define rev(a) reverse(all(a))





#define mem(ar,val) memset(ar,val,sizeof(ar))

#define ssort(ar) sort(ar.begin(),ar.end())

#define csort(ar,comp) sort(ar.begin(),ar.end(),comp)



#define bug1(a) cout<<"test "<<a<<endl;

#define bug2(a,b) cout<<"test "<<a<<" "<<b<<endl;

#define bug3(a,b,c) cout<<"test "<<a<<" "<<b<<" "<<c<<endl;

#define bug4(a,b,c,d) cout<<"test "<<a<<" "<<b<<" "<<c<<" "<<d<<endl;



#define fout freopen("output.txt","w",stdout)

#define fin  freopen("input.txt","r",stdin)



/// unordered_map<int,int>mp;

/// mp.reserve(1024);

/// mp.max_load_factor(0.25);



/// pbds

/// *find_by_order(k) - return k th largest element.. 0 based

/// order_of_key(k) - number of items strictly smaller than k



/*

struct HASH{

  size_t operator()(const pair<int,int>&x)const{

    return (size_t) x.first * 37U + (size_t) x.second;

  }

};

*/



typedef long long ll;

typedef unsigned long long ull;

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;





int main()

{

    ll n,k;

    string st;

    cin>>n>>k>>st;

    ll ans =0;

    for(int i=0;i<n-1;i++)

        if(st[i]==st[i+1])

            ans++;

    cout<<min(n-1,ans+2*k)<<endl;



}




























// Faster C++ solution for the same problem (measured speedup: 5.280833492278951x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <algorithm>

#include <cstring>

#include <vector>

#include <queue>

using namespace std;

#define pb push_back

#define mp make_pair

#define fi first 

#define se second

#define mst(x,y) memset(x,y,sizeof(x))



typedef pair<int,int> pii;

typedef long long ll;

typedef double db;

const ll INF = 0x7fffffff;

const db eps = 1e-5;

const ll mod = 1e9+7;

const int MAXN = 100010;

int MIN(int a, int b) {return a>b?b:a;}

int main() {

	ios_base::sync_with_stdio(0);

	int n,m,x=0;

	char a[MAXN];

	cin>>n>>m>>a;

	x += (a[0] == 'R' && a[1] == 'R')+(a[n-1] == 'L' && a[n-2] == 'L');

	for (int i=1;i<n-1;i++) {

		x += (a[i]=='L' && a[i-1] != 'R') + (a[i] == 'R' && a[i+1] != 'L');

	}

	cout<<MIN(n-1,x+2*m)<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.307003634444159x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>



#define ll long long



using namespace std;



const int N = 100010;

const int mod = 10007;



inline int read() {

	int jg = 0, jk = getchar() - '0', f = 1;

	while(jk < 0 || jk > 9) {

	    if(jk == '-' - '0')  f = -1;

		jk = getchar() - '0';

	}

	while(jk >= 0 && jk <= 9)

		jg *= 10, jg += jk, jk = getchar() - '0';

	return jg * f;

}



int n, ans, k;

char s[N];



int main() {

	//freopen("a.txt", "r", stdin);

    n = read(), k = read();

	scanf("%s", s);

	for(int i = 1; i < n; ++i)

		if(s[i] == s[i - 1])

			ans++;

	ans += 2 * k;

	ans = min(ans, n - 1);

	printf("%d", ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.168550943094141x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<string>

#include<iostream>

using namespace std;

int n,m,ans=0,cnt=1;

string s;

int main(){

    scanf("%d%d",&n,&m);

    cin>>s;

    char pre=s[0];

    for(int i=1;i<n;i++){

        if(s[i]==pre)ans++;

        else pre=s[i],cnt++;

    }



    if(cnt-m*2>=1)cnt-=m*2;

    else cnt=1;

    printf("%d\n",n-cnt);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.23354220653398x):
#include<cstdio>

#include<algorithm>

#include<cmath>

#define ll long long

#define N 100005

#define inf 0x3fffffff

#define rep(i, l, r) for(int i=l; i<=r; ++i)

using namespace std;

int n, k;

char s[N];

int main(){

    scanf("%d%d%s", &n, &k, s);

    int tmp=0;

    rep(i, 0, n-1){

        if(!i || s[i]!=s[i-1]) ++tmp;

    }

    tmp=max(1, tmp-2*k);

    printf("%d", n-tmp);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h> 

using namespace std;



int main() {

	int N;

	int k;

	cin >> N;

	cin >> k;

	int r = 0;

	int sum = 0;

    char a[N+1];

    for(int i = 1; i <= N; i++)

    cin >> a[i];

    for(int i = 1; i <= N; i++)

    {

    	if (a[i] == a[i+1])

     	sum++;

    	if ((a[i+1] != a[i]) && (i != N))

    	r++;

    }

    if((r+1)/2>k)

    sum += k*2;

    else 

    sum = N - 1;

	cout << sum;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.222901691240069x):
#include <cstdio>

#include <algorithm>

using namespace std;



const int maxn = 1e5;

char s[maxn + 1];



int main(){

  int n, k, block = 1;

  scanf("%d%d%s", &n, &k, s);

  for(int i = 1; i < n; i++)

    if(s[i] != s[i - 1]) block++;

  int ans = n - block + min(k, block / 2) * 2;

  if(ans == n) ans--;

  printf("%d", ans);

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n, k;

string s;



int main() {

    cin >> n >> k;

    cin >> s;

    int ret = 0;

    for(int i = 1; i < n; i++)

        if (s[i] == s[i - 1]) ret++;

    cout << min(ret + 2 * k, n - 1) << endl; 

}

// Faster C++ solution for the same problem (measured speedup: 5.238736916763253x):
#include <cstdio>

#include <algorithm>

using namespace std;



const int maxn = 1e5;

char s[maxn + 1];



int main(){

  int n, k, block = 1;

  scanf("%d%d%s", &n, &k, s);

  for(int i = 1; i < n; i++)

    if(s[i] != s[i - 1]) block++;

  int ans = n - block + min(k, block / 2) * 2;

  if(ans == n) ans--;

  printf("%d", ans);

  return 0;

}