Accepted
Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll testcases;
cin >> testcases;
while (testcases--) {
ll n , k;
cin >> n >> k;
string s;
cin >> s;
char num = s[0];
ll i = 0 , cnt = 0;
vector<ll> arr;
while(i < n){
if(s[i] == num){
cnt++;
}
else{
if(cnt && num == '1') arr.push_back(cnt);
num = s[i];
cnt = 0;
continue;
}
i++;
}
if(cnt && num == '1') arr.push_back(cnt);
k++;
ll ans = 0 , curr = 0;
sort(arr.begin() , arr.end() , greater<ll>());
ll sz = arr.size();
for(ll i = 0 ; i < min(k , sz) ; i++){
ans += arr[i];
}
cout << ans << "\n";
}
return 0;
}
// Author : Raj (raj_singh35)
Information
- Submit By
- Type
- Submission
- Problem
- P1159 Binary String
- Contest
- Brain Booster #8
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2025-02-17 15:35:55
- Judged At
- 2025-02-17 15:35:55
- Judged By
- Score
- 100
- Total Time
- 7ms
- Peak Memory
- 2.031 MiB