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:
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define HSORT(V,n) sort(V,V+n)
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
    int n,k;
    cin >> n >> k;
    string s;
    cin >> s;
    int c = 0;
    int ans = 0;
    bool check = false;
    REP(i,n-1){
        if (s[i] == s[i+1]) ans++;
    }
    REP(i,n-1){
        if (s[i] == 'L' && s[i+1] == 'R'){
            c++;
        }
        if (c == k) {
            break;
        }
        if (i == n-2){
            if (s[n-1] == 'L') check = true;
        }
    }
    if (c!=k){
        if (s[0] == 'R') ans++;
    }
    ans += 2*c;
    if (!check) ans--;
    PRINT(ans);
}

Submission status: Runtime Error

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

# find the largest subsequence
packet = []
packet_len = 0
direction = S[0]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction = S[i]
        packet_len += 1

if S[N - 1] == direction:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction = S[N - 1]
    packet.append(seq)
    packet_len += 1

#print(packet)


for _ in range(K):
    if packet_len == 1 or packet_len == 2:
        print(sum(packet) - 1)
        exit()

# There is no need to choose the 'most invertible' index (i.e. max_idx) !!!

    else:
        val = packet[-3] + packet[-2] + packet[-1]
        packet = packet[:-3] + [val]
        packet_len -= 2

    #print(packet)

ans = 0
for v in packet:
    ans += v - 1
print(ans)

Submission status: Runtime Error

Solution code:
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 s[0]==s[n-1]:
  x=ct
  ct-=min(ct,k)
  ct*=2
  ct+=1
  print(n-ct)
elif s[0]=='L':
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<k:
    ct-=1
  print(n-ct)
else:
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<=k
    ct+=1
  print(n-ct)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
                if count==n:break
            else:break
        k -=1
    else:count +=1

print("".join(s))
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=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=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 = map(int, input().split())
 
str = input()
happy = 0
unhappy = 0
 
for i in range(n-1):
  if(str[i] == str[i+1]):
    happy += 1
  else:
    unhappy += 1
if(str[0] != str[1] and str[n-1] != str[n-2] and k >= 2):
  print(happy + 2 * k - 2)
elif(str[n-1] != str[n-2] or str[0] != str[1]):
  print(happy + 2 * k - 1)
else:
  print(happy + 2 * k )

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
 
str = input()
print(str[0])
happy = 0
unhappy = 0
hashi = 0
 
for i in range(n-1):
  if(str[i] == str[i+1]):
    happy += 1
  else:
    unhappy += 1
if(str[0] != str[1] and str[n-1] != str[n-2]):
  hashi = 2
if(str[n-1] != str[n-2] or str[0] != str[1]):
  hashi = 1
if(k >= 2):
  print(happy + 2 * k - hashi)
if(k == 1):
  print(happy + 2 * k - hashi * k)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
 
str = input()
print(str[0])
happy = 0
unhappy = 0
hashi = 0
 
for i in range(n-1):
  if(str[i] == str[i+1]):
    happy += 1
  else:
    unhappy += 1
if(str[0] != str[1]):
  hashi += 1
if(str[n-1] != str[n-2]):
  hashi += 1
if(k == 1):
  hashi = 1
print(happy + 2 * k - hashi)

Submission status: Runtime Error

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

score = 0
for i in range(N-1):
    if (S[i] == S[i+1]):
        score += 1
    ans = min(score+2*K, N-1)

print(ans)

Submission status: Runtime Error

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

def counting(S):
    point = 0
    for i in range(1, len(S)):
        if S[i] == "L":
            if S[i-1] == "L":
                point += 1
        elif i < len(S)-1:
            if S[i+1] == "R":
                point += 1
    return point

max_ = 0
SS = []

def donyoku(S, N, K):
    global max_
    global SS
    if K == 0:
        pass
    else:
        for i in range(len(S)):
            for j in range(i,len(S)):
                SS = S.copy()
                for k in range(i, j+1):
                    if SS[k] == 'L':
                        SS[k] = 'R'
                    else:
                        SS[k] = 'L'
#                 print("S", SS, i, j)
                max_ = donyoku(SS, N, K-1)
    point = counting(S)
    if point > max_:
        max_ = point
    return max_
    
