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 <iostream>

using namespace std;

int n, k, t;
int pref_2[8003], pref_3[8003];
int suf_2[8003], suf_3[8003];
int count_2 = 0, count_3 = 0;
int best = -1;
int main() {
    
    cin >> n >> k >> t;
    string seq;
    cin >> seq;
    for (int i = 1; i <= n; i++) {
        int idx = i-1;
        pref_2[i] = pref_2[i-1];
        pref_3[i] = pref_3[i-1];
        if (seq[idx] == '2') {
            pref_2[i]++;
            count_2++;
        }
        if (seq[idx] == '1') {
            pref_3[i]++;
            count_3++;
        }
    }
    for (int i = n; i >= 1; i--) {
        int idx = i-1;
        suf_2[i] = suf_2[i+1];
        suf_3[i] = suf_3[i+1];
        if (seq[idx] == '2') {
            suf_2[i]++;
        }
        if (seq[idx] == '1') {
            suf_3[i]++;
        }
    }

    if (count_3 <= k) {
        // zostaje na chacie
        best = min(n, n - (count_2 + count_3 - k));
    }
    for (int i = t; i <= n; i++) {
        for (int j = i + 1; j <= n - t + 1; j++) {
            // skonczylem isc do biura w momencie i, zaczalem wracac w j
            int stracone_calle = pref_3[i] + (pref_2[i] - pref_2[i-t]) + suf_3[j] + (suf_2[j] - suf_2[j+t]);
            if (stracone_calle > k) {
                continue;
            }
            int ile_odpuszczam = k - stracone_calle;
            int potyczki = (i - t) + (n - (j + t - 1)) + min(0, - pref_2[i-t] - suf_2[j+t] + ile_odpuszczam);
            if (potyczki > best) {
                best = potyczki;
            }
        }
    }
    cout << best << endl;
    return 0;
}