This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
#define maxn 5005
ll n,k;
ll a[maxn];
ll c[maxn];
ll ps[maxn];
ll sz = 0;
ll dp[maxn][maxn];
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> k;
string s; cin >> s;
for(ll i = 1;i<=2*n;i++){
if(s[i-1]=='A') a[i] = 1;
}
ll b = 0;
for(ll i = 2*n;i>=1;i--){
if(a[i]==1){
c[++sz] = b;
}else b++;
}
for(ll i = 1;i<=n;i++) ps[i] = ps[i-1] + c[i];
dp[0][0] = 0;
for(ll i = 1;i<=n;i++) dp[i][0] = llinf;
for(ll j = 1;j<=k;j++){
for(ll i = 1;i<=n;i++){
dp[i][j] = llinf;
ll sum = 0;
for(ll e = i;e>=1;e--){
sum+=max(0LL,i-c[e]);
dp[i][j] = min(dp[i][j],dp[e-1][j-1]+sum);
}
}
}
cout<<dp[n][k]<<endl;
return (0-0);
}
/**
5 2
AABABABBAB
**/
# | 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... |