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

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

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

	vector <int> p1(n, 0), p2(n, 0); // p1 - ile w biurze na pref, p2 - ile zdalnie na pref

	if(s[0] == '1') ++p1[0];
	if(s[0] == '2') ++p2[0];

	for(int i = 1; i < n; ++i) {
		p1[i] = p1[i - 1], p2[i] = p2[i - 1];
		if(s[i] == '1') ++p1[i];
		if(s[i] == '2') ++p2[i];
	}

	int needed = max(p1[n - 1] + p2[n - 1] - k, 0); // ile trzeba odbyc spotkan

	if(p2[n - 1] >= needed) { // bez jechania do biura
		cout << n - needed << "\n";
		return 0;
	}

	int ans = -1;
	for(int l = t; l < n - t; ++l) for(int r = l; r < n - t; ++r) { // l, r - krance przedzialu czasu spedzonego w biurze
		int left = max(needed - (p1[r] + p2[r] - p1[l - 1] - p2[l - 1]), 0); // ile zostalo w domu
		if(left <= ((l - t) ? p2[l - t - 1] : 0) + (p2[n - 1] - p2[r + t])) {
			ans = max(ans, n - (r - l + 1) - 2 * t - left);
		}
	}

	cout << ans << "\n";
	return 0;
}