#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e3+10, MOD = 1e9+7;
int n, K;
vector<int> one, two;
int dp[N][N], cost[N][N];
void solve() {
cin >> n >> K;
for (int i = 0; i < 2 * n; i++) {
char c; cin >> c;
if (c == 'A') one.push_back(i);
else two.push_back(i);
}
// for (int x : one) cerr << x << ' '; cerr << endl;
// for (int x : two) cerr << x << ' '; cerr << endl;
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {
for (int i = l; i <= r; i++) {
for (int j = l; j < n; j++) {
cost[l][r] += one[i] > two[j];
}
}
}
}
for (int i = 0; i < n; i++) for (int j = 0; j <= K; j++) dp[i][j] = MOD;
for (int i = 0; i < n; i++) {
for (int l = i; l >= 0; l--) {
for (int k = 1; k <= K; k++) {
int prv = MOD;
if (l == 0) prv = (k == 1 ? 0 : MOD);
else prv = dp[l-1][k-1];
dp[i][k] = min(dp[i][k], cost[l][i] + prv);
}
}
}
cout << dp[n-1][K] << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
115 ms |
1872 KB |
Output is correct |
18 |
Correct |
6006 ms |
4968 KB |
Output is correct |
19 |
Correct |
6024 ms |
5328 KB |
Output is correct |
20 |
Correct |
6013 ms |
4844 KB |
Output is correct |
21 |
Correct |
5991 ms |
4756 KB |
Output is correct |
22 |
Correct |
6062 ms |
5820 KB |
Output is correct |
23 |
Correct |
5991 ms |
5736 KB |
Output is correct |
24 |
Correct |
6008 ms |
4768 KB |
Output is correct |
25 |
Correct |
6096 ms |
5828 KB |
Output is correct |
26 |
Correct |
6101 ms |
5576 KB |
Output is correct |
27 |
Correct |
5908 ms |
5188 KB |
Output is correct |
28 |
Correct |
6008 ms |
5116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
115 ms |
1872 KB |
Output is correct |
18 |
Correct |
6006 ms |
4968 KB |
Output is correct |
19 |
Correct |
6024 ms |
5328 KB |
Output is correct |
20 |
Correct |
6013 ms |
4844 KB |
Output is correct |
21 |
Correct |
5991 ms |
4756 KB |
Output is correct |
22 |
Correct |
6062 ms |
5820 KB |
Output is correct |
23 |
Correct |
5991 ms |
5736 KB |
Output is correct |
24 |
Correct |
6008 ms |
4768 KB |
Output is correct |
25 |
Correct |
6096 ms |
5828 KB |
Output is correct |
26 |
Correct |
6101 ms |
5576 KB |
Output is correct |
27 |
Correct |
5908 ms |
5188 KB |
Output is correct |
28 |
Correct |
6008 ms |
5116 KB |
Output is correct |
29 |
Execution timed out |
7029 ms |
436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
115 ms |
1872 KB |
Output is correct |
18 |
Correct |
6006 ms |
4968 KB |
Output is correct |
19 |
Correct |
6024 ms |
5328 KB |
Output is correct |
20 |
Correct |
6013 ms |
4844 KB |
Output is correct |
21 |
Correct |
5991 ms |
4756 KB |
Output is correct |
22 |
Correct |
6062 ms |
5820 KB |
Output is correct |
23 |
Correct |
5991 ms |
5736 KB |
Output is correct |
24 |
Correct |
6008 ms |
4768 KB |
Output is correct |
25 |
Correct |
6096 ms |
5828 KB |
Output is correct |
26 |
Correct |
6101 ms |
5576 KB |
Output is correct |
27 |
Correct |
5908 ms |
5188 KB |
Output is correct |
28 |
Correct |
6008 ms |
5116 KB |
Output is correct |
29 |
Execution timed out |
7029 ms |
436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
115 ms |
1872 KB |
Output is correct |
18 |
Correct |
6006 ms |
4968 KB |
Output is correct |
19 |
Correct |
6024 ms |
5328 KB |
Output is correct |
20 |
Correct |
6013 ms |
4844 KB |
Output is correct |
21 |
Correct |
5991 ms |
4756 KB |
Output is correct |
22 |
Correct |
6062 ms |
5820 KB |
Output is correct |
23 |
Correct |
5991 ms |
5736 KB |
Output is correct |
24 |
Correct |
6008 ms |
4768 KB |
Output is correct |
25 |
Correct |
6096 ms |
5828 KB |
Output is correct |
26 |
Correct |
6101 ms |
5576 KB |
Output is correct |
27 |
Correct |
5908 ms |
5188 KB |
Output is correct |
28 |
Correct |
6008 ms |
5116 KB |
Output is correct |
29 |
Execution timed out |
7029 ms |
436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |