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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <cstdio>
#include <cstdint>
#include <vector>
#include <bitset>
#include <cmath>
#include <iostream>

using namespace std;

int n,k,t;
char *activities;

int *officeMeetingsUpToNow;
int *meetingsUpToNow;
int *onlineMeetingsUpToNow;

int allMeetings;

bool isOffice(int i)
{
    return activities[i]=='1';
}
bool isOnline(int i)
{
    return activities[i]=='2';
}
bool isFree(int i)
{
    return activities[i]=='3';
}
bool isMeeting(int i)
{
    return activities[i]!='3';
}


int * initMeetingsCount(bool (*pred)(int))
{
    int *result = new int[n+2];
    result[0] = pred(0)?1:0;
    for(int i = 1; i< n; ++i)
    {
        result[i] = result[i-1] + (pred(i)?1:0);
    }
    return result;
}


int countActivity(bool (*pred)(int))
{
    int theSum = 0;
    for(int i = 0; i < n; ++i)
    {
        if(pred(i))
        {
            ++theSum;
        }
    }
    return theSum;
}

int calculateNoOffice()
{
    int officeMeetings = countActivity(isOffice);
    int onlineMeetings = countActivity(isOnline);
    if (officeMeetings > k)
    {
        return -1;
    }
    
    int meetings = onlineMeetings + officeMeetings;
    int attendedMeetings = max(0, meetings - k);
    int codingHours = n - attendedMeetings;

    return codingHours;
}

//returns -1 if impossible
int getCodingHours(int t1, int t2)
{
    //cout<<"Testing "<<t1<<" "<<t2<<endl;
    if(t1 < 0)
    {
        return -1;
    }
    if(t2 > n-t || t2 < t1 + t)
    {
        //cout<<"Impossible configuration"<<endl;
        return -1;
    }

    int homeMeetings = 0;
    if (t1 > 0) //before office
    {
        homeMeetings += onlineMeetingsUpToNow[t1-1];
    }
    //after office
    homeMeetings += onlineMeetingsUpToNow[n-1]; // TODO: check if it's correct
    homeMeetings -= onlineMeetingsUpToNow[t2+t-1];

    //cout<<"Home meetings "<<homeMeetings<<endl;

    //possibleMeetings
    //in office
    int officeMeetings = meetingsUpToNow[t2-1];
    officeMeetings -= meetingsUpToNow[t1+t-1];

    //cout<<"OfficeMeetings "<<officeMeetings<<endl;

    int possibleMeetings = homeMeetings + officeMeetings;
    
    int requiredMeetings = max(0, allMeetings - k);
    //cout<<"Required "<<requiredMeetings<<endl;
    if(possibleMeetings < requiredMeetings) // ten scenariusz nie działa :(
    {
        return -1;
    }

    requiredMeetings = max(0, requiredMeetings-officeMeetings);
    //cout<<"XXX Required "<<requiredMeetings<<endl;
    //cout<<"Home meetings "<<homeMeetings<<endl;

    if(homeMeetings < requiredMeetings)
    {
        //cout<<"FAILEEEEEEEEEEEEEEEEEEEEEEEEEEEED"<<homeMeetings<<endl;
        return -1;
    }

    int travelAndOffice = t2-t1+t;
    //cout<<"TRAVEL AND OFFICE: "<<travelAndOffice<<endl;

    int codingHours = n - travelAndOffice - requiredMeetings;

    return codingHours;
}


void init()
{
    scanf("%d %d %d", &n, &k, &t);
    activities = new char[n + 2];
    scanf("%s", activities);

    officeMeetingsUpToNow = initMeetingsCount(isOffice);
    meetingsUpToNow = initMeetingsCount(isMeeting);
    onlineMeetingsUpToNow = initMeetingsCount(isOnline);
    allMeetings = countActivity(isMeeting);
}

int main ()
{
    init();


    bool foundInOfficeResult = false;
    int bestToOfficeIndex = -1;
    int bestFromOfficeIndex = n+1;
    int bestCodingHours = calculateNoOffice();
    for (int t1 = 0; t1 <= n-2*t; ++t1)
    {
        for(int t2 = t1+t+1; t2<=n-t;++t2)
        {
            int codingHours = getCodingHours(t1, t2);
            //cout<<"FOUND:    "<<codingHours<<endl;
            if(codingHours > bestCodingHours)
            {
                bestCodingHours = codingHours;
            }
        }
    }

    printf("%d\n", bestCodingHours);
}