/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 7ms 532.0 KiB
#3 Accepted 8ms 532.0 KiB
#4 Accepted 5ms 324.0 KiB
#5 Accepted 4ms 532.0 KiB
#6 Accepted 5ms 808.0 KiB
#7 Accepted 2ms 576.0 KiB
#8 Accepted 4ms 696.0 KiB
#9 Accepted 18ms 1.777 MiB
#10 Accepted 18ms 1.773 MiB
#11 Accepted 6ms 788.0 KiB
#12 Accepted 20ms 2.809 MiB
#13 Accepted 13ms 1.773 MiB
#14 Accepted 7ms 788.0 KiB

Code

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

#define all(x) (x).begin(), (x).end()
#define repeat(x) for(int taramtam = 0;taramtam<(x);taramtam++)

template<typename S, typename T> void semax(S &a, const T &b) {if (a<b) a = b;}
template<typename S, typename T> void semin(S &a, const T &b) {if (a>b) a = b;}

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H;dbg_out(T...);}
#define debug(...) cerr << "(" #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

const int INF = 1e9+100;
const int MOD = 1e9+7;

#define int long long
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename S>
void displayvec(vector<S> a){
    for (S thing:a){
        cout << thing << ' ';
    }
    cout << '\n';
}
template <typename S> 
void readvec(vector<S>& a){
    for (S& thing:a){
        cin>>thing;
    }
}

void solve(){
    int n, k;cin>>n>>k;
    string s;cin>>s;
    vector<int> rv;
    int cl = 0;
    for(auto thing:s){
        if (thing=='1'){
            cl++;
        }
        else{
            rv.push_back(cl);
            cl=0;
        }
    }
    rv.push_back(cl);
    sort(all(rv));
    reverse(all(rv));
    int res = 0;
    for(int i = 0;i<min((ll)rv.size(), k+1);i++){
        res+=rv[i];
    }
    cout << res << '\n';
}







signed main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    //freopen("compin.txt", "r", stdin);
	//freopen("compout.txt", "w", stdout);
    int TestCase = 1;
    cin >> TestCase;
    while(TestCase--) solve();
}

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:40:19
Judged At
2025-02-17 14:40:19
Judged By
Score
100
Total Time
20ms
Peak Memory
2.809 MiB