Accepted
Code
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int a,b;
cin>>a>>b;
int cnt1=0;
vector<int> v;
bool bin= false;
string ss;
cin>>ss;
for(int i=0; i<a; i++){
if(ss[i]- '0' == 1 && !bin){
bin=true;
cnt1++;
}
else if(ss[i]- '0' == 1 && bin){
cnt1++;
}
else if(ss[i]- '0' == 0 && bin){
v.push_back(cnt1);
bin=false;
cnt1=0;
}
}
if(ss[a-1]-'0'==1) {
v.push_back(cnt1);
}
sort(v.begin(), v.end(), greater<int>());
int c= v.size();
if(b>=c){
int y=0;
for(int x: v){
y+=x;
}
cout<< y<< '\n';
}
else{
int d=v[0];
for(int i=1; i<=b; i++){
d+=v[i];
}
cout<< d <<'\n';
}
}
}
Information
- Submit By
- Type
- Submission
- Problem
- P1159 Binary String
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2025-02-17 17:37:28
- Judged At
- 2025-02-17 17:37:28
- Judged By
- Score
- 100
- Total Time
- 20ms
- Peak Memory
- 1.281 MiB