/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 7ms 532.0 KiB
#3 Accepted 10ms 532.0 KiB
#4 Accepted 8ms 324.0 KiB
#5 Accepted 8ms 532.0 KiB
#6 Accepted 9ms 704.0 KiB
#7 Accepted 3ms 532.0 KiB
#8 Accepted 4ms 740.0 KiB
#9 Accepted 8ms 1.141 MiB
#10 Accepted 14ms 1.219 MiB
#11 Accepted 5ms 788.0 KiB
#12 Accepted 10ms 1.773 MiB
#13 Accepted 8ms 1.25 MiB
#14 Accepted 5ms 788.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

void solve(int T) {
	int n, k; cin >> n >> k;
	string s; cin >> s;
	s.push_back('0');

	priority_queue<int> pq;
	int cnt = 0;
	for(auto c : s) {
		if(c == '1') cnt++;
		else {
			pq.push(cnt);
			cnt = 0;
		}
	}
	ll ans = 0;
	if(!pq.empty()) {
		ans = pq.top();
		pq.pop();
		while(!pq.empty() && k) {
			ans += pq.top();
			pq.pop();
			k--;
		}
	}

	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
#ifdef MYPC
	cout << "==== OUTPUT ====\n";
#endif
	int t; cin >> t;
	for(int i = 1; i <= t; i++) {
		solve(i);
	}
}

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:44:34
Judged At
2025-02-17 14:44:34
Judged By
Score
100
Total Time
14ms
Peak Memory
1.773 MiB