/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 6ms 576.0 KiB
#3 Accepted 9ms 532.0 KiB
#4 Accepted 6ms 536.0 KiB
#5 Accepted 6ms 532.0 KiB
#6 Accepted 7ms 580.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 6ms 700.0 KiB
#9 Accepted 15ms 1.027 MiB
#10 Accepted 15ms 1.023 MiB
#11 Accepted 6ms 788.0 KiB
#12 Accepted 7ms 788.0 KiB
#13 Accepted 13ms 1.277 MiB
#14 Accepted 7ms 1020.0 KiB

Code

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

inline void solve()
{
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    // get them
    vector<int> A;
    int len = 0;
    for(int i=0; i < s.size(); ++i){
        if( s[i] == '1' ) len ++;
        else if( len > 0 ){
            A.push_back( len );
            len = 0;
        }
    }
    if( len > 0 ) A.push_back( len );

    sort(A.rbegin(), A.rend() );

    int lim = min((int)A.size(), k+1);
    int sum = 0;
    for(int i=0; i < lim; ++i) sum += A[i];

    cout << sum << '\n';
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int t = 1;
    cin >> t;
    for(int i=1; i <= t; ++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 14:43:38
Judged At
2025-02-17 14:43:38
Judged By
Score
100
Total Time
15ms
Peak Memory
1.277 MiB