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
61
#include <bits/stdc++.h>

using namespace std;

const int Base = 8 * 1e3 + 7;

int PrefBiuro[Base];
int PrefZdalne[Base];
int Spotkania;

int n, k, t;
int Ans = -1;

int WczytajInta()
{
    char c;
    int wyn = 0;
    while(1) {
        c = getchar();
        if(c == ' ' or c == '\n' or c == EOF) break;
        wyn *= 10;
        wyn += int(c - '0');
    }
    return wyn;
}

int main()
{
    n = WczytajInta();
    k = WczytajInta();
    t = WczytajInta();
    char c;
    int x, y;
    for(int i = 1; i <= n; i++) {
        x = y = 0;
        c = getchar();
        if(c == '1') {
            x++;
        } else if(c == '2') {
            y++;
        }
        PrefBiuro[i] = PrefBiuro[i - 1] + x;
        PrefZdalne[i] = PrefZdalne[i - 1] + y;
    }
    Spotkania = PrefBiuro[n] + PrefZdalne[n];
    k = min(k, Spotkania);
    if(PrefBiuro[n] <= k) {
        Ans = n - (max(Spotkania - k, 0));
    }
    int WBiurze = 0, PozaBiurem = 0;
    for(int i = t + 1; i <= n - t; i++) {
        for(int j = i; j <= n - t; j++) {
            WBiurze = (PrefBiuro[j] - PrefBiuro[i - 1]) + (PrefZdalne[j] - PrefZdalne[i - 1]);
            PozaBiurem = PrefZdalne[i - t - 1] + (PrefZdalne[n] - PrefZdalne[j + t]);
            if(Spotkania - k > WBiurze + PozaBiurem) continue;
            Ans = max(Ans, ((i - t - 1) + (n - (j + t))) - max(Spotkania - WBiurze - k, 0));
            // cerr << i << " - " << j << " :  " << (((i - t - 1) + (n - (j + t))) - max(Spotkania - WBiurze - k, 0)) << "\n";
        }
    }
    printf("%d", Ans);
}