Problem:


Score : 400 points

Problem Statement

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?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7



Sample input:
6 1
LRLRRL


Sample output:
3

Submission status: Wrong Answer

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

see = s[0]
cnt = 0
cluster = 1
for i in range(1, n):
    if see == s[i]:
        cnt += 1
    else:
        see = s[i]
        cluster += 1

for i in range(k):
    if cluster > 2:
        cluster -= 1
        cnt += 2
    else:
        print(n - 1)
        exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
# D
n, k = map(int ,input().split())
s  = input()
countX = 0
countY = 0
ans = n
if s[0] ==  'L':
    ans -= 1
    countY += 1
if s[-1] == 'R':
    ans -= 1
    countY += 1
for i in range(n-1):
    if s[i] == 'R' and s[i+1] == 'L':
        countX += 1
        ans -= 2
for i in range(k,0,-1):
    if countX > 0:
        ans += 2
        countX -= 1
    if countX <= 0 and countY > 0:
        ans += 1
        countY -= 1
print(ans)

Submission status: Wrong Answer

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

l=-1
r=n+1
for i in range(k):
  for j in range(l+1,n-1):
    if s[j:j+2]=='LR' or s[j:j+1]=='RL':
      l=j+1
      break
  for j in range(r-1,-1,-1):
    if s[j-2:j]=='LR' or s[j-2:j]=='RL':
      r=j-1
      break
  tmp=s[l:r]
  tmp=tmp.replace('L','r').replace('R','L').replace('r','R')
  s=s[:l]+tmp[::-1]+s[r:]
ans=0
for i in range(n-1):
  if s[i]==s[i+1]:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K= [int(i) for i in input().split()]
Sn = [1] + [0 if i =="L" else 1 for i in input()] + [0]


init_happiness = 0
change_count2 = 0
change_count1 = 0
for n in range(1, N+1):
  if Sn[n] == 0 and Sn[n] == Sn[n-1]:
    init_happiness += 1
  elif Sn[n] == 1 and Sn[n] == Sn[n+1]:
    init_happiness += 1
    
  if Sn[n] == 1 and Sn[n+1] == 0:
    if n == 1 or n == N:
      change_count1 += 1
    else:
      change_count2 += 1

change2 = min(change_count2, K)
change1 = min(change_count1, max(K - change2, 0))
print(init_happiness + 2 * change2 + change1)

Submission status: Wrong Answer

Solution code:
N, K= [int(i) for i in input().split()]
Sn = [1] + [0 if i =="L" else 1 for i in input()] + [0]


init_happiness = 0
change_count2 = 0
change_count1 = 0
for n in range(1, N+1):
  if Sn[n] == 0 and Sn[n] == Sn[n-1]:
    init_happiness += 1
  elif Sn[n] == 1 and Sn[n] == Sn[n+1]:
    init_happiness += 1
    
  if Sn[n] == 1 and Sn[n+1] == 0:
    if n == 1 or n == N:
      change_count1 += 1
    else:
      change_count2 += 1

change2 = min(change_count2, K)
change1 = min(1, change_count1, max(K - change2, 0))
    
print(init_happiness + 2 * change2 + change1)

Submission status: Wrong Answer

Solution code:
N, K= [int(i) for i in input().split()]
Sn = [1] + [0 if i =="L" else 1 for i in input()] + [0]


init_happiness = 0
change_count = 0
for n in range(1, N+1):
  if Sn[n] == 0 and Sn[n] == Sn[n-1]:
    init_happiness += 1
  elif Sn[n] == 1 and Sn[n] == Sn[n+1]:
    init_happiness += 1
  if Sn[n] == 1 and Sn[n+1] == 0:
    change_count +=1

    
change2 = min(max(0, change_count -1), K)

change1 = 1 if change2 < K else 0
print(init_happiness + 2 * change2 + change1)

Submission status: Wrong Answer

Solution code:
N, K= [int(i) for i in input().split()]
Sn = [1] + [0 if i =="L" else 1 for i in input()] + [0]


init_happiness = 0
change_count = 0
for n in range(1, N+1):
  if Sn[n] == 0 and Sn[n] == Sn[n-1]:
    init_happiness += 1
  elif Sn[n] == 1 and Sn[n] == Sn[n+1]:
    init_happiness += 1
  if Sn[n] == 1 and Sn[n+1] == 0:
    change_count +=1

    
