/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 10ms 532.0 KiB
#3 Accepted 7ms 532.0 KiB
#4 Accepted 4ms 532.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Accepted 11ms 700.0 KiB
#7 Accepted 6ms 580.0 KiB
#8 Accepted 7ms 744.0 KiB
#9 Accepted 16ms 1.277 MiB
#10 Accepted 11ms 1.277 MiB
#11 Accepted 5ms 788.0 KiB
#12 Accepted 5ms 788.0 KiB
#13 Accepted 10ms 1.773 MiB
#14 Accepted 6ms 836.0 KiB

Code

#include <bits/stdc++.h>
#define pb push_back
#define pi 2*acos(0.0)
#define f first
#define s second
#define inf INT_MAX
#define MXN 500001
#define endl "\n"
#define eps 1e-6
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rev(x) x.rbegin(), x.rend()
#define spc(x) cout << fixed << setprecision(x)
#define fastio {ios_base::sync_with_stdio(false); cin.tie(NULL);}

typedef long long ll;
using namespace std;

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

    string s; 
    cin >> s;

    vector<ll> v;
    ll cnt = 0;

    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '1') {
            cnt++;
        } else {
            if (cnt > 0) {
                v.pb(cnt);
            }
            cnt = 0;
        }
    }
    if (cnt > 0) {
        v.pb(cnt);  
    }

    if (v.empty()) {
        cout << 0 << endl;
        return;
    }
    sort(rev(v));
   
      ll ans = 0;
      for (int i = 0; i < min(k+1,(ll)v.size()); i++) {
               ans += v[i];         
      }
       cout << ans << endl;  
}

int32_t main() {
    fastio
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 18:54:42
Judged At
2025-02-17 18:54:42
Judged By
Score
100
Total Time
16ms
Peak Memory
1.773 MiB