/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 6ms 576.0 KiB
#3 Accepted 10ms 576.0 KiB
#4 Accepted 6ms 532.0 KiB
#5 Accepted 6ms 532.0 KiB
#6 Accepted 8ms 700.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 4ms 740.0 KiB
#9 Accepted 9ms 1.277 MiB
#10 Accepted 12ms 1.277 MiB
#11 Accepted 5ms 788.0 KiB
#12 Accepted 5ms 788.0 KiB
#13 Accepted 12ms 1.73 MiB
#14 Accepted 7ms 788.0 KiB

Code

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

#define all(a) a.begin(),a.end()
#define el "\n"
#define int long long

// Optimized prime check
bool isprime(int n){
    if(n < 2) return false;
    if(n == 2 || n == 3) return true;
    if(n % 2 == 0 || n % 3 == 0) return false;
    for(int i = 5; i * i <= n; i += 6) {
        if(n % i == 0 || n % (i + 2) == 0) return false;
    }
    return true;
}
void chori_ka_laptop(){
  int n; cin>>n;
   int k; cin>>k;
  string s; cin>>s;
  int cur=0;
  int len=0;
  char prev='a';
  vector<int>v;
  for(int i=0;i<n;i++){
    if(s[i]!=prev && s[i]=='0'){
        v.push_back(len);
        prev=s[i];
        len=0; 
    } 
    if(s[i]=='1') len++;
    prev=s[i];
  }
  v.push_back(len);
  sort(all(v));
  int ans=0;
  k++;
  while(k--){
    if(v.empty()) break;
    ans+=v.back();
    v.pop_back();
  }
  cout<<ans<<el;
}


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

// #ifndef ONLINE_JUDGE
//     freopen("input1.txt", "r", stdin);
//     freopen("D://sublime text//output6.txt", "w", stdout);
// #endif 

    int t = 1;  
    cin >> t;
    while(t--) {
        chori_ka_laptop();
    }

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