rt,照课上老师的讲法打的
const int N=2e6+10;
int n,a[N];
string s;
bool check(int x){
int i=n,cnti=0,cntoi=0,res=0;
while(i){
if(a[i]==3&&cntoi){
cntoi--;
res++;
}
else if(a[i]==2&&cnti){
cnti--;
cntoi++;
}
else if(a[i]==1){
if(res+cntoi+cnti<x){
cnti++;
}
else{
cntoi--;
res++;
}
}
i--;
}
return res>=x;
}
int main(){
cin>>n>>s;
s='$'+s;
for(int i=1;i<=n;i++){
if(s[i]=='I'){
a[i]=1;
}
else if(s[i]=='O'){
a[i]=2;
}
else{
a[i]=3;
}
}
int l=0,r=n;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
l=mid+1;
}
else{
r=mid-1;
}
}
cout<<r<<endl;
return 0;
}