/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 344.0 KiB
#2 Accepted 33ms 536.0 KiB
#3 Accepted 48ms 344.0 KiB
#4 Accepted 6ms 532.0 KiB
#5 Accepted 4ms 580.0 KiB
#6 Accepted 4ms 580.0 KiB
#7 Accepted 2ms 532.0 KiB
#8 Accepted 6ms 696.0 KiB
#9 Accepted 14ms 1.07 MiB
#10 Accepted 13ms 1.027 MiB
#11 Accepted 3ms 820.0 KiB
#12 Accepted 3ms 852.0 KiB
#13 Accepted 7ms 1.32 MiB
#14 Accepted 3ms 788.0 KiB

Code

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

typedef long long ll;
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define mp make_pair
#define pq priority_queue<int>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define f(i, n) for (int i = 0; i < n; i++)
#define PI acos(-1)
const int M = 1e9 + 7;
ll binExp(ll a, ll b)
{
    int res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a;
        a *= a;
        b /= 2;
    }
    return res;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}
void solve()
{
    int n, k;
    cin >> n >> k;
    vector<int> v;
    string s;
    cin >> s;
    int c = 0;
    for (auto x : s)
    {
        if (x == '1')
            c++;
        else
        {
            if (c)
            {
                v.pb(c);
                c = 0;
            }
        }
    }
    if (c)
        v.push_back(c);
    sort(all(v));
    int ans = 0;
    for (int i = 0; i < min((int)v.size(), k + 1); i++)
    {
        ans += v[v.size() - i - 1];
    }
    cout << ans << endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    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:39:25
Judged At
2025-02-17 14:39:25
Judged By
Score
100
Total Time
48ms
Peak Memory
1.32 MiB