/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 6ms 580.0 KiB
#3 Accepted 7ms 816.0 KiB
#4 Accepted 4ms 556.0 KiB
#5 Accepted 4ms 768.0 KiB
#6 Accepted 4ms 796.0 KiB
#7 Accepted 2ms 580.0 KiB
#8 Accepted 2ms 960.0 KiB
#9 Accepted 5ms 1.285 MiB
#10 Accepted 5ms 1.285 MiB
#11 Accepted 2ms 796.0 KiB
#12 Accepted 3ms 840.0 KiB
#13 Accepted 4ms 2.008 MiB
#14 Accepted 2ms 1.043 MiB

Code

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

#define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<int,int>
#define endl       '\n'

void pipra(int tc) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> a;
    int c = 0;
    for(int i = 0 ; i < n ; i++) {
        if(s[i] == '0') {
            if(c > 0)
                a.pb(c);
            c = 0;
        }
        else
            c++;
    }
    if(c)
        a.pb(c);
    sort(allr(a));
    int ans = 0;
    if(a.size())
        ans = a[0];
    for(int i = 1 ; i < a.size() and k-- ; i++)
        ans += a[i];
    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t = 1;
    cin >> t;
    for(int i = 1 ; i <= t ; i++)
        pipra(i);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 17:45:41
Judged At
2025-04-06 17:45:41
Judged By
Score
100
Total Time
7ms
Peak Memory
2.008 MiB