#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 505;
const ll inf = 1e9 + 5;
int B[nmax];
ll dp[nmax][nmax];
ll sum_under[nmax], cnt_under[nmax];
ll sum_pref[nmax];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, k;
cin >> n >> k;
int cnt[2] = {0, 0};
char ch;
for(int i = 0; i < 2 * n; i++) {
cin >> ch;
if(ch == 'A')
cnt[0]++;
else
B[++cnt[1]] = cnt[0];
//cerr << cnt[0] << '\n';
}
ll fixing = 0;
for(int i = 1; i <= n; i++) {
int target = max(B[i - 1], i);
fixing += max(0, target - B[i]);
B[i] += max(0, target - B[i]);
}
for(int i = 1; i <= n; i++)
cnt_under[B[i]]++,
sum_under[B[i]] += B[i],
sum_pref[i] = sum_pref[i - 1] + B[i];
for(int i = 1; i <= n; i++)
cnt_under[i] += cnt_under[i - 1],
sum_under[i] += sum_under[i - 1];
//for(int i = 1; i <= n; i++)
//cout << B[i] << ' ';
//cout << '\n';
for(int i = 1; i <= n; i++)
dp[0][i] = inf;
dp[0][n + 1] = 0;
for(int dim = 1; dim <= k; dim++) {
for(int i = n; i > 0; i--) {
int ptr = i;
dp[dim][i] = inf;
for(int nv = B[i]; nv <= n; nv++) {
ll cost = (cnt_under[nv] - i + 1) * nv - (sum_under[nv] - sum_pref[i - 1]);
//cerr << i << ": " << B[i] << ' ' << nv << '\t' << cost << '\n';
dp[dim][i] = min(dp[dim - 1][nv + 1] + cost, dp[dim][i]);
}
}
}
cout << dp[k][1] + fixing << '\n';
}
/**
nu toate numerele mari sunt semne de ceva bine
-- Si pe mine m-a surprins sa fiu sincer, dar pbn cautati pe net, sunt sigur ca se gasesc mai multe exemple
*/
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:66:11: warning: unused variable 'ptr' [-Wunused-variable]
66 | int ptr = i;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
16 ms |
796 KB |
Output is correct |
19 |
Correct |
44 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
84 ms |
2408 KB |
Output is correct |
23 |
Correct |
85 ms |
2388 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
85 ms |
2240 KB |
Output is correct |
26 |
Correct |
71 ms |
2008 KB |
Output is correct |
27 |
Correct |
29 ms |
1108 KB |
Output is correct |
28 |
Correct |
28 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
16 ms |
796 KB |
Output is correct |
19 |
Correct |
44 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
84 ms |
2408 KB |
Output is correct |
23 |
Correct |
85 ms |
2388 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
85 ms |
2240 KB |
Output is correct |
26 |
Correct |
71 ms |
2008 KB |
Output is correct |
27 |
Correct |
29 ms |
1108 KB |
Output is correct |
28 |
Correct |
28 ms |
1116 KB |
Output is correct |
29 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
16 ms |
796 KB |
Output is correct |
19 |
Correct |
44 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
84 ms |
2408 KB |
Output is correct |
23 |
Correct |
85 ms |
2388 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
85 ms |
2240 KB |
Output is correct |
26 |
Correct |
71 ms |
2008 KB |
Output is correct |
27 |
Correct |
29 ms |
1108 KB |
Output is correct |
28 |
Correct |
28 ms |
1116 KB |
Output is correct |
29 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
16 ms |
796 KB |
Output is correct |
19 |
Correct |
44 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
84 ms |
2408 KB |
Output is correct |
23 |
Correct |
85 ms |
2388 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
85 ms |
2240 KB |
Output is correct |
26 |
Correct |
71 ms |
2008 KB |
Output is correct |
27 |
Correct |
29 ms |
1108 KB |
Output is correct |
28 |
Correct |
28 ms |
1116 KB |
Output is correct |
29 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |