Problem p03074 (python)

Fixed submission:
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    if en_ix >= len(count_S):
        new_count.append(sum(count_S[st_ix:]))
    else:
        cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
        cur_sum += (count_S[en_ix-2]
                        + count_S[en_ix-1])
    new_count.append(cur_sum)

print(max(new_count))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

S += "!"
cur_char = S[0]
cur_char_count = 1
smart_S = ""
smart_count = []
for c in S[1:]:
    if cur_char == c:
        cur_char_count += 1
    else:
        smart_S += cur_char
        smart_count.append(cur_char_count)
        cur_char = c
        cur_char_count = 1

l = 0
r = 0
cnt_0 = 0
cur_sum = 0

ans_cand = []

# print("smart_S: ", smart_S)

while r == 0 or l < r:
    # print("l: {}, r: {}".format(l, r))
    while cnt_0 < K and r < len(smart_S) - 1:
        cur_sum += smart_count[r]
        if smart_S[r] == '0':
            cnt_0 += 1
        r += 1
        
    # proceed one more 
    if r < len(smart_S) and smart_S[r] == '1':
        cur_sum += smart_count[r]
        cnt_0 += 1
        r += 1

    ans_cand.append(cur_sum)

    if l < len(smart_S) and smart_S[l] == '0':
        cur_sum -= smart_count[l]
        cnt_0 -= 1
        l += 1

    else:
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1


#print("smart_count: ", smart_count)
#print("ans_cand: ", ans_cand)
print(max(ans_cond))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[min(en_ix-2, len(count_S)-1]
                        + count_S[min(en_ix-1, len(count_S)-1])
    new_count.append(cur_sum)

print(max(new_count))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[max(en_ix-2, len(count_S)-1]
                        + count_S[max(en_ix-1, len(count_S)-1])
    new_count.append(cur_sum)

print(max(new_count))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[max(en_ix-2, len(count_S)]
                        + count_S[max(en_ix-1, len(count_S)])
    new_count.append(cur_sum)

print(max(new_count))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)
        

new_count = []
st_ix = 0
maxlen_ix = len(count_S)-1
if S[0] == '0':
    cur_sum = sum(count_S[:min(2*K, maxlen_ix)])
    st_ix = 1
    new_count.append(cur_sum)

else:
    cur_sum = sum(count_S[:max(2*K+1, maxlen_ix)])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:max(en_ix, maxlen_ix)])
new_count.append(cur_sum)
while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[en_ix-2] + count_S[en_ix-1])
    new_count.append(cur_sum)

print(max(new_count))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)
        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)

else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)
while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[en_ix-2] + count_S[en_ix-1])
    new_count.append(cur_sum)

print(max(new_count))