// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 5000 + 4;
const ll oo = 1e18 + 4;
int n, k; string s;
vector<ll> ls; ll dp[maxn];
void solve() {
cin >> n >> k >> s;
int x = 0;
for (int i = 0; i < (2 * n); i++) {
if (s[i] == 'B') x++;
else ls.pb(x);
}
dp[0] = 0;
for (int j = 1; j <= n; j++) dp[j] = oo;
for (int i = 0; i < k; i++) {
for (int j = n; j >= 0; j--) {
if (dp[j] == oo) continue;
ll x = 0;
for (int R = j + 1; R <= n; R++) {
x += max(0ll, (ls[R - 1] - j));
dp[R] = min(dp[R], dp[j] + x);
}
}
}
cout << dp[n] << endl;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
while (T--) {
solve();
}
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... |