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

H = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H += 2 * M
K -= M
N_RL -= M
N_LR -= M

s0 = S[0]
while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
    if s0 == 'R' and N_RL > 0:
        H += 1
        s0 = 'L'
        N_RL -= 1
        K -=1
    elif s0 == 'L' and N_LR > 0:
        H += 1
        s0 = 'R'
        N_LR -= 1
        K -=1

sN = S[N-1]
while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
    if sN == 'R' and N_LR > 0:
        H += 1
        sN = 'L'
        N_LR -= 1
        K -=1
    elif sN == 'L' and N_RL > 0:
        H += 1
        sN = 'R'
        N_RL -= 1
        K -=1

print(H)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H += 2 * M
K -= M
N_RL -= M
N_LR -= M

s0 = S[0]
while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
    if s0 == 'R' and N_RL > 0:
        H += 1
        s0 = 'L'
        N_RL -= 1
        K -=1
    elif s0 == 'L' and N_LR > 0:
        H += 1
        s0 = 'R'
        N_LR -= 1
        K -=1

sN = S[N-1]
while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
    if sN == 'R' and N_LR > 0:
        H += 1
        sN = 'L'
        N_LR -= 1
        K -=1
    elif sN == 'L' and N_RL > 0:
        H += 1
        sN = 'R'
        N_RL -= 1
        K -=1

print(H)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H += 2 * M
K -= M
N_RL -= M
N_LR -= M

s0 = S[0]
while K > 0:
    if s0 == 'R' and N_RL > 0:
        H += 1
        s0 = 'L'
        N_RL -= 1
        K -=1
    elif s0 == 'L' and N_LR > 0:
        H += 1
        s0 = 'R'
        N_LR -= 1
        K -=1

sN = S[N-1]
while K > 0:
    if sN == 'R' and N_LR > 0:
        H += 1
        sN = 'L'
        N_LR -= 1
        K -=1
    elif sN == 'L' and N_RL > 0:
        H += 1
        sN = 'R'
        N_RL -= 1
        K -=1

print(H)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H0 = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H0 += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H0 += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H0 += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H1 = H0 + 2 * M
K -= M
N_RL -= M
N_LR -= M

result = H1 + min(K, max(N_RL, N_LR))
print(result)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H0 = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H0 += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H0 += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H0 += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H1 = H0 + 2 * M
K -= M
N_RL -= M
N_LR -= M

result = H1 + min(K, max(N_RL, N_LR))
print(result)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H0 = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H0 += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H0 += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H0 += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H1 = H0 + 2 * M
K -= M
N_RL -= M
N_LR -= M

result = H1 + min(K, max(N_RL, N_LR))
print(result)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)

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

H0 = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H0 += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H0 += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H0 += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H1 = H0 + 2 * M
K -= M
N_RL -= M
N_LR -= M

result = H1 + min(K, N_RL, N_LR)
print(result)


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

if N == 1:
    print(0)
else:
    H = 0
    N_LR = 0
    N_RL = 0

    if S[0] == 'R' and S[1] == 'R':
        H += 1
    if S[0] == 'R' and S[1] == 'L':
        N_RL += 1
    if S[0] == 'L' and S[1] == 'R':
        N_LR += 1

    if S[N-1] == 'L' and S[N-2] == 'L':
        H += 1

    for i in range(1, N-1):
        if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
            H += 1
        if S[i] == 'R' and S[i+1] == 'L':
            N_RL += 1
        if S[i] == 'L' and S[i+1] == 'R':
            N_LR += 1

    M = min(N_RL, N_LR, K)
    H += 2 * M
    K -= M
    N_RL -= M
    N_LR -= M

    s0 = S[0]
    while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
        if s0 == 'R' and N_RL > 0:
            H += 1
            s0 = 'L'
            N_RL -= 1
            K -=1
        elif s0 == 'L' and N_LR > 0:
            H += 1
            s0 = 'R'
            N_LR -= 1
            K -=1

    sN = S[N-1]
    while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
        if sN == 'R' and N_LR > 0:
            H += 1
            sN = 'L'
            N_LR -= 1
            K -=1
        elif sN == 'L' and N_RL > 0:
            H += 1
            sN = 'R'
            N_RL -= 1
            K -=1

    print(H)