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
#include <bits/stdc++.h>
using namespace std;
int n, k, t, res = -1;
string s;
int prez[8005];
int preb[8005];
int prew[8005];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k >> t >> s;
    s = "#" + s;
    for (int i = 1; i <= n; ++i)
    {
        prez[i] = prez[i-1];
        preb[i] = preb[i-1];
        prew[i] = prew[i-1];
        if (s[i] == '1') ++preb[i];
        if (s[i] == '2') ++prez[i];
        if (s[i] == '3') ++prew[i];
    }
    if (preb[n] <= k)
    {
        k -= preb[n];
        cout << n - max(0, prez[n] - k);
        return 0;
    }
    for (int i = t+1; i+2*t <= n; ++i)
    {
        for (int j = i; j+t <= n; ++j)
        {
            int lft = k;
            lft -= prez[i-1] + preb[i-1] - prez[i-t-1] - preb[i-t-1];
            lft -= prez[j+t] + preb[j+t] - prez[j] - preb[j];
            int czas_w_domu = n - (j+t) + (i-t-1);
            int il_zdalnych_dom = prez[n] - prez[j+t] + prez[i-t-1];
            int il_biurowych_dom = preb[n] - preb[j+t] + preb[i-t-1];
            lft -= il_biurowych_dom;
            if (lft < 0) continue;
            res = max(res, czas_w_domu - max(0, il_zdalnych_dom - lft));
        }
    }
    cout << res;
}