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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#define debug 0

#include <bits/stdc++.h>

#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define perf  1
#define st first
#define nd second
#define remin(a,b) a=min(a,b);
#define remax(a,b) a=max(a,b);
#define mod 998244353
//#define mod 1'000'000'007

using namespace std;
constexpr static int inf = 1e9;
vector<int> one, two, both;

inline int sm(const vector<int>& v, int l, int r) { //returns sum [l, r] i=l, i <= r
	if (l > r)
		return 0;
	return v[r+1]-v[l]; //zero-indexed
}

void solve() {
	int n, k, t;
	string s;
	cin >> n >> k >> t >> s;
	both.assign(n+1, 0);
	one.assign(n+1, 0);
	two.assign(n+1, 0);
	for (int i = 0; i < n; ++i) {
		if (s[i] == '1') {
			++one[i+1];
			++both[i+1];
		}
		if (s[i] == '2') {
			++two[i+1];
			++both[i+1];
		}
		one[i+1] += one[i];
		two[i+1] += two[i];
		both[i+1] += both[i];
	}

	int ans = - inf;
	if (one[n] <= k) {
		if (debug) cerr << "get out of jail free\n";
		remax(ans, n-max(0, both[n]-k)); //nie trzeba jechac do biura, zeby byc na spotkaniach
	}
	

	//jedziemy do biura w godzinach [l, l+t-1] wlacznie, oraz [r, r+t-1]
	
	for (int l = 0; l <= n - t; ++l) { //wyjazd w momencie l
		for (int r = l+t; r <= n - t; ++r) { //wyjazd do domu w momencie r, nie ma sensu r=l+t, bo wtedy nie mamy szansy pracowac xd
			int prac_biur = sm(both, l+t, r-1);
			int prac_dom = sm(two, 0, l-1) + sm(two, r+t, n-1);
			if (debug) {
				cerr << "l " << l << " r " << r << " prac biur " << prac_biur << " prac dom " << prac_dom << " ans " << ans << '\n';
			}
			//ans = -inf;
			if (both[n] - (prac_biur + prac_dom) <= k) {
				int dom = l-1+1 + (n-1 - (r+t) + 1);
				if (both[n] - prac_biur > k)
					dom -= (both[n] - prac_biur) - k;
				remax(ans, dom);
			}
		}
	}

	if (debug) {
		cerr << "the actual ans is: " << ans << '\n';
	}

	if (ans < 0)
		cout << -1 << '\n';
	else
		cout << ans << '\n';
}

int main() {
	fastio();
	int t=1;
	//cin >> t;

	while (t--) {
		solve();
	}
	return 0;
}