#include <bits/stdc++.h>
using namespace std;
const int nx=1e6+5;
#define ll long long
ll n, k, h[nx], p[nx], qs[nx], r=0, c=1, idx=1;
pair<ll, ll> dp[nx];
string s;
ll cost(ll l, ll r)
{
if (p[r]<=l) return 0;
return r*(p[r]-l)-(qs[p[r]-1]-qs[l-1]);
}
pair<ll, ll> solve(ll lambda)
{
for (int i=1; i<=n; i++)
{
dp[i]={LLONG_MAX, 0};
for (int j=1; j<=i; j++)
{
dp[i]=min(dp[i], {dp[j-1].first+cost(j, i)+lambda, dp[j-1].second+1});
}
}
return dp[n];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k>>s;
for (int i=0; i<2*n; i++)
{
if (s[i]=='A') r++, h[c]=r;
if (s[i]=='B') c++, h[c]=r;
}
for (int i=1; i<=n; i++)
{
qs[i]=qs[i-1]+h[i];
while (idx<=i&&h[idx]<i) idx++;
p[i]=idx;
}
//for (int i=1; i<=n; i++) cout<<i<<' '<<h[i]<<' '<<p[i]<<'\n';
ll l=0, r=1e12;
while (l<r)
{
ll md=(l+r)/2;
//cout<<md<<' '<<solve(md).first<<' '<<solve(md).second<<'\n';
if (solve(md).second<=k) r=md;
else l=md+1;
}
//cout<<"debug "<<l<<' '<<solve(l).first<<' '<<solve(l).second<<'\n';
cout<<solve(l).first-k*l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
8 ms |
4560 KB |
Output is correct |
19 |
Correct |
10 ms |
4444 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
9 ms |
4444 KB |
Output is correct |
22 |
Correct |
11 ms |
4444 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
9 ms |
4592 KB |
Output is correct |
25 |
Correct |
9 ms |
4444 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
9 ms |
4592 KB |
Output is correct |
28 |
Correct |
9 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
8 ms |
4560 KB |
Output is correct |
19 |
Correct |
10 ms |
4444 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
9 ms |
4444 KB |
Output is correct |
22 |
Correct |
11 ms |
4444 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
9 ms |
4592 KB |
Output is correct |
25 |
Correct |
9 ms |
4444 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
9 ms |
4592 KB |
Output is correct |
28 |
Correct |
9 ms |
4588 KB |
Output is correct |
29 |
Correct |
723 ms |
4700 KB |
Output is correct |
30 |
Correct |
761 ms |
4704 KB |
Output is correct |
31 |
Correct |
739 ms |
4700 KB |
Output is correct |
32 |
Correct |
801 ms |
4700 KB |
Output is correct |
33 |
Correct |
789 ms |
4700 KB |
Output is correct |
34 |
Correct |
790 ms |
4708 KB |
Output is correct |
35 |
Correct |
722 ms |
4700 KB |
Output is correct |
36 |
Correct |
728 ms |
4700 KB |
Output is correct |
37 |
Correct |
747 ms |
4700 KB |
Output is correct |
38 |
Correct |
738 ms |
4700 KB |
Output is correct |
39 |
Correct |
739 ms |
4700 KB |
Output is correct |
40 |
Correct |
739 ms |
4700 KB |
Output is correct |
41 |
Correct |
768 ms |
4948 KB |
Output is correct |
42 |
Correct |
772 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
8 ms |
4560 KB |
Output is correct |
19 |
Correct |
10 ms |
4444 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
9 ms |
4444 KB |
Output is correct |
22 |
Correct |
11 ms |
4444 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
9 ms |
4592 KB |
Output is correct |
25 |
Correct |
9 ms |
4444 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
9 ms |
4592 KB |
Output is correct |
28 |
Correct |
9 ms |
4588 KB |
Output is correct |
29 |
Correct |
723 ms |
4700 KB |
Output is correct |
30 |
Correct |
761 ms |
4704 KB |
Output is correct |
31 |
Correct |
739 ms |
4700 KB |
Output is correct |
32 |
Correct |
801 ms |
4700 KB |
Output is correct |
33 |
Correct |
789 ms |
4700 KB |
Output is correct |
34 |
Correct |
790 ms |
4708 KB |
Output is correct |
35 |
Correct |
722 ms |
4700 KB |
Output is correct |
36 |
Correct |
728 ms |
4700 KB |
Output is correct |
37 |
Correct |
747 ms |
4700 KB |
Output is correct |
38 |
Correct |
738 ms |
4700 KB |
Output is correct |
39 |
Correct |
739 ms |
4700 KB |
Output is correct |
40 |
Correct |
739 ms |
4700 KB |
Output is correct |
41 |
Correct |
768 ms |
4948 KB |
Output is correct |
42 |
Correct |
772 ms |
4696 KB |
Output is correct |
43 |
Execution timed out |
7031 ms |
7520 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
8 ms |
4560 KB |
Output is correct |
19 |
Correct |
10 ms |
4444 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
21 |
Correct |
9 ms |
4444 KB |
Output is correct |
22 |
Correct |
11 ms |
4444 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
9 ms |
4592 KB |
Output is correct |
25 |
Correct |
9 ms |
4444 KB |
Output is correct |
26 |
Correct |
9 ms |
4444 KB |
Output is correct |
27 |
Correct |
9 ms |
4592 KB |
Output is correct |
28 |
Correct |
9 ms |
4588 KB |
Output is correct |
29 |
Correct |
723 ms |
4700 KB |
Output is correct |
30 |
Correct |
761 ms |
4704 KB |
Output is correct |
31 |
Correct |
739 ms |
4700 KB |
Output is correct |
32 |
Correct |
801 ms |
4700 KB |
Output is correct |
33 |
Correct |
789 ms |
4700 KB |
Output is correct |
34 |
Correct |
790 ms |
4708 KB |
Output is correct |
35 |
Correct |
722 ms |
4700 KB |
Output is correct |
36 |
Correct |
728 ms |
4700 KB |
Output is correct |
37 |
Correct |
747 ms |
4700 KB |
Output is correct |
38 |
Correct |
738 ms |
4700 KB |
Output is correct |
39 |
Correct |
739 ms |
4700 KB |
Output is correct |
40 |
Correct |
739 ms |
4700 KB |
Output is correct |
41 |
Correct |
768 ms |
4948 KB |
Output is correct |
42 |
Correct |
772 ms |
4696 KB |
Output is correct |
43 |
Execution timed out |
7031 ms |
7520 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |