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
#include<bits/stdc++.h>
using namespace std;

vector < int > ans;
const int t = 8'003;
int praca[t];
int zdal[t];
int wolne[t];
int tab[t][4];

int oblicz(int i, int j, int typ){
    if(j < i) return 0;
    int x = tab[j][typ] - tab[i - 1][typ];
    // cout << typ << " " << i << " " << j << " ; " << x << "\n";
    return x;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k, t;
    cin >> n >> k >> t;
    string a;
    cin >> a;
    for(int i = 0; i < a.size(); i++){
        tab[i + 1][1] = tab[i][1] + (a[i] == '1');
        tab[i + 1][2] = tab[i][2] + (a[i] == '2');
        tab[i + 1][3] = tab[i][3] + (a[i] == '3');
    }
    int ma = -1;
    for(int j = 1; j <= 3; j++){
        tab[n + 1][j] = tab[n][j];
    }
    int ogl_p = oblicz(1, n, 1);
    if(ogl_p <= k){
        int cos = oblicz(1, n, 3) + oblicz(1, n, 1) + min(k - ogl_p, oblicz(1, n, 2));
        ma = max(cos, ma);

    }
    for(int i = t + 1; i <= n - t; i++){
        for(int j = i; j <= n - t; j++){
            int pj1 = i - t, kj1 = i - 1, pj2 = j + 1, kj2 = j + t;
            int ile_p = oblicz(pj1, kj1, 1) + oblicz(pj1, kj1, 2) + oblicz(pj2, kj2, 1) + oblicz(pj2, kj2, 2) + oblicz(1, pj1 - 1, 1) + oblicz(kj2 + 1, n, 1);
            if(ile_p <= k){
                int wyn = oblicz(1, pj1 - 1, 3) + oblicz(kj2 + 1, n, 3) + oblicz(1, pj1 - 1, 1) + oblicz(kj2 + 1, n, 1) + min(k - ile_p, oblicz(1, pj1 - 1, 2) + oblicz(kj2 + 1, n, 2));
                // cout << i << " " << j << " " << wyn << "\n";
                ma = max(ma, wyn);
            }
        }
    }
    cout << ma << "\n";
}