#include<bits/stdc++.h>
#define MAXN 5007
using namespace std;
const int inf=1e15;
int n,k;
long long ans;
char s[MAXN];
long long dp[MAXN][MAXN];
int opt[MAXN][MAXN],pref[MAXN];
long long sum[MAXN][MAXN],sum2[MAXN][MAXN];
long long cost(int l,int r){
if((r-l+1)%2!=0)return inf;
return (sum[(r-l+1)/2][(l+r)/2] - sum[0][(l+r)/2-(r-l+1)/2] + sum2[(r-l+1)/2-1][(l+r)/2+1])/2;
}
void solve(int l,int r,int optl,int optr,int k){
if(l>r)return;
int mid=(l+r)/2;
dp[mid][k]=inf;
for(int i=mid;i>=1;i--){
if(dp[i-1][k-1]+cost(i,mid)<dp[mid][k]){
dp[mid][k]=dp[i-1][k-1]+cost(i,mid);
opt[mid][k]=i;
}
}
solve(l,mid-1,optl,opt[mid][k],k);
solve(mid+1,r,opt[mid][k],optr,k);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
n*=2;
cin>>(s+1);
for(int i=1;i<=n;i++){
if(s[i]=='A')pref[i]++;
else pref[i]--;
pref[i]+=pref[i-1];
}
for(int i=0;i<=n/2;i++){
for(int f=1;f<=n;f++){
sum[i][f]=max(i-pref[f],0);
if(i>0)sum[i][f]+=sum[i-1][f-1];
sum2[i][f]=max(i-pref[f],0);
if(i>0)sum2[i][f]+=sum2[i-1][f+1];
}
}
for(int i=1;i<=n;i++){
dp[i][0]=inf;
}
ans=inf;
for(int i=1;i<=k;i++){
solve(1,n,1,n,i);
ans=min(ans,dp[n][i]);
}
cout<<ans<<"\n";
return 0;
}
Compilation message
chorus.cpp:5:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+15' to '2147483647' [-Woverflow]
5 | const int inf=1e15;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
16 ms |
33884 KB |
Output is correct |
18 |
Correct |
167 ms |
96144 KB |
Output is correct |
19 |
Correct |
433 ms |
96156 KB |
Output is correct |
20 |
Correct |
15 ms |
95836 KB |
Output is correct |
21 |
Correct |
17 ms |
95836 KB |
Output is correct |
22 |
Correct |
752 ms |
96408 KB |
Output is correct |
23 |
Correct |
749 ms |
96336 KB |
Output is correct |
24 |
Correct |
28 ms |
95836 KB |
Output is correct |
25 |
Correct |
764 ms |
96212 KB |
Output is correct |
26 |
Correct |
636 ms |
96416 KB |
Output is correct |
27 |
Correct |
277 ms |
96084 KB |
Output is correct |
28 |
Correct |
281 ms |
95948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
16 ms |
33884 KB |
Output is correct |
18 |
Correct |
167 ms |
96144 KB |
Output is correct |
19 |
Correct |
433 ms |
96156 KB |
Output is correct |
20 |
Correct |
15 ms |
95836 KB |
Output is correct |
21 |
Correct |
17 ms |
95836 KB |
Output is correct |
22 |
Correct |
752 ms |
96408 KB |
Output is correct |
23 |
Correct |
749 ms |
96336 KB |
Output is correct |
24 |
Correct |
28 ms |
95836 KB |
Output is correct |
25 |
Correct |
764 ms |
96212 KB |
Output is correct |
26 |
Correct |
636 ms |
96416 KB |
Output is correct |
27 |
Correct |
277 ms |
96084 KB |
Output is correct |
28 |
Correct |
281 ms |
95948 KB |
Output is correct |
29 |
Runtime error |
13 ms |
348 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
16 ms |
33884 KB |
Output is correct |
18 |
Correct |
167 ms |
96144 KB |
Output is correct |
19 |
Correct |
433 ms |
96156 KB |
Output is correct |
20 |
Correct |
15 ms |
95836 KB |
Output is correct |
21 |
Correct |
17 ms |
95836 KB |
Output is correct |
22 |
Correct |
752 ms |
96408 KB |
Output is correct |
23 |
Correct |
749 ms |
96336 KB |
Output is correct |
24 |
Correct |
28 ms |
95836 KB |
Output is correct |
25 |
Correct |
764 ms |
96212 KB |
Output is correct |
26 |
Correct |
636 ms |
96416 KB |
Output is correct |
27 |
Correct |
277 ms |
96084 KB |
Output is correct |
28 |
Correct |
281 ms |
95948 KB |
Output is correct |
29 |
Runtime error |
13 ms |
348 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
16 ms |
33884 KB |
Output is correct |
18 |
Correct |
167 ms |
96144 KB |
Output is correct |
19 |
Correct |
433 ms |
96156 KB |
Output is correct |
20 |
Correct |
15 ms |
95836 KB |
Output is correct |
21 |
Correct |
17 ms |
95836 KB |
Output is correct |
22 |
Correct |
752 ms |
96408 KB |
Output is correct |
23 |
Correct |
749 ms |
96336 KB |
Output is correct |
24 |
Correct |
28 ms |
95836 KB |
Output is correct |
25 |
Correct |
764 ms |
96212 KB |
Output is correct |
26 |
Correct |
636 ms |
96416 KB |
Output is correct |
27 |
Correct |
277 ms |
96084 KB |
Output is correct |
28 |
Correct |
281 ms |
95948 KB |
Output is correct |
29 |
Runtime error |
13 ms |
348 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |