/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 328.0 KiB
#2 Accepted 6ms 588.0 KiB
#3 Accepted 7ms 572.0 KiB
#4 Accepted 4ms 772.0 KiB
#5 Accepted 4ms 584.0 KiB
#6 Accepted 4ms 584.0 KiB
#7 Accepted 2ms 772.0 KiB
#8 Accepted 2ms 704.0 KiB
#9 Accepted 4ms 1.035 MiB
#10 Accepted 4ms 1.035 MiB
#11 Accepted 2ms 796.0 KiB
#12 Accepted 2ms 796.0 KiB
#13 Accepted 4ms 1.285 MiB
#14 Accepted 2ms 1.004 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int tt; cin >> tt;
	while (tt--) {
		int n, k; cin >> n >> k;
		string s; cin >> s;

		int cnt = 0;
		vector<int> v;
		for (int i = 0; i < n; i++) {
			if (s[i] == '1') cnt++;
			else {
				if (cnt) v.push_back(cnt);
				cnt = 0;
			}
		}

		if (cnt) v.push_back(cnt);
		cnt = 0;

		sort(v.rbegin(), v.rend());

		n = v.size();
		for (int i = 0; i < n; i++) {
			// cout << v[i] << ' ';
			if (k >= 0) {
				k--;
				cnt += v[i];
			}
			// else {
			// 	break;
			// }
		}

		cout << cnt << '\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:58:24
Judged At
2025-02-17 14:58:24
Judged By
Score
100
Total Time
7ms
Peak Memory
1.285 MiB