#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 5005;
pair<ll,int> dp[N];
int Bpa[N];
ll Bpasum[N];
int n,K;
ll cost(int i,int j){
int l = i;
int r = j+1;
while(r-l>1){
int mid = (l+r)/2;
if(Bpa[mid]<j) l = mid;
else r = mid;
}
int cnt = l-i;
ll ans = Bpasum[l]-Bpasum[i];
return cnt*j-ans;
}
bool ok(int v){
for(int i=0;i<=n;i++) dp[i] = {1e18,0};
dp[0]={0,0};
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
dp[i] = min({dp[i-j].first+cost(i-j,i)+v,dp[i-j].second+1},dp[i]);
}
}
//cout<<(dp[n].second<=K)<<" "<<dp[n].first<<" "<<v<<"\n";
return (dp[n].second<=K);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
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++){
Bpasum[i] = Bpasum[i-1]+Bpa[i];
}
ll l = -1;ll r = 1e9;
while(r-l>1){
ll mid = l+((r-l)/2);
if(ok(mid)) r = mid;
else l = mid;
}
ok(r);
//cout<<r<<"\n";
cout<<dp[n].first-r*K<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
84 ms |
444 KB |
Output is correct |
19 |
Correct |
91 ms |
344 KB |
Output is correct |
20 |
Correct |
89 ms |
448 KB |
Output is correct |
21 |
Correct |
96 ms |
444 KB |
Output is correct |
22 |
Correct |
90 ms |
344 KB |
Output is correct |
23 |
Correct |
87 ms |
344 KB |
Output is correct |
24 |
Correct |
86 ms |
452 KB |
Output is correct |
25 |
Correct |
86 ms |
344 KB |
Output is correct |
26 |
Correct |
88 ms |
448 KB |
Output is correct |
27 |
Correct |
83 ms |
448 KB |
Output is correct |
28 |
Correct |
83 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
84 ms |
444 KB |
Output is correct |
19 |
Correct |
91 ms |
344 KB |
Output is correct |
20 |
Correct |
89 ms |
448 KB |
Output is correct |
21 |
Correct |
96 ms |
444 KB |
Output is correct |
22 |
Correct |
90 ms |
344 KB |
Output is correct |
23 |
Correct |
87 ms |
344 KB |
Output is correct |
24 |
Correct |
86 ms |
452 KB |
Output is correct |
25 |
Correct |
86 ms |
344 KB |
Output is correct |
26 |
Correct |
88 ms |
448 KB |
Output is correct |
27 |
Correct |
83 ms |
448 KB |
Output is correct |
28 |
Correct |
83 ms |
348 KB |
Output is correct |
29 |
Execution timed out |
7085 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
84 ms |
444 KB |
Output is correct |
19 |
Correct |
91 ms |
344 KB |
Output is correct |
20 |
Correct |
89 ms |
448 KB |
Output is correct |
21 |
Correct |
96 ms |
444 KB |
Output is correct |
22 |
Correct |
90 ms |
344 KB |
Output is correct |
23 |
Correct |
87 ms |
344 KB |
Output is correct |
24 |
Correct |
86 ms |
452 KB |
Output is correct |
25 |
Correct |
86 ms |
344 KB |
Output is correct |
26 |
Correct |
88 ms |
448 KB |
Output is correct |
27 |
Correct |
83 ms |
448 KB |
Output is correct |
28 |
Correct |
83 ms |
348 KB |
Output is correct |
29 |
Execution timed out |
7085 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
348 KB |
Output is correct |
18 |
Correct |
84 ms |
444 KB |
Output is correct |
19 |
Correct |
91 ms |
344 KB |
Output is correct |
20 |
Correct |
89 ms |
448 KB |
Output is correct |
21 |
Correct |
96 ms |
444 KB |
Output is correct |
22 |
Correct |
90 ms |
344 KB |
Output is correct |
23 |
Correct |
87 ms |
344 KB |
Output is correct |
24 |
Correct |
86 ms |
452 KB |
Output is correct |
25 |
Correct |
86 ms |
344 KB |
Output is correct |
26 |
Correct |
88 ms |
448 KB |
Output is correct |
27 |
Correct |
83 ms |
448 KB |
Output is correct |
28 |
Correct |
83 ms |
348 KB |
Output is correct |
29 |
Execution timed out |
7085 ms |
604 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |