#include <bits/stdc++.h>
using namespace std;
bool hey(pair<int,int>a , pair<int,int>b){
if(a.first != b.first){
return a.first > b.first;
}
else{
a.second < b.second;
}
}
int main() {
long long int t;std::cin >> t;
while(t--){
long long int n,k;std::cin >> n>>k;
string s;std::cin >> s;
int temp=0;
for(int i=0;i<n;i++){
if(s[i]=='0'){
temp++;
}
}
if(temp==0||temp==n){
std::cout << 0 << std::endl;continue;
}
std::vector<pair<long long int,long long int>>tejus;
long long int count0=0;
for(long long int i=0;i<s.length();i++){
if(s[i]=='0'){
count0++;
}
else{
if(count0>0){
tejus.push_back({count0,2});
}
count0=0;
}
}
tejus.push_back({count0,2});
if(s[0]=='0'){
tejus[0].second=1;
}
if(s[n-1]=='0'){
tejus[tejus.size()-1].second=1;
}
sort(tejus.begin() , tejus.end() , hey);
/*for(int i=0;i<tejus.size();i++){
std::cout << tejus[i].first<<" "<<tejus[i].second<< std::endl;
}*/
for(int i=0;i<tejus.size();i++){
if(k>=tejus[i].second){
k=k-tejus[i].second;tejus[i].second=0;
}
if(k==0){
break;
}
}
int answer=0;
for(int i=0;i<tejus.size();i++){
if(tejus[i].second!=0){
answer=answer+tejus[i].first;
}
}
std::cout << answer << std::endl;
}
return 0;
}
here i used the same logic with different data structure
in vector pair , the first one is no of zeroes and next second is the cost required to eliminate that using k;
ok!!
kindly tell me the case with which i am not getting AC
https://www.codechef.com/viewsolution/42012210
my WA solution