#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#define ll long long
using namespace std;
ll n, k, dp[2][5000];
vector <ll> A, B, nxA, ps;
string S;
ll C(ll l, ll r) {
if (nxA[l] > r) return 0;
ll nx = max(l, nxA[l]), cnt = r-nx+1, cnt2 = max(0LL, l-nxA[l]);
return ps[r]-(nx ? ps[nx-1] : 0)-cnt*B[l]-cnt*(cnt-1)/2-(r-l+1)*cnt2;
}
void solve(ll l, ll r, ll ql, ll qr) {
ll mid = (l+r)/2;
ll opt = 1e18, optid = -1, tmp;
for (int i=ql; i<mid; ++i) {
tmp = dp[0][i] + C(i+1, mid);
if (opt > tmp) {
opt = tmp;
optid = i;
}
}
dp[1][mid] = opt;
if (l < mid) solve(l, mid-1, ql, optid);
if (mid < r) solve(mid+1, r, optid, qr);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> k >> S;
for (int i=0; i<2*n; ++i) {
if (S[i] == 'A') {
ps.push_back((ps.empty() ? 0 : ps.back()) + i);
A.push_back(i);
}
else {
B.push_back(i);
nxA.push_back(A.size());
}
}
for (int i=0; i<n; ++i) dp[0][i] = C(0, i);
for (int x=1; x<k; ++x) {
solve(0, n-1, 0, n-1);
swap(dp[0], dp[1]);
}
cout << dp[0][n-1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 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 |
348 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 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 |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
14 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
348 KB |
Output is correct |
23 |
Correct |
14 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
7 ms |
564 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
556 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 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 |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
14 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
348 KB |
Output is correct |
23 |
Correct |
14 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
7 ms |
564 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
556 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
543 ms |
808 KB |
Output is correct |
30 |
Correct |
3134 ms |
856 KB |
Output is correct |
31 |
Correct |
943 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
31 ms |
840 KB |
Output is correct |
34 |
Correct |
822 ms |
836 KB |
Output is correct |
35 |
Correct |
821 ms |
828 KB |
Output is correct |
36 |
Correct |
91 ms |
860 KB |
Output is correct |
37 |
Correct |
464 ms |
856 KB |
Output is correct |
38 |
Correct |
717 ms |
856 KB |
Output is correct |
39 |
Correct |
548 ms |
856 KB |
Output is correct |
40 |
Correct |
388 ms |
856 KB |
Output is correct |
41 |
Correct |
406 ms |
860 KB |
Output is correct |
42 |
Execution timed out |
7072 ms |
604 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 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 |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
14 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
348 KB |
Output is correct |
23 |
Correct |
14 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
7 ms |
564 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
556 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
543 ms |
808 KB |
Output is correct |
30 |
Correct |
3134 ms |
856 KB |
Output is correct |
31 |
Correct |
943 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
31 ms |
840 KB |
Output is correct |
34 |
Correct |
822 ms |
836 KB |
Output is correct |
35 |
Correct |
821 ms |
828 KB |
Output is correct |
36 |
Correct |
91 ms |
860 KB |
Output is correct |
37 |
Correct |
464 ms |
856 KB |
Output is correct |
38 |
Correct |
717 ms |
856 KB |
Output is correct |
39 |
Correct |
548 ms |
856 KB |
Output is correct |
40 |
Correct |
388 ms |
856 KB |
Output is correct |
41 |
Correct |
406 ms |
860 KB |
Output is correct |
42 |
Execution timed out |
7072 ms |
604 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 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 |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
14 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
348 KB |
Output is correct |
23 |
Correct |
14 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
7 ms |
564 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
556 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
543 ms |
808 KB |
Output is correct |
30 |
Correct |
3134 ms |
856 KB |
Output is correct |
31 |
Correct |
943 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
31 ms |
840 KB |
Output is correct |
34 |
Correct |
822 ms |
836 KB |
Output is correct |
35 |
Correct |
821 ms |
828 KB |
Output is correct |
36 |
Correct |
91 ms |
860 KB |
Output is correct |
37 |
Correct |
464 ms |
856 KB |
Output is correct |
38 |
Correct |
717 ms |
856 KB |
Output is correct |
39 |
Correct |
548 ms |
856 KB |
Output is correct |
40 |
Correct |
388 ms |
856 KB |
Output is correct |
41 |
Correct |
406 ms |
860 KB |
Output is correct |
42 |
Execution timed out |
7072 ms |
604 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |