#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000009
#define INF 1000000019
#define INFL 1000000000000000099LL
ll a,b,c,t,n,k,k2,k3,k4,ans;
string s;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k>>s;
k2=k;
for(a=0;a<n;a++){
if(s[a]=='J')
k2--;
if(k2==0)
break;
}
k3=k;
for(b=a+1;b<n;b++){
if(s[b]=='O')
k3--;
if(k3==0)
break;
}
k4=k;
for(c=b+1;c<n;c++){
if(s[c]=='I')
k4--;
if(k4==0)
break;
}
if(c>=n){
cout<<-1;
return 0;
}
ans=c+1-3*k;
for(ll i=0;i<n;i++){
if(s[i]!='J')
ans=min(ans,c-i-3*k);
else
k2++;
if(k2!=0)
for(a=a+1;a<n;a++){
if(s[a]=='O')
k3++;
if(s[a]=='J')
k2--;
if(k2==0)
break;
}
if(k3!=0)
for(b=max(b+1,a+1);b<n;b++){
if(s[b]=='I')
k4++;
if(s[b]=='O')
k3--;
if(k3==0)
break;
}
if(k4!=0)
for(c=max(c+1,b+1);c<n;c++){
if(s[a]=='I')
k4--;
if(k4==0)
break;
}
if(c>=n || b>=n || a>=n){
cout<<ans;
return 0;
}
ans=min(ans,c-i-3*k);
}
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... |