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()
p2=0
p1=0
if n==1:
  print(0)
  exit()
if s[0]=="L":
  p1+=1
if s[n-1]=="R":
  p1+=1
  
for i in range(1,n):
  if not (s[i]=="L" and s[i-1]=="R"):
    continue
  p2+=1
if k<=p2-1:
  print(n-2*p2-p1+2*k)
elif p2<k<p2+p1:
  print(n-p1+(k-p2))
else:
  print(n-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
p2=0
p1=0
if n==1:
  print(1)
  exit()
if s[0]=="L":
  p1+=1
if s[n-1]=="R":
  p1+=1
  
for i in range(1,n):
  if not (s[i]=="L" and s[i-1]=="R"):
    continue
  p2+=1
if k<=p2-1:
  print(n-2*p2-p1+2*k)
elif p2<k<p2+p1:
  print(n-p1+(k-p2))
else:
  print(n-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
p2=0
p1=0
if s[0]=="L":
  p1+=1
if s[n-1]=="R":
  p1+=1
  
for i in range(1,n):
  if not (s[i]=="L" and s[i-1]=="R"):
    continue
  p2+=1
if k<=p2-1:
  print(n-2*p2-p1+2*k)
elif p2<k<p2+p1:
  print(n-p1+(k-p2))
else:
  print(n-1)

Submission status: Wrong Answer

Solution code:
n,s=open(0);n,k=map(int,n.split());print(n-max(sum(i==j for i,j in zip(s,s[1:]))-2*k,1))

Submission status: Wrong Answer

Solution code:
K,S=open(0);K=int(K.split()[1])


def count_unhappy():
    u=0
    for i in range(1,len(S)):
        if S[i] != S[i-1]:u+=1
    return max(u - 2 * K, 1)


print(len(S) - count_unhappy())

Submission status: Wrong Answer

Solution code:
K,S=open(0);K=int(K.split()[1]);u=[S[0]]


def count_unhappy():
    for c in S:
        if c != u[-1]:
            u.append(c)
    return max(len(u) - 2 * K, 1)


print(len(S) - count_unhappy())

Submission status: Wrong Answer

Solution code:
K,S=open(0);K=int(K.split()[1]);u=[S[0]]
for c in S:c==u[-1]or u.append(c)
print(len(S)-max(len(u)-2*K,1))

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=input()
snum=[]
now_si=""
now_sinum=0
for i in range(len(s)):
    if(now_si!=s[i]):
        now_si=s[i]
        snum.append(now_sinum)
        now_sinum=1
    else:
        now_sinum+=1
else:
    snum.append(now_sinum)
snum.pop(0)

if(len(snum)<3):
    print(sum(snum)-1)
    sys.exit()

for i in range(k):
    print(snum)
    if(len(snum)<3):
        snum=[sum(snum)]
        break

    snum[-3]+=snum[-2]+snum[-1]
    snum.pop(-1)
    snum.pop(-1)

print(sum(snum)-len(snum))

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=input()
snum=[]
now_si=""
now_sinum=0
for i in range(len(s)):
    if(now_si!=s[i]):
        now_si=s[i]
        snum.append(now_sinum)
        now_sinum=1
    else:
        now_sinum+=1
else:
    snum.append(now_sinum)
snum.pop(0)

if(len(snum)<3):
    print(sum(snum)-1)
    sys.exit()

for i in range(k):
#    print(snum)
    if(len(snum)<3):
        break

    snum[0]+=snum[1]+snum[2]
    snum.pop(1)
    snum.pop(1)

print(sum(snum)-len(snum))

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=input()
snum=[]
now_si=""
now_sinum=0
for i in range(len(s)):
    if(now_si!=s[i]):
        now_si=s[i]
        snum.append(now_sinum)
        now_sinum=1
    else:
        now_sinum+=1
else:
    snum.append(now_sinum)
snum.pop(0)
#print(snum)

if(len(snum)<3):
    print(sum(snum)-1)
    sys.exit()

for i in range(k):
#     min_threesum=9999999999999
#     min_threesum_j=9999999999999
#
#     for j in range(len(snum)-3,-1,-1):
#         threesum=snum[j]+snum[j+1]+snum[j+2]
#         if(threesum==3):
#             snum[j]+=snum[j+1]+snum[j+2]
#             snum.pop(j+1)
#             snum.pop(j+1)
#             break
#         elif(min_threesum>threesum):
#             min_threesum=threesum
#             min_threesum_j=j
#     else:
#         snum[min_threesum_j]+=snum[min_threesum_j+1]+snum[min_threesum_j+2]
#         snum.pop(min_threesum_j+1)
#         snum.pop(min_threesum_j+1)
    snum[-3]+=snum[-2]+snum[-1]
    snum.pop(-1)
    snum.pop(-1)

#    print(snum)
    if(len(snum)<3):
        break

print(sum(snum)-len(snum))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,k=MI()
s=input()
#print(s)
l=0
r=0
if s[0]=="L":
    l=1
if s[-1]=="R":
    r=1
state=""
count=0
for i in range(n):
    u=s[i:i+1]
    #print(state)
    #print(u)
    if state=="":
        if u=="R":
            state="R"
    elif state=="R":
        if u=="L":
            state=""
            count+=1
        else:
            state="R"
#print(count)
#print(l+r)
unhappy=2*count+l+r
if (l+r)==0:
    if k>=count:
        unhappy=1
    elif k==(count-1):
        unhappy=2
    else:
        unhappy-=2*k
elif (l+r)==1:
    if k>=count:
        unhappy=1
    else:
        unhappy-=2*k
elif (l+r)==2:
    if k>=(count+1):
        unhappy=1
    elif k==count:
        unhappy=2
    else:
        unhappy-=2*k
#print(unhappy)
print(n-unhappy)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,k=MI()
s=input()
#print(s)
l=0
r=0
if s[0]=="L":
    l=1
if s[-1]=="R":
    r=1
state=""
count=0
for i in range(n):
    u=s[i:i+1]
    #print(state)
    #print(u)
    if state=="":
        if u=="R":
            state="R"
    elif state=="R":
        if u=="L":
            state=""
            count+=1
        else:
            state="R"
#print(count)
#print(l+r)
unhappy=2*count+l+r
if l+r==0:
    if k>=count:
        unhappy=1
    elif k==count-1:
        unhappy=2
    else:
        unhappy-=2*k
elif l+r==1:
    if k>=count:
        unhappy=1
    else:
        unhappy-=2*k
elif l+r==2:
    if k>=count+1:
        unhappy=1
    elif k==count:
        unhappy=2
    else:
        unhappy-=2*k
#print(unhappy)
print(n-unhappy)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,k=MI()
s=input()
#print(s)
l=0
r=0
if s[0]=="L":
    l=1
if s[-1]=="R":
    r=1
state=""
count=0
for i in range(n):
    u=s[i:i+1]
    #print(state)
    #print(u)
    if state=="":
        if u=="R":
            state="R"
    elif state=="R":
        if u=="L":
            state=""
            count+=1
        else:
            state="R"
#print(count)
#print(l+r)
unhappy=2*count+l+r
if l+r==0:
    if k>=count:
        unhappy=1
    elif k==count-1:
        unhappy=2
    else:
        unhappy-=2*(k-1)
elif l+r==1:
    if k>=count:
        unhappy=1
    else:
        unhappy-=2*k
elif l+r==2:
    if k>=count+1:
        unhappy=1
    elif k==count:
        unhappy=2
    else:
        unhappy-=2*k
#print(unhappy)
print(n-unhappy)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,k=MI()
s=input()
print(s)
l=0
r=0
if s[0]=="L":
    l=1
if s[-1]=="R":
    r=1
state=""
count=0
for i in range(n):
    u=s[i:i+1]
    #print(state)
    #print(u)
    if state=="":
        if u=="R":
            state="R"
    elif state=="R":
        if u=="L":
            state=""
            count+=1
        else:
            state="R"
#print(count)
#print(l+r)
unhappy=2*count+l+r
if l+r==0:
    if k>=count:
        unhappy=1
    elif k==count-1:
        unhappy=2
    else:
        unhappy-=2*(k-1)
elif l+r==1:
    if k>=count:
        unhappy=1
    else:
        unhappy-=2*k
elif l+r==2:
    if k>=count+1:
        unhappy=1
    elif k==count:
        unhappy=2
    else:
        unhappy-=2*k
#print(unhappy)
print(n-unhappy)

Submission status: Wrong Answer

Solution code:
N,k=map(int,input().split())
A=input()
ans=0
for i in range(0,N-1):
  if A[i]==A[i+1]:
    ans=ans+1
if A[N-2]=='R' and A[N-1]=='R':
  ans=ans-1
print(min(N-1,2*k+ans))

Submission status: Wrong Answer

Solution code:
n_k = list(map(int,input().split()))
chr_lr = list(input())
cnt_rl = 0
cnt_con = 0

for i in range(n_k[0]-1):
  if(chr_lr[i] == chr_lr[i+1]):
    cnt_con += 1
  elif(chr_lr[i] == 'R' and chr_lr[i+1] == 'L'):
    cnt_rl += 1

if n_k[1] <= cnt_rl:
  sum = cnt_con + (n_k[1]*2)
else:
  sum = cnt_con + (cnt_rl*2)

if(chr_lr[0] != chr_lr[n_k[0]-1]):
  sum += 1
  
print(sum)

Submission status: Wrong Answer

Solution code:
n_k = list(map(int,input().split()))
chr_lr = list(input())
cnt_rl = 0
cnt_con = 0

for i in range(n_k[0]-1):
  if(chr_lr[i] == chr_lr[i+1]):
    cnt_con += 1
  elif(chr_lr[i] == 'R' and chr_lr[i+1] == 'L'):
    cnt_rl += 1

if n_k[1] <= cnt_rl:
  sum = cnt_con + (n_k[1]*2)
else:
  sum = cnt_con + (cnt_rl*2)
  
print(sum)

Submission status: Wrong Answer

Solution code:
n_k = list(map(int,input().split()))
chr_lr = list(input())
cnt_rl = 0
cnt_con = 0

for i in range(n_k[0]-1):
  if(chr_lr[i] == chr_lr[i+1]):
    cnt_con += 1
  elif(chr_lr[i] == 'R' and chr_lr[i+1] == 'L'):
    cnt_rl += 1

if n_k[1] <= cnt_rl:
  sum = cnt_con + (n_k[1]*2)
else:
  sum = cnt_con + (cnt_rl*2) + (n_k[1] - cnt_rl)
  
print(sum)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for string in s:
    if temp == string:
        cnt += 1
    else:
        press.append(cnt)
        temp = string
        cnt = 1
press.append(cnt)

if len(press) - 2 * k < 0:
    print(n-1)
    exit()

ans = 2 * k
for i in press:
    ans += i - 1
print(ans)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for string in s:
    if temp == string:
        cnt += 1
    else:
        press.append(cnt)
        temp = string
        cnt = 1
press.append(cnt)

if len(press) < 3 * k:
    print(n-1)
    exit()

ans = 2 * k
for i in press:
    ans += i - 1
print(ans)

Submission status: Wrong Answer

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

cnt = 0
blue = 0
red = 0

for i in range(1, n):
	if s[i] == s[i-1]:
		cnt += 1
	elif s[i-1:i+1] == 'LR':
		blue += 1
	elif s[i-1:i+1] == 'RL':
		red += 1

if k <= min(blue, red):
	cnt += 2 * k
else:
	cnt += min(blue, red)
	if s[0] == 'L' and s[-1] == 'R':
		cnt += 1

print(cnt)

Submission status: Wrong Answer

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

happiness = 0

for word in S.split("L"):
    happiness += max(0, len(word) - 1)
for word in S.split("R"):
    happiness += max(0, len(word) - 1)

res = happiness + 2 * K
print(min(res, N))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt =0
for i in range(N-1):
    p = S[i]+S[i+1] 
    if p =='RL':
        cnt += 1
cnt2 = 0
if S[0] == 'L':
    cnt2 += 1
if S[-1] == 'R':
    cnt2 += 1
n =N- cnt *2-cnt2
if cnt - 1 >= K:
    print(n+2*K)
else:
    print(N-1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt =0
for i in range(N-1):
    p = S[i]+S[i+1] 
    if p =='RL':
        cnt += 1
cnt2 = 0
if S[0] == 'L':
    cnt2 += 1
if S[-1] == 'R':
    cnt2 += 1
N -= cnt *2+cnt2
if cnt - 1 >= K:
    print(N+2*K)
else:
    print(N-1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt =0
for i in range(N-1):
    p = S[i]+S[i+1] 
    if p =='RL':
        cnt += 1
if cnt - 1 >= K:
    print(N-2*(cnt-K))
else:
    print(N-1)

Submission status: Wrong Answer

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

ans = 0
for a, b in zip(S, S[1:]):
    if a == b:
        ans += 1
ans += 2*min(K, X)
if K > X:
    ans += min(Y, K-X)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for a, b in zip(S, S[1:]):
    if a == b:
        ans += 1
ans += 2*max(K, X)
if K > X:
    ans += max(Y, K-X)
print(ans)

Submission status: Wrong Answer

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

same = 0
diff = 0

for i in range(n-1):
    if s[i] != s[i+1]:
        diff += 1
    else:
        same += 1

if diff-1 >= k:
    ans = same + 2*k
else:
    ans = same + 2*(diff-1) + 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = [0]
change = [-1]
for i in range(1,n):
    if s[i] != s[i-1]:
        change.append(i-1)
        
        count.append(count[-1])
    else:
        count.append(count[-1]+1)
change.append(n-1)

ans = 0
if 2*k >= len(change):
    print(n-1)
else:
    for i in range(len(change)-2*k):
        if i == 0 or i+2*k == len(change):
            now = count[i]+change[i+2*k]-change[i]-1 + count[-1]-count[change[i+2*k]]
        else:
            now = count[i]+change[i+2*k]-change[i] + count[-1]-count[change[i+2*k]]
        ans = max(ans,now)
        
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = [0]
change = 0
for i in range(1,n):
    if s[i] != s[i-1]:
        if s[i-1] == "R" and s[i] == "L":
            change += 1
        
        count.append(count[-1])
    else:
        count.append(count[-1]+1)
y = 0
if s[0] == "L":
    y += 1  
if s[-1] == "R":
    y += 1
ans = count[-1]
if k <= change:
    ans += 2*k
elif k > change:
    ans += 2*change + min(k-change,y)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = [0]
change = 0
for i in range(1,n):
    if s[i] != s[i-1]:
        if s[i-1] == "R" and s[i] == "L":
            change += 1
        
        count.append(count[-1])
    else:
        count.append(count[-1]+1)
y = 0
if s[0] == "L":
    y += 1  
if s[-1] == "R":
    y += 1
ans = count[-1]
if k <= change:
    ans += 2*k
else:
    ans += 2*change + min(change-k,y)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = [0]
change = [-1]
for i in range(1,n):
    if s[i] != s[i-1]:
        change.append(i-1)
        
        count.append(count[-1])
    else:
        count.append(count[-1]+1)
change.append(n-1)

ans = 0
if 2*k >= len(change):
    print(n-1)
else:
    for i in range(len(change)-2*k):
        if i == 0:
            now = count[i]+change[i+2*k]-change[i]-1 + count[-1]-count[change[i+2*k]]
        else:
            now = count[i]+change[i+2*k]-change[i] + count[-1]-count[change[i+2*k]]
        ans = max(ans,now)
        
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# WA

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

START = S[0]
prev = S[0]
ans = 0
for i in range(1, N):
    s = S[i]

    if prev == s:
        ans += 1
    else:
        if s == START and K > 0:
            ans += 2 if i < N-1 else 1
            K -= 1

        prev = s

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# WA

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

START = S[0]
prev = S[0]
ans = 0
for i in range(1, N):
    s = S[i]

    if prev == s:
        ans += 1
    else:
        if s == START and K > 0:
            ans += 2 if i < N else 1
            K -= 1

        prev = s

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

NUM_L = [0] * (N + 1)
NUM_R = [0] * (N + 1)

S += ['M']
prev = 'M'
cnt = 0

for i in range(N+1):
    s = S[i]
    if prev == s and not (i == 0 or i == N):
        cnt += 1

    if not (prev == s):
        if prev == 'L':
            NUM_L[cnt+1] += 1
        elif prev == 'R':
            NUM_R[cnt+1] += 1

        cnt = 0
        prev = s


def check(s):
    ans = 0
    if s == 'L':
        arr1 = NUM_L
        arr2 = NUM_R
    else:
        arr1 = NUM_R
        arr2 = NUM_L

    for i in range(1, N + 1):
        n = arr1[i]
        ans += (i - 1) * n

    cnt = K
    for i in range(N, -1, -1):
        n = arr2[i]
        if n == 0:
            continue

        p = min(cnt, n)

        ans += (i + 1) * p
        cnt -= p

        if cnt == 0:
            break

    return ans


ans = max(check('L'), check('R'))
ans = min(ans, N-1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

NUM_L = [0] * (N + 1)
NUM_R = [0] * (N + 1)

S += ['M']
prev = 'M'
cnt = 0

for i in range(N+1):
    s = S[i]
    if prev == s and i != N:
        cnt += 1

    if not (prev == s):
        if prev == 'L':
            NUM_L[cnt+1] += 1
        elif prev == 'R':
            NUM_R[cnt+1] += 1

        cnt = 0
        prev = s


def check(s):
    ans = 0
    if s == 'L':
        arr1 = NUM_L
        arr2 = NUM_R
    else:
        arr1 = NUM_R
        arr2 = NUM_L

    for i in range(1, N + 1):
        n = arr1[i]
        ans += (i - 1) * n

    cnt = K
    for i in range(N, -1, -1):
        n = arr2[i]
        if n == 0:
            continue

        p = min(cnt, n)

        ans += (i + 1) * p
        cnt -= p

        if cnt == 0:
            break

    return ans


ans = max(check('L'), check('R'))
ans = min(ans, N-1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

NUM_L = [0] * (N + 1)
NUM_R = [0] * (N + 1)

S += ['M']
prev = 'M'
cnt = 0

for i in range(N+1):
    s = S[i]
    if prev == s:
        cnt += 1

    if not (prev == s):
        if prev == 'L':
            NUM_L[cnt+1] += 1
        elif prev == 'R':
            NUM_R[cnt+1] += 1

        cnt = 0
        prev = s


def check(s):
    ans = 0
    if s == 'L':
        arr1 = NUM_L
        arr2 = NUM_R
    else:
        arr1 = NUM_R
        arr2 = NUM_L

    for i in range(1, N + 1):
        n = arr1[i]
        ans += (i - 1) * n

    cnt = K
    for i in range(N, -1, -1):
        n = arr2[i]
        if n == 0:
            continue

        p = min(cnt, n)

        ans += (i + 1) * p
        cnt -= p

        if cnt == 0:
            break

    return ans


ans = max(check('L'), check('R'))
ans = min(ans, N-1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

NUM_L = [0] * (N + 1)
NUM_R = [0] * (N + 1)

S += ['M']
prev = 'M'
cnt = 0

for i in range(N+1):
    s = S[i]
    if prev == s:
        cnt += 1

    if not (prev == s):
        if prev == 'L':
            NUM_L[cnt+1] += 1
        elif prev == 'R':
            NUM_R[cnt+1] += 1

        cnt = 0
        prev = s

print(NUM_L)
print(NUM_R)


def check(s):
    ans = 0
    if s == 'L':
        arr1 = NUM_L
        arr2 = NUM_R
    else:
        arr1 = NUM_R
        arr2 = NUM_L

    for i in range(1, N + 1):
        n = arr1[i]
        ans += (i - 1) * n

    cnt = K
    for i in range(N, -1, -1):
        n = arr2[i]
        if n == 0:
            continue

        p = min(cnt, n)

        ans += (i + 1) * p
        cnt -= p

        if cnt == 0:
            break

    return ans


ans = max(check('L'), check('R'))
ans = min(ans, N-1)
print(ans)

Submission status: Wrong Answer

Solution code:


def submit():
    n, k = map(int, input().split())
    s = input()

    dlist = []
    dlist.append([s[0], 1])
    for c in s[1:]:
        if c == dlist[-1][0]:
            dlist[-1][1] += 1
        else:
            dlist.append([c, 1])

    curr = 0
    for i in range(1, len(dlist), 2):
        if curr < k:
            if dlist[i][0] == 'R':
                dlist[i][0] = 'L'
            else:
                dlist[i][0] = 'R'
            curr += 1

    unhappy = 0
    for i in range(len(dlist) - 1):
        if dlist[i][0] == 'R' and dlist[i + 1][0] == 'L':
            unhappy += 2
    if dlist[0][0] == 'L':
        unhappy += 1
    
    if len(dlist) > 1 and dlist[-1][0] == 'R':
        unhappy += 1

    print(n - unhappy)

submit()

Submission status: Wrong Answer

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

ans = 0
w = 0
for i in range(n-1):
    if s[i+1] == s[i]:
        ans += 1
    else:
        w += 1

if w == 0:
    p = [0]
elif w == 1:
    p = [1]
else:
    p = [2 for _ in range(w-1)] + [1]

ans + sum(p[:k])


print(ans + sum(p[:k]))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, K = input_nums()
    S = input()
    now=''
    cnt=0
    for s in S:
        if s!=now:
            cnt+=1
        now=s
    """
    cnt => 不幸な人の数+1(最初のみ無条件でインクリメントしているため)
    N-1 => 最大の幸福数（端の人は前に人がいないため-1）
    N-cnt =>
        初期状態の幸福な人の数．
        正確には (N-1) - (cnt-1)である．
        これは最初のcntのインクリメントが無条件に行われるため．
    2*K =>
        1回の操作で幸福な人を2人増やせるので
        K回の操作では2*K人を幸福にできる
    """
    if cnt==2:
        ans = N-1
    else:
        ans = N-cnt+2*K
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())

S=list(input())

clu=[]
temp=0
for i in range(1,len(S)):
    if S[i]==S[i-1]:
        temp+=1
    else:
        clu.append(temp)
        temp=0
clu.append(temp)

ans=sum(clu)
ins=len(clu)-2


ans+=min(K,ins)*2
K-=ins
if K>0:
    ans+=1

#print(ins)
print(ans)
#print(clu)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def calc(s, k):
    ret = 0
    rev = 0
    c = s[0]
    for i in range(1, len(s)):
        if s[i] == c:
            ret += 1
            continue
        rev += 1
        c = s[i]
    ret += min(rev, k*2)
    k -= rev//2
    return ret


def main():
    n, k = rli()
    s = input()
    rs = ""
    for i in range(len(s)):
        if (s[n-i-1] == 'L'): rs += 'R'
        else: rs += 'L'
    a1 = calc(s,k)
    a2 = calc(rs,k)
    print(s)
    print(rs)
    print(max(a1, a2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def calc(s, k):
    ret = 0
    rev = 0
    c = s[0]
    for i in range(1, len(s)):
        if s[i] == c:
            ret += 1
            continue
        rev += 1
        c = s[i]
    ret += min(rev, k*2)
    k -= rev//2
    if c == 'L' and k > 0:
        ret += 1
    return ret


def main():
    n, k = rli()
    s = input()
    rs = ""
    for i in range(len(s)):
        if (s[n-i-1] == 'L'): rs += 'R'
        else: rs += 'L'
    a1 = calc(s,k)
    a2 = calc(rs,k)
    # print(s)
    # print(rs)
    print(max(a1, a2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

if K <= dif//2:
    ans += 2*K
elif dif != 0:
    ans += 2*(dif//2) + 1

print(ans)

Submission status: Wrong Answer

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

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

if K <= dif//2:
    ans += 2*K
else:
    ans += 2*(dif//2) + 1

print(ans)

Submission status: Wrong Answer

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

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

ans += 2 * k

print(n-1,ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

    ans = 0
    rev = False
    rev_count = 0

    for i in range(1,N):
        if S[i] != S[i-1]:
            if rev:
                S[i] = 'L' if S[i] == 'R' else 'L'
                ans += 1
            else:
                if rev_count + 1 <= K:
                    rev_count += 1
                    rev = True
                    S[i] = 'L' if S[i] == 'R' else 'L'
                    ans += 1
        else:
            ans += 1
            rev = False

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Face Produces Unhappiness
https://atcoder.jp/contests/abc140/tasks/abc140_d

"""
import sys

import re

def solve(N, K, S):
    x = len(re.findall(r'RL', S))
    y = (S[0] == 'L') + (S[-1] == 'R')
    ans = N - 2 * x - y
    exe = min(x, K)
    K -= exe
    ans += 2 * exe
    while K and y:
        ans += 1
        K -= 1
        y -= 1
    return ans


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

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

A = 0
B = 0

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

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

ans = A * 2 + B

while(K > 0):
    if A > 0:
        ans -= 2
        A -= 1
    elif B > 0:
        ans -= 1
        B -= 1
    else:
        break
    K -= 1

print(N - ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = 0
happy = 0
for i in range(n-1):
    if s[i:i+2] == 'RL':
        cnt += 1
    elif s[i] == s[i+1]:
        happy += 1
happy += 2 * min(cnt, k)
if k - cnt > 0 and s[0] != s[-1]:
    happy += 1
print(happy)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
ans = s.count("RL") * 2
ans1 = 0
if s[0]=="L":
	ans1 += 1
if s[-1]=="R":
	ans1 += 1
while k > 0:
	if ans > 0:
		ans -= 2
	elif ans1 > 0:
		ans1 -= 1
	k -= 1
print(n - ans - ans1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
ans = s.count("RL") * 2
if s[0]=="L":
	ans += 1
if s[-1]=="R":
	ans += 1
print(n - ans + 2 * k)

Submission status: Wrong Answer

Solution code:
import heapq
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, K = map(int, input().split())
    S = input()
    # groups = []
    # length = 0
    ans = 0
    group = 1
    src = S[0]
    for dst in S[1:]:
        if dst == src:
            ans += 1
        else:
            group += 1
        src = dst
    if group % 2:
        ans += 2 * max(K, group // 2)
    else:
        tmp = group // 2 -1
        if K <= tmp:
            ans += 2 * K
        else:
            ans += 2 * tmp + 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
R=0
B=0
cnt=0
for i in range(len(S)-1):
    if S[i]=='R' and S[i+1]=='L':
        R+=1
    elif S[i]=='L' and S[i+1]=='R':
        B+=1
    elif S[i]=='L' and S[i+1]=='L':
        cnt+=1
    else:
        cnt+=1
N=len(S)
if K<R:
    print(cnt+2*K)
else:
    if K-R<1:
        print(cnt+2*R)
    else:
        print(cnt+2*R+1)