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

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); 
 
    // daje 1 a powinno 0
    //istringstream input("7 4 2\n1113211");    
    
    // daje 0 a powinno -1
    //istringstream input("7 3 2\n1131122"); 
    
    // daje 1 a powinno -1
    //istringstream input("7 3 2\n1133112"); 
    
    //cin.rdbuf(input.rdbuf());

    short n, k, t;
    cin >> n >> k >> t;    

    short wbiurze[n + 1] = {};
    short zdalne[n + 1] = {};

//for (short i = 0; i < n; i++) cout << i << ' ';
//cout << '\n';

    for (short i = 0; i < n; i++) {
		char c;
		cin >> c;
//cout << c << ' ';
		if(c == '1' ) wbiurze[i + 1] = wbiurze[i] +1;
		else wbiurze[i + 1] = wbiurze[i];
		
		if(c == '2' ) zdalne[i + 1] = zdalne[i] +1;
		else zdalne[i + 1] = zdalne[i];
    }
//cout << '\n';
//for (short i = 0; i <= n; i++) cout << wbiurze[i] << ' ';
//cout << '\n';
//for (short i = 0; i <= n; i++) cout << zdalne[i] << ' ';
//cout << '\n';
//cout << '\n';


    short obowiazkowe = max(0, wbiurze[n] + zdalne[n] - k);

	if (obowiazkowe == 0) {
		// nie trzeba uczestniczyc w spotkaniach
        cout << n << "\n";
        return 0;
    }

	if (wbiurze[n] <= k) {
		// nie trzeba jechać do biura
        cout << (n - obowiazkowe) << "\n";
        return 0;
    }    

    short max_potyczki = -1;

    for (short start = t; start + t < n; start++) {
        for (short end = start; end + t < n; end++) {
            short biurowe = wbiurze[end + 1] - wbiurze[start] + zdalne[end + 1] - zdalne[start];
            short domowe = (start >= t ? zdalne[start - t] : 0) + (zdalne[n] - zdalne[end + t+1]);

//cout << start << '-' << end << '\t' << wbiurze[end + 1] - wbiurze[start] << ' ' << zdalne[end + 1] - zdalne[start] << ' '  << domowe << '\n';
//cout << start << '-' << end << '\t' << zdalne[start - t] << ' ' << zdalne[n] << ' '  << zdalne[end + t] << '\n';

            if (biurowe + domowe >= obowiazkowe) {
                short potyczki = (start - t) + (n - 1 - (end + t)) - max(0, obowiazkowe - biurowe);
                max_potyczki = max(max_potyczki, potyczki);
            }
        }
    }

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