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

#include <cstring>

#include <cstdio>

#include <cmath>

#include <algorithm>

#include <vector>

using namespace std;

#define fir first

#define sec second

#define pb push_back

#define lowbit(x) ((x)&(-(x))) 



int n, k; 

string s; 



int main() {

	cin>>n>>k>>s; ;

	int num = 1;  

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

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

	cout<<n-max(1,(num-2*k))<<endl;  

}


// Faster C++ solution for the same problem (measured speedup: 5.236969869620315x):
#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;

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

#include <iostream>

#include <algorithm>





using namespace std;



int main(){

	int n, k, c = 0, an, i;

	char s[100005] = { '\0' };

	

	cin >> n >> k;

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

		cin >> s[i];

	}

	int count = 0;

	i = 0;

	if (s[i] == 'R'&&s[i + 1] == 'R') count++;

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

		if (s[i] == 'L'&&s[i - 1] == 'L') count++;

		if (s[i] == 'R'&&s[i + 1] == 'R') count++;

	}

	if (s[i] == 'L'&&s[i - 1] == 'L') count++;

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

//		if (s[i] != s[i + 1]) c++;

//	}

//	cout << count << endl;

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

	cout << an;

	system("pause");

	return 0;

}

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

#include<string>

#include<algorithm>

#define INF 500050

#define Max 1e9

using namespace std;

int r,l,c;

int n,k,ans=0;

string s;

char ch;

bool flag = false;

int main()

{

	cin>>n>>k;

	cin>>s;

	int t = 0;

	for(int i=0; i<s.length()&&k; i++) {

		if(s[i]=='L'&&s[i+1] == 'R') {

			int pi = i+1;

			while(s[pi]=='R'&&pi<s.length()) 

			{

				s[pi]='L';

				pi++;

			}

				k--;

		}

		if(s[i]=='R'&&s[i+1] == 'L') {

			int pi = i+1;

			while(s[pi]=='L'&&pi<s.length()) 

			{

				s[pi]='R';

				pi++;

			}

			k--;

		}

	}

	

	

	for(int i=1; i<s.length(); i++) {

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

	}



	ans=min(ans,n-1);

	cout << ans <<endl; 

	

	

	return 0;

}
// Slower C++ solution:
#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;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

}
// Slower C++ solution:
#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;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

using namespace std;

int main(){

    int n, k;

    cin >> n >> k;

    int a;

    int last = 0;

    int ans = 0;

    int kol = 1;

    char s;

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

        cin >> s;

        if(s=='R')a=1;

        else a=0;

        if(i==0){

            last = a;

        }else{

            if(a!=last)kol++;

            else ans++;

            last = a;

        }

    }

    if(n>1 && k<n-1){

        if(kol>=2){

            if(k>=kol/2)cout<<n-1;

            else{

                ans+= 2*k;

                cout << ans ;

            }

        }else{

            cout << n-1;

        }

    }else{

        if(n == 1)cout << 0;

        else cout << n-1;

    }

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include<vector>

#include<algorithm>

#include<string>



using namespace std;



int main() {

	int n, k, ans = 0;

	cin >> n >> k;

	string s;

	cin >> s;

	vector<int> a(1, 1);

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

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

			a.back() += 1;

			ans += 1;

		}

		else {

			a.push_back(1);

		}

	}

	ans += min((int)a.size() - 1, 2 * k);

	cout << ans;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include <string>

int main() {

    size_t      n, k;

    std::string s;

    std::cin >> n >> k >> s;

    size_t result = 0;

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

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

    result += k << 1;

    std::cout << (result >= n ? n - 1 : result) << '\n';

    return 0;

}


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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include <iostream>

#include <algorithm>





using namespace std;



int main(){

	int n, k, c = 0, an, i;

	char s[100005] = { '\0' };

	

	cin >> n >> k;

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

		cin >> s[i];

	}

	int count = 0;

	i = 0;

	if (s[i] == 'R'&&s[i + 1] == 'R') count++;

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

		if (s[i] == 'L'&&s[i - 1] == 'L') count++;

		if (s[i] == 'R'&&s[i + 1] == 'R') count++;

	}

	if (s[i] == 'L'&&s[i - 1] == 'L') count++;

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

//		if (s[i] != s[i + 1]) c++;

//	}

//	cout << count << endl;

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

	cout << an;

	system("pause");

	return 0;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

using namespace std;

int n,k,t;

char c[100010];

