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;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
const int N=4005;
struct BIT
{
    int a[N];
    void init()
    {
        rep1(i,N-1) a[i]=0;
    }
    void upd(int x,int val)
    {
        for(;x<N;x+=x&-x) a[x]+=val;
    }
    int qry(int x)
    {
        int ret=0;
        for(;x;x-=x&-x) ret+=a[x];
        return ret;
    }
} bit;
int to[N];
signed main()
{
    MottoHayaku
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> a,b;
    rep(i,n*2)
    {
        if(s[i]=='B') a.pb(i+1);
        else b.pb(i+1);
    }
    int ans=0;
    rep(i,n)
    {
        rep(j,n)
        {
            to[a[j]]=b[(j+i)%n];
            to[b[(j+i)%n]]=a[j];
        }
        bit.init();
        int tot=0;
        rep1(j,n*2)
        {
            if(to[j]<j) bit.upd(j,-1);
            else
            {
                tot+=bit.qry(to[j]);
                bit.upd(to[j],1);
            }
        }
        ans=max(ans,tot);
    }
    cout<<ans<<"\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... |