/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 19ms 540.0 KiB
#3 Accepted 22ms 332.0 KiB
#4 Accepted 10ms 540.0 KiB
#5 Accepted 8ms 540.0 KiB
#6 Accepted 10ms 760.0 KiB
#7 Accepted 6ms 540.0 KiB
#8 Accepted 6ms 700.0 KiB
#9 Accepted 10ms 1.777 MiB
#10 Accepted 10ms 1.781 MiB
#11 Accepted 6ms 796.0 KiB
#12 Accepted 10ms 3.773 MiB
#13 Accepted 9ms 1.777 MiB
#14 Accepted 7ms 848.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
22ms
Peak Memory
3.773 MiB