#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e2 + 42, INF = 1e18 + 42;
vector<int> a, pre;
int n, k, x = 0, cumul[N], fi[N], dp[N][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0; i < N; i++)
fi[i] = N-1;
for(int i = 0; i < 2*n; i++) {
char c; cin >> c;
if(c == 'A') {
if(fi[x] == N-1)
fi[x] = (int)a.size();
a.push_back(i);
pre.push_back(x);
} else {
x++;
}
}
for(int i = n-1; i > -1; i--)
if(fi[i] == N-1)
fi[i] = fi[i+1];
for(int i = 1; i <= n; i++)
cumul[i] = cumul[i-1] + pre[i-1];
for(int i = 1; i <= n; i++)
dp[0][i] = INF;
for(int nbG = 1; nbG <= k; nbG++) {
for(int i = 1; i <= n; i++) {
dp[nbG][i] = INF;
for(int j = 0; j < i; j++)
dp[nbG][i] = min(dp[nbG][i], dp[nbG-1][j] - (max(i, fi[j]) - max(j, fi[j])) * j + cumul[max(i, fi[j])] - cumul[max(j, fi[j])]);
}
}
cout << dp[k][n] << '\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 |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 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 |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
568 KB |
Output is correct |
18 |
Correct |
22 ms |
680 KB |
Output is correct |
19 |
Correct |
69 ms |
1540 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
118 ms |
2456 KB |
Output is correct |
23 |
Correct |
120 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
116 ms |
2364 KB |
Output is correct |
26 |
Correct |
103 ms |
2172 KB |
Output is correct |
27 |
Correct |
39 ms |
972 KB |
Output is correct |
28 |
Correct |
39 ms |
944 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 |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
568 KB |
Output is correct |
18 |
Correct |
22 ms |
680 KB |
Output is correct |
19 |
Correct |
69 ms |
1540 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
118 ms |
2456 KB |
Output is correct |
23 |
Correct |
120 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
116 ms |
2364 KB |
Output is correct |
26 |
Correct |
103 ms |
2172 KB |
Output is correct |
27 |
Correct |
39 ms |
972 KB |
Output is correct |
28 |
Correct |
39 ms |
944 KB |
Output is correct |
29 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
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 |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
568 KB |
Output is correct |
18 |
Correct |
22 ms |
680 KB |
Output is correct |
19 |
Correct |
69 ms |
1540 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
118 ms |
2456 KB |
Output is correct |
23 |
Correct |
120 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
116 ms |
2364 KB |
Output is correct |
26 |
Correct |
103 ms |
2172 KB |
Output is correct |
27 |
Correct |
39 ms |
972 KB |
Output is correct |
28 |
Correct |
39 ms |
944 KB |
Output is correct |
29 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
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 |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
568 KB |
Output is correct |
18 |
Correct |
22 ms |
680 KB |
Output is correct |
19 |
Correct |
69 ms |
1540 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
118 ms |
2456 KB |
Output is correct |
23 |
Correct |
120 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
116 ms |
2364 KB |
Output is correct |
26 |
Correct |
103 ms |
2172 KB |
Output is correct |
27 |
Correct |
39 ms |
972 KB |
Output is correct |
28 |
Correct |
39 ms |
944 KB |
Output is correct |
29 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |