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

LR = S.count("LR")
RL = S.count("RL")
RRLL = N-1-LR-RL

if min(LR,RL) != 0: 
    if K < min(LR,RL):
        ans = RRLL+K*2
        print(ans)
    if K >= min(LR,RL):
        ans = RRLL+min(LR,RL)*2
        print(ans)
if min (LR,RL) == 0:
    if max(LR,RL) !=0:
        print(RRLL+1)
    if max(LR,RL) ==0:
        print(RRLL)

Submission status: Wrong Answer

Solution code:
import itertools
 
N, K = map(int, input().split())
S = input()
 
lis = [len(list(g)) for _, g in itertools.groupby(S)]
 
if len(lis) < 2*K + 1:
  print(N-1)
else:
  print(sum(lis[:2*K+1]))

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=list(input())
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
###幸せな人を数える
count=index-1
for i in range(index,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[N-1]=="L"and S[N-2]=="L" and index<N ):
  count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=list(input())
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=index-1
for i in range(index,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[N-1]=="L"and S[N-2]=="L" and index<N ):
  count+=1
print(count)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 0
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt < 2:
    f = 1
ans1 = ans2 = N-cnt-1
S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1
while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1
while 0 < r and S[r-1] == 0:
    r -= 1
while l < r and k < K:
    k += 1
    ans1 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1

S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1
while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1
while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    ans2 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
print(max(ans1, ans2, f*N-1))

Submission status: Wrong Answer

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

res = 0
pre = None
add = {2: 0}
add[1] = 0 if S[0] == S[-1] else 1
for s in S:
    if pre is None:
        pass
    elif pre == s:
        res += 1
    elif pre == "R":
        add[2] += 1
    pre = s

if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if K > 0:
        res += add[1]
    print(res)

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1

if ((kukann-2)//2) >= k:
    count += k*2
elif kukann % 2 == 0:
    if ((kukann-2)//2) >= 0:
        count += ((kukann-2)//2) * 2
    if ((kukann-2)//2) < k and kukann != 1:
        count += 1
else:
    count += min(k,(kukann-1))
print(count)

Submission status: Wrong Answer

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

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

if bad > k*2:
    ans = (n-1) - bad + (k*2)
else:
    ans = n

print(ans)

Submission status: Wrong Answer

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

res = 0
pre = None
add = {2: 0}
add[1] = 0 if S[0] == S[-1] else 1
for s in S:
    if pre is None:
        pass
    elif pre == s:
        res += 1
    elif pre == "R":
        add[2] += 1
    pre = s

if add[1] + add[2] >= K:
    print(N - 1)
if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if K > 0:
        res += add[1]
    print(res)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
flg = 0
cnt = 0
r = []
ra = 0
for i in range(n):
    if flg == 0 and s[i] == 'R':
        flg = 1
        tmp = i
    elif flg == 1 and s[i] == 'L':
        flg = 0
        r.append((2 if tmp else 1))
        ra += max(0, cnt - 1)
        cnt = 0
    if flg:
        cnt += 1
if flg:
    r.append((1 if tmp else 0))
    ra += max(0, cnt - 1)
flg = 0
cnt = 0
l = []
la = 0
for i in range(n):
    if flg == 0 and s[i] == 'L':
        flg = 1
        tmp = i
    elif flg == 1 and s[i] == 'R':
        flg = 0
        l.append((2 if tmp else 1))
        la += max(0, cnt - 1)
        cnt = 0
    if flg:
        cnt += 1
if flg:
    l.append((1 if tmp else 0))
    la += max(0, cnt - 1)
li = sorted(l + r, reverse=True)
print(sum(li[:k]) + la + ra)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':ct+=1
if ct<=k:
  print(n-1)
  sys.exit()
ans=n-ct*2
ans+=2*k
if s[0]=='L':
  ans-=1
if s[n-1]=='R':
  ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

lis = [len(list(g)) for _, g in itertools.groupby(S)]

if len(lis) <= 2*K + 1:
  print(N-1)
else:
  print(sum(lis[:2*K+1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if n != 1 and s[i] == s[i+1]:
    ans += 1
for i in range(k):
    if ans < n-2:
      ans += 2
    if ans == n-2:
      ans += 1
    else:
      pass
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S = 'x' + S + 'x'
X = []

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

    if (S[i] == 'R') and (S[i + 1] == 'R'):
        ans += 1

n_s = ''
new_S = ''
for s in S[1:-1]:
    if n_s == '':
        n_s = s
        pass

    elif n_s == s:
        pass

    else:
        new_S += n_s
        n_s = s
else:
    new_S += n_s


new_S_len = len(new_S)
X = [2] * ((new_S_len - 1) // 2)

if new_S_len >= 3:
    X.append(1)
    X.append(1)
else:
    X.append(1)

print(ans + sum(X[:K]))

Submission status: Wrong Answer

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

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

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

remove_repulsion = min(K, back2back, repulsion)
back2back -= remove_repulsion
K -= remove_repulsion
repulsion -= remove_repulsion

if K>0 and back2back>0:
    unhappy=1
else:
    unhappy=repulsion*2+repulsion_end

print(N-unhappy)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter

n, k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()
c = Counter(s)

if 2*k >= c['R']:
  print(n-1) 
else:
  init_val = 0
  for i in range(len(s)):
    if s[i] == 'L' and i-1 > 0 and s[i-1] == 'L': init_val += 1
    elif s[i] == 'R' and i+1 < n and s[i+1] == 'R': init_val += 1
  print(min(init_val+2*k, n-1))

Submission status: Wrong Answer

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

def runLength(s):
    a = []
    for c in s:
        if len(a)==0 or a[-1][0]!=c:
            a.append([c,1])
        else:
            a[-1][1] += 1
    return a

def main():
    N,K = map(int, input().split())
    S = input().strip()
    ans = 0
    for i,c in enumerate(S):
        if c == "L":
            if i > 0 and S[i-1] == "L":
                ans += 1
        if c == "R":
            if i < N-1 and S[i+1] == "R":
                ans += 1
    A = runLength(S)
    A.sort(key=lambda x:x[1], reverse=True)
    for i in range(K):
        if i < len(A):
            ans += 2
    if ans == N:
        ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
input = raw_input

def score(s):
    n = len(s)
    ans = 0
    for i in range(n):
        if s[i] == "L" and i >= 1 and s[i-1] == "L":
            ans += 1
        elif s[i] == "R" and i <= n-2 and s[i+1] =="R":
            ans += 1
    return ans

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

s = score(S)
k = 0
lr = 0
rl = 0
for i in range(N-1):
    if S[i] == "L" and S[i+1] =="R":
        lr += 1
    if S[i] == "R" and S[i+1] =="L":
        rl += 1

#print(s)
#print(lr, rl)
c = min(K, lr, rl)
s += 2 * c
if K > min(lr, rl):
    if S[0] == "L" and rl > lr:
        s += 1
    elif S[-1] == "R" and rl < lr:
        s += 1
print(s)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

N, K = inpl()
S = input().rstrip('\n')

elements, nums = get_sequence(S)
if elements[0] == 'R':
    gap = len(elements)//2
    if len(elements) % 2:
        boundary = 1
    else:
        boundary = 0
else:
    gap = (len(elements)-1)//2
    if len(elements) % 2:
        boundary = 1
    else:
        boundary = 2

if K <= gap:
    gap -= K
elif K <= gap + boundary:
    gap = 0
    boundary = boundary + gap - K
else:
    gap = boundary = 0

print(N-gap*2-boundary)

Submission status: Wrong Answer

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

ls = []
c = s[0]
l = 0
for i in range(1, n):
    if s[i] == c:
        l += 1
    else:
        ls.append(l)
        c = s[i]
        l = 0
ls.append(l)
ans = sum(ls)
m = len(ls)
for i in range(k):
    if m == 1:
        break
    elif 3 <= m:
        ans += 2
    else:
        ans += 1
    m -= 1
print(ans)

Submission status: Wrong Answer

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

# 操作はl, r番目の人以外の幸福状態を変えない。適切に選んで操作をすると1回1人(l=r)か2人増える)
groups = []

nowDirection = S[0]
sameCount = 1
for i in range(1, N):
    if S[i] == nowDirection:
        sameCount += 1
    else:
        groups.append(sameCount)
        nowDirection = S[i]
        sameCount = 1
groups.append(sameCount)

groupCount = len(groups)
ans = sum(groups) - groupCount

for _ in range(K):
    if groupCount >= 3:
        ans += 2
        groupCount -= 2
    elif groupCount == 2:
        ans += 1
        groupCount += 1
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

N, K = map(int, input().split())
S = input()
l_list = []
r_list = []
before = S[0]
cnt = 0
for s in S:
  if s != before:
    if s == 'L':
      r_list.append(cnt)
    else:
      l_list.append(cnt)
    cnt = 1
    before = s
  else:
    cnt += 1
if before == 'L':
  l_list.append(cnt)
else:
  r_list.append(cnt)
l_list.sort(reverse=True)
r_list.sort(reverse=True)
if len(l_list) <= K or len(r_list) <= K:
  print(N-1)
else:
  sum_l = reduce(lambda a,b: a+b-1, l_list[:K])-1
  sum_r = reduce(lambda a,b: a+b-1, r_list[:K])-1
  if sum_l > sum_r:
    ans = sum_l + reduce(lambda a,b: a+b-1, r_list)-1 + K*2
    print(ans)
  else:
    ans = sum_r + reduce(lambda a, b: a+b-1, l_list)-1 + K*2
    print(ans)

Submission status: Wrong Answer

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

ret = 0
n_flip_count = 0

last_d = s[0]
for d in s[1:]:
    if last_d == d:
        ret += 1
    else:
        n_flip_count += 1
    last_d = d

if k <= n_flip_count // 2:
    ret += 2 * k

if n_flip_count // 2 < k and n_flip_count % 2 == 1:
    ret += 1

print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
pre = S[0]
ans = 0
for i in range(1, N):
    if pre == S[i]:
        ans += 1
        pre = S[i]
    elif (K != 0) and (pre == "R") and (S[i] == "L"):
        ans += 1
        pre = "R"
        K -= 1
    else:
        pre = S[i]

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split(' ')))
S = input() + 'Z'
old = ''
ct = 0
cts = []
sums = []
for i in range(len(S)):  # 1e5
    if old != S[i]:
        old = S[i]
        if ct:
            cts.append(ct)
            if not sums:
                sums.append(ct)
            else:
                sums.append(sums[-1] + ct)
        ct = 0
    ct += 1
sums = [0, 0] + sums

ans = 0
for uragaesu in range(K + 1):  # 1e5
    for start in range(N):  # 1e5 / 2 ... TLE?
        if start + 2 * uragaesu + 1 >= len(sums):
            break
        ans = max(ans, sums[start + 2 * uragaesu + 1] - sums[start])
    if ans == sums[-1]:
        break
        
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = (int(x) for x in input().split(' '))
S = input()

x = [1]
for i in range(1, N):
    if S[i-1] == S[i]:
        x[-1] += 1
    else:
        x.append(1)

if len(x) > 2+K+1:
    ans = sum(x) - len(x) + 2*K
else:
    ans = N - 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
S=input()
lst=[1 if S[i]=="L" else 0 for i in range(N)]
if lst[0]!=1:
  lst=[1 if e==0 else 0 for e in lst]
score=0
for _ in range(K):
  start, end=N, 0
  for i in range(N):
    if lst[i]==0:
      start=i
      break
  flag=False
  for i in range(N-1, 0, -1):
    if flag and lst[i]==0:
      end=i
      break
    else:
      if lst[i]==1:
        flag=True
  if start==N and end==0:
    break
  if end<start:
    end=N-1
  lst[start:end+1]=[1 if e==0 else 0 for e in lst[end:start-1:-1]]
  score=0
  for i in range(1, N):
    if lst[i-1]==lst[i]:
      score+=1
  if score==N-1:
    break
print(score)

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1
if ((kukann-2)//2) >= k:
    count += k*2
else:
    if ((kukann-2)//2) >= 0:
        count += ((kukann-2)//2) * 2
    if ((kukann-2)//2) < k and kukann != 1:
        count += 1
print(count)

Submission status: Wrong Answer

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

#r=lかつr+1=l-1かつr!=r+1となるようなr,lを選ぶと+2点操作になる.
# また、r=n-1かつl!=l-1あるいはl=0かつr!=r+1となるようなr,lを選ぶと+2点操作になる.
#それ以外の場合は必ず0点以下となる.
# つまり、l!=l-1かつl=R,l!=l-1かつl=L,r!=r+1かつr=R,r!=r+1かつr=Lの数をそれぞれ数え、いい感じにマッチングすれば答えが出る.
mismatch_count = {"R":[0, 0], "L":[0, 0]}
ans = 0
for i in range(n):
    if(i < n-1):
        if(s_list[i] != s_list[i+1]):
            mismatch_count[s_list[i]][1] += 1
        elif(s_list[i] == "R"):
            ans += 1
    if(i > 0):
        if(s_list[i] != s_list[i-1]):
            mismatch_count[s_list[i]][0] += 1
        elif(s_list[i] == "L"):
            ans += 1

plus_r = min(mismatch_count["R"])
plus_l = min(mismatch_count["L"])
if(k >= plus_r):
    ans += plus_r * 2
    k -= plus_r
elif(plus_r > 0):
    ans += k * 2
    k = 0
if(k >= plus_l):
    ans += plus_l * 2
    k -= plus_l
elif(plus_l > 0):
    ans += k * 2
    k = 0
left_r = max(mismatch_count["R"]) - plus_r
left_l = max(mismatch_count["L"]) - plus_l
if(k >= left_r):
    ans += left_r
    k -= left_r
elif(left_r > 0):
    ans += k
    k = 0
if(k >= left_l):
    ans += left_l
    k -= left_l
elif(left_l > 0):
    ans += k
    k = 0

print(ans)

Submission status: Wrong Answer

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

base = 0
odd = 0

pre_char = 'N'
for index, char in enumerate(s):
    if index > 0:
        if pre_char == char:
            base += 1
        else:
            odd += 1
    pre_char = char

if k >= odd:
    output = len(s) - 1
else:
    output = base + 2 * k
print(output)

Submission status: Wrong Answer

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

res = 0
pre = None
add = {1: 0, 2: 0}
stack = ""
for s in S:
    if pre is None:
        if s == "L":
            add[1] += 1
    elif pre == s:
        res += 1
    elif pre == "R":
        add[2] += 1
    pre = s

if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if K > 0:
        res += add[1]
    print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter

n, k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()
c = Counter(s)

if 2*k >= c['R']:
  print(n-1) 
else:
  init_val = 0
  for i in range(len(s)):
    if s[i] == 'L' and i-1 > 0 and s[i-1] == 'L': init_val += 1
    elif s[i] == 'R' and i+1 < n and s[i+1] == 'R': init_val += 1
  print(init_val+2*k)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n, k = na()
s = input()
ct = 0
lll = 1
li = []
for i in range(n-1):
    if s[i] == s[i+1]:
        lll += 1
    else:
        li.append(lll)
        lll = 1
li.append(lll)
ans = []
for a, b in zip(li[0::2], li[1::2]):
    ans.append(a+b)
ans.sort()
if len(ans) == 1:
    print(ans[0] - 1)
else:
    print(sum(ans[-k:]))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
if len(s) == 1:
    print(0)
    exit()
ans = 0
c = 0
for i in range(1,n):
    if s[i] == s[i-1]:
     ans += 1
    else:
        c += 1
if c == 1:
    print(ans+1)
    exit()

t = -1
now = s[0]
for i in range(1,n):
    now = s[i-1]
    if s[i] != now:
        t += 1
    if t == 1 and k > 0:
        k -= 1
        ans += 2
        t = -1
        now = s[i]
    #print(ans)

if k > 0 and now != s[n-1]:
    ans += 1


print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

N, K = map(int, input().split())
S = ['X'] + list(input()) + ['X']

check_list = []

minus = 0
plus = 0
happiness = -1
tmp = S[0]
for i in range(1, N + 1):
    c = S[i]
    if tmp == 'R' and c != 'R':
        # check_list = check_list + [-1]
        minus += 1
    elif tmp == 'L' and c != 'L':
        # check_list = check_list + [1]
        plus += 1
    else:
        happiness += 1
    tmp = c
    # print(c, minus, plus, happiness)

moves = min(minus, plus, K)
ans = happiness + 2 * moves
if K - moves > 0:
    if minus < plus and plus - moves > 0 and S[-2] == 'R':
        ans += 1
    elif minus > plus and minus - moves > 0 and S[1] == 'L':
        ans += 1

print(ans)

Submission status: Wrong Answer

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

s = s+"a" #拡張して扱いやすくする

lst = [i for i in s]

ans = 0
memo1 = 0
memo2 = 0
for i in range(n):
    if lst[i] == lst[i+1]:
        ans += 1
    elif lst[i] == "L" and lst[i+1] == "R":
        memo1 += 1
    elif lst[i] == "R" and lst[i+1] == "L":
        memo2 += 1
        
ans += 2*min(k,memo1,memo2)
num = min(k,memo1,memo2)

jdg = False

if memo1 >= num+1 and lst[n-1] == "R":
    ans += 1
    jdg = True
if jdg:
    if memo1 >= num+2 and lst[0] == "L":
        ans += 1
else:
    if memo1 >= num+1 and lst[0] == "L":
        ans += 1
    

print(ans)

Submission status: Wrong Answer

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

res = 0
pre = None
add = {1: 0, 2: 0}
stack = ""
for s in S:
    if pre is None:
        if s == "L":
            add[1] += 1
    elif pre == s:
        res += 1
    elif pre == "R":
        add[2] += 1
    pre = s

if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if K > 0:
        res += 1
    print(res)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
s_num = []
point = 1
prev = s[0]
ans = 0
for i in range(n - 1):
    if prev != s[i + 1]:
        prev = s[i + 1]
        s_num.append(point)
        ans += point - 1
        point = 1        
    else:
        point += 1
s_num.append(point)
ans += point - 1
kkk = max(len(s_num) - 2, 0)
ans = ans + min(kkk, k) * 2
if kkk < k:
    ans += k - kkk
ans = min(n, ans)
print(ans)

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1
#print(count)
#print(kukann)
if ((kukann-2)//2) >= k:
    count += k*2
else:
    count += ((kukann-2)//2) * 2
    if ((kukann-2)//2) < k:
        count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=list(input())
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) :
    index+=1
S[index-1]=a
###幸せな人を数える
count=index-1
for i in range(index,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[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 = list(input())
s_num = []
point = 1
prev = s[0]
ans = 0
for i in range(n - 1):
    if prev != s[i + 1]:
        prev = s[i + 1]
        s_num.append(point)
        ans += point - 1
        point = 1        
    else:
        point += 1
s_num.append(point)
ans += point - 1
kkk = max(len(s_num) - 2, 0)
ans = ans + min(kkk, k) * 2
if kkk < k:
    ans += k - kkk
ans = max(n, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
# Setup
import sys
import collections
sys.setrecursionlimit(4100000)

n, k = [int(i) for i in input().split()]
s = input()

ans = 0
lblocks = 0
# if s[0] == "L":
#     lblocks += 1
flag = False
if s[0] == "L":
    flag = True
for i,v in enumerate(s):
    if i == 0:
        continue
    if s[i] == s[i-1]:
        ans += 1
    if s[i] == "L":
        if not flag:
            lblocks += 1
            flag = True
    else:
        flag = False
if lblocks == 0:
    print(n-1)
else:
    if lblocks <= k:
        print(n-1)
    else:
        print(ans+k+k)

Submission status: Wrong Answer

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

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

ans = 0
if s[0] == "R" and s[1] == "R":
    ans += 1
if s[n-1] == "L" and s[n-2] == "L":
    ans += 1

hanten = 0
for i in range(1, n-1):
    if s[i] == "L":
        if s[i-1] == "L":
            ans += 1
    else:
        if s[i+1] == "R":
            ans += 1
    if s[i-1] != s[i]:
        hanten += 1

if k <= hanten // 2:
    print(ans + k*2)
else:
    print(ans + hanten)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
s_num = []
point = 1
prev = s[0]
ans = 0
for i in range(n - 1):
    if prev != s[i + 1]:
        prev = s[i + 1]
        s_num.append(point)
        ans += point - 1
        point = 1        
    else:
        point += 1
s_num.append(point)
ans += point - 1
kkk = max(len(s_num) - 2, 0)
ans = ans + min(kkk, k) * 2
if kkk < k:
    ans += k - kkk
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S += 'x'
X = []
n_s = ''
start = 0

for i, s in enumerate(S[:-1]):
    if s != S[i + 1]:
        if (start == 0) or (i == N - 1):
            X.append(1)
        else:
            X.append(2)
        start = i + 1

ans = 0
S = 'x' + S
for i in range(1, N + 1):
    if (S[i] == 'L') and (S[i - 1] == 'L'):
        ans += 1

    if (S[i] == 'R') and (S[i + 1] == 'R'):
        ans += 1

X.sort(reverse=True)
print(ans + sum(X[:K]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from heapq import heappush, heappop, heapify
from bisect import bisect_left
from itertools import groupby

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

        counts = [len(list(g)) for k, g in groupby(S)]

        n = len(counts)
        res = 0
        for i in range(n):
            res += counts[i]-1

        # print(K, n)
        if n > (2 * K):
            res += K*2
        else:
            print('aaa')
            res = N-1
        print(res)


if __name__ == '__main__':
    Solution().main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
ls = []
l = 1
ans = 0
count = 0
pcount = 0
prev = 0
for i in range(N):
  if S[i]=='L':
    if i!=0 and S[i]==S[i-1]:
      ans += 1
  else:
    if i!=N-1 and S[i]==S[i+1]:
      ans += 1
  
  if i!=N-1 and S[i]!=S[i+1]:
    if prev==0 or i==N-1:
      pcount += 1
    else:
      count += 1
    prev = i+1
#print(ans, count, pcount)
for i in range(K):
  if count>0:
    count -= 1
    ans += 2
    continue
  elif pcount>0:
    pcount -= 1
    ans += 1
    continue
  break
print(ans)

Submission status: Wrong Answer

Solution code:

import sys
import collections
sys.setrecursionlimit(4100000)

n, k = [int(i) for i in input().split()]
s = input()

ans = 0
lblocks = 0
# if s[0] == "L":
#     lblocks += 1
flag = False
if s[0] == "L":
    flag = True
for i,v in enumerate(s):
    if i == 0:
        continue
    if s[i] == s[i-1]:
        ans += 1
    if s[i] == "L":
        if not flag:
            lblocks += 1
            flag = True
    else:
        flag = False
if lblocks <= k:
    print(n-1)
else:
    print(ans+k+k)

Submission status: Wrong Answer

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

prev = None
num_regions = 0
score = 0
for c in S:
  if c != prev:
    num_regions += 1
  else:
    score += 1
  prev = c

if K >= num_regions - 2 - (num_regions % 2):
  print(N-1)
else:
  print(score + K*2)

Submission status: Wrong Answer

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

ans = 0
RL = 0
LR = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        ans += 1
    elif S[i] == "R" and S[i+1] == "L":
        RL += 1
    elif S[i] == "L" and S[i+1] == "R":
        LR += 1
if RL != 0 and LR != 0:
    ans += 2 * min(K, RL, LR)
elif RL != 0 or LR != 0:
    ans = N-1
print(ans)

Submission status: Wrong Answer

Solution code:

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1
if (kukann-2) >= k:
    count += k*2
else:
    count += (kukann-2) * 2
    if (kukann-1) == k:
        count += 1
    else:
        count += 2 
print(count)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if s[i] == s[i+1]:
    ans += 1
for i in range(k):
  while True:
    if ans < n-2:
      ans += 2
    if ans == n-2:
      ans += 1
    else:
      break
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
c = s[0]
con = 1
odd = []
even = []
now = 1
ans = 0

for i in range(1, n):
    if s[i] == c:
        con += 1
    else:
        c = s[i]
        if not con == 1:
            ans += con - 1
        if now == 1:
            odd += [con]
            now = 2
        elif now == 2:
            even += [con]
            now = 1
        con = 1
else:
    if not con == 1:
        ans += con - 1
    if now == 1:
        odd += [con]
    elif now == 2:
        even += [con]

odd_l = len(odd)
even_l = len(even)
length = max(odd_l, even_l)
if k >= length:
    print(ans + (length-1) * 2 + 1)
else:
    print(ans + k * 2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
s = input()
ok = False
if s[0] != s[-1]:
    s += 'LR'[s[-1] == 'L']
    n += 1
    ok = True
li = [0] * n
ans = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        ans += 1
    else:
        li[i + 1] += 1
    li[i + 1] += li[i]
for i in range(n):
    li[i] //= 2
if ok and li[-1] <= k:
    print(ans + 2 * li[-1] - 1)
else:
    print(ans + 2 * k)

Submission status: Wrong Answer

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

check=s[0]
right=0
left=0
for i in range(1,n):
    if s[i]==check and check=='R':
        right+=1
    elif s[i]==check and check=='L':
        left+=1
    check = s[i]
ans =n-right-left
while k>0:
    if ans>=5:
        ans-=2
    elif 3<=ans <=4:
        ans-=1
    elif ans==2:
        ans-=1
    else:
        break
    k-=1
print(n-ans)

Submission status: Wrong Answer

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

mae = s[0]
cnt = 0
happy = 0

if mae == "R":
    if n>=2:
        if s[1]=="R":
            happy += 1

for i in range(1,n):
    now = s[i]

    if now=="L":
        if mae=="L":
            happy += 1
    if now=="R":
        if i<n-1:
            if s[i+1]=="R":
                happy+=1


    if now != mae:
        cnt += 1

    mae = now

n_set = cnt//2
n_amari = cnt%2

if k <= n_set:
    happy += k*2
else:
    happy += n_set*2+1

print(happy)