/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 772.0 KiB
#2 Accepted 9ms 580.0 KiB
#3 Accepted 12ms 544.0 KiB
#4 Accepted 7ms 560.0 KiB
#5 Accepted 6ms 576.0 KiB
#6 Accepted 7ms 668.0 KiB
#7 Accepted 3ms 580.0 KiB
#8 Accepted 3ms 692.0 KiB
#9 Accepted 5ms 1.047 MiB
#10 Accepted 8ms 1.113 MiB
#11 Accepted 3ms 796.0 KiB
#12 Accepted 2ms 1.0 MiB
#13 Accepted 3ms 1.551 MiB
#14 Accepted 2ms 844.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
void solve()
{
  int n , k , cnt = 0 , Ans = 0; string s ;  cin >> n >> k >> s ;
  priority_queue <int> pq ;
  for(int i = 0 ; i < n ; i++){
    if(s[i] == '0'){
        if(cnt){
            pq.push(cnt) ;
            cnt = 0 ;
        }
    }
    else cnt++ ;
  }
  if(cnt) pq.push(cnt) ;
  if(!pq.empty()){
    Ans = pq.top() ;
    pq.pop() ;
  }
  while(!pq.empty() && k){
    Ans += pq.top() ;
    pq.pop() ;
    k-- ;
  }
  cout << Ans << "\n" ;

}
int main()
{
     __Heart__
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-13 23:59:00
Judged At
2025-01-13 23:59:00
Judged By
Score
100
Total Time
12ms
Peak Memory
1.551 MiB