/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 560.0 KiB
#2 Accepted 6ms 580.0 KiB
#3 Accepted 7ms 580.0 KiB
#4 Accepted 6ms 540.0 KiB
#5 Accepted 4ms 788.0 KiB
#6 Accepted 4ms 816.0 KiB
#7 Accepted 2ms 588.0 KiB
#8 Accepted 2ms 960.0 KiB
#9 Accepted 5ms 1.312 MiB
#10 Accepted 4ms 1.535 MiB
#11 Accepted 3ms 796.0 KiB
#12 Accepted 2ms 1.07 MiB
#13 Accepted 4ms 1.781 MiB
#14 Accepted 2ms 796.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define lcm(a,b) ((a*b)/__gcd(a,b))
const double PI = 2 * acos(0.0);
const int MOD = 1000000007;

void solve(){
    int n, k;
    cin >> n >> k;
    string s; cin >> s;

    vector <ll> a;
    int x = 0;

    for (int i = 0; i < n; i++){
        if (s[i] == '1'){
            x++;
        }
        else if (x){
            a.push_back(x);
            x = 0;
        }
    }
    if (s[n-1] == '1')
        a.push_back(x);

    if (a.empty()){
        cout << 0 << endl;
        return;
    }
    
    if (k == 0){
        int ans = *max_element(a.begin(), a.end()); 
        cout << ans <<endl;
        return;
    }
    
    sort(a.rbegin(), a.rend());
    // for (auto &i : a) cout << i << ' ';
    // cout << endl;

    ll cnt = 0;
    int run = 0;

    if (k > a.size()-1)
        run = a.size()-1;
    else   
        run = k;

    for (ll i = 0; i <= run; i++){
        cnt += a[i];
    }
    cout << cnt << endl;
}

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

    int t;
    cin >> t;

    while(t--){
        solve();
    }    
}

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:23:32
Judged At
2025-02-17 15:23:32
Judged By
Score
100
Total Time
7ms
Peak Memory
1.781 MiB