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
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdio>
#include <cstdlib>

int c;

void SKIP_WHITESPACE() {
    while (1) {
        c = fgetc(stdin);
        if (c != ' ' && c != '\n' && c != '\r')
            break;
    }
}

int READ_INT() {
    SKIP_WHITESPACE();
    int ret = c - '0';
    while (1) {
        c = fgetc(stdin);
        if (c < '0' || c > '9')
            break;
        ret = ret * 10 + c - '0';
    }
    return ret;
}

int READ_DIGIT() {
    int ret = c-'0';
    c = fgetc(stdin);
    return ret;
}

#define MAXN 8000
int n, k, t, i, j, x, m;
int cumZ[MAXN+1], cumS[MAXN+1], cumSZ[MAXN+1];
int s, z, sz;
int best, w;

int result(int zi, int si, int szi) {
    int f = szi + si + k - m;
    if (f < 0)
        return -1;
    return f + zi;
}

int main(int argc, char* argv[]) {
    std::ios_base::sync_with_stdio (false);

    n = READ_INT();
    k = READ_INT();
    t = READ_INT();
    SKIP_WHITESPACE();

    s = 0;
    z = 0;
    sz = 0;
    m = 0;
    cumZ[0] = z;
    cumS[0] = s;
    cumSZ[0] = sz;
    for (i = 1; i <= n; ++i) {
        x = READ_DIGIT();
        if(x == 1) {
            z++;
            s++;
            m++;
        } else if (x == 2) {
            sz++;
            s++;
            m++;
        } else {
            z++;
        }
        cumZ[i] = z;
        cumS[i] = s;
        cumSZ[i] = sz;
    }
    if (k > m)
        k = m;

    best = result(z, 0, sz);

    for (i = 1; i <= n - 2 * t + 1; ++i) {
        for (j = i + t; j <= n - t + 1; ++j) {
            w = result(cumZ[i - 1] + z - cumZ[j + t - 1], cumS[j - 1] - cumS[i + t - 1], cumSZ[i - 1] + sz - cumSZ[j + t - 1]);
            if (w > best)
                best = w;
        }
    }

    std::cout << best << "\n";
    return EXIT_SUCCESS;
}