Language: C++


#include <stdio.h>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

typedef unsigned long long ULLONG;
typedef long long LLONG;

template <class _T> static _T tp_abs(const _T& a) {
	return ((a < 0) ? (a * -1) : a);
}
template <class _T> static _T tp_min(_T a, _T b) {
	return a < b ? a : b;
}
template <class _T> static _T tp_max(_T a, _T b) {
	return a > b ? a : b;
}
template<class _T> static void get1int(_T& a) {
	const char* fmt = " %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld";
	}

	if (scanf(fmt, &a) < 0) {
		printf("g1int Error\n");
	}
}
template<class _T> static void get2int(_T& a, _T& b) {
	const char* fmt = " %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld";
	}

	if (scanf(fmt, &a, &b) < 0) {
		printf("g2int Error\n");
	}
}
template<class _T> static void get3int(_T& a, _T& b, _T& c) {
	const char* fmt = " %d %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld %lld";
	}

	if (scanf(fmt, &a, &b, &c) < 0) {
		printf("g3int Error\n");
	}
}
static void getstring(char* buff) {
	if (scanf(" %s", buff) < 0) {
		printf("getstr error!\n");
	}
}
static const long long MOD_NUM = 1000000007LL;

static void D_task();

int main()
{
	D_task();
	fflush(stdout);
	return 0;
}

class RunLengthEnc {
public:
	class RLEData {
	public:
		char c = 0;
		int cnt = 0;
	};

	typedef std::vector<RLEData> RLEvec;

	void Exec(RLEvec& vRle, const char* str) {
		int sLen = strlen(str);

		char bfr = 0;
		RLEData ccnt;
		for (int i = 0; i < sLen; i++) {
			if (str[i] != bfr) {
				if (ccnt.c != 0) {
					vRle.push_back(ccnt);
				}
				ccnt.c = str[i];
				ccnt.cnt = 1;
			}
			else {
				ccnt.cnt++;
			}
			bfr = str[i];
		}
		if (ccnt.c != 0) {
			vRle.push_back(ccnt);
		}
	}

	static bool dsc_cnt(RLEData& a, RLEData& b) {
		return a.cnt > b.cnt;
	}
};

static void D_task()
{
	int N, K;
	get2int(N, K);
	
	auto str = new char[N + 1];
	str[N] = 0;
	getstring(str);

	RunLengthEnc::RLEvec data;
	RunLengthEnc enc;
	enc.Exec(data, str);

	std::vector<int> happy;
	int blkCnt = data.size();
	int ans = 0;
	int rest = K;
	for (int i = 0; i < blkCnt; i++) {
		ans += data[i].cnt - 1;
		if ((rest) && (i != 0) && (i != blkCnt - 1)) {
			ans += 2;
			rest--;
		}
	}
	if (rest) ans++;

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

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

typedef unsigned long long ULLONG;
typedef long long LLONG;

template <class _T> static _T tp_abs(const _T& a) {
	return ((a < 0) ? (a * -1) : a);
}
template <class _T> static _T tp_min(_T a, _T b) {
	return a < b ? a : b;
}
template <class _T> static _T tp_max(_T a, _T b) {
	return a > b ? a : b;
}
template<class _T> static void get1int(_T& a) {
	const char* fmt = " %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld";
	}

	if (scanf(fmt, &a) < 0) {
		printf("g1int Error\n");
	}
}
template<class _T> static void get2int(_T& a, _T& b) {
	const char* fmt = " %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld";
	}

	if (scanf(fmt, &a, &b) < 0) {
		printf("g2int Error\n");
	}
}
template<class _T> static void get3int(_T& a, _T& b, _T& c) {
	const char* fmt = " %d %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld %lld";
	}

	if (scanf(fmt, &a, &b, &c) < 0) {
		printf("g3int Error\n");
	}
}
static void getstring(char* buff) {
	if (scanf(" %s", buff) < 0) {
		printf("getstr error!\n");
	}
}
static const long long MOD_NUM = 1000000007LL;

static void D_task();

int main()
{
	D_task();
	fflush(stdout);
	return 0;
}

class RunLengthEnc {
public:
	class RLEData {
	public:
		char c = 0;
		int cnt = 0;
	};

	typedef std::vector<RLEData> RLEvec;

	void Exec(RLEvec& vRle, const char* str) {
		int sLen = strlen(str);

		char bfr = 0;
		RLEData ccnt;
		for (int i = 0; i < sLen; i++) {
			if (str[i] != bfr) {
				if (ccnt.c != 0) {
					vRle.push_back(ccnt);
				}
				ccnt.c = str[i];
				ccnt.cnt = 1;
			}
			else {
				ccnt.cnt++;
			}
			bfr = str[i];
		}
		if (ccnt.c != 0) {
			vRle.push_back(ccnt);
		}
	}

