Accepted
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n, k;
string s;
cin >> n >> k >> s;
k++;
vector<int> X;
for (int i = 0; i < n; i++)
{
if (s[i] == '0')
continue;
int ptr = i;
while (ptr < n && s[ptr] == '1')
ptr++;
X.push_back(ptr - i);
i = ptr - 1;
}
int res = 0;
k = min(k, (int) X.size());
sort (X.begin(), X.end(), greater<>());
for (int i = 0; i < k; i++)
res += X[i];
cout << res << "\n";
}
return 0;
}
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 14:46:48
- Judged At
- 2025-02-17 14:46:48
- Judged By
- Score
- 100
- Total Time
- 14ms
- Peak Memory
- 1.344 MiB