TnT/Multi_CodeNet4Repair huggingface dataset:
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):
n, k = map(int,input().split())
s = input()

if n == 1:
        print(0)
else:        
        t = []
        tt = 0
        ss = s[0]
        for i in range(1,n):
                if s[i] != ss:
                        t.append(i - tt)
                        tt = i
                        ss = s[i]
        t.append(n - tt)
        print(t)
        cnt = sum(t) - len(t) 

        if k <= len(t) - 2:
                cnt += 2 * k
        elif k == len(t) - 1:
                cnt += 2 * (len(t) - 2) + 1
        else:
                cnt += 2 * (len(t) - 2) + 2
        print(cnt)

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

if n == 1:
        print(0)
else:        
        t = []
        tt = 0
        ss = s[0]
        for i in range(1,n):
                if s[i] != ss:
                        t.append(i - tt)
                        tt = i
                        ss = s[i]
        t.append(n - tt)
        cnt = sum(t) - len(t) 

        if len(t) % 2 == 0:
                if k <= (len(t) - 2)/2:
                        cnt += 2 * k
                else:
                        cnt += 2 * (len(t) - 2)/2 + 1
        else:
                if k <= (len(t) - 1)/2:
                        cnt += 2 * k
                else:
                        cnt += 2 * (len(t) - 1)/2
        print(int(cnt)) 

Verdicts: Wrong Answer

Accepted
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):
N, K = list(map(int, input().split(' ')))
S = list(input())

def change():
    check = S[0]
    k = 1
    while S[k] == check:
        k += 1
    i = 0
    while S[k + i] != check:
        S[k + i] = check
        i += 1

group = 1
old = S[0]
happy_num = 0
for i in range(1, N):
    if old == S[i]:
        group += 1
        if i == N - 1:
            happy_num += group - 1
    else:
        happy_num += group - 1
        old = S[i]
        group = 1
print(happy_num)

border = 0
for k in range(K):
    old = S[0]
    for i in range(1, N):
        if old != S[i]:
            border += 1
            if border == 2:
                break
            old = S[i]
    if border == 0:
        break
    elif border == 1:
        happy_num += 1
        S[1:] = S[0]
    else:
        happy_num += 2
        change()

print(happy_num)

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

def change():
    check = S[0]
    k = 1
    while S[k] == check:
        k += 1
    i = 0
    while S[k + i] != check:
        S[k + i] = check
        i += 1

group = 1
old = S[0]
happy_num = 0
for i in range(1, N):
    if old == S[i]:
        group += 1
        if i == N - 1:
            happy_num += group - 1
    else:
        happy_num += group - 1
        old = S[i]
        group = 1
#print(happy_num)


old = S[0]
border = 0
for i in range(1, N):
    if old != S[i]:
        border += 1
        old = S[i]
#print(border)

changed = 2 * K
if changed < border:
    print(happy_num + 2 * K)
else:
    print(N - 1)

Verdicts: Runtime Error

Accepted
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):
n,s=open(0);n,k=map(int,n.split());print(n-max(sum(i==j for i,j in zip(s,s[1:]))-2*k,1))

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


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


print(len(S) - count_unhappy())


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

Verdicts: Wrong Answer

Wrong Answer

Accepted
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):
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 )

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)

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)


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)

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
print( min(n-1,(happy + 2 * k )))

# 10 1


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted
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):
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))


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


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

def f1(s, l, r):
  return s[:l] + f2(s[l:r + 1]) + s[r + 1]

def f2(s):
  t = list(s)
  t.reverse()
  return ''.join('R' if c == 'L' else 'L' for c in t)

def f3(s):
  before = s[0]
  result = 0
  for i in range(1, N):
    if s[i] == before:
      result += 1
    else:
       before = s[i]
  return result

def f4(s, c):
  lstart = -1
  llen = 0
  tstart = -1
  for i in range(N):
    if s[i] == c and tstart == -1:
      tstart = i
    if s[i] != c and tstart != -1:
      tlen = i - tstart
      if tlen > llen:
        lstart = tstart
        llen = tlen
  return (lstart, llen)

