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
#include <iostream>
#include <string>
#include <algorithm>
const int MAX_N = 10000;
int type[MAX_N];
int spotkania_b[MAX_N+1];
int spotkania_z[MAX_N+1];
int spotkania_z_w_d[MAX_N+1];
int main()
{
	int n = 0;
	int k = 0;
	int t = 0;
	std::cin >> n >> k >> t;
	for (int i = 0; i < n; i++) {
		char c;
		std::cin >> c;
		if (c == '1') type[i] = 1;
		if (c == '2') type[i] = 2;
		if (c == '3') type[i] = 3;
	}

	spotkania_b[0] = 0;
	for (int i = 1; i <= n; i++) spotkania_b[i] = spotkania_b[i - 1] + (type[i-1] == 1);
	spotkania_z[0] = 0;
	for (int i = 1; i <= n; i++) spotkania_z[i] = spotkania_z[i - 1] + (type[i-1] == 2);
	int count_b = spotkania_b[n];
	int count_z = spotkania_z[n];

	if (count_b <= k) { // NIE TRZEBA JECHAĆ DO PRACY
		std::cout << n - std::max(0, count_b + count_z - k);
		return 0;
	}

	for (int i = 0; i <= n - t; i++) spotkania_z_w_d[i] = spotkania_z[i + t] - spotkania_z[i];
	int max_ans = -1;
	for (int i = 0; i <= n - 2*t; i++) { // SPRAWDZAM WSZYSTKIE MOŻLIWE CZASY ODJAZDU (Z DOMU I Z PRACY).
		for (int j = i + t + 1; j <= n - t; j++) {
			int pominiete_b = count_b - (spotkania_b[j] - spotkania_b[i + t]);
			int pominiete_z = spotkania_z_w_d[i] + spotkania_z_w_d[j];
			int zdalne_dom = count_z - (spotkania_z[j + t] - spotkania_z[i]);
			if (pominiete_b + pominiete_z <= k) {
				max_ans = std::max(max_ans, 
					               n - (j + t - i) - std::max(0, zdalne_dom - (k - pominiete_b - pominiete_z)));
				//std::cout << "new max " << max_ans << "\n";
			}
			//for (int k = 0; k < n; k++) std::cout << (((i <= k && k < i + t) || (j <= k && k < j + t)) ? "*" : " ");
			//std::cout << i << " " << j << "  " << "pominiete_b " << pominiete_b << " pominiete_z " << pominiete_z << " zdalne_dom " << zdalne_dom << "\n";
		}
	}
	std::cout << max_ans;
}