#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e3;
int a[MAX_N + 1], sa[MAX_N + 1], dp[MAX_N + 1][MAX_N + 1];
int main() {
int n, k;
cin >> n >> k;
int countB = 0, countA = 0;
for ( int i = 0; i < 2 * n; i++ ) {
char ch;
cin >> ch;
if ( ch == 'A' )
countA++;
else {
countB++;
a[countB] = countA;
sa[countB] = sa[countB - 1] + a[countB];
}
}
for ( int i = 1; i <= n; i++ )
dp[i][0] = n * n;
for ( int l = 1; l <= k; l++ ) {
for ( int i = 1; i <= n; i++ ) {
dp[i][l] = n * n;
int p = i;
for ( int j = i - 1; j >= 0 && a[j + 1] >= i; j-- ) {
dp[i][l] = min( dp[i][l], dp[j][l - 1] );
p = j;
}
for ( int j = p - 1; j >= 0; j-- )
dp[i][l] = min( dp[i][l], dp[j][l - 1] + i * (p - j) - sa[p] + sa[j] );
}
}
cout << dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 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 |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
18 ms |
10588 KB |
Output is correct |
19 |
Correct |
42 ms |
10996 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
69 ms |
10584 KB |
Output is correct |
23 |
Correct |
68 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10584 KB |
Output is correct |
25 |
Correct |
79 ms |
10584 KB |
Output is correct |
26 |
Correct |
57 ms |
10584 KB |
Output is correct |
27 |
Correct |
24 ms |
10588 KB |
Output is correct |
28 |
Correct |
25 ms |
10588 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 |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
18 ms |
10588 KB |
Output is correct |
19 |
Correct |
42 ms |
10996 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
69 ms |
10584 KB |
Output is correct |
23 |
Correct |
68 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10584 KB |
Output is correct |
25 |
Correct |
79 ms |
10584 KB |
Output is correct |
26 |
Correct |
57 ms |
10584 KB |
Output is correct |
27 |
Correct |
24 ms |
10588 KB |
Output is correct |
28 |
Correct |
25 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7044 ms |
96944 KB |
Time limit exceeded |
30 |
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 |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
18 ms |
10588 KB |
Output is correct |
19 |
Correct |
42 ms |
10996 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
69 ms |
10584 KB |
Output is correct |
23 |
Correct |
68 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10584 KB |
Output is correct |
25 |
Correct |
79 ms |
10584 KB |
Output is correct |
26 |
Correct |
57 ms |
10584 KB |
Output is correct |
27 |
Correct |
24 ms |
10588 KB |
Output is correct |
28 |
Correct |
25 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7044 ms |
96944 KB |
Time limit exceeded |
30 |
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 |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
18 ms |
10588 KB |
Output is correct |
19 |
Correct |
42 ms |
10996 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
69 ms |
10584 KB |
Output is correct |
23 |
Correct |
68 ms |
10588 KB |
Output is correct |
24 |
Correct |
4 ms |
10584 KB |
Output is correct |
25 |
Correct |
79 ms |
10584 KB |
Output is correct |
26 |
Correct |
57 ms |
10584 KB |
Output is correct |
27 |
Correct |
24 ms |
10588 KB |
Output is correct |
28 |
Correct |
25 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7044 ms |
96944 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |