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:
# https://atcoder.jp/contests/abc140/tasks/abc140_d
# D - Face Produces Unhappiness
import bisect
N, K = list(map(int, input().split()))
S = input().split()[0]

p = 0
lr_index_list = []
rl_index_list = []
for i in range(1, N):
    if S[i] == S[i-1]:
        p += 1
    if S[i-1] + S[i] == 'LR':
        lr_index_list.append(i-1)
    if S[i-1] + S[i] == 'RL':
        rl_index_list.append(i-1)

n_lr = len(lr_index_list)
n_rl = len(rl_index_list)

if n_lr == 0 and n_rl == 0:
    pass
elif n_lr == 1 and n_rl == 0:
    if lr_index_list[0] != 0 and lr_index_list[0] != N - 2:
        # LLLLLRRRRR 系
        p = N - 1
elif n_lr == 0 and n_rl == 1:
    if rl_index_list[0] != 0 and rl_index_list[0] != N - 2:
        # RRRRRLLLLL 系
        p = N - 1
else:
    kouho_n = min(n_lr, n_rl)
    # 少ない方を基準で探す
    if kouho_n == n_lr:
        x_list = lr_index_list
        y_list = rl_index_list
        org_y_list = y_list[:]
    else:
        x_list = rl_index_list
        y_list = lr_index_list
        org_y_list = y_list[:]
    c = 0
    for x in x_list:
        y_index = bisect.bisect_left(y_list, x)
        is_ok = False
        # 1個右隣をチェック
        if y_index != len(y_list):
            if abs(y_list[y_index] - x) >= 2:
                is_ok = True
                y_list.remove(y_list[y_index])
        # 1個左隣をチェック
        if is_ok == False and y_index != 0:
            if abs(y_list[y_index-1] - x) >= 2:  # TODO
                is_ok = True
                y_list.remove(y_list[y_index-1])
        # 2個右隣があるかどうかをチェック
        if is_ok == False and y_index + 1 < len(y_list):
            is_ok = True
            y_list.remove(y_list[y_index+1])
        # 2個左隣があるかどうかをチェック
        if is_ok == False and y_index - 2 >= 0:  # TODO
            is_ok = True
            y_list.remove(y_list[y_index-2])
        if is_ok:
            c += 1
        if c >= K:
            break
    # y_listのあまりものが、最大or最小で
    # 最後尾の文字でなければ、そのインデックス以降 or 以前を変更できる
    p += min(c, K) * 2
    if K - c >= 1 and y_list:
        if max(y_list) == max(org_y_list) and max(y_list) != N - 2 and max(y_list) != 0:
            p += 1
            y_list.remove(max(y_list))
        elif min(y_list) == min(org_y_list) and min(y_list) != N - 2 and min(y_list) != 0:
            p += 1
            y_list.remove(min(y_list))

ans = p
print(ans)

Submission status: Wrong Answer

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

counts = []

cnt = 1
for i in range(len(S)-1):
  if (S[i] == S[i+1]):
    cnt += 1
  else:
    if (cnt > 1):
      counts.append(cnt)
    cnt = 1
  if (i == len(S)-2 and cnt > 1):
    counts.append(cnt)
counts.sort(reverse=True)
ans = 0
if (sum(counts) == N):
  ans = N-1
else:
  for i in range(len(counts)):
    if (i < K):
      ans += counts[i] + 1
    else:
      ans += 1
print(ans)

Submission status: Wrong Answer

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

unhappy = 0

# 端の不幸な人カウント
edge_unhappy=0
if s[0]=='L':
  edge_unhappy+=1
if s[-1]=='R':
  edge_unhappy+=1
unhappy += edge_unhappy  

# 端以外の不幸な人カウント(向かい合っている人は不幸)
rl_count =0
for i in range(len(s)-1):
  if s[i]=='R' and s[i+1]=='L':
    rl_count+=2    
unhappy += rl_count

# 一回の変換で2人ずつ救われる
unhappy = max(unhappy - 2*min(rl_count//2, k) ,0)

# 基本2人ずつ救われるが、端の人は優先度低い
leftover = max(k - rl_count//2,0)
if leftover > 0:
  unhappy -= min(edge_unhappy,leftover)
print(len(s)-unhappy)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(ans)

Submission status: Wrong Answer

Solution code:
def d():
    N, K = map(int,input().split())
    S = list(input())
    x, happiness = 0, 0
    if S[0] == 'R':
        x += 1
    if S[N-1] == 'L':
        x += 1

    for i in range(1, N):
        if S[i] == 'R':
            if S[i-1] == 'L':
                x += 1
            elif S[i-1] == 'R':
                happiness += 1

        if S[i] == 'L' and S[i-1] == 'L':
            happiness += 1

    if K >= x:
        happiness += (x - 1) * 2 + 1
    elif K == x - 1:
        happiness += (x - 1) * 2
    elif K < x - 1:
        happiness += K * 2

    print(happiness)


if __name__ == '__main__':
    d()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(ans)

Submission status: Wrong Answer

Solution code:
import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]


def main():
    [N, K] = [int(number) for number in input().split()]
    S = input()
    s = [len(rl) for rl in list(nchars(S,1))]
    happy = sum(s)-len(s) + K * 2
    if happy > N:
        print(N-1)
    else:
        print(happy)


    """
    i = K
    while (i > 0):
        len_s = len(s)
        if len_s < 3:
            break
        if i == K:
            three_range_arr = [s[j] + s[j+1] + s[j+2] for j in range(len_s) if j < len_s-2]
            max_tra = max(three_range_arr)
            index = three_range_arr.index(max_tra)
        else:
            index = s.index(max(s))
            if index == 0:
                max_tra = s[index]+s[index+1]+s[index+2]
            elif index == len_s - 1:
                max_tra = s[index-2] + s[index-1] + s[index]
                index -= 2
            else:
                if index == 1:
                    maxs = [s[index-1] + s[index] + s[index+1], s[index]+s[index+1]+s[index+2]]
                elif index == len_s-2:
                    maxs = [s[index-2] + s[index-1] + s[index], s[index-1] + s[index] + s[index+1]]
                else:
                    maxs = [s[index-2] + s[index-1] + s[index], s[index-1] + s[index] + s[index+1], s[index]+s[index+1]+s[index+2]]
                max_tra = max(maxs)
                offset = maxs.index(maxs) - 2
                if index == 1:
                    offset += 1
                index += offset
        if index == 0:
            ss = [max_tra]
            ss.extend(s[index+3:len_s])
        elif index == len_s - 3:
            ss = s[0:index-1]
            ss.extend([max_tra])
        else:
            ss = s[0:index-1]
            ss.extend([max_tra])
            ss.extend(s[index+3:len_s])
        #s = s[0:index-2].append(max_tra)#.extend(s[index+1, len_s-1])
        i-=1
        s=ss

    if i > 0:
        print(N-1)
    else:
        print(sum(s)-len(s))
    """


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s = list(str(input()))
ch=0
for i in range(n-1):
    if s[i] == s[i+1]:
        ch+=1
    else:
        ch+=0
if s[0]==s[-1]:
    if ch//2<=k:
        print(n-max(1,ch//2-k))
else:
    if ch//2+1<=k:
        print(n-1)
    elif ch//2+1-k==1:
        print(n-2)
    else:
        print(n+2*k-ch//2-1)

Submission status: Wrong Answer

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

lr = 0
rl = 0
for i in range(1, n):
    if s[i-1] == "L" and s[i] == "R":
        lr += 1
    if s[i-1] == "R" and s[i] == "L":
        rl += 1
ans = n - (lr + rl + 1) + (2 * min(min(lr, rl), k))

if k > min(lr, rl):
    if lr > rl and (s[0] == "L" or s[-1] == "R"):
        ans += 1
    if lr < rl and (s[0] == "R" or s[-1] == "L"):
        ans -= 1
print(ans)

Submission status: Wrong Answer

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

lr = 0
rl = 0
for i in range(1, n):
    if s[i-1] == "L" and s[i] == "R":
        lr += 1
    if s[i-1] == "R" and s[i] == "L":
        rl += 1
ans = n - (lr + rl + 1) + 2 * min(min(lr, rl), k)

if k < min(lr, rl):
    if lr > rl and (s[0] == "L" or s[-1] == "R"):
        ans += 1
    if lr < rl and (s[0] == "R" or s[-1] == "L"):
        ans += 1
print(ans)

Submission status: Wrong Answer

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


hukou = 0
for i in range(N):
    if S[i] == 'L':
        if i == 0 or S[i-1] == 'R':
            hukou += 1
            
    if S[i] == 'R':
        if i == N-1 or S[i+1] == 'L':
            hukou += 1

if hukou-1 <= K:
    print(N-1)
else:
    print(N-hukou+K*2)

Submission status: Wrong Answer

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

counts = []

cnt = 0
for i in range(len(S)-1):
  if (S[i] != S[i+1]):
    cnt += 1
print(N - cnt + K)

Submission status: Wrong Answer

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

P=len(S)-1-gap
if(gap==0):
    print(P)
    exit()

for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
        break
    gap-=2
    
print(P)

Submission status: Wrong Answer

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

counts = []
count = 0
lr = 0
rl = 0
for i in range(N - 1):#start
  z = S[i]
  if S[i] == S[i + 1]:
    count += 1
  elif (S[i] == "L") and (S[i+1] == "R"):
    lr += 1
  elif (S[i] == "R") and (S[i+1] == "L"):
    rl += 1
 
minlr = min(lr, rl)
maxlr = max(lr, rl) 
if (maxlr == 1) and (minlr == 0):
  if (S[0] == "L") and (S[-1] == "R"):
    print(count + 1)
  if S[0] == "L":
    print(count)
  elif S[-1] == "R":
    print(count)
  else:
    count += 1
    print(count)
else:
  minK = min(K, minlr)
  count += 2 * minK
  print(count)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        RL += 2

if (RL - 2 * K) >= 0:
    RL -= 2 * K
    K = 0
else:
    K -= RL//2
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL + RL_L + RL_R)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8


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

    # initialize
    output = 0
    if S[0] == "R" and S[1] == "R":
        output += 1
    for i in range(1, N-1):
        if S[i] == "L":
            if S[i-1] == "L":
                output += 1
        elif S[i] == "R":
            if S[i+1] == "R":
                output += 1
    if S[N-1] == "L" and S[N-2] == "L":
        output += 1

    k = 0
    i = 0
    while i < N-2:
        if S[i] == S[i+1]:
            i += 1
            continue

        # when S[i] != S[i+1]
        start = S[i]
        for j in range(i+2, N):
            if S[j] == start:
                i = j
                k += 1
                output += 2
                if output > N-1:
                    assert 1 == 2
                if k == K:
                    return output
                break
            if j == N-1:
                return N - 1

    return output


if __name__ == "__main__":
    output = main()
    print(output)

Submission status: Wrong Answer

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

P=len(S)-1-gap
if(gap==0):
    print(P)
    exit()

for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
        break
print(P)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s = list(str(input()))
ch=0
for i in range(n-1):
    if s[i] == s[i+1]:
        ch+=1
    else:
        ch+=0
if s[0]==s[-1]:
    print(n-max(1,ch//2-k))
else:
    print(n-max(1,ch//2+1-k))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
Y = 0
if N == 1:
    print(0)
    exit(0)

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

count = 0
X = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        X += 1
    if S[i] == S[i+1]:
        count += 1

count += K*2
if K - X > 0 and count < N-2:
    count += 1 if Y > 0 else 0
print(count)

Submission status: Wrong Answer

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

neg = 1
hazi = 0
lr = 0
rl = 0
for i in range(N - 1):#start
  if S[i] != S[i + 1]:
    neg += 1
    if (S[i] == "L") and (S[i+1] == "R"):
      lr += 1
      if i == 0:
        hazi -= 1
      if i == N - 2:
        hazi -= 1
    if (S[i] == "R") and (S[i+1] == "L"):
      rl += 1

if S[0] == "L":
  rl += 1
  hazi += 1
if S[N-1] == "R":
  lr += 1
  hazi += 1
 
#print(neg, lr, rl)
if neg <= 1:
  print(N - neg)
else:
  minlr = min(lr, rl)
  minK = min(K, minlr)
  neg -= minK * 2
  neg = max(0, neg)
  neg += hazi
  print(N - neg)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split()) 
s = input()
s_ = np.array([1]*n)
for i in range(n):
    if s[i] == 'L':
        s_[i] = -1

if n == 1:
    ans = 0
else:
    l = 0
    r = n-1
    l_value = s_[l]
    r_value = s_[r]
    l = 1
    r = n-2
    count = 0
    ans = 0
    while l < r:
        if count < k:
            if s_[l] == l_value and s_[r] == r_value:
                l += 1
                r -= 1
                ans += 2

            elif s_[l] != l_value and s_[r] == r_value:
                r -= 1
                ans += 1
            elif s_[l] == l_value and s_[r] != r_value:
                l += 1
                ans += 1
            else:
                s_[l:r+1] *= -1
                count += 1
                l += 1
                r -= 1
                ans += 2

        else:
            
            a = s_[l-1]
            for i in s_[l:r+2]:
                if i == a:
                    ans += 1
                else:
                    a = i

            break

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

Submission status: Wrong Answer

Solution code:
N , K = map(int, input().split())
S = list(input())
gap=0
for i in range(N-1):
    if(S[i]!=S[i+1]):
        gap+=1
P=len(S)-1-gap
for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
        break
print(P)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
Y = 0
if N == 1:
    print(0)
    exit(0)

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

count = 0
X = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        X += 1
    if S[i] == S[i+1] == 'R' or S[i] == S[i+1] == 'L':
        count += 1

if K - X > 0:
    count += X*2
    count += 1 if Y > 0 else 0
else:
    count += K*2
print(count)

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=list(input())
S.reverse()
a=S[0]
index=1
for i in range(K):
  while (index<N and S[index]==a) :
    index+=1
  while (index<N and S[index]!=a) :
    S[index]=a
    index+=1
#print(S,index)
###幸せな人を数える
count=0
for i in range(1,N-1):
  if S[i]=="L":
    if S[i-1]=="L":
      count+=1
  if S[i]=="R":
    if S[i+1]=="R":
      count+=1
if (S[0]=="R"and S[1]=="R"):
  count+=1
if (S[N-1]=="L"and S[N-2]=="L"):
  count+=1

print(count)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1


for i in range(N-1):
    if S[i:i+2] == 'RL':
        RL += 2

if (RL - 2 * K) >= 0:
    RL -= 2 * K
    K = 0
else:
    K -= RL//2
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1

ans = N - (RL + RL_L + RL_R)

print(ans)

Submission status: Wrong Answer

Solution code:
N , K = map(int, input().split())
S = list(input())
gap=0
for i in range(N-1):
    if(S[i]!=S[i+1]):
        gap+=1
P=len(S)-1-gap
for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
print(P)

Submission status: Wrong Answer

Solution code:
import bisect
N, K = list(map(int, input().split()))
S = input().split()[0]

p = 0
lr_index_list = []
rl_index_list = []
for i in range(1, N):
    if S[i] == S[i-1]:
        p += 1
    if S[i-1] + S[i] == 'LR':
        lr_index_list.append(i-1)
    if S[i-1] + S[i] == 'RL':
        rl_index_list.append(i-1)

n_lr = len(lr_index_list)
n_rl = len(rl_index_list)

if n_lr == 0 and n_rl == 0:
    pass
elif n_lr == 1 and n_rl == 0:
    if lr_index_list[0] != 0 and lr_index_list[0] != N - 2:
        # LLLLLRRRRR 系
        p = N - 1
elif n_lr == 0 and n_rl == 1:
    if rl_index_list[0] != 0 and rl_index_list[0] != N - 2:
        # RRRRRLLLLL 系
        p = N - 1
else:
    kouho_n = min(n_lr, n_rl)
    # 少ない方を基準で探す
    if kouho_n == n_lr:
        x_list = lr_index_list
        y_list = rl_index_list
        org_y_list = y_list[:]
    else:
        x_list = rl_index_list
        y_list = lr_index_list
        org_y_list = y_list[:]
    c = 0
    for x in x_list:
        y_index = bisect.bisect_left(y_list, x)
        is_ok = False
        # 1個右隣をチェック
        if y_index != len(y_list):
            if abs(y_list[y_index] - x) >= 2:
                is_ok = True
                y_list.remove(y_list[y_index])
        # 1個左隣をチェック
        if is_ok == False and y_index != 0:
            if abs(y_list[y_index-1] - x) >= 2:  # TODO
                is_ok = True
                y_list.remove(y_list[y_index-1])
        # 2個右隣があるかどうかをチェック
        if is_ok == False and y_index + 1 < len(y_list):
            is_ok = True
            y_list.remove(y_list[y_index+1])
        # 2個左隣があるかどうかをチェック
        if is_ok == False and y_index - 2 >= 0:  # TODO
            is_ok = True
            y_list.remove(y_list[y_index-2])
        if is_ok:
            c += 1
        if c >= K:
            break
    # y_listのあまりものが、最大or最小で
    # 最後尾の文字でなければ、そのインデックス以降 or 以前を変更できる
    p += min(c, K) * 2
    if K - c >= 1 and y_list:
        if max(y_list) == max(org_y_list) and max(y_list) != N - 2 and max(y_list) != 0:
            p += 1
            y_list.remove(max(y_list))
        elif min(y_list) == min(org_y_list) and min(y_list) != N - 2 and min(y_list) != 0:
            p += 1
            y_list.remove(min(y_list))

    if K - c >= 2 and y_list:
        if max(y_list) == max(org_y_list) and max(y_list) != N - 2 and max(y_list) != 0:
            p += 1
            y_list.remove(max(y_list))
        elif min(y_list) == min(org_y_list) and min(y_list) != N - 2 and min(y_list) != 0:
            p += 1
            y_list.remove(min(y_list))

ans = p
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution 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:
  score = n - 1
else:
  score += k * 2

print (score)

Submission status: Wrong Answer

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

cnt = 2
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score >= N and K <= cnt:
    print(N-1)
elif score >= N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
Y = 0
if S[0] == 'L':
    Y += 1
if S[N-1] == 'R':
    Y += 1

count = 0
X = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        X += 1
    if S[i] == S[i+1] == 'R' or S[i] == S[i+1] == 'L':
        count += 1

if K - X > 0:
    count += X*2
    count += 1 if Y > 0 else 0
else:
    count += K*2
print(count)

Submission status: Wrong Answer

Solution 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:
  score = n - 1
else:
  score += 2

print (score)

Submission status: Wrong Answer

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

cnt = 1
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score >= N and K < cnt:
    print(N-1)
elif score >= N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

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

cnt = 2
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score >= N and K < cnt:
    print(N-1)
elif score >= N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

Solution 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)
if len(a)!=1:
  ans2 = ans2 +(2*K if len(a)/2>K else len(a)//2*2-1)
  print(ans2)
  exit()
print(ans2)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
prev = 'e'
flag = 0

count = 0
        
for i in range(n):
    if i == n - 1:
        if flag == 1:
            count += 1
    if s[i] == 'L':
        if prev == 'R':
            if k == 0:
                pass
            elif flag == 0:
                flag = 1
            else:
                count += 2
                k -= 1
                flag = 0
        elif prev == 'L':
            count += 1
    else:
        if prev == 'L':
            if k == 0:
                pass
            elif flag == 0:
                flag = 1
            else:
                count += 2
                k -= 1
                flag = 0
        elif prev == 'R':
            count += 1
    prev = s[i]
    
print(count)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
Y = 0
if S[0] == 'L':
    Y += 1
if S[N-1] == 'R':
    Y += 1

count = 0
X = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        X += 1
    if S[i] == S[i+1] == 'R' or S[i] == S[i+1] == 'L':
        count += 1

if K - X > 0:
    count += X*2
    count += min(K-X, Y)
else:
    count += K*2
print(count)

Submission status: Wrong Answer

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

cnt = 0
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score >= N and K < cnt:
    print(N-1)
elif score >= N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

Solution 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)
if length == 1:
    ans += 1
else:
    for i in range(min(K, length)):
        ans += 2

print(ans)

Submission status: Wrong Answer

Solution 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)
if len(a)!=1:
  ans2 = ans2 +(2*K if len(a)//2>=K else len(a)//2*2-1)
  print(ans2)
  exit()
print(ans2)

Submission status: Wrong Answer

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

cnt = 0
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score >= N and K < cnt:
    print(N-1)
elif score > N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

Solution code:
import sys
import copy
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    s = input()
    teko=[0,0]
    bb=[]
    for i in range(n):
        bb.append(1 if s[i]=="R" else 0)
    for mmmm in range(2):
        b=copy.deepcopy(bb)

        kohuku=0

        h=mmmm
        a=[]

        temp=0
    #    print(a,n//2,h)
        for i in range(n):
            if b[i]==h:
                temp+=1
                if i==0 or i==n:
                    temp-=0.5
            else:
                a.append([temp,i])
                temp=0
        a=sorted(a,reverse=True)
        for i in range(len(a)):

            if type(a[i][0]) is float:
    #            print(-(-a[i][0]//1))
                a[i][0]=int(-(-a[i][0]//1))
 #       print(a)
        aaa = [x[0] for x in a]
        if len(aaa)-aaa.count(0)<=k:
            k=len(aaa)-aaa.count(0)
        for i in range(k):

            for l in range(a[i][1]-a[i][0],a[i][1]):
                b[l]=-(h-1)
  #      print(b)
        for i in range(1,n):
            if b[i]==1 and b[i-1]==1:
                kohuku+=1
            if b[i]==0 and b[i-1]==0:
                kohuku+=1
        teko[mmmm]=kohuku
#        print(kohuku)
    print(max(teko))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

cnt = 0
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score > N and K < cnt:
    print(N-1)
elif score > N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

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

happiness = 0
if N >= 2:
    if S[0] == 'R' and S[1] == 'R':
        happiness += 1
    for i in range(1, N-1):
        if S[i] == 'R':
            if S[i+1] == 'R':
                happiness += 1
        else:
            if S[i-1] == 'L':
                happiness += 1
    if S[N-1] == 'L' and S[N-2] == 'L':
        happiness += 1

aihan = []
ai = 1
han = -1
happi = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        aihan.append(ai)
    elif S[i] == 'L' and S[i+1] == 'R':
        aihan.append(han)
    else:
        aihan.append(happi)

aihan_count = 0

for i, e in enumerate(aihan):
    if e * aihan_count < 0:
        happiness += 2
        K -= 1
    aihan_count += e
    if K <= 0:
        break

if K > 0:
    if S[0] == 'L':
        if aihan_count < 0:
            happiness += 1
            K -= 1
            aihan_count += 1
    elif S[N-1] == 'R':
        if aihan_count < 0:
            happiness += 1
            K -= 1
            aihan_count += 1

print(happiness)

Submission status: Wrong Answer

Solution 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)
if len(a)!=1:
  ans2 = ans2 +(2*K if len(a)//2>K else len(a)//2*2-1)
  print(ans2)
  exit()
print(ans2)

Submission status: Wrong Answer

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

cnt = 0
score = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
    else:
        score += 1

if K < cnt:
    score += 2*K
else:
    score += 2*cnt

if score > N and K > cnt:
    print(N-1)
elif score > N:
    print(N-2)
else: 
    print(score)

Submission status: Wrong Answer

Solution 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)
if len(a)!=1:
  ans2 = ans2 +(2*K if len(a)//2>K else len(a)//2*2)
  print(ans2)
  exit()
print(ans2)

Submission status: Wrong Answer

Solution code:
def swap_list(S, l, r):
  result = list(S)
  count = 0
  for i in range(l, r+1):
    if S[r-count] == "R":
      result[i] = "L"
    elif S[r-count] == "L":
      result[i] = "R"
    count += 1
    
  return result 

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

current = 0
for i in range(0, K):
  l_RL_index = -1
  l_LR_index = -1
  r_RL_index = -1
  r_LR_index = -1
  for j in range(current, N-1):  
    if S[j] == "R" and S[j+1] == "L":
      if l_RL_index == -1:
        l_RL_index = j + 1
      if l_LR_index != -1:
        r_RL_index = j
    elif S[j] == "L" and S[j+1] == "R":
      if l_LR_index == -1:
        l_LR_index = j + 1
      if l_RL_index != -1:
        r_LR_index = j
    
    if l_RL_index != -1 and r_LR_index != -1:
      S = swap_list(S, l_RL_index, r_LR_index)
      current = r_LR_index + 1
      break
    elif l_LR_index != -1 and r_RL_index != -1:
      S = swap_list(S, l_LR_index, r_RL_index)
      current = r_RL_index + 1
      break
  
result = 0
for i in range(0, N):
  if i == 0:
    if S[i] == "R" and S[i+1] == "R":
      result += 1
  elif i == N-1:
    if S[i] == "L" and S[i-1] == "L":
      result += 1
  else:
    if (S[i] == "R" and S[i+1] == "R") or (S[i] == "L" and S[i-1] == "L"):
      result += 1

print(result)