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:
#import numpy as np

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

done = 0


out = 0

#gid_list = []
#gnum_list = []
#gkukan_list = []
#
#cur_gid = 0
#cur_gnum = 0
#cur_gstart = 0

for i in range(N):
    cur_c = s[i]
    
    if s[i] == "L" and i >= 1 and s[i-1] == "L":
        out += 1
    elif s[i] == "R" and i <= N-2 and s[i+1] == "R":
        out += 1

#    if i >= 1:
#        if cur_c != before_c:
#            cur_gend =i-1
#            cur_gkukan = [cur_gstart, cur_gend]
#            gkukan_list.append(cur_gkukan)
#            cur_gstart = i
#
#            gnum_list.append(cur_gnum)
#            cur_gnum = 0
#
#            cur_gid += 1
#    
#    cur_gnum += 1
#    gid_list.append(cur_gid)
#
#    before_c = cur_c
#
#cur_gend =N-1
#cur_gkukan = [cur_gstart, cur_gend]
#gkukan_list.append(cur_gkukan)
#gnum_list.append(cur_gnum)
#len_g = len(gnum_list)



#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # L(RL) -> LLL
#            if s[i-1] == "L" and s[i] == "R" and s[i+1] == "L":
#                s[i] = "L"
#                out += 2
#                done += 1
#    else:
#        break

#print("current", out, done, s)
i = 0
while i < N:
    if done < K:
        if 0 <= i <= N-2:
           # L(RL) -> LLL
            if s[i] == "L" and s[i+1] == "R":
                start_i = i+1
                i += 1

                found = True
                while s[i] == "R":
                    i += 1
                    if i >= N:
                        found = False
                        break
               
                # s[i] == "L" made

                if found:
                    end_i = i-1
                else:
                    #end_i = N-1
                    break
                
                for j in range(start_i, end_i+1):
                    s[j] = "L"

                #print("found", found, start_i, end_i)
                if found:
                    out += 2
                #else:
                #    out += 1
                #    break

                done += 1

                i -= 1 # s[i] == "R" ni modosu
        else:
            pass
            #print("false", i)

        i += 1
    else:
        break


#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # R(LR) -> RRR
#            if s[i-1] == "R" and s[i] == "L" and s[i+1] == "R":
#                s[i] = "R"
#                out += 2
#                done += 1
#    else:
#        break

#for i in range(N-1):
#    if done < K:
#        if s[i] == "R" and s[i+1] == "L":
#            s[i] = "L"
#            out += 1
#            done += 1
#    else:
#        break



i = 0
if done < K:
    if s[0] == "R":
        start_i = 0
        i += 1

        found = True
        while s[i] == "R":
            i += 1
            if i >= N:
                found = False
                break

        if found:
            end_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1


i = N-1
if done < K:
    if s[i] == "R":
        #print("test3", out)
        end_i = i
        i -= 1

        found = True
        while s[i] == "R":
            i -= 1
            if i <= -1:
                found = False
                break

        if found:
            start_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1

print(out)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
L = []
pre = S[0]
num = 1
for i in range(1, N):
    if pre == S[i]:
        num += 1
    elif pre != S[i]:
        L.append(num)
        num = 1
    pre = S[i]

L.append(num)

length = len(L)
ans = 0
for i in range(K):
    if length > 2:
        ans += 2
    elif length == 2:
        ans += 1
    else:
        break

    length -= 1


ans += sum(L)-len(L)
print(ans)

Submission status: Wrong Answer

Solution code:
#import numpy as np

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

done = 0


out = 0

#gid_list = []
#gnum_list = []
#gkukan_list = []
#
#cur_gid = 0
#cur_gnum = 0
#cur_gstart = 0

for i in range(N):
    cur_c = s[i]
    
    if s[i] == "L" and i >= 1 and s[i-1] == "L":
        out += 1
    elif s[i] == "R" and i <= N-2 and s[i+1] == "R":
        out += 1

#    if i >= 1:
#        if cur_c != before_c:
#            cur_gend =i-1
#            cur_gkukan = [cur_gstart, cur_gend]
#            gkukan_list.append(cur_gkukan)
#            cur_gstart = i
#
#            gnum_list.append(cur_gnum)
#            cur_gnum = 0
#
#            cur_gid += 1
#    
#    cur_gnum += 1
#    gid_list.append(cur_gid)
#
#    before_c = cur_c
#
#cur_gend =N-1
#cur_gkukan = [cur_gstart, cur_gend]
#gkukan_list.append(cur_gkukan)
#gnum_list.append(cur_gnum)
#len_g = len(gnum_list)



#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # L(RL) -> LLL
#            if s[i-1] == "L" and s[i] == "R" and s[i+1] == "L":
#                s[i] = "L"
#                out += 2
#                done += 1
#    else:
#        break

#print("current", out, done, s)
i = 0
while i < N:
    if done < K:
        if 0 <= i <= N-2:
           # L(RL) -> LLL
            if s[i] == "L" and s[i+1] == "R":
                start_i = i+1
                i += 1

                found = True
                while s[i] == "R":
                    i += 1
                    if i >= N:
                        found = False
                        break
               
                # s[i] == "L" made

                if found:
                    end_i = i-1
                else:
                    #end_i = N-1
                    break
                
                for j in range(start_i, end_i+1):
                    s[j] = "L"

                #print("found", found, start_i, end_i)
                if found:
                    out += 2
                #else:
                #    out += 1
                #    break

                done += 1

                i -= 1 # s[i] == "R" ni modosu
        else:
            pass
            #print("false", i)

        i += 1
    else:
        break


for i in range(N):
    if done < K:
        if 1 <= i <= N-2:
            # R(LR) -> RRR
            if s[i-1] == "R" and s[i] == "L" and s[i+1] == "R":
                s[i] = "R"
                out += 2
                done += 1
    else:
        break

for i in range(N-1):
    if done < K:
        if s[i] == "R" and s[i+1] == "L":
            s[i] = "L"
            out += 1
            done += 1
    else:
        break



i = 0
if done < K:
    if s[0] == "R":
        start_i = 0
        i += 1

        found = True
        while s[i] == "R":
            i += 1
            if i >= N:
                found = False
                break

        if found:
            end_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1


i = N-1
if done < K:
    if s[i] == "R":
        #print("test3", out)
        end_i = i
        i -= 1

        found = True
        while s[i] == "R":
            i -= 1
            if i <= -1:
                found = False
                break

        if found:
            start_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1

print(out)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n, k = list(map(int, input().split()))
    s = input()
    flag = False
    count = 1
    f = 0
    stack = []
    for i in range(0, n-1):
        if s[i] == s[i+1]:
            if count == 1:
                flag = True
                f = i
            count += 1
        elif flag == True:
            stack.append([count, f, i])
            flag = False
            count = 1
            f = 0
    if flag == True:
        stack.append([count, f, i])
        
    st = len(stack)
    
    s = list(s)
    
    for j in range(st):
        for k in range(stack[j][1], stack[j][2]+1):
            if s[k] == "L":
                s[k] = "R"
            elif s[k] == "R" :
                s[k] = "L"
    
    res = 0
    for i in range(n-1):
        if s[i] == s[i+1]:
            res += 1
    print(res)

Submission status: Wrong Answer

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

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

target = S[0]
if(target=="L"):
    changed = "R"
else:
    changed = "L"

ans = N-len(ranlength_l)
for const in (ranlength_l[1:len(ranlength_l)-1][::2]):
    if(K==0):
        break
    ans += 2
    K = K-1

if(K!=0 and len(ranlength_l)!=1):
    ans+=1

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

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[N-1] == "L") and (S[N-2] == "R"):
    print(count)
  elif (S[N-2] == "L") and (S[N-1] == "R"):
    print(count)
  elif (S[0] == "L") and (S[1] == "R"):
    print(count)
  elif (S[1] == "L") and (S[0] == "R"):
    print(count)
  else:
    count += 1
    print(count)
elif maxlr == 0:
  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 = list(str(input()))
eq=0
ch=0
for i in range(n-1):
    if s[i] == s[i+1]:
        eq+=1
    else:
        ch+=1
if s[0] =="R":
    if ch//2 + 1 <= k:
        print(n-1)
    else:
        if eq != 0:
            eq_an = eq +1
        else:
            eq_an = eq
        ans = eq_an + 2*k -1
        print(ans)
else:
    if ch//2 + 2 <= k:
        print(n-1)
    else:
        print(eq + 2*k -1)

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 get_largest(arr, int):
    if int == 0:
        return arr
    sum = 0
    index = 0
    len = len(range)
    for i in range(len):
        if i < len - 2:
            if sum < arr[i] + arr[i+1] + arr[i+2]:
                sum = arr[i] + arr[i+1] + arr[i+2]
                index = i
    int -= 1


def main():
    [N, K] = [int(number) for number in input().split()]
    S = input()
    s = [len(rl) for rl in list(nchars(S,1))]
    i = K
    while (i > 0):
        len_s = len(s)
        if len_s < 3:
            break
        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)
        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))
    #ans_arr = get_largest(s, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution 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
    if count>=5:
        sut += 2
        count -= 2
    elif len(a) == 3:
        sut += 1
        count -= 2
print(sut)

Submission status: Wrong Answer

Solution code:
import heapq

n, k = list(map(int, input().split()))
s = input()

left, right = [], []
current = 0
l_happy, r_happy = 0, 0
for i, (s0, s1) in enumerate(zip(s, s[1:]), 1):
    if s0 != s1:
        dst, len_ = s0, i - current
        current = i
        if dst == 'L':
            heapq.heappush(left, -len_)
            l_happy += len_ - 1
        else:
            heapq.heappush(right, -len_)
            r_happy += len_ - 1
if s[-1] == 'L':
    heapq.heappush(left, -(len(s) - current))
    l_happy += len(s) - current - 1
else:
    heapq.heappush(right, -(len(s) - current))
    r_happy += len(s) - current - 1

if l_happy > r_happy:
    for _ in range(k):
        if right:
            len_ = -1 * heapq.heappop(right)
            l_happy += len_ + (1 if len(right) > 0 else 0)
    print(min(l_happy, n - 1))
else:
    for _ in range(k):
        if left:
            len_ = -1 * heapq.heappop(left)
            r_happy += len_ + (1 if len(left) > 0 else 0)
    print(min(r_happy, n - 1))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
SC=[]
c=1
for i in range(N-1):
    if S[i]!=S[i+1]:
        SC.append(c)
        c=1
    else:
        c+=1

if c!=1:
    SC.append(c)

L=len(SC)

if L<=K+2:
    print(N-1)

else:
    ans=sum(SC[0:2*K+1])-1
    for i in range(2*K+1,L):
        if SC[i]>1:
            ans+=SC[i]-1
    print(ans)

Submission status: Wrong Answer

Solution code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
l_ls = re.findall(r"L+", s)
r_ls = re.findall(r"R+", s)
if len(r_ls) - 1 < k or len(l_ls) - 1 < k:
    cnt += min(len(r_ls) - 1, len(l_ls) - 1) * 2 + 1
else:
    cnt += k * 2
print(cnt)

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:
    counts.append(cnt)
    cnt = 1
  if (i == len(S)-2 and cnt > 1):
    counts.append(cnt)
counts.sort(reverse=True)
if (sum(counts) == N):
  print(N-1)
else:
  print(sum(counts[0:K]) + K + (K - len(counts)))

Submission status: Wrong Answer

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

def main():
    n,k=map(int,input().split())
    s=input()
    b=[]
    for i in range(n):
        b.append(1 if s[i]=="R" else 0)
#    print(b)
    kohuku=0

    if b.count(0)>n//2:
        h=1
    else:
        h=0

    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

    print(kohuku)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
#LR数できまる.
#両側が同じときはさらにもう一つ減る
LR = 0

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

if LR > K:
    ans = N - (LR-K)*2
else:
    ans = N-1

if S[0] == S[-1]:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

n,k = map(int,input().split())
s = [0] + list(input())
#print(s)
count_L = 0
count_R = 0
ans = 0
for i in range(1,n):#調べる対象はs[2]からs[n-1]まで
    if s[i-1] == 'R' and s[i] == 'L' and s[i+1] == 'R':
        count_L += 1
    elif s[i-1] == 'L' and s[i] == 'R' and s[i+1] == 'L':
        count_R += 1
    elif s[i-1] == 'R' and s[i] == 'R':
        ans += 1
    elif s[i-1] == 'L' and s[i] == 'L':
        ans += 1
count_max = max(count_L,count_R)
#print(count_max,ans)

if count_max >= k:
    ans = ans + 2*k
else:
    ans = ans + 2*count_max + 1

print(ans)

Submission status: Wrong Answer

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

def main():
    n,k=map(int,input().split())
    s=input()
    b=[]
    for i in range(n):
        b.append(1 if s[i]=="R" else 0)
#    print(b)
    kohuku=0

    if b.count(0)>n//2:
        h=1
    else:
        h=0

    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

    print(kohuku)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
#LR数できまる.
#両側が同じときはさらにもう一つ減る
LR = 0

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

if LR > K:
    ans = N - (LR-K)*2
else:
    ans = N-1

if S[0] == S[-1]:
    ans -= 1

RL = 0

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

if RL > K:
    ans2 = N - (RL-K)*2
else:
    ans2 = N-1

if S[0] == S[-1]:
    ans2 -= 1

if ans2 > ans:
    print(ans2)
else:
    print(ans)

Submission status: Wrong Answer

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

cnt = []
tmp = 1
for i in range(N - 1):
    if S[i] != S[i + 1]:
        cnt.append(tmp)
        tmp = 1
    else:
        tmp += 1

cnt.append(tmp)
bn = 1 + 2 * K
if len(cnt) <= bn:
    print(N - 1)
    exit(0)

ans = sum(cnt[:bn])
l = len(cnt)
s = ans
for i in range(l - bn):
    s -= cnt[i]
    s += cnt[bn + i]
    ans = max(s, ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n==1:
  print(0)
  exit()

count=0
for i in range(1,n//2+1):
  if count==k:
    break
  if s[i-1]==s[i+1] and s[i]!=s[i-1]:
    s[i]=s[i-1]
    count+=1

  if count==k:
    break
  if s[n-i]==s[n-i-2] and s[n-i-1]!=s[n-i]:
    s[n-i-1]=s[n-i]
    count+=1

result=0
if count!=k:
  for i in range(1,n):
    if s[i-1]!=s[i]:
      result+=1
      count+=1
    if count==k:
      break
    
if s[0]=='R' and s[1]=='R':
  result+=1
if s[-1]=='L' and s[-2]=='L':
  result+=1
for i in range(1,n-1):
  if (s[i]=='R' and s[i+1]=='R') or (s[i]=='L' and s[i-1]=='L'):
    result+=1
print(result)

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:
    counts.append(cnt)
    cnt = 1
  if (i == len(S)-2 and cnt > 1):
    counts.append(cnt)
counts.sort(reverse=True)
if (sum(counts) == N):
  print(N-1)
else:
  print(sum(counts[0:K]) + K + (len(counts) - K))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

if N ==1:
    print(1)
    exit()


v = Counter(S)
d, _  = v.most_common()[0]

# R, L
# def search(c1, c2):
#     first = False
#
#     for i in range(1, N):
#         if not first and S[i - 1] == c1 and S[i] == c2:
#             S[i] = c1
#             first = True


def search(c1, c2):
    ret1 = 0
    ret2 = 0

    cnt = 0
    for i in range(1, N):
        if S[i - 1] == c1 and S[i] == c2:
            ret1 += 1
        elif S[i - 1] == c2 and S[i] == c1:
            ret2 += 1

        if S[i - 1] == S[i]:
            cnt += 1

    return ret1, ret2, cnt


if d == 'R':
    r1, r2, cnt = search('L', 'R')
else:
    r1, r2, cnt = search('R', 'L')


# print(r1, r2, cnt)
if r1 == 0 or r2 == 0:
    print(N - 1)
    # print(cnt + min(min(r1, r2), K) * 1)
else:
    print(cnt + min(min(r1, r2), K) * 2)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
#LR数できまる.
#両側が同じときはさらにもう一つ減る
LR = 0

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

if LR > K:
    ans = N - (LR-K)*2
else:
    ans = N-1

if S[0] == S[-1]:
    ans -= 1

RL = 0

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

if RL > K:
    ans2 = N - (RL-K)*2
else:
    ans2 = N-1

if S[0] == S[-1]:
    ans2 -= 1

if ans2 > ans:
    print(ans)
else:
    print(ans2)

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

print (score)

Submission status: Wrong Answer

Solution code:
import math
def get_score(s):
    score = 0
    for s_idx in range(len(s) - 1):
        if s[s_idx] == s[s_idx + 1]:
            score += 1
    return  score
N,K = [int(x) for x in input().split()]
s = list(input().lower())
default_score = get_score(s)
span_list = []
span_lens = []
s_idx = 0
while s_idx != len(s):
    start = s_idx
    while True:
        if s_idx == len(s) - 1:
            span_list.append((start,s_idx))
            span_lens.append(s_idx - start)
            break
        if s[s_idx] != s[s_idx + 1]:
            span_list.append((start,s_idx))
            span_lens.append(s_idx - start)
            break
        s_idx += 1
    s_idx +=1
discout = 0
if (len(span_list) - K) < 2:
    discout = -1
print(default_score + 2 * min(math.floor(len(span_list)/2),K) + discout)

Submission status: Wrong Answer

Solution code:

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

f = True

il = 0
ir = n-1

dl = 1
dr = -1

wl = s[0]
wr = wl

gl = True
gr = True

hap = 0

if ( n == 1 ):
    print(0)
else:
    while(f):
        #print(il,ir,hap)
        if (gl):
            il += dl
            if (il == ir):
                break
            if (s[il] == wl):
                hap += 1
            else:
                gl = False

        if (gr):
            ir += dr
            if (il == ir):
                break
            if (s[ir] == wr):
                hap += 1
            else:
                gr = False

        if (gr == False) and (gl == False):
            gl = True
            gr = True

            if (k > 0):
                k -= 1
                hap += 2

                tmp = il
                il = ir
                ir = tmp
                if (wl == "L"):
                    wl = "R"
                else:
                    wl = "L"
                if (wr == "L"):
                    wr = "R"
                else:
                    wr = "L"
                dl *= -1
                dr *= -1
    
    if (k > 0):
        hap += 1

print(hap)
#print(k)

Submission status: Wrong Answer

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

def change(s):
  if s == "L":
    return "R"
  else:
    return "L"
  
for _ in range(K):
  l = S.index(S[0]) + 1
  r = N - 1 - (S[::-1].index(S[-1])+1)
  if l > r:
    break
  S[l:r+1] = map(change,S[l:r+1])
count = 0
ans = 0
before = S[0]
for i in range(1,N):
  if S[i-1] == S[i]:
    count += 1
  else:
    ans += count-1
print(ans)

Submission status: Wrong Answer

Solution code:
L = [int(i) for i in input().split()]
S = input()
N = L[0]
K = L[1]
count = 0

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

count += K * 2
if count > N:
    print(N)
elif count < N:
    print(count)
elif count == N:
    counter = S.count('N',0)
    if counter % 2 == 0:
        print(N-1)
    else:
        print(N)

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

Submission status: Wrong Answer

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

S = input()

S = [c  for c in S]

# はじめの計算
counter = 0

if S[0] == "R" and S[1] =="R":
	counter += 1

if S[N-2] == "L" and S[N-1] =="L":
	counter += 1

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



reached = [False]*N

Que_1 = 0

Que_2 = 0			

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

	elif S[i] == "R" and S[i+1] == "L" and reached[i] == False and reached[i+1] == False:
		Que_2 += 1
		reached[i] = True
		reached[i+1] = True

counter += 2 * min(Que_1,Que_2)

print(counter)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split()) 
s = input()

def set_list(es):
    return len(list(set(es))) == 1

double = 0
single =0
s0 = s[0]

i = 1
while i<n:
    print(i)
    if k==0:
        break
    if s0 == s[i]:
        i += 1
    else:
        si = s[i]
        ll = s[i+1:]
        if set_list(ll):
            i += 1
            single+=1
        else:
            double+=1
        for sj in ll:
            i+=1
            if si!=sj:
                k -= 1
                break
                
c = len([i for i in range(n-1) if s[i]==s[i+1]])
print(c+double*2+single)

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最小で
    # 最後尾の文字でなければ、もう1個増やせる
    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 - 1 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 - 1 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 - 1 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 - 1 and min(y_list) != 0:
            p += 1
            y_list.remove(min(y_list))

ans = p
print(ans)