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
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
    int n, k, t;
    cin >> n >> k >> t;
    string s;
    cin >> s;

    // Initialize DP table
    // dp[i][j][l] where:
    // i is the segment index
    // j is the number of missed meetings
    // l is the location (0: home, 1: office, 2: on the way to office, 3: on the way to home)
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(k + 1, vector<int>(4, -1)));
    dp[0][0][0] = 0;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k; ++j) {
            for (int l = 0; l < 4; ++l) {
                if (dp[i][j][l] == -1) continue;

                int current_time = i + 1;
                int current_segment = s[i] - '0';

                // Stay at home
                if (l == 0) {
                    if (current_segment == 1) {
                        // Cannot attend office meeting from home
                        if (j < k) {
                            dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][0]);
                        }
                    } else if (current_segment == 2) {
                        // Can attend remote meeting
                        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]);
                    } else {
                        // Can solve problems
                        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0] + 1);
                    }
                }

                // Stay at office
                if (l == 1) {
                    if (current_segment == 1 || current_segment == 2) {
                        // Can attend any meeting
                        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]);
                    } else {
                        // Must work, cannot solve problems
                        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]);
                    }
                }

                // On the way to office
                if (l == 2) {
                    if (current_time + t - 1 <= n) {
                        dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][2]);
                    }
                }

                // On the way to home
                if (l == 3) {
                    if (current_time + t - 1 <= n) {
                        dp[i + 1][j][3] = max(dp[i + 1][j][3], dp[i][j][3]);
                    }
                }

                // Transition to office
                if (l == 0 && current_time + t - 1 <= n) {
                    dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][0]);
                }

                // Transition to home
                if (l == 1 && current_time + t - 1 <= n) {
                    dp[i + 1][j][3] = max(dp[i + 1][j][3], dp[i][j][1]);
                }

                // Arrive at office
                if (l == 2 && current_time == i + t) {
                    dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][2]);
                }

                // Arrive at home
                if (l == 3 && current_time == i + t) {
                    dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][3]);
                }
            }
        }
    }

    int result = -1;
    for (int j = 0; j <= k; ++j) {
        for (int l = 0; l < 4; ++l) {
            result = max(result, dp[n][j][l]);
        }
    }

    cout << result << endl;

    return 0;
}