/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 6ms 532.0 KiB
#3 Accepted 9ms 532.0 KiB
#4 Accepted 15ms 532.0 KiB
#5 Accepted 13ms 532.0 KiB
#6 Accepted 15ms 684.0 KiB
#7 Accepted 6ms 532.0 KiB
#8 Accepted 7ms 760.0 KiB
#9 Accepted 17ms 1.277 MiB
#10 Accepted 16ms 1.152 MiB
#11 Accepted 3ms 788.0 KiB
#12 Accepted 3ms 788.0 KiB
#13 Accepted 12ms 1.82 MiB
#14 Accepted 6ms 832.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define nl "\n"
#define ws " "
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007

typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vii;

int main () {
    FASTIO
    int t;
    cin>>t;
    while (t--) {
        int n, k;
        cin>>n>>k;
        string s;
        cin>>s;
        ll cnt = 0;
        vi v;
        v.push_back(0);
        for (int i = 0; i < n; i++) {
            if (s[i] == '0') {
                if (cnt > 0) {
                    v.push_back(cnt);
                    cnt = 0;
                }
            } else {
                cnt++;
            }
        }
        if (cnt > 0) {
            v.push_back(cnt);
        }
        sort (v.rbegin(), v.rend());
        ll ans = v[0];
        for (int i = 1; i < (int) v.size(); i++) {
            if (k == 0) {
                break;
            }
            ans += v[i];
            k--;
        }
        cout<<ans<<nl;
    }
    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 15:07:10
Judged At
2025-02-17 15:07:10
Judged By
Score
100
Total Time
17ms
Peak Memory
1.82 MiB