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;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n;
	string s;
	cin >> n >> s;
	vector <int> W, B;
	for (int i = 0; i < 2 * n; ++i) {
		(s[i] == 'W' ? W : B).pb(i);
	} 
	vector <vector <int>> w(n, vector <int> (n));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int a = W[i], b = B[j];
			if (a > b) {
				swap(a, b);
			}
			int cnt0 = 0, cnt1 = 0;
			for (int ii = a + 1; ii < b; ++ii) {
				(s[ii] == 'W' ? cnt0 : cnt1)++;
			}
			w[i][j] = min(cnt0, n - 1 - cnt1) + min(cnt1, n - 1 - cnt0);
		}
	}
	vector <int> dp(1 << n, 0);
	for (int s = 0; s < (1 << n); ++s) {
		int num = __builtin_popcount(s);
		for (int i = 0; i < n; ++i) if (~s >> i & 1) {
			dp[s | (1 << i)] = max(dp[s | (1 << i)], dp[s] + w[i][num]);
		}
	}
	cout << dp.back() / 2 << '\n';
}
| # | 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... |