Problem: 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?

Submission attempts (in order):
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import heapq


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        
        return self.find(x) == self.find(y)

    def get_size(self, x):
        
        x = self.find(x)
        return self.size[x]
    




N, K = list(map(int, input().split()))
S = input()
    
def main():
    res = N
    past = S[0]
    cnt = K
    
    if N == 2:
        if S[0] == "R" and S[1] == "L" and S[0] == "L" and S[1] == "R":
            return print(0)
        if S[0] == "R" and S[1] == "R" and S[0] == "L" and S[1] == "L":
            return print(1)
    
    for n in range(1, N):
        now = S[n]
        print(past, now)
        if past == "R" and now == "L":
            print(n, ": BAD")
            res -= 2
            if 0 < cnt:
                res+= 2
                cnt -= 1
        past = now
    if S[0] == "L":
        print(0, ": BAD")
        res -= 1
        if 0 < cnt:
            res += 1
            cnt -= 1
    if S[-1] == "R":
        print(N-1, ": BAD")        
        res -= 1
        if 0 < cnt:
            res += 1
            cnt -= 1
        
        
    return print(res)
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import heapq


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        
        return self.find(x) == self.find(y)

    def get_size(self, x):
        
        x = self.find(x)
        return self.size[x]
    




N, K = list(map(int, input().split()))
S = input()
    
def main():
    res = N
    past = S[0]
    cnt = K
    cnt2= 0
    if N == 1:
        return print(0)
    if N == 2:
        if (S[0] == "R" and S[1] == "L") or (S[0] == "L" and S[1] == "R"):
            if 0 < cnt:
                return print(1)
            else:
                return print(0)
        
        if (S[0] == "R" and S[1] == "R") or (S[0] == "L" and S[1] == "L"):
            return print(1)
    
    for n in range(1, N):
        now = S[n]
        #print(past, now, res)
        if past == "R" and now == "L":
            #print(n, ": BAD")
            res -= 2
            if 0 < cnt:
                #print("reverse")
                if not n == N-1:
                    res += 2
                else:
                    res += 1
                cnt -= 1
                cnt2 += 1
        past = now
    if  cnt2 == 1:
        return print(N-1)
    
    if S[0] == "L":
        #print(0, ": edge BAD")
        res -= 1
        if 0 < cnt:
            #print("reverse")
            res += 1
            cnt -= 1        
    if S[-1] == "R":
        res -= 1
        #print(N-1, ": edge BAD")
        
    return print(res)
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import heapq



N, K = list(map(int, input().split()))
S = input()
    
def main():
    res = N
    past = S[0]
    cnt = K
    cnt2= 0
    if N == 1:
        return print(0)
    flg = False
    for n in range(1, N):
        now = S[n]
        #print(past, now, res)
        if past == "R" and now == "L":
            #print(n, ": BAD")
            res -= 2
            flg = True
        if past == "L" and now == "R" and flg == True:
            if 0 < cnt:
                #print("reverse")
                res += 2
                cnt -= 1
                cnt2 += 1
            flg = False
        past = now
    
    if flg == True and 0 < cnt:
        res += 1
    
    if S[0] == "L":
        #print(0, ": edge BAD")
        res -= 1
        if 0 < cnt and ("R" in S) :
            #print("reverse")
            res += 1
            cnt -= 1        
    if S[-1] == "R":
        res -= 1
        #print(N-1, ": edge BAD")
        
    return print(res)
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted