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

void solve(){
    int n, k, t;
    cin >> n >> k >> t;
    string s;
    cin >> s;
    vector<int> a(n+1, 0), b(n+1, 0), c(n+1, 0);
    for(int i=1; i<=n; i++){
        a[i]+=a[i-1];
        b[i]+=b[i-1];
        c[i]+=c[i-1];
        if(s[i-1]=='1') a[i]++;
        if(s[i-1]=='2') b[i]++;
        if(s[i-1]=='3') c[i]++;
    }
    int spotkania = a[n]+b[n];
    int trzeba = spotkania - k;
    trzeba = max(trzeba, 0);
    int best = -1;
    if(trzeba <= b[n]){
        cout << n - trzeba<<"\n";
        return;
    }
    for(int l = t+1; l+t <= n; l++){
        for(int r = l; r+t<=n; r++){
            int biuro = a[r] + b[r] -a[l-1] - b[l-1];
            int zdalne = b[n] - b[r+t] + b[l-t-1];
            int wolne = c[n] - c[r+t] + c[l-t-1] + a[n] - a[r+t] + a[l-t-1];
            if(biuro + zdalne >= trzeba){
                int nadwyzka = min(zdalne, biuro + zdalne - trzeba);
                best = max(best, nadwyzka + wolne);
            }
            
        }
    }
    cout << best<<"\n";
}


int main(){
    ios_base::sync_with_stdio(0);
    solve();
}