/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 6ms 600.0 KiB
#3 Accepted 7ms 540.0 KiB
#4 Accepted 4ms 540.0 KiB
#5 Accepted 4ms 592.0 KiB
#6 Accepted 4ms 796.0 KiB
#7 Accepted 2ms 796.0 KiB
#8 Accepted 2ms 980.0 KiB
#9 Accepted 4ms 1.535 MiB
#10 Accepted 5ms 1.535 MiB
#11 Accepted 2ms 1.027 MiB
#12 Accepted 2ms 796.0 KiB
#13 Accepted 4ms 2.031 MiB
#14 Accepted 2ms 1.027 MiB

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