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 pair<int, int> pii;
typedef long long ll;
const int MX=100010, inf=2e9;
int n;
char S[MX];
int Jcnt[MX], Icnt[MX];
ll base;
ll J(){
ll res=base;
for(int i=1; i<=n; i++)
if(S[i]=='O') res+=Icnt[i];
return res;
}
ll I(){
ll res=base;
for(int i=n; i>=1; i--)
if(S[i]=='O') res+=Jcnt[i];
return res;
}
ll O(){
ll mx=0;
for(int i=1; i<=n; i++){
mx=max(mx, 1LL*Jcnt[i-1]*Icnt[i]);
}
return base+mx;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>(S+1);
for(int i=1; i<=n; i++)
Jcnt[i]=Jcnt[i-1]+(S[i]=='J');
for(int i=n; i>=1; i--)
Icnt[i]=Icnt[i+1]+(S[i]=='I');
for(int i=1; i<=n; i++)
if(S[i]=='O') base+=1LL*Jcnt[i]*Icnt[i];
ll ans=max({J(), I(), O()});
cout<<ans;
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... |