/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 6ms 532.0 KiB
#3 Accepted 7ms 584.0 KiB
#4 Accepted 5ms 532.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Accepted 9ms 580.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 5ms 696.0 KiB
#9 Accepted 11ms 1.027 MiB
#10 Accepted 11ms 1.121 MiB
#11 Accepted 6ms 788.0 KiB
#12 Accepted 7ms 832.0 KiB
#13 Accepted 15ms 1.277 MiB
#14 Accepted 7ms 788.0 KiB

Code

#include <bits/stdc++.h>
#define nl '\n'
#define ll long long int
#define all(c) c.begin(),c.end()
#define print(c) for(auto e : c) cout << e << " "; cout << nl
using namespace std;
void solve()
{
    int n, k; cin >> n >> k;
    string s; cin >> s;

    vector<int> v;
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        if(s[i] == '1') cnt++;
        else 
        {
            if(cnt) v.push_back(cnt);
            cnt = 0;
        }
    }
    if(cnt) v.push_back(cnt);

    sort(all(v)); reverse(all(v));

    if(count(all(s), '1') == 0)
    {
        cout << 0 << nl; return;
    }

    int ans = v.front();
    for (int i = 1; i < v.size() && (k>0); i++)
    {
        ans += v[i]; k--;
    }
    cout << ans << nl;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    for(int tt = 1; tt <= t; tt++)
    {
        // cout << "TEST CASE-" << tt << nl;
        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 15:12:05
Judged At
2025-02-17 15:12:05
Judged By
Score
100
Total Time
15ms
Peak Memory
1.277 MiB