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
62
63
64
65
66
67
68
#include <bits/stdc++.h>

using namespace std;

const int N = 8004;

int biuro[N];
int zdalne[N];

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

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

    biuro[0] = 0;
    zdalne[0] = 0;
    for(int i = 1; i <= n; i++) {
        char c;
        cin >> c;
        if(c == '1') {
            biuro[i] = biuro[i - 1] + 1;
        }
        else {
            biuro[i] = biuro[i - 1];
        }

        if(c == '2') {
            zdalne[i] = zdalne[i - 1] + 1;
        }
        else {
            zdalne[i] = zdalne[i - 1];
        }
    }

    if(biuro[n] <= k) {
        int potyczki = n - zdalne[n];
        potyczki += min(k - biuro[n], zdalne[n]);
        cout << potyczki;
        return 0;
    }       

    if(n <= 2 * t) {
        cout << -1;
        return 0;
    }

    int best = -1;
    for(int i = t+1; i <= n-t; i++) {
        for(int j = i; j <= n-t; j++) {
            int opuszczone_biuro = biuro[i-1] + (biuro[n] - biuro[j]);
            int opuszczone_zdalne = (zdalne[i-1] - zdalne[i-t-1]) + (zdalne[j+t] - zdalne[j]);
            int suma = opuszczone_biuro + opuszczone_zdalne;
            if(suma > k) continue;

            int zdalne_dom = zdalne[i-t-1] + (zdalne[n] - zdalne[j+t]);
            int obecne_potyczki = (i-t-1 + n-j-t) - zdalne_dom;
            obecne_potyczki += min(k - suma, zdalne_dom);
            best = max(best, obecne_potyczki);
        }
    }
    
    cout << best;


    return 0;
}