qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n, k = map(int,input().split())
s = input()

if n == 1:
        print(0)
else:        
        t = []
        tt = 0
        ss = s[0]
        for i in range(1,n):
                if s[i] != ss:
                        t.append(i - tt)
                        tt = i
                        ss = s[i]
        t.append(n - tt)
        print(t)
        cnt = sum(t) - len(t) 

        if k <= len(t) - 2:
                cnt += 2 * k
        elif k == len(t) - 1:
                cnt += 2 * (len(t) - 2) + 1
        else:
                cnt += 2 * (len(t) - 2) + 2
        print(cnt)

Fixed code:
n, k = map(int,input().split())
s = input()

if n == 1:
        print(0)
else:        
        t = []
        tt = 0
        ss = s[0]
        for i in range(1,n):
                if s[i] != ss:
                        t.append(i - tt)
                        tt = i
                        ss = s[i]
        t.append(n - tt)
        cnt = sum(t) - len(t) 

        if len(t) % 2 == 0:
                if k <= (len(t) - 2)/2:
                        cnt += 2 * k
                else:
                        cnt += 2 * (len(t) - 2)/2 + 1
        else:
                if k <= (len(t) - 1)/2:
                        cnt += 2 * k
                else:
                        cnt += 2 * (len(t) - 1)/2
        print(int(cnt))
Buggy code:
import itertools

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, K = ZZ()
    S = input()
    gr = itertools.groupby(S)
    ll = []
    for c, d in gr: ll.append([c, len(list(d))])

    output = N - len(ll)
    i = 1
    for _ in range(K):
        if i < len(ll): output += 2
        else: break
        i += 2

    print(output)

    return

if __name__ == '__main__':
    main()


Fixed code:
import itertools

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, K = ZZ()
    S = input()
    gr = itertools.groupby(S)
    ll = []
    for c, d in gr: ll.append([c, len(list(d))])

    output = N - len(ll)
    i = 1
    for _ in range(K):
        if i == len(ll)-1: output += 1
        elif i < len(ll): output += 2
        else: break
        i += 2

    print(output)

    return

if __name__ == '__main__':
    main()
Buggy code:
N, K = list(map(int, input().split()))
S = list(map(int, input().split()))

happy = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    happy += 1
return min([N-1, happy + 2 * K])

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

happy = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    happy += 1
print(min([N-1, happy + 2 * K]))
Buggy code:
N, K = list(map(int, input().split()))
S = list(map(int, input().split()))
 
happy = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    happy += 1
print(min([N-1, happy + 2 * K]))

Fixed code:
N,K = map(int,input().split())
S = input()
x = 0
for i in range(N-1):
    if S[i]==S[i+1]:
        x +=1

print( min(N-1,x+K*2))
Buggy code:
N, K = map(int, input().split())
S = input()

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(max(count-2*K,1))


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

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(N-max(count-2*K,1))
Buggy code:
N, K=map(int, input().split())
S=list(input())
SS=[S[0]]

if len(SS)==1:
  print(N-1)
  exit()

for i in range(1, len(S)):
  if SS[-1]!=S[i]:
    SS.append(S[i])

for i in range(1, len(SS)-1):
  if SS[i]!=SS[i-1] and SS[i]!=SS[i+1] and K>0:
    SS[i]=SS[i-1]
    K-=1
  
if SS[0]!=SS[1] and K>0:
  S[0]=S[1]
  
if SS[-1]!=SS[-2] and K>0:
  SS[-1]=SS[-2]
  
unhappy=0
if (SS[0]=='L') or (SS[0]=='R' and SS[1]=='L'):
  unhappy+=1
  
for i in range(1, len(SS)-1):
  if (SS[i]=='L' and SS[i-1]=='R') or (SS[i]=='R' and SS[i+1]=='L'):
    unhappy+=1
    
if (SS[-1]=='R') or (SS[-1]=='L' and SS[-2]=='R'):
  unhappy+=1
  
happy=N-unhappy
print(happy)

Fixed code:
N, K=map(int, input().split())
S=list(input())
SS=[S[0]]

for i in range(1, len(S)):
  if SS[-1]!=S[i]:
    SS.append(S[i])

if len(SS)==1:
  print(N-1)
  exit()
  
if len(SS)==2:
  if K>0:
    print(N-1)
    exit()
  else:
    print(N-2)
    exit()
  
for i in range(1, len(SS)-1):
  if SS[i]!=SS[i-1] and SS[i]!=SS[i+1] and K>0:
    SS[i]=SS[i-1]
    K-=1
  
if SS[0]!=SS[1] and K>0:
  S[0]=S[1]
  K-=1
  
if SS[-1]!=SS[-2] and K>0:
  SS[-1]=SS[-2]
  K-=1
  
unhappy=0
if (SS[0]=='L') or (SS[0]=='R' and SS[1]=='L'):
  unhappy+=1
  
for i in range(1, len(SS)-1):
  if (SS[i]=='L' and SS[i-1]=='R') or (SS[i]=='R' and SS[i+1]=='L'):
    unhappy+=1
    
if (SS[-1]=='R') or (SS[-1]=='L' and SS[-2]=='R'):
  unhappy+=1
  
happy=N-unhappy
print(happy)
Buggy code:
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''
#print(ix)
s += 'L'
for i in range(n+1):
  if i!=n:
    if temp=='R' and s[i]=='L':
      inside += 1
    elif temp==s[i]:
      cnt += 1
  temp = s[i]
print(inside,cnt,edge)
if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))

Fixed code:
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''
#print(ix)
s += 'L'
for i in range(n+1):
  if i!=n:
    if temp=='R' and s[i]=='L':
      inside += 1
    elif temp==s[i]:
      cnt += 1
  temp = s[i]
#print(inside,cnt,edge)
if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))
Buggy code:
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    print(f's[i]: {s[i]} s[i-1]: {s[i-1]} s[i+1]:{s[i+1]}')
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
if count == n-2:
    print(count + 1)
else:
    print(count + 2*k)

Fixed code:
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    #print(f's[i]: {s[i]} s[i-1]: {s[i-1]} s[i+1]:{s[i+1]}')
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
print(min(n-1, count + 2*k))
Buggy code:
import re
a = input().split(' ')
N = int(a[0])
K = int(a[1])
S = input()

LR = re.findall(r'LR', S)
RL = re.findall(r'RL', S)

num1 = len(LR)
num2 = len(RL)

if num1 < num2:
  num = num1
else:
  num = num2
  
if K < num:
  num = K
  
if (num1 - num2 == 1) or (num2 - num1 == 1):
  point = num * 2 + 1
else:
  point = num * 2
  
for i in range(N):
  if i != 0:
    if S[i] == S[i-1]:
      point = point + 1


print(point)



Fixed code:
import re
a = input().split(' ')
N = int(a[0])
K = int(a[1])
S = input()

LR = re.findall(r'LR', S)
RL = re.findall(r'RL', S)

num1 = len(LR)
num2 = len(RL)

if num1 < num2:
  num = num1
else:
  num = num2
  
if K < num:
  num = K
  
if ((num1 - num2 == 1) and num1 <= K) or ((num2 - num1 == 1) and num2 <= K):
  point = num * 2 + 1
else:
  point = num * 2
  
for i in range(N):
  if i != 0:
    if S[i] == S[i-1]:
      point = point + 1


print(point)
Buggy code:
import sys

n,k=map(int,input().split())
#s=list(map(int,input().split()))
s=input()

lcnt=-1
rcnt=-1

rr=[0]*n
ll=[0]*n
sim=""
for si in s:
#    print(si)
    if si=="R" and sim=="R":
        rr[rcnt]+=1
    elif si=="R" and sim!="R":
        rcnt+=1
        rr[rcnt]=1
    if si=="L" and sim=="L":
        ll[lcnt]+=1
    elif si=="L" and sim!="L":
        lcnt+=1
        ll[lcnt]=1
    sim=si
        

ll=[i for i in ll if i!=0]
rr=[i for i in rr if i!=0]
#print(ll)
#print(rr)

maxlen=max(len(ll),len(rr))
if maxlen<=k:
    print(n-1)
    sys.exit()
    
for i in range(k):
    ll[i]+=rr[i]
    rr[i]=0
#print(lr)
for i in range(maxlen-1):
    if ll[i]!=0 and rr[i]==0 :
        ll[i+1]+=ll[i]
        ll[i]=0
#print(lr)

icnt=0
for i in range(maxlen):
    icnt+=max(ll[i]-1,0)+max(rr[i]-1,0)
    
print(icnt)


Fixed code:
import sys

n,k=map(int,input().split())
s=input()

lcnt=-1
rcnt=-1

rr=[0]*n
ll=[0]*n
sim=""
for si in s:
    if si=="R" and sim=="R":
        rr[rcnt]+=1
    elif si=="R" and sim!="R":
        rcnt+=1
        rr[rcnt]=1
    if si=="L" and sim=="L":
        ll[lcnt]+=1
    elif si=="L" and sim!="L":
        lcnt+=1
        ll[lcnt]=1
    sim=si
        
ll=[i for i in ll if i!=0]
rr=[i for i in rr if i!=0]
#print(ll)
#print(rr)

minlen=min(len(ll),len(rr))
if minlen<=k:
    print(n-1)
    sys.exit()
    
for i in range(k):
    ll[i]+=rr[i]
    rr[i]=0

for i in range(len(ll)-1):
    if i<len(rr) and ll[i]!=0 and rr[i]==0 :
        ll[i+1]+=ll[i]
        ll[i]=0

icnt=0
for i in range(len(ll)):
    icnt+=max(ll[i]-1,0)
for i in range(len(rr)):
    icnt+=max(rr[i]-1,0)
    
print(icnt)
Buggy code:
N, K = map(int, input().split())
S = input()

d = 0
for i in range(N-1):
	if(S[i]!=S[i-1]):
		d += 1
d = max(0, d - 2 * K)
print(N - 1 - d)

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

d = 0
for i in range(N-1):
	if(S[i] != S[i+1]):
		d += 1
d = max(0, d - 2 * K)
print(N - 1 - d)
Buggy code:
n,k= map(int,(input().split()))
b= input()
LRn=len(re.findall('LR',b))
RLn=len(re.findall('RL',b))

if min(LRn,RLn)>=k:
    ans=k*2
elif min(LRn,RLn)<=k and max(LRn,RLn)>=k:
    ans=k+min(LRn,RLn)
else:
    ans=LRn+RLn

a= list(b)
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        ans +=1

print(ans)

Fixed code:
import re

n,k= map(int,(input().split()))
b= input()
LRn=len(re.findall('LR',b))
RLn=len(re.findall('RL',b))

if min(LRn,RLn)>=k:
    ans=k*2
elif min(LRn,RLn)<=k and max(LRn,RLn)>=k:
    ans=k+min(LRn,RLn)
else:
    ans=LRn+RLn

a= list(b)
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        ans +=1

print(ans)
Buggy code:
n,k = map(int, input().split())
s = input()

if n == 1:
  print(0)
  exit()

pre = s[0]
cont = 1
for c in s[1:]:
  if c != pre:
    if c == s[0]:
      two += 1
    ans += cont-1
    cont = 1
    pre = c
  else:
    cont += 1
ans += cont-1

if s[0] != s[1]:
  ans += 1
print(ans + 2*min(k,two))

Fixed code:
n, k = map(int, input().split())
s = input()

if n == 1:
    print(0)
    exit()

two = 0
pre = s[0]
cont = 1
ans = 0
for c in s[1:]:
    if c != pre:
        if c == s[0]:
            two += 1
        ans += cont - 1
        cont = 1
        pre = c
    else:
        cont += 1
ans += cont - 1

if s[0] != s[-1] and k > two:
    ans += 1
print(ans + 2 * min(k, two))
Buggy code:
n, k = map(int, input().split())
s = input()
happy_counter = 0
for count in range(1, n):
    if s[count] == s[count - 1]:
        happy_counter += 1
happy_counter += k * 2
if happy_counter > n:
    happy_counter = n

print(happy_counter)

Fixed code:
n, k = map(int, input().split())
s = input()
happy_counter = 0
for count in range(1, n):
    if s[count] == s[count - 1]:
        happy_counter += 1
happy_counter += k * 2
if happy_counter > n - 1:
    happy_counter = n - 1

print(happy_counter)
Buggy code:
N, K = map(int, input().split())
S = input()

dup = 0
for i in range(1,N):
    if S[i] == S[i-1]:
        dup += 1
#print(min(N-1, dup + K*2))
print(dup + K * 2)

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

dup = 0
for i in range(1,N):
    if S[i] == S[i-1]:
        dup += 1
#print(min(N-1, dup + K*2))
print(min(N-1,dup+K*2))
Buggy code:
(N,K) = map(int,input().split())
S = input()
dan = 1

for i in range(N-1):
    if S[i] != S[i+1]:
        dan += 1

if 2*K >= (dan-1):
    print(N)
else:
    print(N-dan+2*K)

Fixed code:
(N,K) = map(int,input().split())
S = input()
dan = 1

for i in range(N-1):
    if S[i] != S[i+1]:
        dan += 1

if 2*K >= (dan-1):
    print(N-1)
else:
    print(N-dan+2*K)
Buggy code:
N,K = map(int,input().split())
S = list(input())

group = N-1
for i in range(1, N-1):
  if S[i-1] != S[i]:
    group -= 1
    
print(min(N-1, group+2*K))    

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

group = 0
for i in range(1, N):
  if S[i-1] == S[i]:
    group += 1
    
print(min(N-1, group+2*K))
Buggy code:
n_k_str = input()
n_k_list = n_k_str.split(" ")
N = int(n_k_list[0])
K = int(n_k_list[1])
S = list(input())

pre_s = S.pop(0)
pre_index = -1
result = 0
for i,s in enumerate(S):
    if s != pre_s:
        temp = i - pre_index - 1
        print(temp)
        result += temp
        pre_index = i
    pre_s = s

temp = len(S) - pre_index - 1
result += temp

result += K*2
if result >= N:
    result = N - 1

print(result)

Fixed code:
n_k_str = input()
n_k_list = n_k_str.split(" ")
N = int(n_k_list[0])
K = int(n_k_list[1])
S = list(input())

pre_s = S.pop(0)
pre_index = -1
result = 0
for i,s in enumerate(S):
    if s != pre_s:
        temp = i - pre_index - 1
        result += temp
        pre_index = i
    pre_s = s

temp = len(S) - pre_index - 1
result += temp

result += K*2
if result >= N:
    result = N - 1

print(result)
Buggy code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def main():
    N, K = map(int, input().split())
    S = list(input())

    ans = 0
    rev = False
    rev_count = 0

    for i in range(1,N):
        if S[i] != S[i-1]:
            if rev:
                S[i] = 'L' if S[i] == 'R' else 'L'
                ans += 1
            else:
                if rev_count + 1 <= K:
                    rev_count += 1
                    rev = True
                    S[i] = 'L' if S[i] == 'R' else 'L'
                    ans += 1
        else:
            ans += 1
            rev = False

    print(ans)


if __name__ == "__main__":
    main()