answer = donyoku(S, N, K)
print(answer)

Submission status: Runtime Error

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

str = input().split()

happy = 0
unhappy = 0

for i in range(n):
  if(str[i] == str[i+1]):
    happy += 1
  else:
    unhappy += 1
    
print(happy + 2 * k)

Submission status: Runtime Error

Solution code:
N,K = [int(x) for x in input().split()]
S = input()
li = [1]
ans = 0
cnt = 0
for ind,s in enumerate(S[:-1]):
    if(s==S[ind+1]):
        li[-1]+=1
    else:
        li.append(1)
for ind,i in enumerate(li):
    if(ind%2==1 and cnt<=K):

Submission status: Runtime Error

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

l = []
for i in range(N):
  if i == 0:
    tmp = i
  elif S[i - 1] != S[i]:
    l.append([tmp, i - 1, i - tmp])
    tmp = i
  elif i == N - 1:
    l.append([tmp, i, i - tmp + 1])

for k in range(K):
  i = 2 * k + 1
  if S[l[i][0]] == "L":
    S[l[i][0] : l[i][1] + 1] = "R" * l[i][2]
  else:
    S[l[i][0] : l[i][1] + 1] = "L" * l[i][2]

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 = map(int, input().split())
S = input()
counter = 0

ans = N-1

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

print(min(N-1), ans+2*K)

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)
        i += count
for i in range(K):
    max_key = max(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = max(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]

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

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

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

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)
        i += count
