Problem p02891 (python)

Fixed submission:
S = list(input())
R = int(input())
lengths = []
same = False
All = False
odd = False
sl = 1
Sum = 0
if S[0] == S[len(S)-1]:
  same = True
S.append('0')
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    sl += 1
  else:
    lengths.append(sl)
    sl = 1
if len(lengths) == 1:
  All = True
if lengths[0]%2 == 1 and lengths[len(lengths)-1]%2 == 1:
  odd = True
if same:
  if All:
    Sum += (lengths[0]*R)//2
  else:
    temp = lengths[0]
    lengths[0] = 0
    lengths[len(lengths)-1] += temp
    if odd:
      for l in range(len(lengths)):
        Sum += (lengths[l]//2)*R
      Sum -= 1
    else:
      for g in range(len(lengths)):
        Sum += (lengths[g]//2)*R
else:
  for j in range(len(lengths)):
    Sum += (lengths[j]//2)*R
print(Sum)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
for i in range(len(S)-1):
  if S[i] != S[i+1]:
    J = False
S.append('end')
if S[0] != S[len(S)-2]:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif J:
  print((len(S)-1)*K//2)
else:
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)-1])*(K-1)//2) + length[0]//2 + length[len(length)-1]//2
  length[0] = 0
  length[len(length)-1] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)-2] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)

Buggy submission (verdict: Runtime Error):
S = list(input())
S.append('end')
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
if S[0] != S[len(S)-1] and J
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = len(S)*K//2
  print(ans)
else:
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)