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: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
S = input()
out = ['0'] * len(S)
for i in range(K):
    if i!=0:
        S = ''.join(out)
    idx = 0
    maxidx = 0
    length = 1
    maxlen = 10000
    back = S[0]
    for j in range(len(S)):
        if back == S[j]:
            length += 1
        else:
            if length <= maxlen:
                #print(length)
                maxlen = length
                maxidx = j
                length = 1
            else:
                length = 1
                idx = j
        if length == j+2 and S[0] == 'L':
            length -= 1
        if j == len(S)-1 and S[j] == 'R':
            length -= 1
        back = S[j]
    temp = ['0'] * maxlen
    for j in range(maxlen):
        #print([maxidx,maxlen])
        if maxlen == 1:
            if S[maxidx + maxlen - j - 2] == 'L':
                temp[j] = 'R'
            else:
                temp[j] = 'L'
        else:
            if S[maxidx + maxlen - j - 1] == 'L':
                temp[j] = 'R'
            else:
                temp[j] = 'L'
    for j in range(len(S)):
        out[j] = S[j]
    for j in range(maxlen):
        if maxlen == 1:
            out[maxidx + j - 1] = temp[j]
        else:
            out[maxidx + j] = temp[j]
#print(''.join(out))
count=0
#right
for i in range(len(S)):
    if i==0 and out[i]=='L':
        continue
    elif i==N-1 and out[i]=='R':
        break
    if out[i]=='R' and out[i+1]=='R':
        count += 1
    if out[i]=='L' and out[i-1]=='L':
        count += 1
print(count)

Submission status: Runtime Error

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

S = input()

b = 0
p1 = 0
p2 = 0
for i in range(N):
    if i == 0:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "L":
            p1 += 1
        else:
            if S[i] == "R" and S[i+1] == "L":
                p2 += 1
    elif 0 < i < N-1:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "R" and S[i+1] == "L":
            p2 += 1
    elif i == N-1:
        if S[i] == "R":
            p1 += 1

#print(b)
if K <= p2:
    b += 2*K
else:
    b += 2*K
    K -= p2
    b += max(p1, K)

print(min(N-1,b))

Submission status: Runtime Error

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

# Lの数 - Rの数として一番小さい数を返す部分列を返す
def REVERSE():
    sub_str = (-1, -1) # index of sub_string, both are inclusive
    score = 100
    for i in range(1, N): # 0は前いないからskip
        tmp = 0
        for j in range(i, N):
            tmp = S[i:j+1].count('L') - S[i:j+1].count('R')
            if tmp < score:
                score = tmp
                sub_str = (i, j)
    return sub_str

for i in range(0, K):
    sub_str = REVERSE()
    tmp = S[0:sub_str[0]]
    for j in range(sub_str[0], sub_str[1]+1):
        if S[j] == 'L':
            tmp += 'R'
        else:
            tmp += 'L'
    if sub_str[1] != len(S)-1:
        tmp += S[sub_str[1]+1: len(S)]
    S = tmp

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

print(happiness)

Submission status: Runtime Error

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

s_c = []

left = 0
right = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        right = i + 1
    else:
        s_c.append(''.join(s[left:right + 1]))
        i += 1
        left = i
        right = i
s_c.append(''.join(s[left:right + 1]))

cc = 'R'
if s_c[1][0] == 'R':
    cc = 'L'

s_c_len = len(s_c)
for i in range(k):
    if (2 * i + 1) > s_c_len - 1:
        break
    s_c[2 * i + 1] = cc * len(s_c[2 * i + 1])

ans_s = ''.join(s_c)
ans = 0
for i in range(n):
    if ans_s[i] == 'R' and i != n - 1 and ans_s[i + 1] == 'R':
        ans += 1
    elif ans_s[i] == 'L' and i != 0 and ans_s[i - 1] == 'L':
        ans += 1
print(ans)

Submission status: Runtime Error

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

s_c = []

left = 0
right = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        right = i + 1
    else:
        s_c.append(''.join(s[left:right + 1]))
        i += 1
        left = i
        right = i
s_c.append(''.join(s[left:right + 1]))

cc = 'R'
if s_c[1][0] == 'R':
    cc = 'L'
for i in range(k):
    s_c[2 * i + 1] = cc * len(s_c[2 * i + 1])

