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
buf = input()
n = int(buf.split()[0])
k = int(buf.split()[1])
t = int(buf.split()[2])
timetable = input()

pre_work = []
for i in range(n):
    if timetable[i] == "1":
        pre_work = list(timetable[:i])
        break

after_work = []
for i in range(n-1, -1, -1):
    if timetable[i] == "1" and i < n:
        after_work = list(timetable[i+1:])
        break

# print(pre_work)
# print(after_work)

work = list(timetable[len(pre_work):len(timetable)-len(after_work)])
# print(work)

skipped_meeting = False
for i in range(k):
    skipped_meeting = False
    if len(pre_work) < len(after_work):
        for j in range(len(work)):
            if work[j] != "3":
                work[j] = "3"
                skipped_meeting = True
                break
    else:
        for j in range(len(work)-1, -1, -1):
            if work[j] != "3":
                work[j] = "3"
                skipped_meeting = True
                break
    if skipped_meeting:
        # print(i, "work")
        continue
    if len(pre_work) < len(after_work):
        for j in range(len(pre_work)-1, -1, -1):
            if pre_work[j] == "2":
                pre_work[j] = "3"
                skipped_meeting = True
                break
        if skipped_meeting:
            # print(i, "pre_work")
            continue
    for j in range(len(after_work)):
        if after_work[j] == "2":
            after_work[j] = "3"
            skipped_meeting = True
            break
    if skipped_meeting:
        # print(i, "after_work")
        continue

new_timetable = "".join(pre_work) + "".join(work) + "".join(after_work)

for i in range(n):
    if new_timetable[i] == "1":
        pre_work = list(new_timetable[:i])
        break

for i in range(n-1, -1, -1):
    if new_timetable[i] == "1" and i < n-1:
        after_work = list(new_timetable[i+1:])
        break

work = list(new_timetable[len(pre_work):len(new_timetable)-len(after_work)])

invalid_input = False
if len(pre_work) < t or len(after_work) < t:
    invalid_input = True
    # print("pre_work or after_work is too small")

enough_time_to_drive = False
pre_work_index = -1
for i in range(len(pre_work), -1, -1):
    if pre_work[i-t:i] == list("3" * t):
        enough_time_to_drive = True
        pre_work_index = i-t
        break
if not enough_time_to_drive:
    invalid_input = True
    # print("Not enough time to return home/go to work")

enough_time_to_drive = False
after_work_index = -1
for i in range(len(after_work)):
    if after_work[i:i+t] == list("3" * t):
        enough_time_to_drive = True
        after_work_index = i
        break
if not enough_time_to_drive:
    invalid_input = True
    # print("Not enough time to return home/go to work")

# print(pre_work_index)
# print(after_work_index)

# print(pre_work)
# print(after_work)
# print(work)

output = 0

for i in range(pre_work_index):
    if pre_work[i] == "3":
        output += 1

for i in range(after_work_index+t, len(after_work)):
    if after_work[i] == "3":
        output += 1

has_to_go_to_work = False
for i in range(len(work)):
    if work[i] == "1":
        has_to_go_to_work = True
        break
if not has_to_go_to_work:
    output += 2 * t
    for i in range(len(work)):
        if work[i] == "3":
            output += 1
# print(has_to_go_to_work)

if invalid_input:
    output = -1

print(output)