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
#include <bits/stdc++.h>
using namespace std;
int biur[6000], zdaln[6000], ans=-1;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N, K, T;
    cin >> N >> K >> T;
    vector<int> v(N+3);
    for (int i=1; i<=N; i++){
        char zn;
        cin >> zn;
        v[i]=zn-'0';
        biur[i]=biur[i-1];
        zdaln[i]=zdaln[i-1];
        if (v[i]==1)
            biur[i]++;
        if (v[i]==2)
            zdaln[i]++;


    }
    if (biur[N]<=K){
        int a=K-biur[N];
        zdaln[N]-=a;
        cout << min(N, N-zdaln[N]);
        return 0;
    }
    int ib=biur[N]-K;
    for (int i=1; i<N; i++){
        // zaczynasz od i
        for (int j=T+i+1; j<N; j++){
            // wracasz od j
            int B=biur[j-1]-biur[i+T-1];
            int Z=zdaln[j-1]-zdaln[i+T-1];
            // B-> tyle w biurze
            // Z -> tyle zdalnych w biurze
            int omB=biur[j+T-1]-biur[j-1];
            omB+=biur[i+T-1]-biur[i-1];

            int omZ=zdaln[j+T-1]-zdaln[j-1];
            omZ+=zdaln[i+T-1]-zdaln[i-1];
            //cout << i << " " << j << " " << ib << " " << B << " " << Z << endl;
            // jestes w burze B, a oni by chcieli ib
            if (omB+omZ>K || ib>B) continue;
            //cout << "aaa";
            int odj=2*T+(j-i-T); // czas poza domem
            int pozZ=K-omZ-omB;

            // mozesz skipnac jeszcze pozZ zdalnych
            int zda=zdaln[N]-(zdaln[j+T-1]-zdaln[i-1]); // liczba zda poza droga
            int bird=biur[N]-(biur[j+T-1]-biur[i-1]);
            pozZ-=bird;
            //cout << zda << " " << pozZ << endl;
            if (pozZ<0 || bird>K) continue;
            zda-=pozZ;
          //  cout << i << " " << j << " " << pozZ << endl;
            zda=max(zda, 0);
            int C=N-odj-zda;
            //cout << odj << endl;
            ans=max(ans, C);


        }
    }
    cout << ans;

    return 0;
}