for _ in range(K):
  lstart, llen = f4(S, 'L')
  rstart, rlen = f4(S, 'R')
  if lstart < rstart:
    S = f2(S, lstart + llen, rstart + rlen - 1)
  else:
    S = f2(S, rstart + rlen, lstart + llen - 1)
  if lstart == -1 or rstart == -1:
    break

print(f3(S))

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


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
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):
if __name__ == '__main__':
    N, K = map(int, input().split())
    S = str(input())
    count = 0
    
    LR = 0
    RL = 0
    
    for i in range(N-1):
        if S[i] == 'R' and S[i+1] == 'R':
            count += 1
        elif S[i+1] == 'L' and S[i] == 'L':
            count += 1
    
    for i in range(N-1):
        if S[i] == 'L' and S[i+1] == 'R': 
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': 
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
            print(LR, RL)
        elif LR > 0 or RL > 0:
            count += 1
            break
        
            
    print(count)
    

if __name__ == '__main__':
    N, K = map(int, input().split())
    S = str(input())
    count = 0
    
    LR = 0
    RL = 0
    
    for i in range(N-1):
        if S[i] == 'R' and S[i+1] == 'R':
            count += 1
        elif S[i+1] == 'L' and S[i] == 'L':
            count += 1
    
    for i in range(N-1):
        if S[i] == 'L' and S[i+1] == 'R': 
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': 
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
            print(LR, RL)
        elif LR > 0 or RL > 0:
            count += 1
        
            
    print(count)
    

if __name__ == '__main__':
    N, K = map(int, input().split())
    S = str(input())
    count = 0
    
    LR = 0
    RL = 0
    
    for i in range(N-1):
        if S[i] == 'R' and S[i+1] == 'R':
            count += 1
        elif S[i+1] == 'L' and S[i] == 'L':
            count += 1
    
    for i in range(N-1):
        if S[i] == 'L' and S[i+1] == 'R': 
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': 
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
        else:
            if LR > 0 or RL > 0:
                count += 1
                break
            
    print(count)
    

Verdicts: Wrong Answer

Wrong Answer

Accepted
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):
from itertools import groupby

n,k = map(int,input().split())
s = [0] + list(input())
print(s)
count_L = 0
count_R = 0
ans = 0
for i in range(1,n):
    if s[i-1] == 'R' and s[i] == 'L' and s[i+1] == 'R':
        count_L += 1
    elif s[i-1] == 'L' and s[i] == 'R' and s[i+1] == 'L':
        count_R += 1
    elif s[i-1] == 'R' and s[i] == 'R':
        ans += 1
    elif s[i-1] == 'L' and s[i] == 'L':
        ans += 1
count_max = max(count_L,count_R)
print(count_max,ans)

if count_max >= k:
    ans = ans + 2*k
else:
    ans = ans + 2*count_max + 1

print(ans)


from itertools import groupby

n,k = map(int,input().split())
s = [0] + list(input())
ans = 0
for i in range(1,n):
    if s[i] == s[i+1]:
        ans += 1

print(min(ans+ k*2,n-1))


Verdicts: Wrong Answer

Accepted
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):
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)

N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL-counter*2 + RL_L + RL_R)

print(ans)

N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(max(N-1, ans))

N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL-counter*2 + RL_L + RL_R)

print(max(N-1,ans))

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

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted
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):
N,K = map(int,input().split())
P = list(input())
count = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count += 1
time = 0
for i in range(1,N):
    if P[i] != P[i-1]:
        time += 1
if time == 1:
    print(count + 1)
elif time >= K:
    print(count + 2*time - 1)
else:
    print(count + 2*K)



N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if P[N-1] == 'L':
        print(count2 + 2*min(K,count))
    else:
        if K >= count:
            print(count2 + 2 * count - 1)
        else:
            print(count2 + (2*K))
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if P[N-1] == 'R':
        print(count2 + 2*min(K,count))
    else:
        if  K >= count:
            print(count2 + 2*count -1)
        else:
            print(count2 + 2*K)



Verdicts: Wrong Answer

Accepted
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):
import sys
from heapq import heappop, heappush
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

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

    if N == 1:
        print(0)
        return
    if N <= 3:
        print(N - 1)
        return

    if S == ''.join(sorted(S)) or S == ''.join(sorted(S))[:: -1]:
        print(N - 1)
        return

    RLcnt = S.count('RL')

    if RLcnt == 0:
        print(N - 1)
        return

    add = 2 * min(K, RLcnt)
    if S[0] == 'R' and S[1] == 'L':
        add -= 1
    if S[-1] == 'L' and S[-2] == 'R':
        add -= 1

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

    ans = min(N - 1, ans + add)
    print(ans)

sol()

import sys
from heapq import heappop, heappush
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

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

    if N == 1:
        print(0)
        return

    
    
    
    

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

    for _ in range(K):
        if ans < N - 2:
            ans += 2
        elif ans == N - 2:
            ans += 1
        elif ans == N - 1:
            break

    print(ans)

sol()

Verdicts: Wrong Answer

Accepted
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):
n = int(input())
k = int(input())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)):
  if s[i] == 'R' and s[i+1] == 'L':
    res += 1
print(min(now + K, min(l, r)))

n = int(input())
k = int(input())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    res += 1
print(min(now + K, min(l, r)))

n = int(input())
k = int(input())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 1
print(min(now + K, min(l, r)))

n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 1
print(min(now + K, min(l, r)))

n, k = map(int, input().split())
s = input()
l = s.count('L')
r = s.count('R')
now = 0
for i in range(len(s)-1):
  if s[i] == 'R' and s[i+1] == 'L':
    now += 2
print(min(now + k * 2, min(l, r) * 2))

n, k = map(int, input().split())
s = input()
grp = 0
now = ""
for c in s:
  if c != now:
    grp += 1
    now = c
hap = 0
for i in range(n):
  if (i>0 and s[i]=='L' and s[i-1]=='L') or (i<n-1 and s[i]=='R' and s[i+1]=='R'):
    hap += 1
p_2 = (grp - 1) // 2
p_1 = (grp + 1) % 2
print(hap + 2 * min(k, p_2) + int(k > p_2) * p_1)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted
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):
n, k =map(int, input().split()) 
s = input()
count = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
print(str(count+k*2)

n, k =map(int, input().split()) 
s = input()
count = 0
even =0
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        even +=1

for i in range(k):
        if even >= 2:
            count +=2
            even -= 2
        elif even ==1:
            count+=1
            even -=2
print(str(count))

Verdicts: Runtime Error

Accepted
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):
#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)
  



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

c=0
for i in range(1,n):
  if r[i-1]==r[i]:
    c+=1
  
print(min(n-1,c+2*k))
  



Verdicts: Runtime Error

Accepted
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):
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+1
            else :
                end = i
            print(s, i, end, 'end')
            if s[place] == 'L' :
                if end == i :
                    s[place:end] = 'R'
                else :
                    s[place:end] = 'R'
                    #s[end-1] ='R'
            else :
                if end == i :
                    s[place:end] = 'L'
                else :
                    s[place:end] = 'L'
                    #s[end-1] = '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)


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

seqCnt = 0
seqTotal = 0
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)
        seqTotal += seqCnt
        seqCnt = 0

print(min(seqTotal + 2*k, n-1))

Verdicts: Runtime Error