int main(){

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

	for(int i=2;i<=n;++i)t+=c[i]!=c[i-1];

	printf("%d",n-1-max(0,t-(k<<1)));

	return 0;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include <cstring>

#include <cstdio>

#include <cmath>

#include <algorithm>

#include <vector>

using namespace std;

#define fir first

#define sec second

#define pb push_back

#define lowbit(x) ((x)&(-(x))) 



int n, k; 

string s; 



int main() {

	cin>>n>>k>>s; ;

	int num = 1;  

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

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

	cout<<n-max(1,(num-2*k))<<endl;  

}


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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include<string>

#include<malloc.h>



using namespace std;



int ishappy(string s,int k){

	int len=s.size();

	char direction=s[k];

	if(direction=='L'){

		if(k==0) return 0;

		char front=s[k-1];

		if(front=='L') return 1;

	}

	if(direction=='R'){

		if(k==len-1) return 0;

		char front=s[k+1];

		if(front=='R') return 1;

	}

	return 0;

}



int Cal(string s){

	int sum=0;

	for(int i=0;i<s.size();i++)

	    sum+=ishappy(s,i);

	return sum;

}



int main(){

	

    int N,K,MAX;

    string S; 

    cin>>N>>K>>S;

    MAX=Cal(S); 

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

	 if(MAX==N-1)  break;

	 if(MAX==N-2)  {

	     MAX+=1;

	     break;

	 }

     MAX+=2;

	}

	

    cout<<MAX<<endl;

    return 0;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include<string>

#include<algorithm>

#define INF 500050

#define Max 1e9

using namespace std;

int r,l,c;

int n,k,ans=0;

string s;

char ch;

bool flag = false;

int main()

{

	cin>>n>>k;

	cin>>s;

	int t = 0;

	for(int i=0; i<s.length()&&k; i++) {

		if(s[i]=='L'&&s[i+1] == 'R') {

			int pi = i+1;

			while(s[pi]=='R'&&pi<s.length()) 

			{

				s[pi]='L';

				pi++;

			}

				k--;

		}

		if(s[i]=='R'&&s[i+1] == 'L') {

			int pi = i+1;

			while(s[pi]=='L'&&pi<s.length()) 

			{

				s[pi]='R';

				pi++;

			}

			k--;

		}

	}

	

	

	for(int i=1; i<s.length(); i++) {

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

	}



	ans=min(ans,n-1);

	cout << ans <<endl; 

	

	

	return 0;

}

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

#include<cstring>

char ren[1000000];

int main()

{

	int n,k;

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

	int xf=0;

	int j2=0;

	scanf("%s",&ren); 

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

	{

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

		xf++;

	}

//	printf("xf%d\n",xf);

	if(xf+2*k>n-1)

	xf=n-1;

	else

	xf=xf+2*k;

	

	printf("%d",xf);

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

#include <iostream>

#include <algorithm>





using namespace std;



int main(){

	int n, k, c = 0, an, i;

	char s[100005] = { '\0' };

	

	cin >> n >> k;

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

		cin >> s[i];

	}

	int count = 0;

	i = 0;

	if (s[i] == 'R'&&s[i + 1] == 'R') count++;

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

		if (s[i] == 'L'&&s[i - 1] == 'L') count++;

		if (s[i] == 'R'&&s[i + 1] == 'R') count++;

	}

	if (s[i] == 'L'&&s[i - 1] == 'L') count++;

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

//		if (s[i] != s[i + 1]) c++;

//	}

//	cout << count << endl;

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

	cout << an;

	system("pause");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.113645774041919x):
#include<bits/stdc++.h>



#define SORT(v) sort(v.begin(),v.end())

#define si(n) scanf("%d",&n)

#define sii(n,m) scanf("%d %d",&n,&m)

#define sl(n) scanf("%lld",&n)

#define sll(n,m) scanf("%lld %lld",&n,&m)

#define ss(cad) scanf("%s",cad)

#define PB push_back

#define fst first

#define scn second

#define DBG(x) cerr << #x << " = " << (x) << endl; 

#define M 1000000007

#define N_MAX 100010



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<bool> vb;

typedef vector<ll> vl;

typedef pair<int,int> pi;

typedef vector<pi> vp;



int main()

{

	int n, k;

	sii(n, k);

	char cad[n+6];

	ss(cad);

	int tip = 1, sum = 0, cnt = 0;

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

	{

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

			cnt++;

		else

		{

			tip++;

			sum += cnt;

			cnt = 0;

		}

	}

	sum += cnt;

	if(tip/2 <= k)

		sum = n-1;

	else

		sum += k*2;

	printf("%d\n", sum);

    return 0;

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

#include<algorithm>

#include<math.h>

#include<string>

#include<iostream>

#include<vector>

#include<cstdlib>

using namespace std;

 

template <class T> using V = vector<T>;

 

template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}

 

