/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 31ms 564.0 KiB
#3 Accepted 36ms 548.0 KiB
#4 Accepted 15ms 540.0 KiB
#5 Accepted 12ms 396.0 KiB
#6 Accepted 13ms 788.0 KiB
#7 Accepted 9ms 584.0 KiB
#8 Accepted 9ms 976.0 KiB
#9 Accepted 15ms 1.648 MiB
#10 Accepted 15ms 1.75 MiB
#11 Accepted 9ms 832.0 KiB
#12 Accepted 16ms 3.82 MiB
#13 Accepted 13ms 1.73 MiB
#14 Accepted 9ms 856.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;
#define int long long int
int32_t main() {
    int t, n, k;
    cin >> t;
    while (t--) {
        cin >> n >> k;
        string s;
        cin >> s;

        vector < int > v;
        int c = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '1') {
                c++;
            }
            else {
                v.push_back(c);
                c = 0;
            }
        }
        if (c) {
            v.push_back(c);
        }
        sort(rbegin(v),rend(v));
        int res = 0;
        for (int i = 0; i < min(k + 1, (int)v.size()); i++) {
            res += v[i];
        }
        cout << res << "\n";
    }
    // your code goes here
}

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:57:24
Judged At
2025-02-17 15:57:24
Judged By
Score
100
Total Time
36ms
Peak Memory
3.82 MiB