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>
#define fi first
#define se second
using namespace std;
const int N=4e5;
int mtch[N+10];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	string s;
	cin>>s;
	int b=2*n-1,w=2*n-1;
	int bw=0,ww=0;
	long long ans=0;
	for(int i=n-1;i>=0;i--)
	{
		if(s[i]=='B')
		{
			while(s[w]!='W')
			{
				ww+=mtch[w];
				w--;
			}
			//cerr<<"w "<<w<<"\n";
			ans+=ww;
			mtch[w]++;
			if(b<w)
				bw++;
			ww++;
			w--;
		}
		else
		{
			while(s[b]!='B')
			{
				bw+=mtch[b];
				b--;
			}
			//cerr<<"b "<<b<<"\n";
			ans+=bw;
			mtch[b]++;
			if(w<b)
				ww++;
			bw++;
			b--;
		}
		//cerr<<ans<<"\n";
	}
	cout<<ans<<"\n";
	return 0;
}
| # | 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... |