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;
void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<int> black, white;
    for (int i = 0; i < 2 * n; ++i) {
        if (s[i] == 'B') {
            black.emplace_back(i);
        } else {
            white.emplace_back(i);
        }
    }
    auto countIntersection = [&](int j) -> long long {
        long long ans = 0;
        for (int i = 0; i < n; ++i, ++j) {
            if (j == n) {
                j = 0;
            }
            int side1 = abs(black[i] - white[j]) - 1;
            ans += min(side1, (int)s.size() - 2 - side1);
        }
        return ans;
    };
    int left = 0, right = n - 1;
    while (right - left > 2) {
        int mid1 = left + (right - left) / 3;
        int mid2 = right - (right - left) / 3;
        if (countIntersection(mid1) < countIntersection(mid2)) {
            left = mid1;
        } else {
            right = mid2;
        }
    }
    long long ans = 0;
    for (int start = left; start <= right; ++start) {
        ans = max(ans, countIntersection(start));
    }
    cout << ans / 2 << '\n';
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
        cerr << "-----------\n";
        cout << "-----------\n";
    }
#else
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
#endif
    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... |