/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 17ms 540.0 KiB
#3 Accepted 20ms 540.0 KiB
#4 Accepted 9ms 540.0 KiB
#5 Accepted 7ms 540.0 KiB
#6 Accepted 7ms 540.0 KiB
#7 Accepted 5ms 796.0 KiB
#8 Accepted 5ms 800.0 KiB
#9 Accepted 8ms 1.031 MiB
#10 Accepted 8ms 1.039 MiB
#11 Accepted 5ms 796.0 KiB
#12 Accepted 5ms 952.0 KiB
#13 Accepted 7ms 1.281 MiB
#14 Accepted 5ms 796.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int a,b;
        cin>>a>>b;
        int cnt1=0;
        vector<int> v;
        bool bin= false;
        string ss;
        cin>>ss;
        for(int i=0; i<a; i++){
            if(ss[i]- '0' == 1 && !bin){
                bin=true;
                cnt1++;
            }
            else if(ss[i]- '0' == 1 && bin){
                cnt1++;
               
            }
            else if(ss[i]- '0' == 0 && bin){
                v.push_back(cnt1);
                bin=false;
                cnt1=0;
            }
        }
        if(ss[a-1]-'0'==1) {
            v.push_back(cnt1);
        }
         sort(v.begin(), v.end(), greater<int>());
         
         int c= v.size();
         if(b>=c){
             int y=0;
         for(int x: v){
             y+=x;
         }
         cout<< y<< '\n';
         }
         else{
             int d=v[0];
              for(int i=1; i<=b; i++){
                  d+=v[i];
              }
              cout<< d <<'\n';
         }
    }
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 17:37:28
Judged At
2025-02-17 17:37:28
Judged By
Score
100
Total Time
20ms
Peak Memory
1.281 MiB