	static bool dsc_cnt(RLEData& a, RLEData& b) {
		return a.cnt > b.cnt;
	}
};

static void D_task()
{
	int N, K;
	get2int(N, K);
	
	auto str = new char[N + 1];
	str[N] = 0;
	getstring(str);

	RunLengthEnc::RLEvec data;
	RunLengthEnc enc;
	enc.Exec(data, str);

	std::vector<int> happy;
	int blkCnt = data.size();
	int ans = 0;
	int rest = K;
	for (int i = 0; i < blkCnt; i++) {
		ans += data[i].cnt - 1;
		if ((rest) && (i != 0) && (i != blkCnt - 1)) {
			ans += 2;
			rest--;
		}
	}
	if (rest) ans++;

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


Compiler output:
a.cc:150:31: error: redefinition of 'template<class _T> _T tp_abs(const _T&)'
  150 | template <class _T> static _T tp_abs(const _T& a) {
      |                               ^~~~~~
a.cc:15:31: note: 'template<class _T> _T tp_abs(const _T&)' previously declared here
   15 | template <class _T> static _T tp_abs(const _T& a) {
      |                               ^~~~~~
a.cc:153:31: error: redefinition of 'template<class _T> _T tp_min(_T, _T)'
  153 | template <class _T> static _T tp_min(_T a, _T b) {
      |                               ^~~~~~
a.cc:18:31: note: 'template<class _T> _T tp_min(_T, _T)' previously declared here
   18 | template <class _T> static _T tp_min(_T a, _T b) {
      |                               ^~~~~~
a.cc:156:31: error: redefinition of 'template<class _T> _T tp_max(_T, _T)'
  156 | template <class _T> static _T tp_max(_T a, _T b) {
      |                               ^~~~~~
a.cc:21:31: note: 'template<class _T> _T tp_max(_T, _T)' previously declared here
   21 | template <class _T> static _T tp_max(_T a, _T b) {
      |                               ^~~~~~
a.cc:159:32: error: redefinition of 'template<class _T> void get1int(_T&)'
  159 | template<class _T> static void get1int(_T& a) {
      |                                ^~~~~~~
a.cc:24:32: note: 'template<class _T> void get1int(_T&)' previously declared here
   24 | template<class _T> static void get1int(_T& a) {
      |                                ^~~~~~~
a.cc:169:32: error: redefinition of 'template<class _T> void get2int(_T&, _T&)'
  169 | template<class _T> static void get2int(_T& a, _T& b) {
      |                                ^~~~~~~
a.cc:34:32: note: 'template<class _T> void get2int(_T&, _T&)' previously declared here
   34 | template<class _T> static void get2int(_T& a, _T& b) {
      |                                ^~~~~~~
a.cc:179:32: error: redefinition of 'template<class _T> void get3int(_T&, _T&, _T&)'
  179 | template<class _T> static void get3int(_T& a, _T& b, _T& c) {
      |                                ^~~~~~~
a.cc:44:32: note: 'template<class _T> void get3int(_T&, _T&, _T&)' previously declared here
   44 | template<class _T> static void get3int(_T& a, _T& b, _T& c) {
      |                                ^~~~~~~
a.cc:189:13: error: redefinition of 'void getstring(char*)'
  189 | static void getstring(char* buff) {
      |             ^~~~~~~~~
a.cc:54:13: note: 'void getstring(char*)' previously defined here
   54 | static void getstring(char* buff) {
      |             ^~~~~~~~~
a.cc:194:24: error: redefinition of 'const long long int MOD_NUM'
  194 | static const long long MOD_NUM = 1000000007LL;
      |                        ^~~~~~~
a.cc:59:24: note: 'const long long int MOD_NUM' previously defined here
   59 | static const long long MOD_NUM = 1000000007LL;
      |                        ^~~~~~~
a.cc:198:5: error: redefinition of 'int main()'
  198 | int main()
      |     ^~~~
a.cc:63:5: note: 'int main()' previously defined here
   63 | int main()
      |     ^~~~
a.cc:205:7: error: redefinition of 'class RunLengthEnc'
  205 | class RunLengthEnc {
      |       ^~~~~~~~~~~~
a.cc:70:7: note: previous definition of 'class RunLengthEnc'
   70 | class RunLengthEnc {
      |       ^~~~~~~~~~~~
a.cc:243:13: error: redefinition of 'void D_task()'
  243 | static void D_task()
      |             ^~~~~~
a.cc:108:13: note: 'void D_task()' previously defined here
  108 | static void D_task()
      |             ^~~~~~