1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;

const int N = 8005;

int pref[N][4];

int calc_pref(int b, int e, int type) {
    return pref[e][type] - pref[b - 1][type];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, k, t;
    string s;
    cin >> n >> k >> t >> s;

    for(int i = 1; i <= n; i++) {
        int event = s[i - 1] - '0';
        for(int j = 1; j <= 3; j++)
            pref[i][j] = pref[i - 1][j] + (event == j);
    }

    // check if we can stay whole day home
    int skips_left = k - pref[n][1]; // how many calls can we skip if we skip all office meetings
    if(skips_left >= 0) {
        cout << n - max(0, pref[n][2] - skips_left) << "\n";
        return 0;
    }

    int max_time = -1;

    for(int i = t + 1; i <= n - t; i++) {
        for(int j = i; j <= n - t; j++) {
            // meetings skipped on the way to office
            int skipped = calc_pref(i - t, i - 1, 1) + calc_pref(i - t, i - 1, 2);
            // meetings skipped on the way home
            skipped += calc_pref(j + 1, j + t, 1) + calc_pref(j + 1, j + t, 2);
            // office meetings skipped when at home
            // and possible online meetings when at home
            int online = 0;
            if(i > 1) {
                skipped += calc_pref(1, i - t - 1, 1);
                online += calc_pref(1, i - t - 1, 2);
            }
            if(j < n) {
                skipped += calc_pref(j + t + 1, n, 1);
                online += calc_pref(j + t + 1, n, 2);
            }

            skips_left = k - skipped;
            if(skips_left >= 0)
                max_time = max(max_time, i - t - 1 + n - j - t - max(0, online - skips_left));
        }
    }

    cout << max_time << "\n";
}