/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 6ms 532.0 KiB
#3 Accepted 10ms 584.0 KiB
#4 Accepted 6ms 532.0 KiB
#5 Accepted 6ms 532.0 KiB
#6 Accepted 6ms 700.0 KiB
#7 Accepted 4ms 580.0 KiB
#8 Accepted 6ms 696.0 KiB
#9 Accepted 16ms 1.348 MiB
#10 Accepted 12ms 1.277 MiB
#11 Accepted 5ms 788.0 KiB
#12 Accepted 5ms 788.0 KiB
#13 Accepted 11ms 1.816 MiB
#14 Accepted 5ms 792.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:08:08
Judged At
2025-02-17 15:08:08
Judged By
Score
100
Total Time
16ms
Peak Memory
1.816 MiB