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

num = [b[0]]

for i in range(1, n - 1):
  if(b[i] <  b[i - 1]):
    num[-1] = b[i]
    num.append(b[i])
  else:
    num.append(b[i])

num.append(b[0])

print(num)
print(sum(num))


Fixed code:
n = int(input())
b = list(map(int, input().split()))

num = [b[0]]

for i in range(1, n - 1):
  if(b[i] <  b[i - 1]):
    num[-1] = b[i]
    num.append(b[i])
  else:
    num.append(b[i])

num.append(b[0])

print(sum(num))
Buggy code:
N = int(input())
B = list(map(int, input().split()))

S = B[0] + B[-1]

for i in range(1, N-2):
  S += min(B[i], B[i+1])

print(S)

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

S = B[0] + B[-1]

for i in range(0, N-2):
  S += min(B[i], B[i+1])

print(S)
Buggy code:
N = input()
B = input().split(" ")
A = [0]*int(N)

B = list(map(int,B))
total = 0


A[0] = B[0]

for i in range(1,int(N)-1):
    print(i)
    A[i] = min(B[i],B[i-1])

A[int(N)-1] = B[int(N)-2]
print(A)
for i in range(int(N)):
    total = A[int(i)] + total

print(total)


Fixed code:
N = input()
B = input().split(" ")
A = [0]*int(N)

B = list(map(int,B))
total = 0


A[0] = B[0]

for i in range(1,int(N)-1):
    #print(i)
    A[i] = min(B[i],B[i-1])

A[int(N)-1] = B[int(N)-2]
#print(A)
for i in range(int(N)):
    total = A[int(i)] + total

print(total)
Buggy code:
N = int(input())
B = list(map(int,input().split()))
ins = 0
count = 0
for i in range(N):
  if i == 0:
    count += B[i]
  if i > 0 and B[i-1] > B[i]:
    count += B[i]
  elif i > 0 and B[i-1] <= B[i]:
    count += B[i-1]
print(count)

Fixed code:
N = int(input())
B = list(map(int,input().split()))
ins = 0
count = 0
for i in range(N-1):
  if i == 0:
    count += B[i]
  if i > 0 and B[i-1] > B[i]:
    count += B[i]
  elif i > 0 and B[i-1] <= B[i]:
    count += B[i-1]
count += B[i]
print(count)
Buggy code:
import math

N = int(input())
B = list(map(int, input().split())) 
import numpy as np
A=np.zeros([N])
for i in range(N):
    if i == 0:
        A[i] = B[i]
    elif i== N-1:
        A[i] = B[i-1]
    else:
        A[i] = min(B[i-1],B[i])

int(A.sum())
        

Fixed code:
import math

N = int(input())
B = list(map(int, input().split())) 
import numpy as np
A=np.zeros([N])
for i in range(N):
    if i == 0:
        A[i] = B[i]
    elif i== N-1:
        A[i] = B[i-1]
    else:
        A[i] = min(B[i-1],B[i])

print(int(A.sum()))
Buggy code:
N = int(input())
B = list(map(int, input().split()))

A = [10 ** 6] * N

for i in range(N - 1):
    A[i] = min(A[i], B[i])
    A[i + 1] = min(A[i + 1], B[i])

print(A)

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

A = [10 ** 6] * N

for i in range(N - 1):
    A[i] = min(A[i], B[i])
    A[i + 1] = min(A[i + 1], B[i])

print(sum(A))
Buggy code:
N = int(input())
B = list(map(int, input().split()))

ans = B[0] + B[-1]
for i in range(1, N-1):
    print(min(B[i-1], B[i]))
    ans += min(B[i-1], B[i])
print(ans)


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

ans = B[0] + B[-1]
for i in range(1, N-1):
    ans += min(B[i-1], B[i])
print(ans)
Buggy code:
N=int(input().split())
B=list(map(int,input().split()))
A=[0}*N
A[0]=B[0}
A{N-1]=B[N-2]
for i in range(1,N-2):
  A[i]=min(B[i-1],B[i])
print(sum(A))

Fixed code:
N=int(input())
B=list(map(int,input().split()))
A=[0]*N
A[0]=B[0]
A[N-1]=B[N-2]
for i in range(1,N-1):
  A[i]=min(B[i-1],B[i])
print(sum(A))
Buggy code:
N=int(input().split())
B=list(map(int,input().split()))
A=[0]*N
A[0]=B[0]
A{N-1]=B[N-2]
for i in range(1,N-2):
  A[i]=min(B[i-1],B[i])
print(sum(A))

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

A = [0]*N


A[0] = B[0]

for i in range(1, N-1):
    A[i] = min(B[i-1], B[i])
    
A[-1] = min(B[-1], B[-1])

print(sum(A))
Buggy code:
N = int(input())
B = list(map(int,input().split()))
A = []
for i in range(N):
  A.append(0)

for i in range(N):
  if i = 0:
    A[i] = B[i]
  else:
    A[i] = min(B[i], B[i - 1])
    
print(A)

Fixed code:
N = int(input())
B = list(map(int, input().split()))
A = []

for i in range(N):
    if i == 0:
        A.append(B[i])
    elif i == N - 1:
        A.append(B[i-1])
    else:
        A.append(min(B[i-1], B[i]))

print(sum(A))
Buggy code:
a=input()
b=input().split()
sum=0
for i in range(len(b)-1) :
  sum+=min(int(b[i]),int(b[i+1]))
sum+=int(b[-1])

print(sum)


Fixed code:
a=input()
b=input().split()
sum=int(b[0])

for i in range(len(b)-1) :
  sum+=min(int(b[i]),int(b[i+1]))
sum+=int(b[-1])

print(sum)
Buggy code:
num = int(input())

arr = list(map(int,input().split()))
arr.append(arr[num-2])
a=[]
for i in range(1,num):
    a0 = min(arr[i],arr[i-1])

    a.append(a0)

a.append(a[-1])

print(sum(a))

Fixed code:
num = int(input())

arr = list(map(int,input().split()))

a=[arr[0]]
for i in range(1,num-1):
    a0 = min(arr[i],arr[i-1])

    a.append(a0)
a.append(arr[-1])

print(sum(a))
Buggy code:
n = int(input())
b = list(map(int,input().split()))
ans = 0
aa = 0
ab = 0
for i in range(n-1):
    aa = min(ab,b[i])
    ab = b[i]
    ans += aa
print(ans+ab)

Fixed code:
n = int(input())
b = list(map(int,input().split()))
ans = 0
aa = 0
ab = 100001
for i in range(n-1):
    aa = min(ab,b[i])
    ab = b[i]
    ans += aa
print(ans+ab)
Buggy code:
N = int(input())
B = [int(i) for i in input().split(' ')]
A = []
A.append(B[0])
for i in range(1,N-1):
    A.append(min(B[i],B[i-1]))
A.append(B[N-2])

Fixed code:
N = int(input())
B = [int(i) for i in input().split(' ')]
A = []
A.append(B[0])
for i in range(1,N-1):
    A.append(min(B[i],B[i-1]))
A.append(B[N-2])
print(sum(A))
Buggy code:
N = int(input())
B = list(map(int, input().split()))

inf = 10**10
B = [inf] + B + [inf]
ans = 0
for i in range(N):
    ans += min(B[i], B[i+1])


Fixed code:
N = int(input())
B = list(map(int, input().split()))
 
inf = 10**10
B = [inf] + B + [inf]
ans = 0
for i in range(N):
    ans += min(B[i], B[i+1])
print(ans)
Buggy code:
N=int(input())
b=list(map(int,input().split()))
a=b[0]
for i in range(0,N-2):
    print(i)
    a+=min(b[i],b[i+1])
print(a+b[-1])


Fixed code:
N=int(input())
b=list(map(int,input().split()))
a=b[0]
for i in range(0,N-2):
    a+=min(b[i],b[i+1])
print(a+b[-1])
Buggy code:
n = int(input())
a = map(int,input().split())
s = 0
for i in range(n-2):
  s+=min(a[n-2-i],a[n-i-3])
print(s+a[n-2])


Fixed code:
n = int(input())
a = list(map(int,input().split()))
s = 0
for i in range(n-2):
  s+=min(a[n-2-i],a[n-i-3])
print(s+a[n-2]+a[0])
Buggy code:
n = int(input())
b = list(map(int,input().split()))
c = 0
b.append(b[-1])
for i in range(1,n):
    c += min(b[i],b[i-1])
print(c)

Fixed code:
n = int(input())
b = list(map(int,input().split()))
c = 0
b.append(b[-1])
for i in range(1,n):
    c += min(b[i],b[i-1])
print(c+b[0])
Buggy code:
N=int(input())
B=map(int,input().split())
sum=B[0]
for i in range(len(B)-1):
  sum+=min(B[i],B[i+1])
sum+=B[-1]
print(sum)

Fixed code:
N=int(input())
B=list(map(int,input().split()))
sum=B[0]
for i in range(len(B)-1):
  sum+=min(B[i],B[i+1])
sum+=B[-1]
print(sum)
Buggy code:
N,K=map(int,input().split())
S=list(input())

old='X'
count=0
for i,s in enumerate(S):
    if s==old:
        count+=1

    old=s
min(count+2*K,N-1)

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

old='X'
count=0
for i,s in enumerate(S):
    if s==old:
        count+=1

    old=s
print(min(count+2*K,N-1))
Buggy code:
n, k = map(int, input().split())
s = input()
t = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        t += 1
print(min([n - 1, h + 2 * k]))


Fixed code:
n, k = map(int, input().split())
s = input()
t = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        t += 1
print(min([n - 1, t + 2 * k]))
Buggy code:
n, k = map(int, input().split())
s = input()
segment = 0
now = '#'
ans = 0
for i in range(s):
    if i=now:
        segment+=1
        now = i
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment-t, ans+t
print(ans)


Fixed code:
n, k = map(int, input().split())
s = input()
segment = 0
now = '#'
ans = 0
for i in s:
    if i!=now:
        segment+=1
        now = i
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment-t, ans+t
print(ans)
Buggy code:
n, k = map(int, input().split())
b = input()

sut = 0
a = ""
for i,s in enumerate(b):
    if i < n-1:
        if b[i] == b[i+1]:
            sut += 1
        else:
            a += s
    else:
        a+=s
a

count = len(a)
for i in range(k):
    if count>=5:
        sut += 2
        count -= 2
    elif len(a) == 3:
        sut += 1
        count -= 2
print(sut)

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

sut = 0
a = ""
for i,s in enumerate(b):
    if i < n-1:
        if b[i] == b[i+1]:
            sut += 1
        else:
            a += s
    else:
        a+=s
a

count = len(a)
for i in range(k):
    if count <= 1:
        break
    elif count >= 3:
        sut += 2
        count -= 2
    elif count == 2:
        sut += 1
        break
print(sut)
Buggy code:
N, K = map(int, input().split())
S = input()
print(sum([1 for i in range(1, N) if S[i] == S[i-1]]) + K * 2)

Fixed code:
N, K = map(int, input().split())
S = input()
print(min(sum([1 for i in range(1, N) if S[i] == S[i-1]]) + K*2, N-1))
Buggy code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
s = input();

hukou = 0;
for i in range(n-1) :
	if s[i] == 'R' and s[i+1] == 'L' :
		hukou += 2;

edge = 0;
if s[0] == 'L' :
	edge += 1;
if s[n-1] == 'R' :
	edge += 1;

while hukou > 0 and k > 0 :
	hukou -= 2;
	k -= 1;

while edge > 1 and k > 0 :
	edge -= 1;
	k -= 1;

ans = n-edge-hukou;
if n-edge-hukou <= n-1 :
	ans = n-1

print(ans);

Fixed code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
s = input();

hukou = 0;
for i in range(n-1) :
	if s[i] == 'R' and s[i+1] == 'L' :
		hukou += 2;

edge = 0;
if s[0] == 'L' :
	edge += 1;
if s[n-1] == 'R' :
	edge += 1;

while hukou > 0 and k > 0 :
	hukou -= 2;
	k -= 1;

while edge > 1 and k > 0 :
	edge -= 1;
	k -= 1;

ans = n-edge-hukou;
if ans > n-1 :
	ans = n-1

print(ans);
Buggy code:
N,K = map(int,input().split())
s = str(input())

ans = N - 1 - s.count('RL') - s.count('LR')
print(min( N-1 , ans - 2 * K ))


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

ans = N - 1 - s.count('RL') - s.count('LR')
print(min( N-1 , ans + 2 * K ))
Buggy code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
S = str(input())

count = 0
ans = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        count += 1
    else:
        ans += 1
count += 1

n = (count-1)//2
if count%2==1:
    if n+1<=K:
        ans += 2*n+1
    else:
        ans += 2*K
else:
    if n<=K:
        ans += 2*n
    else:
        ans += 2*K

print(ans)

Fixed code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
S = str(input())

count = 0
ans = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        count += 1
    else:
        ans += 1
count += 1

n = (count-1)//2
if count%2==0:
    if n+1<=K:
        ans += 2*n+1
    else:
        ans += 2*K
else:
    if n<=K:
        ans += 2*n
    else:
        ans += 2*K

print(ans)
Buggy code:
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1
print(ans,lrcnt,rlcnt,lrch,rlch)
     
if min(lrcnt,rlcnt)>=k:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  if k>=2:
    ans+=lrch
    ans+=rlch
  else:
    ans+=1
  
print(ans)

Fixed code:
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1

if min(lrcnt,rlcnt)>=k*2:
  ans+=k*2
else:
  tmp=min(lrcnt,rlcnt)
  ans+=tmp
  k-=tmp//2
  lrcnt-=tmp
  rlcnt-=tmp

  if k>=2:
    if lrcnt>0 and rlch>0:
      ans+=1
    if rlcnt>0 and lrch>0:
      ans+=1
  else:
    if lrcnt>0 and rlch>0:
      ans+=1
    else:
      if rlcnt>0 and lrch>0:
        ans+=1
  
print(ans)
Buggy code:
n, k = map(int, input().split())
s = input()

unhappiness = 0
m1, m2 = 0, 0

if s[0] == 'L':
    unhappiness += 1
if s[-1] == 'R':
    unhappiness += 1
i = 0
while i < n and s[i] == 'L':
    i += 1
while i < n:
    h = i
    while i < n and s[i] == 'R':
        i += 1
    if i == n:
        break
    while i < n and s[i] == 'L':
        i += 1
    if h != 0 and i != n:
        m2 += 1
    elif h != 0 or i != n:
        m1 += 1
    unhappiness += 2

if  k <= m2:
    unhappiness -= 2 * k
else:
    unhappiness -= 2 * m2
    unhappiness -= m1
print(n-unhappiness)

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

unhappiness = 0
m1, m2 = 0, 0

if s[0] == 'L':
    unhappiness += 1
if s[-1] == 'R':
    unhappiness += 1
i = 0
while i < n and s[i] == 'L':
    i += 1
while i < n:
    h = i
    while i < n and s[i] == 'R':
        i += 1
    if i == n:
        break
    while i < n and s[i] == 'L':
        i += 1
    if h != 0 and i != n:
        m2 += 1
    elif h != 0 or i != n:
        m1 += 1
    unhappiness += 2

if  k <= m2:
    unhappiness -= 2 * k
else:
    unhappiness -= 2 * m2
    k -= m2
    if m1 == 2:
        k -= 1
        unhappiness -= 2
    if k > 0:
        unhappiness = 1
print(n-unhappiness)
Buggy code:
N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
ans2 = ans2 +(2*K if len(a)*(len(a)+1)//2>=K else len(a)*(len(a)+1)//2*2)
print(ans2)


Fixed code:
N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
#print(len(a)//2*2-1)
if len(a)!=1:
  ans2 = ans2 +(2*K)
print(min(ans2,len(S)-1))
Buggy code:
N ,K =map(int,input().split())
S ="R" +input() +"L"
coun = 0
foo = 0
for i in range(N+1):
    if S[i:i+2] =="RL":
        coun += 1
    if i==0 or i==N:
        foo  +=1
if K >= coun:
    print(N)
elif coun -K <= foo:
    print(N-foo)
else:
    print(N-2*(coun-K) +foo)

Fixed code:
N ,K =map(int,input().split())
S ="R" +input() +"L"
coun = 0
foo = 0
for i in range(N+1):
    if S[i:i+2] =="RL":
        coun += 1
        if i==0 or i==N:
            foo  +=1
if K >= coun:
    print(N-1)
elif coun -K <= foo:
    print(N-(coun-K))
else:
    print(N-2*(coun-K) +foo)
Buggy code:
def count_happiness(S):
    happiness = 0
    for n in range(N):
        if(S[n] == "L" and n > 0 and S[n] == S[n-1]):
            happiness += 1
        elif(S[n] == "R" and n < N -1 and S[n] == S[n+1]):
            happiness += 1
    return happiness

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

LR_RL_unhappy_pair = 0
LR_unhappy = 0
RL_unhappy = 0
for n in range(N-1):
    if(S[n] == "R" and S[n+1] == "L"):
        RL_unhappy += 1
    if(S[n] == "L" and S[n+1] == "R"):
        LR_unhappy += 1

change_pair_count  = min(K, min(RL_unhappy, LR_unhappy))
change_single_count = 0
if(K > change_pair_count):
    remain = K - change_pair_count
    changable = abs(RL_unhappy  - LR_unhappy)
    change_single_count = min(RL_unhappy, changable) 
print(count_happiness(S) + 2 * change_pair_count + change_single_count)

Fixed code:
def count_happiness(S):
    happiness = 0
    for n in range(N):
        if(S[n] == "L" and n > 0 and S[n] == S[n-1]):
            happiness += 1
        elif(S[n] == "R" and n < N -1 and S[n] == S[n+1]):
            happiness += 1
    return happiness

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

LR_RL_unhappy_pair = 0
LR_unhappy = 0
RL_unhappy = 0
for n in range(N-1):
    if(S[n] == "R" and S[n+1] == "L"):
        RL_unhappy += 1
    if(S[n] == "L" and S[n+1] == "R"):
        LR_unhappy += 1

change_pair_count  = min(K, min(RL_unhappy, LR_unhappy))
change_single_count = 0
if(K > change_pair_count):
    remain = K - change_pair_count
    changable = abs(RL_unhappy  - LR_unhappy)
    change_single_count = min(remain, changable) 
print(count_happiness(S) + 2 * change_pair_count + change_single_count)
Buggy code:
N, K = map(int, input().split())
S = input()

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

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

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

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


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

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

hikkuri = 0

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

shiawase = len(S) - hikkuri - 1

for i in range(K):
    if hikkuri >= 2:
        shiawase += 2
        hikkuri -= 2
    elif hikkuri == 1:
        shiawase += 1
        break

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

hikkuri = 0

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

shiawase = len(S) - hikkuri - 1

for i in range(K):
    if hikkuri >= 2:
        shiawase += 2
        hikkuri -= 2
    elif hikkuri == 1:
        shiawase += 1
        break

print(shiawase)

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

hikkuri = 0

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

shiawase = len(S) - hikkuri - 1

for i in range(K):
    if hikkuri >= 2:
        shiawase += 2
        hikkuri -= 2
    elif hikkuri == 1:
        shiawase += 1
        break

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

Fixed code:
n,k=map(int,input().split())
s=input()
ct=0
for i in range(n-1):
  if s[i]==s[i+1]:ct+=1
print(min(ct+2*k,n-1))
Buggy code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
if s[0]==s[n-1]:
  x=ct
  ct-=min(ct,k)
  ct*=2
  ct+=1
  print(n-ct)
elif s[0]=='L':
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<k:
    ct-=1
  print(n-ct)
else:
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<=k
    ct+=1
  print(n-ct)

Fixed code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
if s[0]==s[n-1]:
  x=ct
  ct-=min(ct,k)
  ct*=2
  ct+=1
  print(n-ct)
elif s[0]=='L':
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<k:
    ct+=1
  else:
    ct+=2
  print(n-ct)
else:
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<=k:
    ct+=1
  print(n-ct)
Buggy code:
n, k = map(int,input().split())
s = input()

press = []
temp = s[0]
cnt = 0
for string in s:
    if temp == string:
        cnt += 1
    else:
        press.append(cnt)
        temp = string
        cnt = 1
press.append(cnt)

if len(press) - 2 * k < 0:
    print(n-1)
    exit()

ans = 2 * k
for i in press:
    ans += i - 1
print(ans)

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

press = []
temp = s[0]
cnt = 0
for string in s:
    if temp == string:
        cnt += 1
    else:
        press.append(cnt)
        temp = string
        cnt = 1
press.append(cnt)

if len(press) - 2 * k <= 0:
    print(n-1)
    exit()

ans = 2 * k
for i in press:
    ans += i - 1
print(ans)
Buggy code:
N, K = map(int, input().split())
S = input()

Llist = []
Rlist = []
now = S[0]
cnt = 0

for i in range(1, N+1):
    if i < N and now == S[i]:
        cnt += 1
    else:
        if now == 'L':
            Llist.append(cnt)
        else:
            Rlist.append(cnt)
        if i < N:
            now = S[i]
            cnt = 0

length = min(len(Llist), len(Rlist))
ans = sum(Llist) + sum(Rlist)
print(Llist, Rlist, length, ans)
if K == 0:
    print(ans)
else:
    for i in range(min(K, length)):
        length -= 1
        if length == 0 and len(Llist) != len(Rlist):
            ans += 1
        else:
            ans += 2
    print(ans)

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

Llist = []
Rlist = []
now = S[0]
cnt = 0

for i in range(1, N+1):
    if i < N and now == S[i]:
        cnt += 1
    else:
        if now == 'L':
            Llist.append(cnt)
        else:
            Rlist.append(cnt)
        if i < N:
            now = S[i]
            cnt = 0

length = min(len(Llist), len(Rlist))
ans = sum(Llist) + sum(Rlist)
#print(Llist, Rlist, length, ans)
if K == 0:
    print(ans)
else:
    for i in range(min(K, length)):
        length -= 1
        if len(Llist) != len(Rlist):
            ans += 2
        else:
            if length == 0:
                ans += 1
            else:
                ans += 2
    print(ans)
Buggy code:
n,k=map(int,input())
s=[i for i in range input()]
num=0
for i in range(len(s)):
    if s[i]==s[i+1]:
        num+=1
print(min(n-1,num+2*k))

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

count = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        count += 1

ans = min(count + 2+K, N - 1)
print(ans)

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

count = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        count += 1

ans = min(count + 2*K, N - 1)
print(ans)
Buggy code:
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1

for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
ans = 0
if K >= len(Rs) - 2:
    ans = N-1
elif K == len(Rs) - 3:
    ans = N-2
else:
    ans = sum(Rs) + 2 * K

print(ans)

Fixed code:
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1
 
for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
Rs.append(count)
ans = 0

if S[0] == S[N-1]:
    if K >= (len(Rs)-1) // 2:
        ans = N-1
    else:
        ans = sum(Rs) + 2 * K
else:
    if K >= len(Rs) // 2:
        ans = N-1
    else:
        ans = sum(Rs) + 2 * K
  
print(ans)
Buggy code:
N,K = list(map(int,input().split()))
S = input()
Now = S[0]
switch = 0
happiness = 0
for i in range(N-1):
  if(S[i+1] == Now):
    happiness += 1
  if(S[i+1] != Now):
    Now = S[i+1]
    switch += 1
number = 0
while((switch > 1) and (number < K)):
  number += 1
  happiness += 2
  switch -= 2
print(happiness)

Fixed code:
N,K = list(map(int,input().split()))
S = input()
Now = S[0]
switch = 0
happiness = 0
for i in range(N-1):
  if(S[i+1] == Now):
    happiness += 1
  if(S[i+1] != Now):
    Now = S[i+1]
    switch += 1
number = 0
while((switch > 1) and (number < K)):
  number += 1
  happiness += 2
  switch -= 2
if((switch == 1) and (number < K)):
  happiness += 1
print(happiness)