Problem: There are N people standing in a queue from west to east.  Given is a string S of length N representing the directions of the people. The i-th person from the west is facing west if the i-th character of S is `L`, and east if that character of S is `R`.  A person is happy if the person in front of him/her is facing the same direction. If no person is standing in front of a person, however, he/she is not happy.  You can perform the following operation any number of times between 0 and K (inclusive):  Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.  What is the maximum possible number of happy people you can have?

Submission attempts (in order):
# -*- coding:utf-8 -*-




def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL = 0  
    RL, LR = 0, 0  
    happy_num = 0  

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1 if endRL == 0 else 0
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            LR += 1

        
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)



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

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

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


if __name__ == "__main__":
    solve()


# -*- coding:utf-8 -*-




def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL, endLR = 0, 0  
    RL, LR = 0, 0  
    happy_num = 0  

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            if (i-1)==0 or i==(M-1):
                endLR += 1
            else:
                LR += 1

        
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
            if K >= min(RL, endLR):
                happy_num += min(RL, endLR)
            else:
                happy_num += K
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)



if __name__ == "__main__":
    solve()


def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL = 0  
    RL, LR = 0, 0  
    happy_num = 0  

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            LR += 1

        
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)

if __name__ == "__main__":
    solve()


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

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

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


if __name__ == "__main__":
    solve2()


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted