#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, K;
ll dp[5005][5005], in[1000006], suf[1000006], tot;
char c[2000006];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> K;
for (int i = 1; i <= 2 * N; i++)
cin >> c[i];
for (int i = 1, j = 0, k = 1; i <= N; i++, k++)
{
while (c[k] != 'B')
j++, k++;
in[i - 1] = j;
}
for (int i = 0; i < N; i++)
in[i] = max((ll)i, in[i]);
for (int i = 1, j = N, k = 1; i <= N; i++, k++)
{
while (c[k] != 'A')
j--, k++;
suf[i] = j;
}
for (int i = N; i >= 0; i--)
suf[i] += suf[i + 1];
for (int k = 0; k <= K; k++)
for (int i = 0; i <= N; i++)
dp[i][k] = 1e18;
dp[0][0] = 0;
for (int k = 1; k <= K; k++)
for (int i = 1; i <= N; i++)
{
for (int j = 0; j < i; j++)
if (in[j] < i)
dp[i][k] = min(dp[i][k], dp[j][k - 1] + (i - in[j]) * (N - j) - (suf[in[j] + 1] - suf[i + 1]));
else
dp[i][k] = min(dp[i][k], dp[j][k - 1] + 0);
// cout << dp[i][k] << " \n"[i == N];
}
cout << dp[N][K] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1108 KB |
Output is correct |
18 |
Correct |
37 ms |
2748 KB |
Output is correct |
19 |
Correct |
108 ms |
3456 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
196 ms |
4332 KB |
Output is correct |
23 |
Correct |
193 ms |
4344 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
197 ms |
4336 KB |
Output is correct |
26 |
Correct |
160 ms |
3924 KB |
Output is correct |
27 |
Correct |
65 ms |
3036 KB |
Output is correct |
28 |
Correct |
65 ms |
3036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1108 KB |
Output is correct |
18 |
Correct |
37 ms |
2748 KB |
Output is correct |
19 |
Correct |
108 ms |
3456 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
196 ms |
4332 KB |
Output is correct |
23 |
Correct |
193 ms |
4344 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
197 ms |
4336 KB |
Output is correct |
26 |
Correct |
160 ms |
3924 KB |
Output is correct |
27 |
Correct |
65 ms |
3036 KB |
Output is correct |
28 |
Correct |
65 ms |
3036 KB |
Output is correct |
29 |
Execution timed out |
7081 ms |
99140 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1108 KB |
Output is correct |
18 |
Correct |
37 ms |
2748 KB |
Output is correct |
19 |
Correct |
108 ms |
3456 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
196 ms |
4332 KB |
Output is correct |
23 |
Correct |
193 ms |
4344 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
197 ms |
4336 KB |
Output is correct |
26 |
Correct |
160 ms |
3924 KB |
Output is correct |
27 |
Correct |
65 ms |
3036 KB |
Output is correct |
28 |
Correct |
65 ms |
3036 KB |
Output is correct |
29 |
Execution timed out |
7081 ms |
99140 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
1108 KB |
Output is correct |
18 |
Correct |
37 ms |
2748 KB |
Output is correct |
19 |
Correct |
108 ms |
3456 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
196 ms |
4332 KB |
Output is correct |
23 |
Correct |
193 ms |
4344 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
197 ms |
4336 KB |
Output is correct |
26 |
Correct |
160 ms |
3924 KB |
Output is correct |
27 |
Correct |
65 ms |
3036 KB |
Output is correct |
28 |
Correct |
65 ms |
3036 KB |
Output is correct |
29 |
Execution timed out |
7081 ms |
99140 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |