Problem p02918 (python)

Fixed submission:
N, K = map(int, input().split())
S = input()

# find the largest subsequence
packet = []
packet_len = 1
direction = S[0]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction = S[i]
        packet_len += 1

if S[N - 1] == direction:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction = S[N - 1]
    packet.append(seq)
    packet_len += 1

#print(packet)
#print(packet_len)

# There is no need to choose the 'most invertible' index (i.e. max_idx) !!!
# Also, there is no need to rewrite the packet

num = 2 * K + 1
if packet_len == 1 or packet_len == 2 or num >= packet_len:
    print(N - 1)
elif num < packet_len:
    rem = packet_len - num
    print(N - rem - 1)

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

# find the largest subsequence
packet = []
packet_len = 0
direction = S[0]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction = S[i]
        packet_len += 1

if S[N - 1] == direction:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction = S[N - 1]
    packet.append(seq)
    packet_len += 1

#print(packet)


for _ in range(K):
    if packet_len == 1 or packet_len == 2:
        print(sum(packet) - 1)
        exit()

# There is no need to choose the 'most invertible' index (i.e. max_idx) !!!

    else:
        val = packet[-3] + packet[-2] + packet[-1]
        packet = packet[:-3] + [val]
        packet_len -= 2

    #print(packet)

ans = 0
for v in packet:
    ans += v - 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

N, K = map(int, input().split())
S = input()

# find the largest subsequence
packet = []
direction = [S[0]]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction[-1]:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction.append(S[i])
if S[N - 1] == direction[-1]:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction.append(S[N - 1])
    packet.append(seq)

#print(packet)


start_color = S[0]
packet_len = len(packet)

max_sum = packet[0] + packet[1]
max_idx = 0
for i in range(1, packet_len - 1):
    if max_sum < packet[i - 1] + packet[i] + packet[i + 1]:
        max_sum = packet[i - 1] + packet[i] + packet[i + 1]
        max_idx = i
if max_sum < packet[packet_len - 2] + packet[packet_len - 1]:
    max_idx = packet_len - 1


for _ in range(K):
    if packet_len == 1:
        print(packet_len - 1)
        exit()
    #print(max_idx)

    if max_idx == 0:
        val = packet[0] + packet[1]
        packet = [val] + packet[2:]
        packet_len -= 1

    elif max_idx == packet_len - 1:
        val = packet[-1] + packet[-2]
        packet = packet[:packet_len - 2] + [val]
        packet_len -= 1
        max_idx = packet_len - 2
    else:
        val = packet[max_idx - 1] + packet[max_idx] + packet[max_idx + 1]
        packet = packet[:max_idx - 1] + [val] + packet[max_idx + 2:]
        packet_len -= 2
        max_idx = max_idx - 1

    #print(packet)

ans = 0
for v in packet:
    ans += v - 1
print(ans)