ans_s = ''.join(s_c)
ans = 0
for i in range(n):
    if ans_s[i] == 'R' and i != n - 1 and ans_s[i + 1] == 'R':
        ans += 1
    elif ans_s[i] == 'L' and i != 0 and ans_s[i - 1] == 'L':
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
a,b=input().strip().split(' ')
a,b=[int(a),int(b)]
c=input().strip()
d=0
if c[0]=='R':
  if c[1]=='R':
    d+=1
  
for i in range(1,a-1):
  if c[i]=='L':
    if c[i-1]=='L':
      d+=1
  elif c[i]=='R':
    if c[i+1]=='R':
      d+=1
if c[a-1]=='L':
  if c[a-2]=='L':
    d+=1
d=d+2*k
if d>(a-1):
  print (a-1)
else:
  print(d)

Submission status: Runtime Error

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

if s[0] == 'R':
  s.insert(0, 'L')
if s[-1] == 'R':
  s.append('L')
s.append(0)
l = []
r = []


for i in range(len(s)-1):
  if s[i] == 'L':
    l.append(i)
    if s[i+1] == 'L':
      ans += 1
      
for i in range(len(l)-1):
  r.append(l[i+1] - l[i] - 1)

r.sort(reverse = True)

for i in range(k):
  if r[i] != 0:
    ans += r[i] + 1
  else:
    break

if n == 1:
  print(0)
elif n == 2:
  print(1)
elif ans == n:
  print(n-1)
else:
  print(ans)

Submission status: Runtime Error

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

if s[0] == 'R':
  s.insert(0, 'L')
if s[-1] == 'R':
  s.append('L')
s.append(0)
l = []
r = []


for i in range(len(s)-1):
  if s[i] == 'L':
    l.append(i)
    if s[i+1] == 'L':
      ans += 1
      
for i in range(len(l)-1):
  r.append(l[i+1] - l[i] - 1)

r.sort(reverse = True)

for i in range(k):
  if r[i] != 0:
    ans += r[i] + 1
  else:
    break

if n == 1:
  print(0)
elif ans == n:
  print(n-1)
else:
  print(ans)

Submission status: Runtime Error

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

if s[0] == 'R':
  s.insert(0, 'L')
if s[-1] == 'R':
  s.append('L')
s.append(0)
l = []
r = []


for i in range(len(s)-1):
  if s[i] == 'L':
    l.append(i)
    if s[i+1] == 'L':
      ans += 1
      
for i in range(len(l)-1):
  r.append(l[i+1] - l[i] - 1)

r.sort(reverse = True)

for i in range(k):
  if r[i] != 0:
    ans += r[i] + 1

if ans == n:
  print(n-1)
else:
  print(ans)

Submission status: Runtime Error

Solution code:
#include "bits/stdc++.h"

#define REP(i, a, b) for (ll i = (a); i <= (b); ++i)
#define REV(i, a, b) for (ll i = (a); i >= (b); --i)
#define FORV(a, A) for (auto &a : A)

using namespace std;

using ll = long long;

void Main()
{
   int N, K;
   cin >> N >> K;
   int k = 0;
   char S, prevS = 0;
   REP(i, 0, N-1) {
      cin >> S;
      if (S!=prevS) {
         k++;
         swap(S, prevS);
      }
   }

   cout << min(N-k+2*K, N-2) << endl;
}

int main()
{
   std::cin.tie(nullptr);
   std::ios_base::sync_with_stdio(false);
   std::cout << std::fixed << std::setprecision(15);
   Main();
   return 0;
}

Submission status: Runtime Error

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

c = 0
t = 0
ans = 0

for i in range(n-1):
  try:
    if s[i] == s[i+1]  t += 1
  except IndexError:
    continuw:
  for j in range(1:n-1):
      try:
        if s[i-1] != s[i] and s[i]s[i] == s[j] and s[i-1] == s[j+1]:
          c += 1
          if c =< k:
            break
          continue:
      except IndexError:
        continue:

ans = t + 2 * c + (k - c)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(map(str, input()))
score = 0
for i in range(1, N):
	if S[i] == S[i + 1]:
		score += 1
print(min(score + 2 * K, N - 1))

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
S = input()
tmp = S[0]
cnt = 0
L = []
for x in S:
    if tmp == x:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
        tmp = x
L.append(cnt)
tmp = 0
for _ in range(b):
    for _ in range(2):
        tmp += L.pop(0)
tmp += L.pop(0)
L = [tmp] + L
print(sum(L) - len(L))

Submission status: Runtime Error

Solution code:
n,k=map(int,input())
s=[i for i in range input()]
num=0
for i in range(len(s)):
    if s[i]==s[i+1]:
        num+=1
print(min(n-1,num+2*k))

Submission status: Runtime Error

Solution code:
#
def happiness(s):
    h = 0
    c = s[0]
    for i in range(1,len(s)):
        if s[i] == c:
            h = h + 1
        else:
            c = s[i]
    return h

def schrun(s,l):
    c = s[l]
    i = l+1
    while (i<len(s)) and (s[i] == c) :
        i = i+1
    return i
        
#
n, k = map(int, input().split())
s = input()
#
h = happiness(s)
i = 0
l = 0
while( (i<k) and (l<n) ):
    l = schrun(s,l)
    r = schrun(s,l)-1
    if r == n-1:
        h = h + 1
    else:
        h = h + 2
    i = i + 1
    l = r + 1

print(h)

Submission status: Runtime Error

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

happy = 0
for n in range(N - 1):
     if B[n] == B[n + 1]:
            happy += 1

print(N -1 if happy + K*2 > n-1 else happy + K*2)

Submission status: Runtime Error

Solution code:
## 幸福度が変わるかどうかは操作Kによって変更されたl, r番目の人間だけなので、
## 左右l,r番目の人間が幸福であるかどうかを見てゆく
## for(n)回のループでまず「既に 1 + x, 任意の r が幸福であるかどうか」を確認して、
## 幸福度2, 1の順でそのl,rをK回覚える
n, k = map(int, input().split())
humans = list(input())
k_count = 0
last_r = n ## 最後に操作したr(番目)
dev_null = 0

for x in range(n - 1):
  if x == last_r:
    break ## すべての可能性を確認した
  if x == 0:
    continue
  l = r = "n"
  if k_count == k:
    break ## 既にK回試行したのでbreak
  if humans[x] == "L" and humans[x - 1] == "L":
    continue
  elif humans[x] == "R" and humans[x - 1] == "R" and humans[x + 1] == "R":
    continue
  else:
    ## 不幸なので、lを確定
    l = x + 1 ## 不幸な人
    if l == last_r - 1:
      break ## 前に操作したrとlが等しい
    ## rを探す(x + 1からlast_rまで)
    for y in reversed(range(l, last_r)): ## l, l+1, last_r - 1
      if y == n - 1 and humans[x] == "R":
        ## 不幸なので、rを確定
        r = y
        last_r = r
        k_count += 1
        break
      elif humans[y] == "L" and humans[y - 1] == "L":
        dev_null += 1 ## 既に同じ方向を向いてる
      elif humans[y] == "R" and humans[y + 1] == "R":
        dev_null += 1 ## 既に同じ方向を向いてる
      else:
        ## 不幸なので、rを確定
        r = y
        last_r = r
        k_count += 1
        break
    if l != "n" and r != "n":
      ## ともに値があるなら操作Kを実行
      for z in range(l, r + 1):
        if humans[z - 1] == "L":
          humans[z - 1] = "R"
        else:
          humans[z - 1] = "L"

## ポイントを計算
happy_point = 0
for x in range(n):
  if x == 0 and humans[x] == "L":
    ## do nothing
    dev_null += 1
  elif x == n - 1 and humans[x] == "R":
    ## do nothing
    dev_null += 1
  else:
    if humans[x] == "L" and humans[x - 1] == "L":
      happy_point += 1
    elif humans[x] == "R" and humans[x + 1] == "R":
      happy_point += 1
print(happy_point)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

vector<int> clusters;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    char last = 'm';
    FOR(i, 0, n) {
        char c;
        cin >> c;
        if (last == c) clusters.back()++;
        else clusters.push_back(1);
        last = c;
    }

    int ans = n - clusters.size() + 2 * min(k, (int)(clusters.size() - 1) / 2);
    if (k > (clusters.size() - 1) / 2 && clusters.size() % 2 == 0) ans++;
    cout << ans;
    return 0;
}

Submission status: Runtime Error

Solution code:
import re

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

pattern = re.compile("RL")
X = len(pattern.findall(str))
result = n - X * 2

if str[0] == 'L' and str[-1] == 'R':
    result -= 2
    Y = 1
elif str[0] == 'L' or str[-1] == 'R':
    result -= 1
    Y = 0

if X + Y <= k:
    result += 2 * X + Y
else:
    if X >= k:
        result += 2 * k
    else:
        result += 2 * X + (k-X)

print(result)

Submission status: Runtime Error

Solution code:
foo

Submission status: Runtime Error

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

unhapiness = 0
div = deque([])
conv = deque([])
left = "R"
right = "L"
cntr = 0
s = s + "L"
for si in s:
    right = si
    if (left == "R") & (right == "L"):
        if 0 < cntr < n:
            unhapiness += 2
            div.append(cntr)
        else:
            unhapiness += 1
            div.appendleft(cntr)
    if (left == "L") & (right == "R"):
        conv.appendleft(cntr)

    left = si
    cntr +=1

for i in range(k):
    p = div.pop()
    conv.pop()
    if 0 < p < n:
        unhapiness -= 2
    else:
        unhapiness -= 1

print(n-unhapiness)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

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

def searchLR(S):
    ans = -1
    for i in range(N-2,-1,-1):
        if S[i] == "L" and S[i+1] == "R":
            return i

def reverse(S):
    S = list(S)
    for i in range(len(S)):
        if S[i] == 'L':
            S[i] = 'R'
        if S[i] == 'R':
            S[i] = 'L'
    return ''.join(S)


def solve(S):
    RL = searchRL(S)
    LR = searchLR(S)
    if RL == -1 and LR == -1:
        return S, 0
    if RL == -1:
        S = reverse(S[LR::-1]) + S[LR+1:]
        return S, 1
    if LR == -1:
        S = reverse(S[RL::-1]) + S[RL+1:]
        return S, 1
    fir = min(LR, RL)
    sec = max(LR, RL)
    S = S[:fir+1] + reverse(S[sec:fir:-1]) + S[sec+1:]
    return S, 2

def cal(S):
    ans = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans +=1
    return ans


ans = cal(S)
for i in range(K):
    S, res = solve(S)
    ans += res
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

def searchRL(S):
    ans = []
    for i in range(N-1):
        if S[i] == "R" and S[i+1] == "L":
            ans.append(i)
    return ans

def searchLR(S):
    ans = []
    for i in range(N-2,-1,-1):
        if S[i] == "L" and S[i+1] == "R":
            ans.append(i)
    return ans

def reverse(S):
    S = list(S)
    for i in range(len(S)):
        if S[i] == 'L':
            S[i] = 'R'
        if S[i] == 'R':
            S[i] = 'L'
    return ''.join(S)


def solve(S):
    RL = searchRL(S)
    LR = searchLR(S)
    if len(RL) == 0 and len(LR) == 0:
        return S, 0
    if len(RL) == 0:
        S = reverse(S[LR[0]::-1]) + S[LR[0]+1:]
        return S, 1
    if len(LR) == 0:
        S = reverse(S[RL[0]::-1]) + S[RL[0]+1:]
        return S, 1
    fir = min(LR[0], RL[0])
    sec = max(LR[0], RL[0])
    S = S[:fir+1] + reverse(S[sec:fir:-1]) + S[sec+1:]
    return S, 2

def cal(S):
    ans = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans +=1
    return ans


ans = cal(S)
for i in range(K):
    S, res = solve(S)
    ans += res
    print(S)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
count = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, count + K * 2))

Submission status: Runtime Error

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

def result(S):
    count = 0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            count += 1
    return count
  
def reverse(S, l, r):
    for i in range(l-1, r):
        if S[i] == "R":
            S[i] = "L"
        else:
            S[i] = "R"
        return S
M = 0
for k in range(K):
  for r in range(1, N+1):
      for l in range(1, r+1):
          new = result(reverse(S))
          if new > M:
              M = new
print(new)

Submission status: Runtime Error

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

def result(S):
    count = 0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            count += 1
    return count
  
def reverse(S, l, r):
    for i in range(l-1, r):
        if S[i] == "R":
            S[i] = "L"
        else:
            S[i] = "R"
        return S
M = 0
for r in range(1, N+1):
    for l in range(1, r+1):
        new = result(reverse(S))
        if new > M:
            M = new
print(new)

Submission status: Runtime Error

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

L=[]

now=None
for i,s in enumerate(S):
	if i==0:
		now=s
		cnt=1
	elif now==s:
		cnt+=1
	elif now!=s:
		L.append([now,cnt])
		cnt=1
		now=s
else:
	L.append([now,cnt])	

l=len(L)

ans=0
if l>2:
	for i in range(1,l-1):
		s=L[i][0]
		if s=="L":
			L[i][0]="R"
			K-=1
			if K==0:
				break

	lcnt=0
	now=None
	for i,t in enumerate(L):
		s=t[0]
		cnt=t[1]
		if i==0:
			now=s
			lcnt+=cnt
		else:
			if now==s:
				lcnt+=cnt
			elif now!=s:
				#print s,lcnt
				ans+=max(0,(lcnt-1))
				lcnt=cnt
				now=s
	else:
		ans+=(lcnt-1)

	print ans

else:
	if K<=1:
		print N-1
	else:
		for s,cnt in L:
			ans+=max(0,(lcnt-1))
		print ans

Submission status: Runtime Error

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

L=[]

now=None
for i,s in enumerate(S):
	if i==0:
		now=s
		cnt=1
	elif now==s:
		cnt+=1
	elif now!=s:
		L.append([now,cnt])
		cnt=1
		now=s
else:
	L.append([now,cnt])	

l=len(L)

ans=0
if l>2:
	for i in range(1,l-1):
		s=L[i][0]
		if s=="L":
			L[i][0]="R"
			K-=1
			if K==0:
				break
	lcnt=0
	for i,t in enumerate(L):
		s=t[0]
		cnt=t[1]
		if i==0:
			now=s
		else:
			if now==s:
				lcnt+=cnt
			elif now!=s:
				ans+=max(0,(lcnt-1))
				lcnt=cnt
				now=s
	else:
		ans+=(lcnt-1)

	print ans

else:
	if K<=1:
		print N-1
	else:
		for s,cnt in L:
			ans+=max(0,(lcnt-1))
		print ans

Submission status: Runtime Error

Solution code:
#D-Face Produces Unhappiness
N, K = map(int, input().split())
S = str(input())
l_index = [l[i]+l[i+1] for i in range(0,N-1)]

count_LRorRL =0
count_LRorRL += S.count('LR')
count_LRorRL += S.count('RL')

count_LLorRR = 0
count_LLorRR += S.count('LL')
count_LLorRR += S.count('RR')

if count_LRorRL > 2*K:
    print(count_LLorRR+2*K)
else:
    print(count_LLorRR+count_LRorRL)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
S = list(input())

print(S)

l = 0
for _k in range(k):
    r = len(S) - 1
    for i in range(l + 1, len(S) - 1):
        if l == 0 and S[i] != S[i - 1]:
            l = i
        if l > 0 and S[i] == S[l] and S[i + 1] != S[l]:
            r = i
            break

    buf = S[:]
    for i in range(r - l + 1):
        S[r - i] = 'L' if buf[l + i] == 'R' else 'R'

    if _k > n / 2:
        break

    print(l, r)
    print(S)

print(S)

score = 0
for i in range(len(S)):
    if i > 0 and S[i] == 'L' and S[i - 1] == 'L':
        score += 1
    elif i < len(S) - 1 and S[i] == 'R' and S[i + 1] == 'R':
        score += 1

print(score)

Submission status: Runtime Error

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

H = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H += 2 * M
K -= M
N_RL -= M
N_LR -= M

s0 = S[0]
while K > 0 and (s0 == 'R' and N_RL > 0 or s0 == 'L' and N_LR > 0):
    if s0 == 'R' and N_RL > 0:
        H += 1
        s0 = 'L'
        N_RL -= 1
        K -=1
    elif s0 == 'L' and N_LR > 0:
        H += 1
        s0 = 'R'
        N_LR -= 1
        K -=1

sN = S[N-1]
while K > 0 and (sN == 'R' and N_LR > 0 or sN == 'L' and N_RL > 0):
    if sN == 'R' and N_LR > 0:
        H += 1
        sN = 'L'
        N_LR -= 1
        K -=1
    elif sN == 'L' and N_RL > 0:
        H += 1
        sN = 'R'
        N_RL -= 1
        K -=1

print(H)

Submission status: Runtime Error

Solution code:
#b is limit
a,b=map(int,input().split())
r=input()

if len(r)==1:
  print(0)
  exit()
  
i=1  
count=0
while i<len(r):
  if r[i-1:i+1]=="RL":
    count+=1
  i+=1
result=(a-1)-2*max(0,count-b)

if r[0]!=r[-1]:
  count-b<=0:
    result-=1

  
print(result)

Submission status: Runtime Error

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

H = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H += 1
if S[0] == 'R' and S[1] == 'L':
    N_RL += 1
if S[0] == 'L' and S[1] == 'R':
    N_LR += 1

if S[N-1] == 'L' and S[N-2] == 'L':
    H += 1

for i in range(1, N-1):
    if S[i] == 'L' and S[i-1] == 'L' or S[i] == 'R' and S[i+1] == 'R':
        H += 1
    if S[i] == 'R' and S[i+1] == 'L':
        N_RL += 1
    if S[i] == 'L' and S[i+1] == 'R':
        N_LR += 1

M = min(N_RL, N_LR, K)
H += 2 * M
K -= M
N_RL -= M
N_LR -= M

s0 = S[0]
while K > 0:
    if s0 == 'R' and N_RL > 0:
        H += 1
        s0 = 'L'
        N_RL -= 1
        K -=1
    elif s0 == 'L' and N_LR > 0:
        H += 1
        s0 = 'R'
        N_LR -= 1
        K -=1

sN = S[N-1]
while K > 0:
    if sN == 'R' and N_LR > 0:
        H += 1
        sN = 'L'
        N_LR -= 1
        K -=1
    elif sN == 'L' and N_RL > 0:
        H += 1
        sN = 'R'
        N_RL -= 1
        K -=1

print(H)

Submission status: Runtime Error

Solution code:
from collections import Counter
mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n, k = map(int, input().split())
    s = input()
    cnt = 0
    for i in range(n-1):
        if s[i] != s[i+1]:
            cnt += 1
    ans = n - 1 - cnt
    ans += k * 2
    ans = min(ans, n-1)
    print(ans)

main()
c

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(input())
m=0
for i in range(1,n-1):
  if l[i]=="R":
    if l[i+1]=="R":
      m+=1
  if l[i]=="L":
    if l[i-1]=="L":
      m+=1
if l[0]=="R" and l[0]==l[1]:
  m+=1
if l[-1]=="L" and l[-2]==l[-1]:
  m+=1
print(min(m+k*2,n-1))

Submission status: Runtime Error

Solution code:
# import copy

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

def calc(LR_str):
    re_sum = 0
    for i, s in enumerate(LR_str):
        if s == 'L' and not i==0:
            if LR_str[i-1] == 'L':
                re_sum += 1
        if s == 'R' and not i==N-1:
            if LR_str[i+1] == 'R':
                re_sum += 1
    return re_sum

result_list = []
str_list = []
str_list_copy = []
select_str = ""

def add_str(select_str):
    for start in range(N):
        end = N-1
        while start < end:
            S_copy = select_str # select str
            S_copy = list(S_copy)
            S_slice = S_copy[start:end+1]
            S_slice = S_slice[::-1]
            for ii, S_moji in enumerate(S_slice):
                if S_moji == 'L':
                    S_slice[ii] = 'R'
                else:
                    S_slice[ii] = 'L'
            S_copy[start:end+1] = S_slice
            S_copy = "".join(S_copy)
            str_list.append(S_copy)
            end -= 1

for i in range(K):
    if i ==0:
        select_str = S
        add_str(select_str)
    else:
        str_list_copy = str_list.copy()
        for single_str in str_list_copy:
            add_str(single_str)

for n in str_list:
    calc_int = calc(n)
    result_list.append(calc_int)

print(max(result_list))

Submission status: Runtime Error

Solution code:
import copy

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

def calc(LR_str):
    re_sum = 0
    for i, s in enumerate(LR_str):
        if s == 'L' and not i==0:
            if LR_str[i-1] == 'L':
                re_sum += 1
        if s == 'R' and not i==N-1:
            if LR_str[i+1] == 'R':
                re_sum += 1
    return re_sum

result_list = []
str_list = []
str_list_copy = []
select_str = ""

def add_str(select_str):
    for start in range(N):
        end = N-1
        while start < end:
            S_copy = copy.deepcopy(select_str) # select str
            S_copy = list(S_copy)
            S_slice = S_copy[start:end+1]
            S_slice = S_slice[::-1]
            for ii, S_moji in enumerate(S_slice):
                if S_moji == 'L':
                    S_slice[ii] = 'R'
                else:
                    S_slice[ii] = 'L'
            S_copy[start:end+1] = S_slice
            S_copy = "".join(S_copy)
            str_list.append(S_copy)
            end -= 1

for i in range(K):
    if i ==0:
        select_str = S
        add_str(select_str)
    else:
        str_list_copy = copy.deepcopy(str_list)
        for single_str in str_list_copy:
            add_str(single_str)

for n in str_list:
    calc_int = calc(n)
    result_list.append(calc_int)

print(max(result_list))

Submission status: Runtime Error

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

a = []
temp = S[0]
s = []
ans = 0

if temp == S[1]:
    hantei = True
    a.append(temp)
else:
    hantei = False
    s.append(list(S[0]))


for i in range(1, N):
    if temp == S[i]:
        ans += 1
    if hantei and temp != S[i]:
        s.append(a)
        a = []
        hantei = False
    if temp != S[i] or hantei:
        hantei = True
        a.append(S[i])
        temp = S[i]
s.append(a)

length = len(s)
for i in range(K):
    if length > 2:
        ans += 2
        length -= 2
    elif length == 2:
        ans += 1
        break

print(ans)

Submission status: Runtime Error

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

cnt = 0
x = 1
tmp = S[0]
while cnt < K and x <= N-1:
    if S[x] != tmp:
        S[x] = tmp
        x += 1
        while S[x] != tmp and x <= N-1:
            S[x] = tmp
            x += 1
            if x > N-1:
                break
        cnt += 1
        if x > N-1:
            break

    else:
        x += 1


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

print(ans)

Submission status: Runtime Error

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

cnt = 0
x = 1
tmp = S[0]
while cnt < K and x <= N-1:
    if S[x] != tmp:
        S[x] = tmp
        x += 1
        while S[x] != tmp and x <= N-1:
            S[x] = tmp
            x += 1
            if x > N-1:
                break
        cnt += 1

    else:
        x += 1

    if x > N-1:
        break

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

print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N, K = map(int, input())
    S = input()
    ans = 0
    for i in range(N-1):
        if S[i] == S[i+1]:
            ans += 1
    ans = min(N-1, ans+K*2)
    print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if s[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if s[i] == s[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
def section_sum(numbers,separate):
    sep1=array(numbers[separate:],dtype=int)
    sep2=array(numbers[:-separate],dtype=int)
    sep3=sum(numbers[:separate])
    return insert(sep3+cumsum(sep1-sep2),0,sep3)
    
    
n,k,a=open(0).read().split()
n,k=int(n),int(k)
ar=[len(list(v))for k,v in groupby(a)]

#print(ar)
if n<=k:
    print(sum(ar)-1)
else:
    ar2=[i-1 for i in ar]
    s=cumsum(ar2[::2])
    t=cumsum(ar2[1::2])
    mae=[0]+[t[i//2] if i%2 else s[i//2] for i in range(len(ar))]+[0,0,0]
    p=cumsum(ar2[::2][::-1])[::-1]
    q=cumsum(ar2[1::2][::-1])[::-1]
    usiro=[0]+[q[i//2] if i%2 else p[i//2] for i in range(len(ar))]+[0,0,0]
#    print(mae,usiro)
#    print([[[kk,kk+2*k+2],mae[kk],usiro[kk+2*k+2],vv] for kk,vv in enumerate(section_sum(ar,k*2+1))])
    print(max(mae[kk]+usiro[kk+2*k+2]+vv for kk,vv in enumerate(section_sum(ar,k*2+1)))-1)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

dict = defaultdict(list)

L_num = S.count('L')
R_num = S.count('R')

ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'R':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'R':
                count += 1
            else:
                break
        dict[count].append(i)
        skip = count + i
count = 0
for i in range(K + 10):
    max_key = max(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        max_key = max(dict.keys())
    target_index = dict[max_key].pop()
    if target_index == 0 or target_index == N - 1:
        continue
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]
    count += 1
    if count == K:
        break

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

print(score)

Submission status: Runtime Error

Solution code:
n , k = [ (int) i for i in input().split()]
s =input()
a =0
for i , ch in enumerate(s[1:] , 1):
  if ch == s[i-1]:
   	a+=1
print( min( n -1, a +2*k))

Submission status: Runtime Error

Solution code:
n , k = [ (int) i for i in input().split()]
s =input()
a =0
for i , ch in enumerate(s[1:] , 1):
  if ch == s[i-1]:
   	a+=1
print( min( n -1, a +2*k)