#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'A') {
a.push_back(i);
} else {
b.push_back(i);
}
}
vector<int> ia(2 * n);
for (int i = 0; i < n; i++) {
ia[a[i]] = i + 1;
}
vector<int> pref(2 * n);
for (int i = 0; i < 2 * n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] += (s[i] == 'A' ? 1 : 0);
}
vector<int> ib(2 * n);
for (int i = 0; i < n; i++) {
ib[b[i]] = pref[b[i]];
}
auto Solve = [&](int L, int R) {
int res = 0;
for (int i = L; i <= R; i++) {
res += max(0, ia[a[R]] - ib[b[i]]);
}
return res;
};
const int inf = (int) 1e9;
vector<int> dp(n + 1, inf);
dp[0] = 0;
for (int foo = 0; foo < k; foo++) {
vector<int> new_dp(n + 1, inf);
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
new_dp[j + 1] = min(new_dp[j + 1], dp[i] + Solve(i, j));
}
}
swap(dp, new_dp);
}
cout << dp[n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
42 ms |
440 KB |
Output is correct |
18 |
Correct |
1413 ms |
448 KB |
Output is correct |
19 |
Correct |
4148 ms |
448 KB |
Output is correct |
20 |
Correct |
16 ms |
344 KB |
Output is correct |
21 |
Correct |
30 ms |
348 KB |
Output is correct |
22 |
Execution timed out |
7036 ms |
348 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
42 ms |
440 KB |
Output is correct |
18 |
Correct |
1413 ms |
448 KB |
Output is correct |
19 |
Correct |
4148 ms |
448 KB |
Output is correct |
20 |
Correct |
16 ms |
344 KB |
Output is correct |
21 |
Correct |
30 ms |
348 KB |
Output is correct |
22 |
Execution timed out |
7036 ms |
348 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
42 ms |
440 KB |
Output is correct |
18 |
Correct |
1413 ms |
448 KB |
Output is correct |
19 |
Correct |
4148 ms |
448 KB |
Output is correct |
20 |
Correct |
16 ms |
344 KB |
Output is correct |
21 |
Correct |
30 ms |
348 KB |
Output is correct |
22 |
Execution timed out |
7036 ms |
348 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
42 ms |
440 KB |
Output is correct |
18 |
Correct |
1413 ms |
448 KB |
Output is correct |
19 |
Correct |
4148 ms |
448 KB |
Output is correct |
20 |
Correct |
16 ms |
344 KB |
Output is correct |
21 |
Correct |
30 ms |
348 KB |
Output is correct |
22 |
Execution timed out |
7036 ms |
348 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |