stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}




// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

} 

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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));

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;



}




























// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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; 

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>



using namespace std;



int n, k;

int main()

{

    cin >> n >> k;

    char c, cc;

    int kk = 0;

    cin >> c;

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

    {

        cin >> cc;

        if (cc != c) ++kk;

        c = cc; 

    }

    if (kk < 2 * k) kk = 0;

    else kk -= 2 * k;

    cout << n - 1 - kk << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstdlib>

#include <cstring>

#include <cassert>

#include <string>

using namespace std;



int N, K, worry;

string S;



int main(){

	cin >> N >> K >> S;

	int S_length = S.length();

	worry = 0;

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

		if (S[i] != S[i - 1])

			worry++;

	worry = max(worry - K * 2, 0);

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>



using namespace std;



int n, k,h=0;



int main()

{

	char s[100005];

	cin >> n >> k;

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

		s[i] = getchar();

	}



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

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

	if (n - h + 2 * k - 1 <= n - 1)h = n - h + 2 * k - 1;

	else h = n - 1;

	printf("%d",h);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std; 



int main()

{

	int N,K;

	char a[100000];

	cin>>N;

	cin>>K;

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

	{

		cin>>a[i];

	}	

	int m=1;

	char n=a[0];

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

	{

		

		if(n==a[i]);

		else {

			m++;

			n=a[i];		

		}

	}

	int M=N-m;

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

	{

		m-=2;

		if(m>0) M+=2;

		else if(m<0) break;

		 else

			{

			M+=1;

			break;

			} 			

	}

	cout<<M;

	return 0;

 } 

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

int main(){

int n,k,t, tt = 0;

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

int res = k * 2;

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

for (; (t = getchar()) < 33; );

if (t == tt) res ++ ;

tt = t;

}

if (res >= n) res = n - 1;

printf("%d", res);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>



void swap(char& left, char& right)

{

  char temp = left;

  left = right;

  right = temp;

}



void reverse(char* arr, int begin, int end)

{

  int i = begin, k = end;

  while (i < k)

  {

    k -= 1;

    swap(arr[i], arr[k]);

    i += 1;

  }

  for (int j = begin; j < end; j += 1)

    arr[j] = (arr[j] == 'L' ? 'R' : 'L');

}



long long count_happy(char* arr, int length)

{

  long long res = 0;

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

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

      res += 1;

  return res;

}



void print(char* queue, int n)

{

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

    std::cout << queue[i];

  std::cout << "\n";

}



int main()

{

  int n, k;

  std::cin >> n >> k;

  char* queue = new char[n];

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

    std::cin >> queue[i];

  

  for (int i = 1; i < n && k > 0; i += 1)

    if (queue[i] != queue[i - 1])

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

        if (queue[j - 1] == queue[i] && queue[j] == queue[i - 1])

        {

          reverse(queue, i, j);

          k -= 1;

          break;

        }



  for (int i = 1; i < n && k > 0; i += 1)

    if (queue[i] != queue[i - 1])

    {

      k -= 1;

      if (queue[i - 1] == queue[0])

        reverse(queue, 0, i);

      else if (queue[i] == queue[n - 1])

        reverse(queue, i, n);

      else

        k += 1;

    }



  std::cout << count_happy(queue, n);

  delete[] queue;

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

char s[100005];

int main(){

	int n,k,ans,ls=1;

	char x,xx='A';

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

	scanf("%s",s);

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

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

	}

	if(ls-2*k<=1) ans=n-1;

	else ans=n-(ls-2*k);

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>



using namespace std;



//inline int min(int a, int b) { return a > b ? b : a; }



int main()

{

	int n, ans = 0;

	cin >> n;



	int k; cin >> k; int sch = 0;

	char ol = 0, ne;

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

	{

		cin >> ne;

		if (ol != ne)

			sch++;

		ol = ne;

	}

	int vsp = n - sch + 2 * k;

	ans = min(vsp, n - 1);



	cout << ans;

	system("pause");

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>

using namespace std;

int min(int a, int b);

int main()

{

    char queue[100000];

    int happy_num = 0;

    int len, time;

    scanf("%d%d",&len, &time);

    scanf("%s", queue);

    for(int i = 0; i < strlen(queue);i++)

    {

        if((i != strlen(queue)-1)&&(queue[i+1] == 'R')&&(queue[i] == 'R'))

            happy_num++;

        else if((i!=0)&&(queue[i-1] == 'L')&&(queue[i] == 'L'))

            happy_num ++;

    }

    printf("%d", min((happy_num + 2*time), len -1));

    return 0;

}

int min(int a, int b)

{

    return (a>=b)?b:a;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

using namespace std;



int main()

{

	int n, k, pairs = 0;

	bool fst_pair = 0;

	long long sum = 0;

	cin >> n >> k;

	string s;

	cin >> s;

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

	{

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

		{

			sum++;

		}

	}

	

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

	{

		if (fst_pair == 0 && s[i] != s[i + 1])

		{

			fst_pair = 1;

		}

		else if (fst_pair == 1 && s[i] != s[i + 1])

		{

			pairs++;

			fst_pair = 0;

		}

	}

	while (pairs && k)

	{

		pairs--;

		k--;

		sum += 2;

	}

	if (k && s[0] != s[n - 1])

	{

		k--;

		sum++;

	}

	cout << sum;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>

using namespace std;

int min(int a, int b);

int main()

{

    char queue[100000];

    int happy_num = 0;

    int len, time;

    scanf("%d%d",&len, &time);

    scanf("%s", queue);

    for(int i = 0; i < strlen(queue);i++)

    {

        if((i != strlen(queue)-1)&&(queue[i+1] == 'R')&&(queue[i] == 'R'))

            happy_num++;

        else if((i!=0)&&(queue[i-1] == 'L')&&(queue[i] == 'L'))

            happy_num ++;

    }

    printf("%d", min((happy_num + 2*time), len -1));

    return 0;

}

int min(int a, int b)

{

    return (a>=b)?b:a;

}

// Fastest known solution for this problem: