/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 7ms 540.0 KiB
#3 Accepted 8ms 580.0 KiB
#4 Accepted 5ms 540.0 KiB
#5 Accepted 5ms 540.0 KiB
#6 Accepted 5ms 796.0 KiB
#7 Accepted 2ms 540.0 KiB
#8 Accepted 2ms 704.0 KiB
#9 Accepted 7ms 1.828 MiB
#10 Accepted 8ms 1.781 MiB
#11 Accepted 4ms 796.0 KiB
#12 Accepted 6ms 3.824 MiB
#13 Accepted 4ms 1.781 MiB
#14 Accepted 2ms 796.0 KiB

Code

/* SRIJON
        SINGHA
            SAMANTA */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define yes cout << "YES" << nl
#define no cout << "NO" << nl
#define fi first
#define sec second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define FIO                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
const int mod = 1e9 + 7;

void solve()
{

    ll n,k; cin>>n>>k;
    string s;   cin>>s;
    vector<ll>ans;
    ll x=0;
    for(ll i=0;i<n;i++)
    {
        if(s[i]=='1')   x++;
        else
        {
            ans.pb(x);
            x=0;
        }
    }
    ans.pb(x);
    sort(all(ans),greater<ll>());
    ll sz=ans.size();
    ll total=0;
    for(ll i=0;i<min(sz,k+1);i++)
    {
        total+=ans[i];          
    }
    cout<<total<<nl;

}

int32_t main()
{
    FIO

    ll t=1;
    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        // cout<<"Case "<<i<<": ";
        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 15:51:29
Judged At
2025-02-17 15:51:29
Judged By
Score
100
Total Time
8ms
Peak Memory
3.824 MiB