/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 380.0 KiB
#2 Accepted 6ms 540.0 KiB
#3 Accepted 7ms 580.0 KiB
#4 Accepted 5ms 540.0 KiB
#5 Accepted 4ms 772.0 KiB
#6 Accepted 4ms 700.0 KiB
#7 Accepted 2ms 540.0 KiB
#8 Accepted 2ms 704.0 KiB
#9 Accepted 5ms 1.285 MiB
#10 Accepted 5ms 1.512 MiB
#11 Accepted 2ms 796.0 KiB
#12 Accepted 2ms 796.0 KiB
#13 Accepted 4ms 1.781 MiB
#14 Accepted 2ms 796.0 KiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

void solve() {
    ll n, k;
    cin >> n >> k;
    string s1;
    cin >> s1;
    vector<ll> con1;
    for(int i = 0; i < n; ) {
        int j = i;
        while(j < n && s1[j] == '1') ++j;
        if(j - i > 0) con1.push_back(j - i);
        while(j < n && s1[j] == '0') ++j;
        i = j;
    }
    sort(con1.begin(), con1.end());
    ll ans = 0;
    k++;
    while(k-- && con1.size()) {
        ans += con1.back();
        con1.pop_back();
    }
    cout << ans << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    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:47:51
Judged At
2025-02-17 14:47:51
Judged By
Score
100
Total Time
7ms
Peak Memory
1.781 MiB