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 long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 5005;
pair<ll,int> dp[N];
int Bpa[N];
int n,K;
bool ok(int v){
for(int i=0;i<=n;i++) dp[i] = {1e18,0};
dp[0]={0,0};
for(int i=1;i<=n;i++){
int cost = 0;
for(int j=1;j<=i;j++){
cost+=max(0,i-Bpa[i-j+1]);
dp[i] = min({dp[i-j].first+cost+v,dp[i-j].second+1},dp[i]);
}
}
//cout<<(dp[n].second<=K)<<" "<<dp[n].first<<" "<<v<<"\n";
return (dp[n].second<=K);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>K;
string s;cin>>s;
int pa = 0;
int pb = 0;
for(int i=1;i<=2*n;i++){
if(s[i-1]=='A') pa++;
if(s[i-1]=='B') Bpa[++pb]=pa;
}
ll l = -1;ll r = 1e9;
while(r-l>1){
ll mid = l+((r-l)/2);
if(ok(mid)) r = mid;
else l = mid;
}
ok(r);
//cout<<r<<"\n";
cout<<dp[n].first-r*K<<"\n";
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |