Language: C++

//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(void){

    int N, K;
    string s;
    cin >> N >> K >> s;

    queue<int> sep;
    char pre = s[0];
    FOR(i,1,s.size()){
        if(pre != s[i]) sep.push(i);
        pre = s[i];
    }
    sep.push(s.size());

    while(K>0 && sep.size()>1){
            sep.pop(); sep.pop();
            K--;
    }

    int sum = 0;
    if(sep.empty()){cout << s.size()-1 << endl; return 0;}
    int back = sep.back(), size = sep.size(); 
    while(sep.size()>1){ sum += sep.front(); sep.pop();}

    int s1 = sum, s2 = sum-back;
    cout << s1-s2-size << endl;

    return 0;
}
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(void){

    int N, K;
    string s;
    cin >> N >> K >> s;

    queue<int> sep;
    char pre = s[0];
    FOR(i,1,s.size()){
        if(pre != s[i]) sep.push(i);
        pre = s[i];
    }
    sep.push(s.size());
    REP(i,K){ sep.pop(); sep.pop();}

    int sum = 0;
    int back = sep.back(), size = sep.size();
    if(sep.empty()){cout << s.size()-1 << endl; return 0;}
    while(sep.size()>1){ sum += sep.front(); sep.pop();}

    int s1 = sum, s2 = sum-back;
    cout << s1-s2-size << endl;

    return 0;
}

Compiler output:
a.cc:20:12: error: redefinition of 'int toInt(std::string)'
   20 | inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
      |            ^~~~~
a.cc:7:12: note: 'int toInt(std::string)' previously defined here
    7 | inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
      |            ^~~~~
a.cc:21:33: error: redefinition of 'template<class T> std::string toString(T)'
   21 | template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
      |                                 ^~~~~~~~
a.cc:8:33: note: 'template<class T> std::string toString(T)' previously declared here
    8 | template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
      |                                 ^~~~~~~~
a.cc:24:28: error: redefinition of 'template<class T> T sqr(T)'
   24 | template<class T> inline T sqr(T x) {return x*x;}
      |                            ^~~
a.cc:11:28: note: 'template<class T> T sqr(T)' previously declared here
   11 | template<class T> inline T sqr(T x) {return x*x;}
      |                            ^~~
a.cc:108:14: error: redefinition of 'const double EPS'
  108 | const double EPS = 1e-10;
      |              ^~~
a.cc:48:14: note: 'const double EPS' previously defined here
   48 | const double EPS = 1e-10;
      |              ^~~
a.cc:109:14: error: redefinition of 'const double PI'
  109 | const double PI  = acos(-1.0);
      |              ^~
a.cc:49:14: note: 'const double PI' previously defined here
   49 | const double PI  = acos(-1.0);
      |              ^~
a.cc:116:31: error: redefinition of 'template<class T> bool chmax(T&, T)'
  116 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:56:31: note: 'template<class T> bool chmax(T&, T)' previously declared here
   56 | template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:117:31: error: redefinition of 'template<class T> bool chmin(T&, T)'
  117 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:57:31: note: 'template<class T> bool chmin(T&, T)' previously declared here
   57 | template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
      |                               ^~~~~
a.cc:119:5: error: redefinition of 'int main()'
  119 | int main(void){
      |     ^~~~
a.cc:59:5: note: 'int main()' previously defined here
   59 | int main(void){
      |     ^~~~