This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
const ll INF = 1e12;
int N, K;
char S[MAXN*2+10];
int A[MAXN+10], C[MAXN+10];
ll B[MAXN+10];
ll ans;
ll f(int l, int r)
{
	if(C[r]<l) return 0;
	return (ll)r*(C[r]-l+1)-(B[C[r]]-B[l-1]);
}
pll dp[MAXN+10];
int cross(int p, int q)
{
	if(p>q) swap(p, q);
	int lo=q-1, hi=N+1;
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		if(dp[p].first+2*f(p+1, mid)<=dp[q].first+2*f(q+1, mid)) lo=mid;
		else hi=mid;
	}
	return hi;
}
ll solve(ll lambda)
{
	dp[0]={0, 0};
	deque<pii> DQ;
	DQ.push_back({0, 0});
	for(int i=1; i<=N; i++)
	{
		while(DQ.size()>1 && dp[DQ[0].first].first+2*f(DQ[0].first+1, i)>=dp[DQ[1].first].first+2*f(DQ[1].first+1, i)) DQ.pop_front();
		dp[i]={dp[DQ.front().first].first+lambda+2*f(DQ.front().first+1, i), dp[DQ.front().first].second+1};
		while(DQ.size()>1 && (DQ.back().second>N || dp[DQ.back().first].first+2*f(DQ.back().first+1, max(DQ.back().second, i+1))>=dp[i].first+2*f(i+1, max(DQ.back().second, i+1)))) DQ.pop_back();
		DQ.push_back({i, cross(DQ.back().first, i)});
	}
	return dp[N].second;
}
int main()
{
	scanf("%d%d", &N, &K);
	scanf(" %s", S+1);
	int t=0, k=0;
	for(int i=1; i<=N+N; i++)
	{
		if(S[i]=='A') k++;
		else A[++t]=k;
	}
	for(int i=1; i<=N; i++)
	{
		if(A[i]<i)
		{
			ans+=i-A[i];
			A[i]=i;
		}
		B[i]=B[i-1]+A[i];
	}
	for(int i=1, j=1; i<=N; i++)
	{
		for(; j<=N && A[j]<i; j++);
		C[i]=j-1;
	}
	ll lo=-1, hi=INF;
	while(lo+1<hi)
	{
		ll mid=lo+hi>>1;
		if(solve(mid*2+1)<K) hi=mid;
		else lo=mid;
	}
	solve(hi*2);
	printf("%lld\n", ans+dp[N].first/2-K*hi);
}
Compilation message (stderr)
chorus.cpp: In function 'int cross(int, int)':
chorus.cpp:31:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |   int mid=lo+hi>>1;
      |           ~~^~~
chorus.cpp: In function 'int main()':
chorus.cpp:83:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   83 |   ll mid=lo+hi>>1;
      |          ~~^~~
chorus.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
chorus.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  scanf(" %s", S+1);
      |  ~~~~~^~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |