#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll INF=1e17;
ll n,k,lga,lgb,makegood;
ll A[1000005],B[1000005];
struct date
{
ll val,l,r;
} dp[1000005];
pll v[1000005];
string str;
date combine(date a, date b)
{
if(a.val<b.val)
return a;
if(a.val>b.val)
return b;
a.l=min(a.l,b.l);
a.r=max(a.r,b.r);
return a;
}
void solve(ll cost)
{
for(int i=n;i>=1;i--)
{
dp[i]={INF,0,0};
ll suma=0;
ll C=0;
int ind=i;
for(int j=i;j<=n;j++)
{
while(ind<=n&&B[ind]<A[j])
{
suma++;
ind++;
}
C+=suma;
date a=dp[j+1];
a.val+=C+cost;
a.l++;
a.r++;
dp[i]=combine(dp[i],a);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
cin>>str;
for(int i=0;i<str.size();i++)
{
if(str[i]=='A')
A[++lga]=i+1;
else
B[++lgb]=i+1;
}
for(int i=1;i<=n;i++)
if(A[i]>B[i])
{
ll x=A[i];
A[i]=B[i];
for(int j=i;j<=n;j++)
{
if(B[j]<x)
{
B[j]++;
makegood++;
}
else
break;
}
}
ll st=0;
ll dr=2e12;
while(st<=dr)
{
ll mij=(st+dr)/2;
solve(mij);
if(dp[1].l<=k&&dp[1].r>=k)
{
ll rez=dp[1].val-mij*k;
cout<<rez+makegood;
return 0;
}
if(dp[1].l>k)
st=mij+1;
else
dr=mij-1;
}
return 0;
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<str.size();i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6560 KB |
Output is correct |
17 |
Correct |
5 ms |
6492 KB |
Output is correct |
18 |
Correct |
19 ms |
6492 KB |
Output is correct |
19 |
Correct |
24 ms |
6488 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
8 ms |
6492 KB |
Output is correct |
22 |
Correct |
12 ms |
6492 KB |
Output is correct |
23 |
Correct |
11 ms |
6492 KB |
Output is correct |
24 |
Correct |
17 ms |
6488 KB |
Output is correct |
25 |
Correct |
12 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6492 KB |
Output is correct |
27 |
Correct |
13 ms |
6620 KB |
Output is correct |
28 |
Correct |
13 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6560 KB |
Output is correct |
17 |
Correct |
5 ms |
6492 KB |
Output is correct |
18 |
Correct |
19 ms |
6492 KB |
Output is correct |
19 |
Correct |
24 ms |
6488 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
8 ms |
6492 KB |
Output is correct |
22 |
Correct |
12 ms |
6492 KB |
Output is correct |
23 |
Correct |
11 ms |
6492 KB |
Output is correct |
24 |
Correct |
17 ms |
6488 KB |
Output is correct |
25 |
Correct |
12 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6492 KB |
Output is correct |
27 |
Correct |
13 ms |
6620 KB |
Output is correct |
28 |
Correct |
13 ms |
6744 KB |
Output is correct |
29 |
Correct |
1210 ms |
8664 KB |
Output is correct |
30 |
Correct |
2426 ms |
8788 KB |
Output is correct |
31 |
Correct |
2421 ms |
8668 KB |
Output is correct |
32 |
Correct |
37 ms |
8784 KB |
Output is correct |
33 |
Correct |
518 ms |
8668 KB |
Output is correct |
34 |
Correct |
1055 ms |
8664 KB |
Output is correct |
35 |
Correct |
1072 ms |
8788 KB |
Output is correct |
36 |
Correct |
1469 ms |
8672 KB |
Output is correct |
37 |
Correct |
2019 ms |
8672 KB |
Output is correct |
38 |
Correct |
1080 ms |
8664 KB |
Output is correct |
39 |
Correct |
1569 ms |
8664 KB |
Output is correct |
40 |
Correct |
1166 ms |
8788 KB |
Output is correct |
41 |
Correct |
1055 ms |
8668 KB |
Output is correct |
42 |
Correct |
1063 ms |
8672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6560 KB |
Output is correct |
17 |
Correct |
5 ms |
6492 KB |
Output is correct |
18 |
Correct |
19 ms |
6492 KB |
Output is correct |
19 |
Correct |
24 ms |
6488 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
8 ms |
6492 KB |
Output is correct |
22 |
Correct |
12 ms |
6492 KB |
Output is correct |
23 |
Correct |
11 ms |
6492 KB |
Output is correct |
24 |
Correct |
17 ms |
6488 KB |
Output is correct |
25 |
Correct |
12 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6492 KB |
Output is correct |
27 |
Correct |
13 ms |
6620 KB |
Output is correct |
28 |
Correct |
13 ms |
6744 KB |
Output is correct |
29 |
Correct |
1210 ms |
8664 KB |
Output is correct |
30 |
Correct |
2426 ms |
8788 KB |
Output is correct |
31 |
Correct |
2421 ms |
8668 KB |
Output is correct |
32 |
Correct |
37 ms |
8784 KB |
Output is correct |
33 |
Correct |
518 ms |
8668 KB |
Output is correct |
34 |
Correct |
1055 ms |
8664 KB |
Output is correct |
35 |
Correct |
1072 ms |
8788 KB |
Output is correct |
36 |
Correct |
1469 ms |
8672 KB |
Output is correct |
37 |
Correct |
2019 ms |
8672 KB |
Output is correct |
38 |
Correct |
1080 ms |
8664 KB |
Output is correct |
39 |
Correct |
1569 ms |
8664 KB |
Output is correct |
40 |
Correct |
1166 ms |
8788 KB |
Output is correct |
41 |
Correct |
1055 ms |
8668 KB |
Output is correct |
42 |
Correct |
1063 ms |
8672 KB |
Output is correct |
43 |
Execution timed out |
7031 ms |
12980 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6560 KB |
Output is correct |
17 |
Correct |
5 ms |
6492 KB |
Output is correct |
18 |
Correct |
19 ms |
6492 KB |
Output is correct |
19 |
Correct |
24 ms |
6488 KB |
Output is correct |
20 |
Correct |
3 ms |
6744 KB |
Output is correct |
21 |
Correct |
8 ms |
6492 KB |
Output is correct |
22 |
Correct |
12 ms |
6492 KB |
Output is correct |
23 |
Correct |
11 ms |
6492 KB |
Output is correct |
24 |
Correct |
17 ms |
6488 KB |
Output is correct |
25 |
Correct |
12 ms |
6492 KB |
Output is correct |
26 |
Correct |
13 ms |
6492 KB |
Output is correct |
27 |
Correct |
13 ms |
6620 KB |
Output is correct |
28 |
Correct |
13 ms |
6744 KB |
Output is correct |
29 |
Correct |
1210 ms |
8664 KB |
Output is correct |
30 |
Correct |
2426 ms |
8788 KB |
Output is correct |
31 |
Correct |
2421 ms |
8668 KB |
Output is correct |
32 |
Correct |
37 ms |
8784 KB |
Output is correct |
33 |
Correct |
518 ms |
8668 KB |
Output is correct |
34 |
Correct |
1055 ms |
8664 KB |
Output is correct |
35 |
Correct |
1072 ms |
8788 KB |
Output is correct |
36 |
Correct |
1469 ms |
8672 KB |
Output is correct |
37 |
Correct |
2019 ms |
8672 KB |
Output is correct |
38 |
Correct |
1080 ms |
8664 KB |
Output is correct |
39 |
Correct |
1569 ms |
8664 KB |
Output is correct |
40 |
Correct |
1166 ms |
8788 KB |
Output is correct |
41 |
Correct |
1055 ms |
8668 KB |
Output is correct |
42 |
Correct |
1063 ms |
8672 KB |
Output is correct |
43 |
Execution timed out |
7031 ms |
12980 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |