/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Accepted 7ms 576.0 KiB
#3 Accepted 8ms 532.0 KiB
#4 Accepted 7ms 560.0 KiB
#5 Accepted 10ms 532.0 KiB
#6 Accepted 16ms 696.0 KiB
#7 Accepted 6ms 532.0 KiB
#8 Accepted 7ms 696.0 KiB
#9 Accepted 19ms 1.277 MiB
#10 Accepted 17ms 1.277 MiB
#11 Accepted 4ms 788.0 KiB
#12 Accepted 9ms 1.773 MiB
#13 Accepted 6ms 1.277 MiB
#14 Accepted 4ms 788.0 KiB

Code

#include <bits/stdc++.h>

#define nl              cout<<'\n';
#define space           cout<<' ';
#define print(s)        cout<<s;

#define ll              long long
#define all(x)          (x).begin(), (x).end()
#define Point(x,y)      setprecision((y))<<fixed<<(x)

#define yes             cout<<"YES\n";
#define no              cout<<"NO\n";

#define Fast            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define dbg(x)          cout<<"line " << __LINE__ << " says: " << #x << " = " << x << "\n";

#define printarry(a)    for(auto it : a) cout<<it<<' ';
#define put_array(x,n)  for (int i = 0; i < n; ++i) cin >> x[i];




using namespace std;





//-----------------------------------------------------------------------------------------------------------------------------------
//                                                  PRACTICE AND LEARN                                                                                         
//-----------------------------------------------------------------------------------------------------------------------------------


void test_case()
{
       int n,k;
       cin >>  n >> k;

       string s; 
       cin >> s;

        std::vector<int> v;

        int cnt = 0;

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

       sort(v.rbegin(),v.rend());


       long long ans = 0 ;

       int sz = v.size();

       for (int i = 0; i < min(k+1,sz); ++i)
       {
            ans += v[i];
           
       }

       cout << ans ;



      

    



    nl
    return;

}




int32_t main()
{
    Fast;

#ifdef NAYEEM

    int start_time = clock();
    int end_time = clock();
    printf("Time = %.4f\n", (end_time - start_time + 0.0) / CLOCKS_PER_SEC);
    
#endif

    int test_number = 1;
    cin >> test_number;

    while (test_number--)
    {
      
        test_case();

    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 17:33:25
Judged At
2025-02-17 17:33:25
Judged By
Score
100
Total Time
19ms
Peak Memory
1.773 MiB