stablegradients/pie-gem5-pairs huggingface dataset:
// 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.217252880274261x):
#include <cstdio>

#include <cstdlib>

#include <algorithm>

#include <cstring>

using namespace std;

#define N 100005

int n,k,ans=0;

char s[N];

int main()

{

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

    scanf("%s",s);

    int L=strlen(s);

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

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

    ans=min(ans+(k<<1),n-1);

    printf("%d\n",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.233070979264941x):
#include <cstdio>

#include <cstdlib>

#include <algorithm>

#include <cstring>

using namespace std;

#define N 100005

int n,k,ans=0;

char s[N];

int main()

{

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

    scanf("%s",s);

    int L=strlen(s);

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

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

    ans=min(ans+(k<<1),n-1);

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

    return 0;

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

}

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

#include <cstdlib>

#include <algorithm>

#include <cstring>

using namespace std;

#define N 100005

int n,k,ans=0;

char s[N];

int main()

{

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

    scanf("%s",s);

    int L=strlen(s);

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

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

    ans=min(ans+(k<<1),n-1);

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

    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.217777280168686x):
#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;

}
// 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.233596969078359x):
#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;

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

}

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

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

}

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

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

}

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

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

 } 

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

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

}

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

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

}

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

}
// 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.224074329575716x):
#include <cstdio>

#include <cstring>



#include <algorithm>



using namespace std;



#define NMAX 100000



static int n, K;

static char s[NMAX + 10];



int main() {

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

    int m = unique(s + 1, s + n + 1) - s - 1;

    int ans = n - m;

    int c = (m + 1) / 2 - 1;

    ans += 2 * min(c, K);

    if (K > c && !(m & 1)) ans++;

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

    return 0;

}
// 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.253184492350115x):
#include <cstdio>

#include <cstring>



#include <algorithm>



using namespace std;



#define NMAX 100000



static int n, K;

static char s[NMAX + 10];



int main() {

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

    int m = unique(s + 1, s + n + 1) - s - 1;

    int ans = n - m;

    int c = (m + 1) / 2 - 1;

    ans += 2 * min(c, K);

    if (K > c && !(m & 1)) ans++;

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

    return 0;

}
// 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.208315943469849x):
#include <cstdio>

#include <cstring>



#include <algorithm>



using namespace std;



#define NMAX 100000



static int n, K;

static char s[NMAX + 10];



int main() {

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

    int m = unique(s + 1, s + n + 1) - s - 1;

    int ans = n - m;

    int c = (m + 1) / 2 - 1;

    ans += 2 * min(c, K);

    if (K > c && !(m & 1)) ans++;

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

    return 0;

}
// 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.264867661478188x):
#include <cstdio>

#include <cstring>



#include <algorithm>



using namespace std;



#define NMAX 100000



static int n, K;

static char s[NMAX + 10];



int main() {

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

    int m = unique(s + 1, s + n + 1) - s - 1;

    int ans = n - m;

    int c = (m + 1) / 2 - 1;

    ans += 2 * min(c, K);

    if (K > c && !(m & 1)) ans++;

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

    return 0;

}
// 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.21219040482269x):
#include <cstdio>

#include <cstring>



#include <algorithm>



using namespace std;



#define NMAX 100000



static int n, K;

static char s[NMAX + 10];



int main() {

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

    int m = unique(s + 1, s + n + 1) - s - 1;

    int ans = n - m;

    int c = (m + 1) / 2 - 1;

    ans += 2 * min(c, K);

    if (K > c && !(m & 1)) ans++;

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

    return 0;

}
// 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.114283445384037x):
#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;

}
// 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.237134957597812x):
#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 <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.266317898243212x):
#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 <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.221337174192793x):
#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<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.278030276283042x):
#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 <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.225221322026974x):
#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: 5.7571935645259265x):
#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<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.1667226937412805x):
#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;

}