Fixed code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def main():
    N, K = map(int, input().split())
    S = list(input())

    rev = False
    rev_count = 0
    for i in range(1,N):
        if S[i] == S[i-1]:
            rev = False
        else:
            if not rev and rev_count < K:
                rev_count += 1
                rev = True

            if rev:
                S[i] = 'L' if S[i] == 'R' else 'R'


    ans = 0
    for i in range(1,N):
        if S[i] == S[i-1]:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
N, K = map(int, input().split())
Ss = input()
 
num = Ss.count('RL')*2
if list(Ss)[-1]=='L':
  if list(Ss)[0]=='L':
    num += 1
  num += 1

ans = N - max(1, num-2*K)
print(ans)

Fixed code:
N, K = map(int, input().split())
Ss = input()
 
num = Ss.count('RL')*2
if list(Ss)[-1]=='R':
  num += 1
if list(Ss)[0]=='L':
  num += 1
ans = N - max(1, num-2*K)
print(ans)
Buggy code:
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1
#print("now HP =>{}".format(happyCount))
happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(min(n - 1, happyCount)


Fixed code:
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1
#print("now HP =>{}".format(happyCount))
happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(min(n - 1, happyCount))
Buggy code:
N,K = map(int,input().split())
S = input()
ans = 0
for i in range(N):
    if S[i] == S[i+1]:
        ans += 1
ans += 2*K
if ans > N-1:
    ans = N-1
print(ans)

Fixed code:
N,K = map(int,input().split())
S = input()
ans = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        ans += 1
ans += 2*K
if ans > N-1:
    ans = N-1
print(ans)
Buggy code:
#D-Face Produces Unhappiness
N, K = map(int, input().split())
S = str(input())
l_index = [l[i]+l[i+1] for i in range(0,N-1)]

count_LRorRL =0
count_LRorRL += S.count('LR')
count_LRorRL += S.count('RL')

count_LLorRR = 0
count_LLorRR += S.count('LL')
count_LLorRR += S.count('RR')

if count_LRorRL > 2*K:
    print(count_LLorRR+2*K)
else:
    print(count_LLorRR+count_LRorRL)

Fixed code:
#D-Face Produces Unhappiness
N, K = map(int, input().split())
S = str(input())

def count_LRorRL(l):
    ans = 0
    l_index = [l[i]+l[i+1] for i in range(0,N-1)]
    ans += l_index.count('LR')
    ans += l_index.count('RL')
    return ans

def count_LLorRR(l):
    ans = 0
    l_index = [l[i]+l[i+1] for i in range(0,N-1)]
    ans += l_index.count('LL')
    ans += l_index.count('RR')
    return ans
  
if count_LRorRL(S) > 2*K:
    print(count_LLorRR(S)+2*K)
else:
    print(count_LLorRR(S)+count_LRorRL(S))
Buggy code:
n,k = map(int, input().split())
s = input()

bad = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        bad += 1

if bad > k*2:
    ans = (n-1) - bad + (k*2)
else:
    ans = n

print(ans)

Fixed code:
n,k = map(int, input().split())
s = input()

bad = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        bad += 1

if bad > k*2:
    ans = (n-1) - bad + (k*2)
else:
    ans = n-1

print(ans)
Buggy code:
n,k = map(int, input().split())
s = input()

rl_cnt = 0
for s1,s2 in zip(s[:-1],s[1:]):
    if s1 == 'R' and s2 == 'L':
        rl_cnt += 1

left_happy = 1 if s[0] == 'R' else 0
right_happy = 1 if s[-1] == 'L' else 0

first_happy = n - rl_cnt*2 -2 + left_happy + right_happy

if k <= rl_cnt:
    print(first_happy + k*2)
else:
    rem = k-rl_cnt
    ans = first_happy + k*2 + min(rem, left_happy+right_happy)
    print(ans)

Fixed code:
n,k = map(int, input().split())
s = input()

rl_cnt = 0
for s1,s2 in zip(s[:-1],s[1:]):
    if s1 == 'R' and s2 == 'L':
        rl_cnt += 1

left_happy = 1 if s[0] == 'R' else 0
right_happy = 1 if s[-1] == 'L' else 0

first_happy = n - rl_cnt*2 -2 + left_happy + right_happy
# print(first_happy)
if k <= rl_cnt:
    ans = first_happy + k*2
else:
    rem = k-rl_cnt
    ans = first_happy + rl_cnt*2 + min(rem, 2-left_happy+right_happy)

print(min(ans,n-1))
Buggy code:
n,k=map(int,input().split())
S=input()

cnt=0
for i in range(1,n):
  if S[i-1]==S[i]:
    cnt+=1
print(min(n-1,cnt*2+k))

Fixed code:
n,k=map(int,input().split())
S=input()

cnt=0
for i in range(1,n):
  if S[i-1]==S[i]:
    cnt+=1
print(min(n-1,cnt+2*k))
Buggy code:
n,k = (int(i) for i in input().split())  
string = input()
s = [string[i] for i in range(len(string))]

count = 0
first = False
end = False

if s[0] == 'L':
    first = True

if s[-1] == 'R':
    end = True
print(first)
print(end)

for i in range(len(s)):
    d = s[i]
    if d == 'L' and i>0 and s[i-1]=='L':
        count += 0
    elif d == 'R' and i<n-1 and s[i+1]=='R':
        count += 0
    else:
        count += 1
print(count)
for i in range(k):
    if count-2 >= 2:
        count += -2
    else:
        if first:
            count += -1
            first = False
        elif end:
            count += -1
            end = False
        else:
            count += -2

if count < 1:
    count = 1
    
print(n-count)

Fixed code:
n,k = (int(i) for i in input().split())  
string = input()
s = [string[i] for i in range(len(string))]

count = 0
first = 0
end = 0

if s[0] == 'L':
    first = 1

if s[-1] == 'R':
    end = 1

for i in range(len(s)):
    d = s[i]
    if d == 'L' and i>0 and s[i-1]=='L':
        count += 0
    elif d == 'R' and i<n-1 and s[i+1]=='R':
        count += 0
    else:
        count += 1

line = first + end

for i in range(k):
    if count-2 >= line:
        count += -2
    else:
      	count += -1


if count < 1:
    count = 1
    
print(n-count)
Buggy code:
tmp = list(map(int,(input().split())))
n   = tmp[0]
k   = tmp[1]
s   = input()

bound = 0
cnt = 0
score = 0
for i in range(1,n):
  if s[i] != s[i-1]:
    bound += 1
    score += cnt
    cnt = 0
  else:
    cnt += 1

if bound <= k:
  score = n - 1
else:
  score += 2

print (score)

Fixed code:
tmp = list(map(int,(input().split())))
n   = tmp[0]
k   = tmp[1]
s   = input()

bound = 0
cnt = 0
score = 0
for i in range(1,n):
  if s[i] != s[i-1]:
    bound += 1
    score += cnt
    cnt = 0
  else:
    cnt += 1
    if i == n-1:
      score += cnt

if bound <= k*2:
  score = n - 1
else:
  score += k * 2

print (score)
Buggy code:
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if s[i] == s[i+1]:
    ans += 1
for i in range(k):
  if ans < n-2:
    ans += 2
  if ans == n-2:
    ans += 1
  else:
    break
print(ans)

Fixed code:
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if s[i] == s[i+1]:
    ans += 1
for i in range(k):
  if ans < n-2:
    ans += 2
  elif ans == n-2:
    ans += 1
  else:
    pass
print(ans)
Buggy code:
import sys
n, k = map(int, input().split(' '))
s = str(input())

i = 0
counter = 1
assyuku = [1]
while i+counter < n:
    if s[i] == s[i+counter]:
        assyuku[-1] += 1
        counter += 1
    else:
        assyuku.append(1)
        i += counter
        counter = 1
assyuku = [x-1 for x in assyuku]
print(sum(assyuku)+2*k)

Fixed code:
import sys
n, k = map(int, input().split(' '))
s = str(input())

i = 0
counter = 1
assyuku = [1]
while i+counter < n:
    if s[i] == s[i+counter]:
        assyuku[-1] += 1
        counter += 1
    else:
        assyuku.append(1)
        i += counter
        counter = 1
assyuku = [x-1 for x in assyuku]
if len(assyuku) > k*2:
    print(sum(assyuku)+2*k)
else:
    print(n-1)
Buggy code:
n,k=map(int,input().split())
a=input()
p=0
for i in range(n-1):
    if a[i]==a[i+1]:
        p+=1
print(min(p+2*k,n-n%2))

Fixed code:
n,k=map(int,input().split())
a=input()
p=0
for i in range(n-1):
    if a[i]==a[i+1]:
        p+=1
print(min(p+2*k,n-1))
Buggy code:
N, K = map(int, input().split())
S = input()
r = []
l = []
cnt1 = 0
cnt2 = 0

p = 0
if 'L' == S[0]:
    for i in range(N):
        if 'L' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            l.append(p)
            break
else:
    for i in range(N):
        if 'R' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            r.append(p)
            break

ans = [1]
for i in range(ind, N):
    if S[i] == 'L':
        if cnt2:
            r.append(cnt2)
            ans.append(2)
            cnt2 = 0
        cnt1 += 1
    else:
        if cnt1:
            l.append(cnt1)
            ans.append(2)
            cnt1 = 0
        cnt2 += 1
if cnt1:
    ans.append(1)
else:
    ans.append(1)

ans.sort()
ans.reverse()

sums = 0
for i in range(N-2):
    if 'LL' == S[i:i+2]:
        sums += 1
    elif 'RR' == S[i:i+2]:
        sums += 1
#print(sums)
if K >= len(ans):
    print(len(S) - 1)
else:
    if sums + sum(ans[:K]) >= len(S):
        print(len(S) - 1)
    else:
        print(sums + sum(ans[:K]))


Fixed code:
N, K = map(int, input().split())
S = input()
cnt1 = 0
cnt2 = 0
if len(S) == 1:
    print(0)
    exit()
p = 0
if 'L' == S[0]:
    for i in range(N):
        if 'L' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
else:
    for i in range(N):
        if 'R' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
if p == N:
    ind = N-1
ans = [1]
for i in range(ind, N):
    if S[i] == 'L':
        if cnt2:
            ans.append(2)
            cnt2 = 0
        cnt1 += 1
    else:
        if cnt1:
            ans.append(2)
            cnt1 = 0
        cnt2 += 1
if cnt1:
    ans.append(1)
else:
    ans.append(1)

ans.sort()
ans.reverse()

sums = 0
for i in range(N-1):
    if 'LL' == S[i:i+2]:
        sums += 1
    elif 'RR' == S[i:i+2]:
        sums += 1

if K >= len(ans):
    print(len(S) - 1)
else:
    if len(ans) == 0:
        print(len(S) - 1)
    if sums + sum(ans[:K]) >= len(S):
        print(len(S) - 1)
    else:
        print(sums + sum(ans[:K]))
Buggy code:
N,K=map(int,input().split())
S=input()

ch_list=[]
for i in range(1,N):
  if S[i-1]!=S[i]:
    ch_list.append(i)
print(ch_list)
len_ch=len(ch_list)

if len_ch>=2*K:
  print(N-1-len_ch+2*K)
elif len_ch==2*K-1:
  print(N-1-len_ch+2*K-1)
else:
  print(N-1)

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

ch_list=[]
for i in range(1,N):
  if S[i-1]!=S[i]:
    ch_list.append(i)
#print(ch_list)
len_ch=len(ch_list)

if len_ch>=2*K:
  print(N-1-len_ch+2*K)
elif len_ch==2*K-1:
  print(N-1-len_ch+2*K-1)
else:
  print(N-1)