// 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;
const ll inf = 1e12 + 4;
int n, k; string s;
vector<ll> ls; pll dp[maxn];
void cal(ll valx) {
dp[0] = Mp(0, 0);
for (int j = 1; j <= n; j++) dp[j] = Mp(oo, oo);
for (int j = 0; j <= n; j++) {
ll x = valx;
for (int R = j + 1; R <= n; R++) {
x += max(0ll, (ls[R - 1] - j));
dp[R] = min(dp[R], Mp(dp[j].F + x, dp[j].S + 1));
}
}
}
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);
}
ll l = -1, r = inf;
while (r - l > 1) {
ll mid = (l + r) / 2; cal(mid);
if (dp[n].S > k) l = mid;
else r = mid;
}
cal(r);
cout << dp[n].F - dp[n].S * r << 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... |