#include <bits/stdc++.h>
#define MAX 300005
#define MOD (ll)(1048573)
#define INF (ll)(1e18)
#define inf (int)(1e9)
#define eps (double)(1e-8)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int tt, n, m, k, q;
string str;
ll cnt[MAX], sum[MAX], pos[MAX];
ll dp[5005][5005];
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k >> str;
for(int i = 0, tmp = 0, p = 0; i < 2 * n; ++i) {
if(str[i] == 'A') cnt[++p] = tmp;
else ++tmp;
}
for(int i = 0, j = 1; i <= n; ++i) {
while(j <= n && cnt[j] < i) ++j;
pos[i] = max(i + 1, j);
}
for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + cnt[i];
for(int i = 0; i <= n; ++i) for(int j = 0; j <= k; ++j) dp[i][j] = INF;
dp[0][0] = 0;
for(int i = 1; i <= n; ++i) for(int j = 1; j <= k; ++j) {
for(int ii = 0; ii < i; ++ii) {
if(pos[ii] <= i) dp[i][j] = min(dp[i][j], dp[ii][j - 1] + sum[i] - sum[pos[ii] - 1] - (i - pos[ii] + 1) * ii);
else dp[i][j] = min(dp[i][j], dp[ii][j - 1]);
}
}
cout << dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
20 ms |
2904 KB |
Output is correct |
19 |
Correct |
62 ms |
3576 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
107 ms |
4444 KB |
Output is correct |
23 |
Correct |
101 ms |
4440 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
101 ms |
4472 KB |
Output is correct |
26 |
Correct |
84 ms |
4140 KB |
Output is correct |
27 |
Correct |
34 ms |
3192 KB |
Output is correct |
28 |
Correct |
39 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
20 ms |
2904 KB |
Output is correct |
19 |
Correct |
62 ms |
3576 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
107 ms |
4444 KB |
Output is correct |
23 |
Correct |
101 ms |
4440 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
101 ms |
4472 KB |
Output is correct |
26 |
Correct |
84 ms |
4140 KB |
Output is correct |
27 |
Correct |
34 ms |
3192 KB |
Output is correct |
28 |
Correct |
39 ms |
3160 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
99164 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
20 ms |
2904 KB |
Output is correct |
19 |
Correct |
62 ms |
3576 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
107 ms |
4444 KB |
Output is correct |
23 |
Correct |
101 ms |
4440 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
101 ms |
4472 KB |
Output is correct |
26 |
Correct |
84 ms |
4140 KB |
Output is correct |
27 |
Correct |
34 ms |
3192 KB |
Output is correct |
28 |
Correct |
39 ms |
3160 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
99164 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
20 ms |
2904 KB |
Output is correct |
19 |
Correct |
62 ms |
3576 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
107 ms |
4444 KB |
Output is correct |
23 |
Correct |
101 ms |
4440 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
101 ms |
4472 KB |
Output is correct |
26 |
Correct |
84 ms |
4140 KB |
Output is correct |
27 |
Correct |
34 ms |
3192 KB |
Output is correct |
28 |
Correct |
39 ms |
3160 KB |
Output is correct |
29 |
Execution timed out |
7104 ms |
99164 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |