/ SeriousOJ /

Record Detail

Accepted


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

Code

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

#define endl '\n'
#ifdef LOCAL
    #include "debug.h"    
#else
    #define print(...)
    #define printt(...)
#endif

#define int long long

void run(){
    
    int n, k; string s;
    cin >> n >> k >> s;

    vector<int>a;
    int cnt = 0;
    for( int i = 0 ; i < n ; i++ ) {
        if(s[i] == '1') {
            cnt++;
        }else{
            if(cnt > 0)a.push_back(cnt);
            cnt = 0;
        }
    }
    if(cnt > 0)a.push_back(cnt);
    
    sort(a.rbegin(), a.rend());
    print(a);
    int ans = 0;
    for( int i = 0 ; i < min((int)a.size(), k+1) ; i++ )
        ans += a[i];
    
    cout << ans << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
    cin >> t;
    
    while( t --> 0 ) 
        run();
    
    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 14:42:31
Judged At
2025-02-17 14:42:31
Judged By
Score
100
Total Time
11ms
Peak Memory
1.773 MiB