change2 = min(max(0, change_count -1), K)

change1 = change_count %  2 if change2 < K else 0
print(init_happiness + 2 * change2 + change1)

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

RL=0
LR=0
unhappy=0

for i in range(0,N):
    if S[i]=='R' and S[i+1]=='L':
        RL+=1
    elif S[i]=='L' and S[i+1]=='R':
        LR+=1

decrease=min(LR,K)
if RL>=decrease:
    RL-=decrease
    K-=decrease
    LR=0
    if S[0]=='R':
        LR+=1
    if S[N-1]=='L':
        LR+=1
    seconddecreasable=min(K,LR)
    unhappy=2*RL
    if S[0]=='L':
        unhappy+=1
    if S[N-1]=='R':
        unhappy+=1
    print(N-unhappy+seconddecreasable)
elif RL==decrease-1:
    print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

RL=0
LR=0
unhappy=0

for i in range(0,N):
    if S[i]=='R' and S[i+1]=='L':
        RL+=1
    elif S[i]=='L' and S[i+1]=='R':
        LR+=1

decrease=min(LR,K)
if RL>=decrease:
    RL-=decrease
    unhappy=2*RL
    if S[0]=='L':
        unhappy+=1
    if S[N-1]=='R':
        unhappy+=1
    print(N-unhappy)
elif RL==decrease-1:
    print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

RL=0
LR=0
unhappy=0

for i in range(0,N):
    if S[i]=='R' and S[i+1]=='L':
        RL+=1
    elif S[i]=='L' and S[i+1]=='R':
        LR+=1

decrease=min(LR,K)
if RL>=decrease:
    RL-=decrease
    unhappy=2*RL
    if S[0]=='L':
        unhappy+=1
    if S[N-1]=='R':
        unhappy+=1
    print(N-unhappy)
elif RL==decrease-1:
    unhappy=0
    test=0
    if S[0]=='L':
        unhappy+=1
    if S[N-1]=='R':
        unhappy+=1
    print(N-max(0,unhappy-1))
else:
    print(N)

Submission status: Wrong Answer

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

how_many=[]
LR=[]
 
counter=0
for i in range(1,n):
    counter+=1
    if s[i-1]!=s[i]:
        how_many.append(counter)
        LR.append(s[i-1])
        counter=0
how_many.append(counter+1)
LR.append(s[-1])

ans=0
accumulate=[0]
for i in range(len(how_many)):
    accumulate.append(accumulate[-1]+how_many[i])

max_left=0
max_right=0
for g in range(len(LR)):
    l=g
    if LR[l]=="L":
        r=min(g+2*k,len(LR))
    else:
        r=min(g+2*k+1,len(LR))
    if ans<=accumulate[r]-accumulate[l]:
        ans=accumulate[r]-accumulate[l]
        max_left=l
        max_right=r

remain=how_many[:max_left]+how_many[max_right:]
print(ans-1+sum(remain)-len(remain))

Submission status: Wrong Answer

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

how_many=[]
LR=[]
 
counter=0
for i in range(1,n):
    counter+=1
    if s[i-1]!=s[i]:
        how_many.append(counter)
        LR.append(s[i-1])
        counter=0
how_many.append(counter+1)
LR.append(s[-1])

ans=0
accumulate=[0]
for i in range(len(how_many)):
    accumulate.append(accumulate[-1]+how_many[i])

max_left=0
max_right=0
for g in range(len(LR)):
    l=g
    if LR[l]=="R":
        r=min(g+2*k,len(LR))
    else:
        r=min(g+2*k+1,len(LR))
    if ans<=accumulate[r]-accumulate[l]:
        ans=accumulate[r]-accumulate[l]
        max_left=l
        max_right=r

remain=how_many[:max_left]+how_many[max_right:]
print(ans-1+sum(remain)-len(remain))

Submission status: Wrong Answer

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

not_happy = 0
R = 0
R_edge = 0

for i in range (N-1):
    if S[i] == "R" and S[i+1] == "L":
        R += 1

if S[0] == "L":
    R_edge +=1
if S[N-1] == "R":
    R_edge +=1

#print(R)
#print(R_edge)

if K <= R:
    R -= K
elif K <= R + R_edge:
    R = 0
    R_edge -= K - R
else:
    R = 0
    R_edge = 0

not_happy = R*2 + R_edge
happy = N - not_happy

#print(R)
#print(R_edge)
#print(not_happy)
print(happy)

Submission status: Wrong Answer

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

unhappy = 0
if S[0] == "L":
    unhappy += 1
if S[-1] == "R":
    unhappy += 1
for i in range(1, N-1):
    if S[i:i+2] == "RL":
        unhappy += 2

if unhappy > 2:
    if S[0]=="L" and S[-1]=="R":
        if unhappy-2 > 2*K:
            print(N-(unhappy-2*K))
        else:
            print(N-1)
    else:
        if unhappy > 2*K:
            print(N-(unhappy-2*K))
        else:
            print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

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

unhappy = 0
if S[0] == "L":
    unhappy += 1
if S[-1] == "R":
    unhappy += 1
for i in range(1, N-1):
    if S[i:i+2] == "RL":
        unhappy += 2

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

Submission status: Wrong Answer

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

unhappy = 0
if S[0] == "L":
    unhappy += 1
if S[-1] == "R":
    unhappy += 1
for i in range(1, N-1):
    if S[i:i+2] == "RL":
        unhappy += 2

if unhappy > 2:
    if unhappy > 2*K:
        print(N-(unhappy-2*K))
    else:
        print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

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

unhappy = 0
if S[0] == "L":
    unhappy += 1
if S[-1] == "R":
    unhappy += 1
for i in range(1, N-1):
    if S[i:i+2] == "RL":
        unhappy += 2

if unhappy > 2:
    if unhappy > K:
        print(N-(unhappy-2*K))
    else:
        print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

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

unhappy = 0
if S[0] == "L":
    unhappy += 1
if S[-1] == "R":
    unhappy += 1
for i in range(1, N-1):
    if S[i:i+2] == "RL":
        unhappy += 2

if unhappy > 3:
    if unhappy > K:
        print(N-(unhappy-2*K))
    else:
        print(N-1)
else:
    print(N-1)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
S = input()
if N == 1:
    print(0)
else:
    lc = S.count('L')
    rc = N-lc
    maxc = lc+rc-2
    nowc = 0
    now = S[0]
    for i in range(1,N):
        if S[i] == now:
            nowc += 1
        else:
            now = S[i]
    print(min(nowc+2*K,maxc))

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
S = input()
lc = S.count('L')
rc = N-lc
maxc = lc+rc-2
nowc = 0
now = S[0]
for i in range(1,N):
    if S[i] == now:
        nowc += 1
    else:
        now = S[i]
print(min(nowc+2*K,maxc))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = []
prev = "-"
cnt = 0
for i in input():
    if prev!=i:
        s.append(cnt)
        cnt = 0
    else:
        cnt += 1
    prev = i
s.append(cnt)
ans = sum(s)
if len(s)-1==k+1: ans -= 1
elif len(s)-1<=k: ans -= 2
print(ans + 2*min(k, len(s)))

Submission status: Wrong Answer

Solution code:
def run_length(s) -> list:
  res = [[s[0], 1]]
  for i in range(1, len(s)):
    if res[-1][0] == s[i]:
      res[-1][1] += 1
    else:
      res.append([s[i], 1])
  return res

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

RL = run_length(S)

tmp = 0
for r, l in RL:
  tmp += l - 1

if K >= len(RL) - 1:
  answer = N - 1
else:
  answer = tmp + K * 2
  
print(answer)

Submission status: Wrong Answer

Solution code:
def run_length(s) -> list:
  res = [[s[0], 1]]
  for i in range(1, len(s)):
    if res[-1][0] == s[i]:
      res[-1][1] += 1
    else:
      res.append([s[i], 1])
  return res

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

RL = run_length(S)

tmp = 0
for r, l in RL:
  tmp += l - 1

if K >= len(RL) - 2:
  answer = N - 1
else:
  answer = tmp + K * 2
  
print(answer)

Submission status: Wrong Answer

Solution code:


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

L = []

tmp = None
cnt = 0
for s in S:
  if tmp == None:
    tmp = s
    cnt = 1
  else:
    if tmp != s:
      L.append((cnt, tmp))
      cnt = 1
      tmp = s
    else:
      cnt += 1
else:
  L.append((cnt, tmp))


ll = len(L)
ans = 0

for i in range(len(L)):
  cnt, s = L[i]
  ans += cnt - 1
 
for i in range(K):
  if ll == 1:
    break
  if ll == 2:
    ans += 1
  else:
    ans += 2

print(ans)

Submission status: Wrong Answer

Solution 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]:
    ans += 1
print(ans + 2 * min(k, two))

Submission status: Wrong Answer

Solution 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]:
    ans += 1
print(ans + 2 * min(k, two))

Submission status: Wrong Answer

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

ans = 0
c = 0

pre = ''
for s in S:
    if s == 'L':
        if pre == 'L':
            ans += 1
        else:
            pre = s
            c += 1
    else:
        if pre == 'R':
            ans += 1
        else:
            pre = s

cc = 0
if S[0] == 'L':
    cc += 1
if S[-1] == 'R':
    cc += 1

if c <= K:
    ans += 2 * c
    K -= c
    c = 0
else:
    ans += 2 * K
    c -= K
    K = 0

if c == 0:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

ans = 0
c = 0

pre = ''
for s in S:
    if s == 'L':
        if pre == 'L':
            ans += 1
        else:
            pre = s
            c += 1
    else:
        if pre == 'R':
            ans += 1
        else:
            pre = s

cc = 0
if S[0] == 'L':
    cc += 1
if S[-1] == 'R':
    cc += 1

if c <= K:
    ans += 2 * c
    K - c
else:
    ans += 2 * K
    K = 0
if cc <= K:
    ans += cc
else:
    ans += K

print(ans)

Submission status: Wrong Answer

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

happiness = 0

li = []
lamp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        lamp += 1
    else:
        li.append(lamp)
        lamp = 1
li.append(lamp)

flag = False
num = 1
for q in range(k):
    if len(li) == 2 and k > 0:
        print(n-1)
        quit()
    for i in range(1, len(li)-1):
        if li[i] == num:
            a = li[i-1] + li[i] + li[i+1]
            li[i-1:i+2] = [a]
            k -= 1
            flag = True
            break
        flag = False
    if flag == False:
        num += 1
    if len(li) == 2:
        break

for i in li:
    happiness += i-1

print(happiness)

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in input().split())
S = list(input())
i = 0
ans = N

while i < len(S)-1:
    if S[i] == 'R' and S[i+1] == 'L':
        if K == 0:
            ans -= 2
        else:
            K -= 1

    i += 1

if S[0] == 'L':
    if K > 0:
        K -= 1
    else:
        ans -= 1

if S[N-1] == 'R':
    if K > 0:
        K -= 1
    else:
        ans -= 1


if N == 1:
    ans = 0


print(ans)

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in input().split())
S = list(input())
i = 0
ans = N

while i < len(S)-1:
    if S[i] == 'R' and S[i+1] == 'L':
        if K == 0:
            if ans > 0:
                ans -= 2
                if ans < 0:
                    ans = 0
        else:
            K -= 1

    i += 1

if S[0] == 'L':
    if K > 0:
        K -= 1
    else:
        if ans > 0:
            ans -= 1

if S[N-1] == 'R':
    if K > 0:
        K -= 1
    else:
        if ans > 0:
            ans -= 1   

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in input().split())
S = list(input())
i = 0
ans = N

while i < len(S)-1:
    if S[i] == 'R' and S[i+1] == 'L':
        if K == 0:
            ans -= 2
        else:
            K -= 1

    i += 1

if S[0] == 'L':
    if K > 0:
        K -= 1
    else:
        ans -= 1

if S[N-1] == 'R':
    if K > 0:
        K -= 1
    else:
        ans -= 1   

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(N):
    if S[i] == 'L':
        if i - 1 < 0:
            continue
        if S[i - 1] == 'L':
            ans += 1
    else:
        if i + 1 >= N:
            continue
        if S[i + 1] == 'R':
            ans += 1


X = (len([s for s in S.split('L') if s != '']) + len([s for s in S.split('R') if s != '']))
ans += min(X, K) * 2
X -= K

if X <= 0:
    ans -= 2
elif X == 1:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
e=(S[0]=='L')+(S[-1]=='R')
i=S.count("RL")
c=max(i-K,0)*2+max(e-max(K-i,0),0)

print(N-c)

Submission status: Wrong Answer

Solution code:
nk=list(map(int,input().split()))
s=input()
L=[]
R=[]
lr="a"
for i in range(nk[0]):
    if lr=="L":
        if s[i]=="R":
            lr="R"
            R.append(1)
        else:
            L[-1]+=1
    elif lr=="R":
        if s[i]=="L":
            lr="L"
            L.append(1)
        else:
            R[-1]+=1
    else:
        lr=s[0]
        if lr=="L":
            L.append(1)
        else:
            R.append(1)
L.sort(reverse=True)
R.sort(reverse=True)
lsum=sum(L[:nk[1]])
rsum=sum(R[:nk[1]])
if len(L)<=nk[1] or len(R)<=nk[1]:
    print(nk[0]-1)
else:
    if lsum+sum(R)>rsum+sum(L):
        rmerge=sum(R[:nk[1]+1])+lsum
        R=R[nk[1]:]
        if R!=[]:
            R[0]=rmerge
        else:
            R=[rmerge]
        ans=sum(R)-len(R)
    else:
        lmerge=sum(L[:nk[1]+1])+rsum
        L=L[nk[1]:]
        if L!=[]:
            L[0]=lmerge
        else:
            L=[lmerge]
        ans=sum(L)-len(L)
    print(ans)

Submission status: Wrong Answer

Solution code:
nk=list(map(int,input().split()))
s=input()
L=[]
R=[]
lr="a"
for i in range(nk[0]):
    if lr=="L":
        if s[i]=="R":
            lr="R"
            R.append(1)
        else:
            L[-1]+=1
    elif lr=="R":
        if s[i]=="L":
            lr="L"
            L.append(1)
        else:
            R[-1]+=1
    else:
        lr=s[0]
        if lr=="L":
            L.append(1)
        else:
            R.append(1)
L.sort(reverse=True)
R.sort(reverse=True)
lsum=sum(L[:nk[1]])
rsum=sum(R[:nk[1]])
if lsum+sum(R)>rsum+sum(L):
    rmerge=sum(R[:nk[1]+1])+lsum
    R=R[nk[1]:]
    if R!=[]:
        R[0]=rmerge
    else:
        R=[rmerge]
    ans=sum(R)-len(R)
else:
    lmerge=sum(L[:nk[1]+1])+rsum
    L=L[nk[1]:]
    if L!=[]:
        L[0]=lmerge
    else:
        L=[lmerge]
    ans=sum(L)-len(L)
print(ans)

Submission status: Wrong Answer

Solution code:
nk=list(map(int,input().split()))
s=input()
if "L" not in s or "R" not in s:
    print(nk[0])
else:
    L=[]
    R=[]
    lr="a"
    for i in range(nk[0]):
        if lr=="L":
            if s[i]=="R":
                lr="R"
                R.append(1)
            else:
                L[-1]+=1
        elif lr=="R":
            if s[i]=="L":
                lr="L"
                L.append(1)
            else:
                R[-1]+=1
        else:
            lr=s[0]
            if lr=="L":
                L.append(1)
            else:
                R.append(1)
    L.sort(reverse=True)
    R.sort(reverse=True)
    lsum=sum(L[:nk[1]])
    rsum=sum(R[:nk[1]])
    if lsum>rsum:
        rmerge=sum(R[:nk[1]+1])+lsum
        R=R[nk[1]:]
        if R!=[]:
            R[0]=rmerge
        else:
            R=[rmerge]
        ans=sum(R)-len(R)
    else:
        lmerge=sum(L[:nk[1]+1])+rsum
        L=L[nk[1]:]
        if L!=[]:
            L[0]=lmerge
        else:
            L=[lmerge]
        ans=sum(L)-len(L)
    print(ans)

Submission status: Wrong Answer

Solution code:
nk=list(map(int,input().split()))
s=input()
L=[]
R=[]
lr="a"
for i in range(nk[0]):
    if lr=="L":
        if s[i]=="R":
            lr="R"
            R.append(1)
        else:
            L[-1]+=1
    elif lr=="R":
        if s[i]=="L":
            lr="L"
            L.append(1)
        else:
            R[-1]+=1
    else:
        lr=s[0]
        if lr=="L":
            L.append(1)
        else:
            R.append(1)
L.sort(reverse=True)
R.sort(reverse=True)
lsum=sum(L[:nk[1]])
rsum=sum(R[:nk[1]])
if lsum>rsum:
    rmerge=sum(R[:nk[1]+1])+lsum
    R=R[nk[1]:]
    if R!=[]:
        R[0]=rmerge
    else:
        R=[rmerge]
    ans=sum(R)-len(R)
else:
    lmerge=sum(L[:nk[1]+1])+rsum
    L=L[nk[1]:]
    if L!=[]:
        L[0]=lmerge
    else:
        L=[lmerge]
    ans=sum(L)-len(L)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' and s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      break
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' or s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      break
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')-1
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' or s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      break
print(cnt)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

N,K = map(int,input().split())
S = input()
gnum = len(list(groupby(S)))
if gnum-1<=K:
  print(N-1)
elif gnum-2==K:
  print(N-2)
else:
  print(N-(gnum-2*K))

Submission status: Wrong Answer

Solution 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)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
x = 0
y = 0
firsthappiness = 0
for i in range(n-1):
    if s[i]+s[i+1] == "RL":
        x+=1
    if s[i] == s[i+1]:
        firsthappiness += 1
if s[0] == "L":
    y += 1
if s[n-1] == "R":
    y += 1
for i in range(k):
    if x > 0:
        x -= 1
        firsthappiness+=2
        continue
    if y > 0:
        y -= 1
        firsthappiness += 1
print(firsthappiness)

Submission status: Wrong Answer

Solution code:
n, k = [int(_) for _ in input().split()]
s = input()
c = 0
ans = 0
new_s = '' + s[0]
for i in range(1, n):
    if s[i] == s[i - 1]:
        c += 1
    else:
        ans += c
        c = 0
        new_s += s[i]
if c != 0:
    ans += c

length = len(new_s)
l = []
if length == 2:
    l = [1]
if length >= 3:
    if length % 2 == 1:
        l = [2] * (length // 2)
    else:
        l.append(1)
        l = [2] * ((length // 2) - 1)

if len(l) >= k:
    for i in range(k):
        ans += l.pop()
else:
    for i in range(len(l)):
        ans += l.pop()
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N,K=LI()
S=input()

cnt = 0
lst = None
RNGS=[]
for i in range(N):
    if lst and lst != S[i]:
        RNGS.append((lst, cnt))
        cnt = 0
    cnt += 1
    lst = S[i]
RNGS.append((lst, cnt))

# print(RNGS)

mod_cnt = 0
if len(RNGS) >= 3:
    for i in range(0, len(RNGS)-2, 2):
        if mod_cnt < K:
            lr, num = RNGS[i+2]
            lr = 'L' if lr ==  'R' else 'R'
            mod_cnt += 1
            RNGS[i+2] = (lr, num)

# print(RNGS)
if mod_cnt < K:
    print(len(S) - 1)
else:
    prev = None
    res = 0
    for i in range(len(RNGS)):
        lr, num = RNGS[i]
        res += num
        if not prev or prev != lr:
            res -= 1
        prev = lr
    print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
s = input()

rl = 0
for i in range(n-1):
    if s[i:i+2]=="RL":
        rl += 1

if k>=rl:
    print(n-1)
else:
    res = n - 2*(rl-k)
    if s[0]=="L":
        res -= 1
    if s[n-1]=="R":
        res -= 1

    print(res)

Submission status: Wrong Answer

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

happy_count = 0
pre = ''
for index in range(n - 1):
  if index == 0:
    continue
  if s[index - 1] == s[index]:
    happy_count += 1

print(min(happy_count + (2 * k), n-1))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def count(S):
	N = len(S)
	cnt = 0
	for i, s in enumerate(S):
		if i > 0 and s == "L" and S[i-1] == "L":
			cnt += 1
		elif i < N-1 and s == "R" and S[i+1] == "R":
			cnt += 1
	return cnt
	ans = max(ans, cnt)

def main():
	N, K = MAP()
	STR = input().rstrip()
	STR = [s for s in STR]
	# print(STR)

	ans = 0
	for num in range(2):
		S = STR[:]
		if not num%2: target = "L"
		else: target = "R"
		if not S.count(target): continue
		cand = []
		tmp = [-1,-1,-1,-1]
		for i, s in enumerate(S):
			# print(i, s)
			if s == target and tmp[0] == -1:
				tmp[0] = i
			elif (s != target or i == N-1) and tmp[0] != -1:
				tmp[1] = i if i != N-1 else i+1
				tmp[2] = i-tmp[0] if i != N-1 else i+1-tmp[0]
				if tmp[0]==0 or tmp[1]==N-1:
					tmp[3] = i-tmp[0]
				else:
					tmp[3] = i-tmp[0]+1
				cand.append(tmp)
				tmp = [-1,-1,-1,-1]

		cand.sort(key=lambda x:x[3], reverse=True)
		# print(cand)
		if not cand: continue
		
		s = "L" if target == "R" else "R"
		K = min(K, len(cand))
		for i in range(K):
			fr, to, num, _ = cand[i]
			S[fr:to] = s*num
		# print(S)

		cnt = count(S)
		# print(S, "-", s,  "-", cnt)
		ans = max(ans, cnt)

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N, K = MAP()
	STR = input().rstrip()
	STR = [s for s in STR]

	ans = 0
	for num in range(2):
		S = STR[:]
		if not num%2: target = "L"
		else: target = "R"
		if not S.count(target): continue
		cand = []
		tmp = [-1,-1,-1,-1]
		for i, s in enumerate(S):
			# print(i, s)
			if s == target and tmp[0] == -1:
				tmp[0] = i
			elif (s != target or i == N-1) and tmp[0] != -1:
				tmp[1] = i if i != N-1 else i+1
				tmp[2] = i-tmp[0] if i != N-1 else i+1-tmp[0]
				if target=="L" and tmp[0]==0 or target=="R" and tmp[1]==N-1:
					tmp[3] = i-tmp[0]
				else:
					tmp[3] = i-tmp[0]+1
				cand.append(tmp)
				tmp = [-1,-1,-1,-1]

		cand.sort(key=lambda x:x[3], reverse=True)
		# print(cand)
		if not cand: continue
		
		s = "L" if target == "R" else "R"
		K = min(K, len(cand))
		for i in range(K):
			fr, to, num, _ = cand[i]
			S[fr:to] = s*num
		# print(S)

		cnt = 0
		for i, s in enumerate(S):
			if i > 0 and s == "L" and S[i-1] == "L":
				cnt += 1
			elif i < N-1 and s == "R" and S[i+1] == "R":
				cnt += 1
		# print(cnt)
		ans = max(ans, cnt)

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')-1
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' or s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' or s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
x=s.count('RL')
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L':
  y+=1
if s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 2
print(min(now + k * 2, min(l, r) * 2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 2
print(min(now + k * 2, min(l, r)))

Submission status: Wrong Answer

Solution code:
import re

n,k = input().split(" ")
n = int(n)
k = int(k)

content = input()
start = 0
end = 0

for i in range(k):
  lrPoint = content.find('LR')
  rlPoint = content.find('RL')
  print("lrPoint" ,lrPoint)
  print("rlPoint" , rlPoint)
  if lrPoint < rlPoint:
    start = lrPoint+1
    end = rlPoint
  else:
    start = rlPoint+1
    end = lrPoint

  content = list(content)
  tmp = content[start:end+1]
  tmp.reverse()
  for i in range(len(tmp)):
    if 'L' == tmp[i]:
      tmp[i] = 'R'
    else:
      tmp[i] = 'L'
  content[start:end+1] = tmp
  content = "".join(content)
  print(content)
 
count = 0

for i in range(len(list(content)) - 1):
  if content[i] == content[i+1]:
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 2
print(min(now + k, min(l, r)))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 1
print(min(now + k, min(l, r)))