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
#include <bits/stdc++.h>
#define OFFICE 0
#define REMOTE 1
#define IDLE 2
#define ALL 3

using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;

const int N = 8005;
int pref[3][N];

int cnt(int num, int a, int b){
    if(num == ALL) return b-a+1;
    return pref[num][b] - pref[num][a-1];
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n,k,t;
    cin >> n >> k >> t;
    for(int i=1; i<=n; ++i){
        char c;
        cin >> c;
        for(int j=0; j<3; ++j) pref[j][i] = pref[j][i-1];
        ++pref[c-'1'][i];
    }
    int max_spare_hours = -1;
    int cnt_remote,cnt_office,spare_hours,deficit,cnt_skipped_meetings,cnt_remote_home;
    // Nie idziemy do biura
    {
        cnt_remote = cnt(REMOTE,1,n);
        cnt_office = cnt(OFFICE,1,n);
        if(cnt_office <= k){ // Nie idziemy do biura wiec nie bedziemy na zadnym spotkaniu biurowym
            if(cnt_remote+cnt_office <= k){
                spare_hours = n;
                max_spare_hours = max(max_spare_hours, spare_hours);
            }else{
                deficit = cnt_remote + cnt_office - k;
                spare_hours = n - deficit;
                max_spare_hours = max(max_spare_hours, spare_hours);                
            }
        }
    }
    // Czas spędzony w biurze := [b1,b2]
    for(int b1=t+1; b1<=n-t; ++b1){
        for(int b2=b1; b2<=n-t; ++b2){
            cnt_skipped_meetings = cnt(OFFICE, 1, b1-1) + cnt(OFFICE, b2+1, n)
                                     + cnt(REMOTE, b1-t, b1-1) + cnt(REMOTE, b2+1, b2+t);
            cnt_remote_home = cnt(REMOTE, 1, b1-t-1) + cnt(REMOTE, b2+t+1, n);
            if(cnt_skipped_meetings <= k){
                if(cnt_skipped_meetings + cnt_remote_home <= k){ // Czy mozemy nie przychodzic na zadne zdalne spotkanie?
                    spare_hours = cnt(ALL, 1, b1-t-1) + cnt(ALL, b2+t+1, n);
                    max_spare_hours = max(max_spare_hours, spare_hours);
                }else{
                    deficit = cnt_skipped_meetings + cnt_remote_home - k;
                    spare_hours = cnt(ALL, 1, b1-t-1) + cnt(ALL, b2+t+1, n) - deficit;
                    max_spare_hours = max(max_spare_hours, spare_hours);
                }
            }
        }
    }
    cout << max_spare_hours;
}