const long long INF = 1LL << 60;

 

using ll = long long;

using db = double;

using st = string;

using ch = char;

using bl = bool;

using vll = V<ll>;

using vpll =V<pair<ll,ll>>;

using vst = V<st>;

using vdb = V<db>;

using vch = V<ch>;

using vbl = V<bl>;

 

 

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

#define rFOR(i,a,b) for(ll i=(a);i>(b);i--)

#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)

#define bgn begin()

#define en end()

#define SORT(a) sort((a).bgn,(a).en)

#define REV(a) reverse((a).bgn,(a).en)

#define M(a,b) max(a,b)

#define rM(a,b) min(a,b)

#define fi first

#define se second

#define sz size()

#define gcd(a,b) __gcd(a,b)

#define co(a) cout<<a<<endl;

#define ci(a) cin>>a;

 



vll dp(100100);



ll sum(ll n) {

ll m=0;

  FOR(i,0,20){

    m+=n%10;

    n/=10;

    if(n==0){

      break;

    }

  }

  return m;

}

 

 

const int MAX = 510000;

const int MOD = 1000000007;

 

long long fac[MAX], finv[MAX], inv[MAX];

 

void Comuse() {

    fac[0] = fac[1] = 1;

    finv[0] = finv[1] = 1;

    inv[1] = 1;

    for (int i = 2; i < MAX; i++){

        fac[i] = fac[i - 1] * i % MOD;

        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;

        finv[i] = finv[i - 1] * inv[i] % MOD;

    }

}

 

 

#define comuse Comuse()

 

ll combi(int n, int k){

    if (n < k) return 0;

    if (n < 0 || k < 0) return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;

}

 

 

 

 

ll lcm(ll a,ll b){

  ll n;

  n=a/gcd(a,b)*b;

  return n;

}





 

/****************************************\

| Thank you for viewing my code:)        |

| Written by RedSpica a.k.a. RanseMirage |

| Twitter:@asakaakasaka                  | 

\****************************************/

 

 

signed main() {

  ll n,k;

    ci(n>>k);

  st s;

    ci(s);

  vll A;

  ll count=1;

  FOR(i,1,n-1){

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

      A.push_back(count);

      count=1;

    }

    else{

      count++;

    }

  }



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

    A.push_back(count);

    A.push_back(1);

  }

  else{

    A.push_back(count+1);

  }



  ll a=A.sz;

  ll ans=0;

  FOR(i,0,a){

    ans+=A[i]-1;

  }

  ans+=2*k; 

  co(min(n-1,ans))

  }


// Faster C++ solution for the same problem (measured speedup: 1.8067330702200672x):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

  int N,K,A=0;

  string S;

  cin >> N >> K >> S;

  int count=1;

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

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

      count ++;

    }

  }

  int ans = N-count+2*K;

  cout <<min(N-1,ans)<<endl;

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

#include<algorithm>

#include<math.h>

#include<string>

#include<iostream>

#include<vector>

#include<cstdlib>

using namespace std;

 

template <class T> using V = vector<T>;

 

template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}

 

const long long INF = 1LL << 60;

 

using ll = long long;

using db = double;

using st = string;

using ch = char;

using bl = bool;

using vll = V<ll>;

using vpll =V<pair<ll,ll>>;

using vst = V<st>;

using vdb = V<db>;

using vch = V<ch>;

using vbl = V<bl>;

 

 

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

#define rFOR(i,a,b) for(ll i=(a);i>(b);i--)

#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)

#define bgn begin()

#define en end()

#define SORT(a) sort((a).bgn,(a).en)

#define REV(a) reverse((a).bgn,(a).en)

#define M(a,b) max(a,b)

#define rM(a,b) min(a,b)

#define fi first

#define se second

#define sz size()

#define gcd(a,b) __gcd(a,b)

#define co(a) cout<<a<<endl;

#define ci(a) cin>>a;

 



vll dp(100100);



ll sum(ll n) {

ll m=0;

  FOR(i,0,20){

    m+=n%10;

    n/=10;

    if(n==0){

      break;

    }

  }

  return m;

}

 

 

const int MAX = 510000;

const int MOD = 1000000007;

 

