#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 505;
ll dp[N][N];
int Bpa[N];
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,K;cin>>n>>K;
string s;cin>>s;
int pa = 0;
int pb = 0;
for(int i=1;i<=2*n;i++){
if(s[i-1]=='A') pa++;
if(s[i-1]=='B') Bpa[++pb]=pa;
}
for(int i=1;i<=n;i++) dp[i][0]=1e15;
for(int k=1;k<=K;k++){
for(int i=1;i<=n;i++){
dp[i][k] = 1e18;
int cost = 0;
for(int j=1;j<=i;j++){
cost+=max(0,i-Bpa[i-j+1]);
dp[i][k] = min(dp[i][k],cost+dp[i-j][k-1]);
}
//cout<<dp[i][k]<<" ";
}
//cout<<"\n";
}
cout<<dp[n][K]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
472 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
472 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
15 ms |
2392 KB |
Output is correct |
19 |
Correct |
44 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
77 ms |
2420 KB |
Output is correct |
23 |
Correct |
76 ms |
2420 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
77 ms |
2396 KB |
Output is correct |
26 |
Correct |
64 ms |
2648 KB |
Output is correct |
27 |
Correct |
26 ms |
2396 KB |
Output is correct |
28 |
Correct |
27 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
472 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
15 ms |
2392 KB |
Output is correct |
19 |
Correct |
44 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
77 ms |
2420 KB |
Output is correct |
23 |
Correct |
76 ms |
2420 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
77 ms |
2396 KB |
Output is correct |
26 |
Correct |
64 ms |
2648 KB |
Output is correct |
27 |
Correct |
26 ms |
2396 KB |
Output is correct |
28 |
Correct |
27 ms |
2396 KB |
Output is correct |
29 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
472 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
15 ms |
2392 KB |
Output is correct |
19 |
Correct |
44 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
77 ms |
2420 KB |
Output is correct |
23 |
Correct |
76 ms |
2420 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
77 ms |
2396 KB |
Output is correct |
26 |
Correct |
64 ms |
2648 KB |
Output is correct |
27 |
Correct |
26 ms |
2396 KB |
Output is correct |
28 |
Correct |
27 ms |
2396 KB |
Output is correct |
29 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
472 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
15 ms |
2392 KB |
Output is correct |
19 |
Correct |
44 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
77 ms |
2420 KB |
Output is correct |
23 |
Correct |
76 ms |
2420 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
77 ms |
2396 KB |
Output is correct |
26 |
Correct |
64 ms |
2648 KB |
Output is correct |
27 |
Correct |
26 ms |
2396 KB |
Output is correct |
28 |
Correct |
27 ms |
2396 KB |
Output is correct |
29 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |