Problem p03069 (python)

Fixed submission:
def main():
    N = int(input())
    S = input()
    # ans_w, ans_b = 0, 0

    # started = False
    # for s in S:
    #     if s == '#':
    #         started = True
    #     if started and s == '.':
    #         ans_w += 1
    # started = False
    # for i in range(N):
    #     s = S[N - 1 - i]
    #     if s == '.':
    #         started = True
    #     if started and s == '#':
    #         ans_b += 1
    # print(min(ans_w, ans_b))
    Ws, Bs = [], []
    c = S[0]
    l = 1
    for i in range(1, N):
        s = S[i]
        if c == s:
            l += 1
        else:
            if c == '.':
                Ws.append(l)
            else:
                Bs.append(l)
            l = 1
            c = s
    if c == '.':
        Ws.append(l)
    else:
        Bs.append(l)
    # print(Ws, Bs)
    len_ws = len(Ws)
    len_bs = len(Bs)
    if len_ws == 0 or len_bs == 0:
        print(0)
        return
    if len_ws == 1:
        if len_bs == 1:
            if S[0] == '.':
                print(0)
            else:
                print(min(Bs[0], Ws[0]))
        else:
            # len_bs == 2
            print(min(Ws[0], Bs[0]))
        return
    if len_bs == 1:
        # len_ws == 2
        print(min(Ws[1], Bs[1]))
        return

    # len_ws >= 2 and len_bs >= 2
    sum_w_s, sum_w_r, sum_b_s, sum_b_r = [0]*len_ws, [0]*len_ws,  [0]*len_bs, [0]*len_bs
    sum_w_s[0] = Ws[0]
    sum_w_r[0] = Ws[-1]
    sum_b_s[0] = Bs[0]
    sum_b_r[0] = Bs[-1]
    for i in range(1, len_ws):
        sum_w_s[i] = sum_w_s[i - 1] + Ws[i]
        sum_w_r[i] = sum_w_r[i - 1] + Ws[len_ws - 1 - i]
    for i in range(1, len_bs):
        sum_b_s[i] = sum_b_s[i - 1] + Bs[i]
        sum_b_r[i] = sum_b_r[i - 1] + Bs[len_bs - 1 - i]
    # print(sum_w_s, sum_w_r, sum_b_s, sum_b_r)
    ans = min(sum_w_s[-1], sum_b_s[-1])
    if S[0] == '#':
        for i in range(len_ws - 1):
            a = sum_b_s[i] + sum_w_r[len_ws - 2 - i]
            ans = min(a, ans)
    else:
        ans = min(ans, sum_w_s[-1] - Ws[0])
        for i in range(len_ws - 1):
            a = sum_w_r[i] + sum_b_s[len_bs - 2 - i]
            ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

l = raw_input().split()
N = int(l[0])
l = raw_input().split()
S1 = list(l[0])

S = []
found = False
for s in S1:
    if s == '#':
        found = True
    if not found:
        continue
    S.append(s)

i = len(S) - 1
while i >= 0:
    if S[i] == '#':
        del S[i]
    else:
        break
    i -= 1
# print(S)
num_black = 0
num_white = 0

temp_black = 0
for s in S:
    if s == '#':
        temp_black += 1
    else:
        num_white += 1
        num_black += temp_black
        temp_black = 0

A = []
B = []
found_black = False
found_white = False
for i in range(len(S)):
    s = S[i]
    if s == '#':
        if found_white:
            B.append(A)
            found_white = False
            A = []
        A.append(s)
    else:
        A.append(s)
        found_white = True
    if i == len(S) - 1:
        B.append(A)

def count_num_black(S):
    count = 0
    for s in S:
        if s == '#':
            count += 1
    return count

num_black = count_num_black(B[0])
num_white = len(B[0]) - num_black
ret = min(num_black, num_white)
del B[0]
num_black = num_white = 0
for b in B:
    for a in b:
        if a == '#':
            num_black += 1
        else:
            num_white += 1

print(ret + min(num_black, num_white))