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

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, k, t;
    std::cin >> n >> k >> t;
    
    std::string s;
    std::cin >> s;
    
    std::vector pre(3, std::vector<int>(n + 1));
    for (int x = 0; x < 3; x++) {
        for (int i = 0; i < n; i++) {
            pre[x][i + 1] = pre[x][i] + (s[i] == '1' + x);
        }
    }
    
    int ans = -1;
    
    if (pre[0][n] <= k) {
        ans = std::max(ans, pre[0][n] + pre[2][n] + std::min(pre[1][n], k - pre[0][n]));
    }
    
    for (int l = t; l <= n - t; l++) {
        for (int r = l; r <= n - t; r++) {
            int miss = pre[0][l] + pre[0][n] - pre[0][r] + pre[1][l] - pre[1][l - t] + pre[1][r + t] - pre[1][r];
            if (miss <= k) {
                int res = pre[2][l - t] + pre[2][n] - pre[2][r + t] + pre[0][l - t] + pre[0][n] - pre[0][r + t];
                int can = pre[1][l - t] + pre[1][n] - pre[1][r + t];
                res += std::min(can, k - miss);
                ans = std::max(ans, res);
            }
        }
    }
    
    std::cout << ans << "\n";
    
    return 0;
}