/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 21ms 576.0 KiB
#3 Wrong Answer 26ms 368.0 KiB

Code

// Author: Shawn Das Shachin-->(shawn_das)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mod 1000000007
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define OPTIMIZE_IO ios::sync_with_stdio(false); cin.tie(nullptr);


void solve() {
    ll n, k;
    cin >> n >> k;
    string str;
    cin >> str;

    vector<ll> counts;
    ll count = 0,tc=0;
    for (char it : str) {
        if (it == '1') {
            count++;
            tc++;
        } else {
            if (count > 0) {
                counts.pb(count);
            }
            count = 0;
        }
    }
    if (count > 0) {
        counts.pb(count);
    }
    rsrt(counts);
    if (counts.empty()) {
        cout << 0 << endl;
        return;
    }

    if (k == 0) {
        cout << counts[0] << endl;
        return;
    }

    ll ans = counts[0];
    

    for (ll i = 1; i<=min(n,k); i++) {
        ans += counts[i];
    }
    cout << min(tc,ans) << endl;
}

int main() {
    OPTIMIZE_IO;
    int t;
    cin >> t;
    while (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 16:12:54
Judged At
2025-02-17 16:12:54
Judged By
Score
0
Total Time
26ms
Peak Memory
576.0 KiB