long long fac[MAX], finv[MAX], inv[MAX];

 

void Comuse() {

    fac[0] = fac[1] = 1;

    finv[0] = finv[1] = 1;

    inv[1] = 1;

    for (int i = 2; i < MAX; i++){

        fac[i] = fac[i - 1] * i % MOD;

        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;

        finv[i] = finv[i - 1] * inv[i] % MOD;

    }

}

 

 

#define comuse Comuse()

 

ll combi(int n, int k){

    if (n < k) return 0;

    if (n < 0 || k < 0) return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;

}

 

 

 

 

ll lcm(ll a,ll b){

  ll n;

  n=a/gcd(a,b)*b;

  return n;

}





 

/****************************************\

| Thank you for viewing my code:)        |

| Written by RedSpica a.k.a. RanseMirage |

| Twitter:@asakaakasaka                  | 

\****************************************/

 

 

signed main() {

  ll n,k;

    ci(n>>k);

  st s;

    ci(s);



  ll count=1;

  FOR(i,1,n-1){

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

      count++;

    }

  }



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

    count++;

  }



  ll ans=n-count+2*k;



  co(min(n-1,ans))

  }

// Faster C++ solution for the same problem (measured speedup: 1.8048513104070483x):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

  int N,K,A=0;

  string S;

  cin >> N >> K >> S;

  int count=1;

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

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

      count ++;

    }

  }

  int ans = N-count+2*K;

  cout <<min(N-1,ans)<<endl;

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

#include<algorithm>

#include<math.h>

#include<string>

#include<iostream>

#include<vector>

#include<cstdlib>

using namespace std;

 

template <class T> using V = vector<T>;

 

template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}

 

const long long INF = 1LL << 60;

 

using ll = long long;

using db = double;

using st = string;

using ch = char;

using bl = bool;

using vll = V<ll>;

using vpll =V<pair<ll,ll>>;

using vst = V<st>;

using vdb = V<db>;

using vch = V<ch>;

using vbl = V<bl>;

 

 

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

#define rFOR(i,a,b) for(ll i=(a);i>(b);i--)

#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)

#define bgn begin()

#define en end()

#define SORT(a) sort((a).bgn,(a).en)

#define REV(a) reverse((a).bgn,(a).en)

#define M(a,b) max(a,b)

#define rM(a,b) min(a,b)

#define fi first

#define se second

#define sz size()

#define gcd(a,b) __gcd(a,b)

#define co(a) cout<<a<<endl;

#define ci(a) cin>>a;

 



vll dp(100100);



ll sum(ll n) {

ll m=0;

  FOR(i,0,20){

    m+=n%10;

    n/=10;

    if(n==0){

      break;

    }

  }

  return m;

}

 

 

const int MAX = 510000;

const int MOD = 1000000007;

 

long long fac[MAX], finv[MAX], inv[MAX];

 

void Comuse() {

    fac[0] = fac[1] = 1;

    finv[0] = finv[1] = 1;

    inv[1] = 1;

    for (int i = 2; i < MAX; i++){

        fac[i] = fac[i - 1] * i % MOD;

        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;

        finv[i] = finv[i - 1] * inv[i] % MOD;

    }

}

 

 

#define comuse Comuse()

 

ll combi(int n, int k){

    if (n < k) return 0;

    if (n < 0 || k < 0) return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;

}

 

 

 

 

ll lcm(ll a,ll b){

  ll n;

  n=a/gcd(a,b)*b;

  return n;

}





 

/****************************************\

| Thank you for viewing my code:)        |

| Written by RedSpica a.k.a. RanseMirage |

| Twitter:@asakaakasaka                  | 

\****************************************/

 

 

signed main() {

  ll n,k;

    ci(n>>k);

  st s;

    ci(s);

  vll A;

  ll count=1;

  FOR(i,1,n-1){

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

      A.push_back(count);

      count=1;

    }

    else{

      count++;

    }

  }



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

    A.push_back(count);

    A.push_back(1);

  }

  else{

    A.push_back(count+1);

  }



  ll a=A.sz;

  ll ans=0;

  FOR(i,0,a){

    ans+=A[i]-1;

  }

  ans+=2*k; 

  co(min(n-1,ans))

  }


// Faster C++ solution for the same problem (measured speedup: 1.8070079011423654x):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

  int N,K,A=0;

  string S;

  cin >> N >> K >> S;

  int count=1;

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

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

      count ++;

    }

  }

  cout <<N+min(-1,-count+2*K)<<endl;

}