Accepted
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):
N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
print(ans2)
print(len(a)//2*2-1)
if len(a)!=1:
  ans2 = ans2 +(2*K)
print(min(ans2,len(S)-1))


N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
if len(a)!=1:
  ans2 = ans2 +(2*K if len(a)*(len(a)+1)//2>K else len(a)*(len(a)+1)//2*2)
  if len(a)-K>=1:
    ans2 -=1
  print(ans2)
  exit()
print(ans2)


N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
if len(a)!=1:
  if len(a)*(len(a)+1)//2!=K:
    ans2 = ans2 +(2*K if len(a)*(len(a)+1)//2>K else len(a)*(len(a)+1)//2*2)-1
    print(ans2)
    exit()
print(ans2)


N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
if len(a)!=1:
  ans2 = ans2 +(2*K-1 if len(a)*(len(a)+1)//2>K else len(a)*(len(a)+1)//2*2)
  print(ans2)
  exit()
print(ans2)


N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
#print(len(a)//2*2-1)
if len(a)!=1:
  ans2 = ans2 +(2*K)
print(min(ans2,len(S)-1))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted
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):
N, K = map(int, input().split())
S = input()

num_RL = 0
for i in range(N):
	if i == 0 and S[0] == "L":
		num_RL += 1
	elif i == N - 1 and S[N-1] == "R":
		num_RL += 1
	elif S[i:i+2] == "RL":
		num_RL += 1

if K >= num_RL - 1:
	print(N - 1)
else:
	print(N - 2*(num_RL - 1 - K))


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

LR = 0
RL = 0
happy = 0
for i in range(N-1):
	if S[i:i+2] == "RL":
		RL += 1
	elif S[i: i+2] == "LR":
		LR += 1
	elif S[i] == S[i+1]:
		happy += 1

if K <= min(RL, LR):
	print(happy + 2 * K)
else:
	print(N - 1)

Verdicts: Wrong Answer

Accepted
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):
n, k = map(int, input().split())
s = input()

i = 0
c = 0
cc = 0
while i < n:
	if s[i] == s[i - 1]:
		c += 1
	else:
		cc += 1

	i += 1

print(c + min(2 * k, cc))

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

i = 1
c = 0
cc = 0
while i < n:
	if s[i] == s[i - 1]:
		c += 1
	else:
		cc += 1

	i += 1

print(c + min(2 * k, cc))

Verdicts: Wrong Answer

Accepted
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):
N,K = map(int,input().split())
s = str(input())

ans = N - 1 - s.count('RL') - s.count('LR')
print(min( N-1 , ans - 2 * K ))


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

ans = N - 1 - s.count('RL') - s.count('LR')
print(min( N-1 , ans + 2 * K ))


Verdicts: Wrong Answer

Accepted
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):
N,K = map(int,input().split())
S = input()

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        count += 1

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

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

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        count += 1

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

Verdicts: Runtime Error

Accepted
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):
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	sls='R'+sl+'L'
	ss=0
	for cp, cn in zip(sls[:-1], sls[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'R':
			ss+=1
			
	for cp, cn in zip(sls[1:], sls[2:]):
		#print(cp,cn)
		if cp == cn and cp == 'L':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	ss=0
	for cp, cn in zip(sl, sl[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'L':
			ss+=1
			
	for cp, cn in zip(sl, sl[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'R':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Verdicts: Wrong Answer

Accepted
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):
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if s[i] == s[i+1]:
    ans += 1
while True:
  for i in range(k):
    if ans < n-2:
      ans += 2
    if ans == n-2:
      ans += 1
    else:
      break
print(ans)

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
  elif ans == n-2:
    ans += 1
  else:
    pass
print(ans)

Verdicts: Time Limit Exceeded

Accepted
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):
n,k= map(int,(input().split()))
b= input()
LRn=len(re.findall('LR',b))
RLn=len(re.findall('RL',b))

if min(LRn,RLn)>=k:
    ans=k*2
elif min(LRn,RLn)<=k and max(LRn,RLn)>=k:
    ans=k+min(LRn,RLn)
else:
    ans=LRn+RLn

a= list(b)
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        ans +=1

print(ans)

import re

n,k= map(int,(input().split()))
b= input()
LRn=len(re.findall('LR',b))
RLn=len(re.findall('RL',b))

if min(LRn,RLn)>=k:
    ans=k*2
elif min(LRn,RLn)<=k and max(LRn,RLn)>=k:
    ans=k+min(LRn,RLn)
else:
    ans=LRn+RLn

a= list(b)
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        ans +=1

print(ans)

Verdicts: Runtime Error

Accepted
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):
n,k = map(int,input().split())
s = list(input())

ans  = 0
tran = 0

for i in range(n):
    chk = s[i] + s[i+1]

    if chk == 'LR' or chk == 'RL':
        tran += 1
    else:
        ans += 1

if k*2 >= tran:
    print(n-1)
else:
    print(tran+(k*2))

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

ans  = 0
tran = 0

for i in range(n-1):
    chk = s[i] + s[i+1]

    if chk == 'LR' or chk == 'RL':
        tran += 1
    else:
        ans += 1

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

Verdicts: Runtime Error

Accepted