for i in range(K):
    max_key = min(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = min(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]

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

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

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


print(max(score1, score2, score3, score4))

Submission status: Runtime Error

Solution code:
import sys
#input=sys.stdin.readline
sys.setrecursionlimit(3000)


#L=-1  R=1
n,k=map(int,input().split())
s=input()
#print(s)
t=s[0]
c=[-1,1][s[0]=="R"]
d=[0]

for i in range(1,n):
    if t!=s[i]:
        d.append(c)
        c=0
    t=s[i]
    c+=[-1,1][s[i]=="R"]
d.append(c)
c=len(d)

#print(c,d)
if c>2:
    for i in range(1,k+1):
        d[i*2]*=-1
#print(d)
"""
p=[-1,-1]
for i in range(1,c-1):
    tmp=(abs(d[i-1])+abs(d[i])+abs(d[i+1]))
    if tmp>p[0]:
        p[0]=tmp
        p[1]=i
i=p[1]
for j in range(k):
    if i+j*2>=c: break
    d[i+j*2]*=-1
"""
chk=0
ans=0
for i in d:
    if (i>0 and chk>0) or (i<0 and chk<0):
        chk+=i
    else:
        if chk!=0: ans+=abs(chk)-1
        chk=i

print(ans+(abs(chk)-1))

Submission status: Runtime Error

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

def counting(S):
    point = 0
    for i in range(1, len(S)):
        if S[i] == "L":
            if S[i-1] == "L":
                point += 1
        elif i < len(S)-1:
            if S[i+1] == "R":
                point += 1
    return point

max_ = 0

def donyoku(S, N, K):
    global max_
    if K == 0:
        pass
    else:
        max_ = donyoku(S, N, K-1)
        for i in range(len(S)):
            for j in range(i, len(S)):
                for k in range(i, j+1):
                    if S[k] == 'L':
                        S[k] = 'R'
                    else:
                        S[k] = 'L'
#                 print("S", S, K)
                point = counting(S)
                if point > max_:
                    max_ = point
    return max_
    
donyoku(S, N, K)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=1

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
["L", "R", "R", "R"]N, K = map(int, input().split())
S = str(input())

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=1

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

Submission status: Runtime Error

Solution code:
# coding: utf-8


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

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

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

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

    return output


if __name__ == "__main__":
    output = main()
    print(output)
    assert 1 == 2

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np


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

    happiness_array = np.array([], dtype='int')
    happiness = 0

    for i in range(n - 1):
        if s[i] == s[i + 1]:
            happiness += 1
            if i == n - 2:
                happiness_array = np.append(happiness_array, happiness)
        else:
            happiness_array = np.append(happiness_array, happiness)
            if i == n - 2:
                happiness_array = np.append(happiness_array, 0)

            happiness = 0


    for j in range(k):
        max_sum_of_three = 0
        place_of_change = 0
        if len(happiness_array) >= 3:
            for i in range(len(happiness_array) - 2):
                sum_of_three = happiness_array[i] + happiness_array[i + 1] + happiness_array[i + 2]
                if sum_of_three > max_sum_of_three:
                    max_sum_of_three = sum_of_three
                    place_of_change = i + 1
            happiness_array[i] += happiness_array[i + 1] + happiness_array[i + 2] + 2
            happiness_array = np.delete(happiness_array, i + 1)
            happiness_array = np.delete(happiness_array, i + 1)
        else:
            happiness_array[0] += happiness_array[1] + 1
            happiness_array = np.delete(happiness_array, 1)

        if len(happiness_array) == 1:
            break
        
    ans = np.sum(happiness_array)


    print(ans)

main()

Submission status: Runtime Error

Solution code:
import re

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

def main():
    [N, K] = [int(number) for number in input().split()]
    S = input()
    s = [len(rl) for rl in list(nchars(S,1))]
    i = K
    while (i > 0):
        len_s = len(s)
        if len_s < 3:
            break
        if i == K:
            three_range_arr = [s[j] + s[j+1] + s[j+2] for j in range(len_s) if j < len_s-2]
            max_tra = max(three_range_arr)
            index = three_range_arr.index(max_tra)
        else:
            index = s.index(max(s))
            if index == 0:
                max_tra = s[index]+s[index+1]+s[index+2]
            elif index == len_s - 1:
                max_tra = s[index-2] + s[index-1] + s[index]
                index -= 2
            else:
                if index == 1:
                    maxs = [s[index-1] + s[index] + s[index+1], s[index]+s[index+1]+s[index+2]]
                elif index == len_s-2:
                    maxs = [s[index-2] + s[index-1] + s[index], s[index-1] + s[index] + s[index+1]]
                else:
                    maxs = [s[index-2] + s[index-1] + s[index], s[index-1] + s[index] + s[index+1], s[index]+s[index+1]+s[index+2]]
                max_tra = max(maxs)
                offset = maxs.index(maxs) - 2
                if index == 1:
                    offset += 1
                index += offset
        if index == 0:
            ss = [max_tra]
            ss.extend(s[index+3:len_s])
        elif index == len_s - 3:
            ss = s[0:index-1]
            ss.extend([max_tra])
        else:
            ss = s[0:index-1]
            ss.extend([max_tra])
            ss.extend(s[index+3:len_s])
        #s = s[0:index-2].append(max_tra)#.extend(s[index+1, len_s-1])
        i-=1
        s=ss

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

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

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

print(result)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import re

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

def main():
    [N, K] = [int(number) for number in input().split()]
    S = input()
    s = [len(rl) for rl in list(nchars(S,1))]
    i = K
    while (i > 0):
        len_s = len(s)
        if len_s < 3:
            break
        if i == K:
            three_range_arr = [s[j] + s[j+1] + s[j+2] for j in range(len_s) if j < len_s-2]
            max_tra = max(three_range_arr)
            index = three_range_arr.index(max_tra)
        else:
            index = s.index(max(s))
            if index == 0:
                max_tra = s[index]+s[index+1]+s[index+2]
            elif index == len_s - 1:
                max_tra = s[index-2] + s[index-1] + s[index]
                index -= 2
            else:
                maxs = [s[index-2] + s[index-1] + s[index], s[index-1] + s[index] + s[index+1], s[index]+s[index+1]+s[index+2]]
                max_tra = max(maxs)
                offset = maxs.index(maxs) - 2
                index += offset
        if index == 0:
            ss = [max_tra]
            ss.extend(s[index+3:len_s])
        elif index == len_s - 3:
            ss = s[0:index-1]
            ss.extend([max_tra])
        else:
            ss = s[0:index-1]
            ss.extend([max_tra])
            ss.extend(s[index+3:len_s])
        #s = s[0:index-2].append(max_tra)#.extend(s[index+1, len_s-1])
        i-=1
        s=ss

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=1
while k!=0:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=1

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=1
while k!=0:
    if s[count]!=point:
        while 1:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=1

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=1
while k!=0:
    if s[count]!=point:
        while 1:
            if s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:
        count +=1

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

print(ans)

Submission status: Runtime Error

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

S = input()

s = 0
numRL = 0

for i in range(N-1):
    if S[i:i+2] == 'RL':
        numRL = numRL + 1
        
couhuku = 0
for i in range(N-1):
    if (S[i:i+2] == 'RR') | (S[i:i+2] == 'LL') : 
        couhuku = couhuku + 1
        
if S[0:2] == 'LL' :
    couhuku = couhuku - 1
if S[N-2:N] == 'RR' :
    couhuku = couhuku - 1
    

def func(num, k):
    global s
    if num <= k:
        s = s + num*2
        if (num % 2) :
            func(num//2 + 1, k-num)
        else :
            func(num//2, k-num)
    else: 
        s = s + (k)*2
    
        
func(numRL, K)

print(couhuku + s)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s = list(s)
#print(s)
before = s[1]
flag = False
place = -1
end = -1
cnt = 0

for i in range(1, n-1) :

    if s[i] != before :
        flag = True
        place = i

    if flag :
        #print(s, i)
        if i + 1 == n - 1 :
            if s[place] == s[i+1] :
                end = i+2
            else :
                end = i+1
            #print(s, i, end, 'end')
            if s[place] == 'L' :
                if end == i+1 :
                    s[place:end] = 'R'
                else :
                    for j in range(place, n) :
                        s[j] = 'R'
            else :
                if end == i+1 :
                    s[place:end] = 'L'
                else :
                    for j in range(place, n) :
                        s[j] = 'L'
            #print(s, 'end2')
            cnt += 1
            if cnt == k :
                break
            flag = False
            before = s[i]
            continue

        if s[i] != s[i+1] :
            #print(s)
            if s[place] == 'L' :
                s[place:i+1] = 'R'
            else :
                s[place:i+1] = 'L'

            cnt += 1
            #print(s)
            if cnt == k:
                break
            flag = False
            before = s[i]
            continue
    before = s[i]

#print(s)
if cnt != k :
    if s[0] != s[1] :
        s[0] = s[1]
    if k >= cnt + 2 :
        if s[n-1] != s[n-2] :
            s[n-1] = s[n-2]

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

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=1
while k!=0:
    if s[count]!=point:
        while 1:
            if s[count]!=point:
                s[count]=point
                count +=1
            elif s[count]==point:break
        k -=1
    else:
        count +=1

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

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

t = 1
for k in range(1,N):
    if S[k] == S[k-1]:
        t += 1
    else:
        A.append(t)
        t = 1
A.append(t)
if len(A) <= 2:
    print(N-1)
else:
    B = []
    for k in range(len(A)-2):
        B.append(A[k]+A[k+1]+A[k+2])
    C = sorted(B)[::-1]
    S = C[min(K-1,len(C))]
    F = K
    k = -1
    while k < len(A)-3:
        k += 1
        if F == 0:
            break
        if A[k]+A[k+1]+A[k+2] >= S:
            t = A[k]+A[k+1]+A[k+2]
            A[k] = 0
            A[k+1] = 0
            A[k+2] = t
            F -= 1
            k += 1
    ans = 0
    for e in A:
        ans += max(0,e-1)
    print(ans)

Submission status: Runtime Error

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

H0 = 0
N_LR = 0
N_RL = 0

if S[0] == 'R' and S[1] == 'R':
    H0 += 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':
    H0 += 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':
        H0 += 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)
H1 = H0 + 2 * M
K -= M
N_RL -= M
N_LR -= M

result = H1 + min(K, max(N_RL, N_LR))
print(result)

Submission status: Runtime Error

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

T = [-1]
i, F, P = 0, 0, 0
while i < N-1:
  if S[i] != S[i+1]:
    T.append(i)
T.append(N-1)
Q = []
if len(T) > 2:
  for j in range(len(T)-2):
  	Q.append(T[j+2]-T[j])
else:
  Q.append(N)
Ki = 0
Si = 0
while len(Q) == 0 or Ki < N:
  Si += max(Q)
  Q.remove(max(Q))
  
print(Si)