/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 6ms 532.0 KiB
#3 Accepted 8ms 584.0 KiB
#4 Accepted 5ms 532.0 KiB
#5 Accepted 10ms 532.0 KiB
#6 Accepted 12ms 584.0 KiB
#7 Accepted 6ms 536.0 KiB
#8 Accepted 6ms 696.0 KiB
#9 Accepted 14ms 1.32 MiB
#10 Accepted 10ms 1.32 MiB
#11 Accepted 5ms 796.0 KiB
#12 Accepted 6ms 788.0 KiB
#13 Accepted 16ms 1.773 MiB
#14 Accepted 7ms 792.0 KiB

Code

/*
 * Author: Md. Mahfuzur Rahman
 * Purpose: Competitive Programming
 */

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define pb push_back
#define endl '\n'
#define nl cout << endl
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define srt(v) sort(v.begin(),v.end())
#define pii pair<int, int>
#define pll pair<long,long>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define maxheap priority_queue<long long, vector<long long>>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
template <typename... T>
void read(T&... args){((cin>>args), ...);}
template <typename... T>
void write(T... args){((cout<<args<<" "), ...);}

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
const int MAX = 1e6;


void solve(int tc)
{
    ll n,k;cin>>n>>k;
    string s;cin>>s;
    // ll ct=0;
    // for(auto x:s){
    //     if(x=='1')ct++;
    // }
    // if(k>=n/2){
    //     cout<<ct<<endl;
    //     return;
    // }

    vector<ll>cons;

    ll ct=0;
    ll start=0;
    for(ll i=0;i<n;i++)
    {
        if(start==0&&s[i]=='1'){
            start=1;
            ct++;
        }
        else if(start==1&&s[i]=='1'){
            ct++;
        }
        else if(start==1&&s[i]=='0'){
            start=0;
            cons.push_back(ct);
            ct=0;
        }
    }
    
    if(ct){
        cons.push_back(ct);
    }
    sort(all(cons));
    reverse(all(cons));
    cons.push_back(0);
    // debug(cons);
    
    ll ans=cons[0],sz=cons.size();;
    // debug(ans);
    for(ll i=1;i<=min(sz-1,k);i++){
        ans+=cons[i];
        // debug(ans);

    }   
    cout<<ans<<endl;
}

int main()
{
    FAST_IO;
    int t = 1;
    int tc=1;
    cin >> t;
    while (t--)
    {
        solve(tc++);
    }
    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:04:14
Judged At
2025-02-17 15:04:14
Judged By
Score
100
Total Time
16ms
Peak Memory
1.773 MiB