Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02684 (python)

Fixed submission:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def readInt():
    return list(map(int,input().split()))

def main():
    n,k = readInt()
    a = readInt()
    pos = 0
    visit = [0] * n
    move = []
    roop = []
    while visit[pos]!=2:
        if visit[pos]==0:
            move.append(pos)
        else:
            roop.append(pos)
        visit[pos] += 1
        pos = a[pos] - 1
    num = 0
    if len(move)>k:
        print(move[k]+1)
    else:
        num = (k - (len(move)-len(roop))) % len(roop)
        print(roop[num]+1)
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)

Buggy submission (verdict: Runtime Error):
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def readInt():
    return list(map(int,input().split()))

def main():
    n,k = readInt()
    a = readInt()
    pos = 0
    visit = [0] * n
    move = []
    roop = []
    while visit[pos]!=2:
        if visit[pos]==0:
            move.append(pos)
        else:
            roop.append(pos)
        visit[pos] += 1
        pos = a[pos] - 1
    
    roop.reverse()
    num = 0
    if len(move)>=k:
        print(move[k]+1)
    else:
        num = (k - (len(move)-len(roop))) % len(roop)
        print(roop[num]+1)
    return

if __name__=='__main__':
    main()
Problem p02719 (python)

Fixed submission:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n,k = readInt()
    ans = n % k
    ans = min(ans,abs(ans-k))
    print(ans)
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    f = abs(n-k)
    print(max(f,k)%min(f,k))
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    if k==1:
        print(0)
        return
    f = abs(n-k)
    print(n%f)
    return

if __name__=='__main__':
    main()
Problem p02747 (python)

Fixed submission:
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
# import collections # queue
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    s = input()
    if len(s)%2!=0:
        print('No')
        return
    for i in range(0,len(s),2):
        if s[i]!='h' or s[i+1]!='i':
            print('No')
            return
    print('Yes')
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
# import collections # queue
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    s = input()
    for i in range(0,len(s),2):
        if s[i]!='h' or s[i+1]!='i':
            print('No')
            return
    print('Yes')
    return

if __name__=='__main__':
    main()
Problem p02756 (python)

Fixed submission:
import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
import collections
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29

def main():
    s = collections.deque(list(input()))
    q = int(input())
    count = 0
    for i in range(q):
        tfc = list(input().split())
        if tfc[0]=='1':
            count = abs(count - 1)
        else:
            t,f,c = tfc
            if count:
                if f=='1':
                    s.append(c)
                else:
                    s.appendleft(c)
            else:
                if f=='1':
                    s.appendleft(c)
                else:
                    s.append(c)
    if count:
        t = []
        for i in range(len(s)):
            t.append(s.pop())
        print(''.join(t))
    else:
        print(''.join(s))
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
s = collections.deque(list(input()))
q = int(input())
rev = 0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1)
    else:
        t,f,c = tfc
        if rev:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print(''.join(t))
else:
    print(''.join(s))
Problem p02843 (python)

Fixed submission:
import math
import queue
import bisect
import heapq
import time
import itertools

mod = 1e9+7

def swap(a,b):
    return (b,a)

def gcd(a,b): #最大公約数
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def divisors(a): # 約数列挙
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def is_prime(a): #素数判定
    if a<2:
        return False
    elif a==2:
        return True
    elif a%2==0:
        return False
    sqrt_num = int(a**0.5)
    for i in range(3,sqrt_num+1,2):
        if a%i==0:
            return False
    return True

def prime_num(a): #素数列挙
    pn = [2]
    for i in range(3,int(a**0.5),2):
        prime = True
        for j in pn:
            if i%j==0:
                prime = False
                break
        if prime:
            pn.append(i)
    return pn

def main():
    x = int(input())
    if x<100:
        print(0)
        return
    buy = x//100
    sur = x%100
    num = (sur+4)//5
    if num<=buy:
        print(1)
    else:
        print(0)
    return

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i,n) for(int i=0;i<n;i++)

const ll mod = 1e9;
const int INF = 1<<29;

int main(){
    int x;
    cin >> x;
    if (x < 100) cout << 0 << endl;
    else{
        int num = x / 100;
        x %= 100;
        for (int i=5;i>0;i--){
            num -= x / i;
            x %= i;
        }
        if (num >= 0) cout << 1 << endl;
        else cout << 0 << endl;
    }
    return 0;
}
Problem p03069 (python)

Fixed submission:
N = int(input())
S = input()
min = N
clm_sum = [0]
for s in S:
    if s == '#':
        clm_sum.append(clm_sum[-1] + 1)
    else:
        clm_sum.append(clm_sum[-1])
for i in range(1,len(clm_sum)):
    if clm_sum[i-1] + ((N - i) - (clm_sum[-1] - clm_sum[i])) < min:
        min = clm_sum[i-1] + ((N - i) - (clm_sum[-1] - clm_sum[i]))
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
if S.find('#') == -1 or S.find('.') == -1:
    min = 0
else:
    for i in range(S.find('#'),N - 1):
        if S[i] == '.' and S[i+1] == '#':
            left = S.count('#',0,i)
            right = S.count('.',i+1)
        elif S[i] == '#' and S[i+1] =='.':
            left = S.count('#',0,i)
            right = S.count('.',i)
        if left + right < min:
            min = left + right
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
if S.find('#') == -1 or S.find('.') == -1:
    min = 0
else:
    for i in range(S.find('#'),S.rfind('#')+1):
        if S[i] == '.' and S[i+1] == '#':
            left = S.count('#',0,i)
            right = S.count('.',i+1)
        elif S[i] == '#' and S[i+1] =='.':
            left = S.count('#',0,i)
            right = S.count('.',i)
        if left + right < min:
            min = left + right
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
if S.find('#') == -1 or S.find('.') == -1:
    min = 0
else:
    for i in range(S.find('#'),S.rfind('#')):
        if S[i] == '.' and S[i+1] == '#':
            left = S.count('#',0,i)
            right = S.count('.',i+1)
        elif S[i] == '#' and S[i+1] =='.':
            left = S.count('#',0,i)
            right = S.count('.',i)
        if left + right < min:
            min = left + right
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
for i in range(N-1):
    if S.find('.') == -1 or S.find('#') == -1:
        min = 0
        break
    if S[i] == '.' and S[i+1] == '#':
        left = S.count('#',0,i)
        right = S.count('.',i+1)
    elif S[i] == '#' and S[i+1] =='.':
        left = S.count('#',0,i)
        right = S.count('.',i)
    if left + right < min:
        min = left + right
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
for i in range(N-1):
    if '.' not in S or '#' not in S:
        min = 0
        break
    if S[i] == '.' and S[i+1] == '#':
        left = S.count('#',0,i+1)
        right = S.count('.',i+1)
    elif S[i] == '#' and S[i+1] =='.':
        left = S.count('#',0,i)
        right = S.count('.',i)
    if left + right < min:
        min = left + right
print(min)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
min = N
for i in range(N-1):
    if '.' not in S or '#' not in S:
        min = 0
        break
    if S[i] == '.' and S[i+1] == '#':
        left = S.count('#',0,i)
        right = S.count('.',i)
    elif S[i] == '#' and S[i+1] =='.':
        left = S.count('#',0,i)
        right = S.count('.',i)
    if left + right < min:
        min = left + right
print(min)
Problem p02553 (python)

Fixed submission:
a,b,c,d = map(int,input().split())
answer = 0
ac =a*c
ad = a*d
bc = b*c
bd = b*d
answer = max(ac,ad)
answer = max(answer,bc)
answer = max(answer,bd)
print(answer)

Buggy submission (verdict: Runtime Error):
a,b,c,d = map(int,input().split())
answer = 0
ac =a*c
ad = a*d
bc = b*C
bd = b*d
answer = max(ac,ad)
answer = max(answer,bc)
answer = max(answer,bd)
print(answer)
Problem p02608 (python)

Fixed submission:
N = int(input())
a = [0]*10**5
a = [0]+a
x = 0
y = 0
z = 0
total = 0
for i in range(1,101):
  x = i
  for j in range(i,101):
    y = j
    for k in range(i,j+1):
      z = k
      total = int(((x+y)**2+(y+z)**2+(z+x)**2)/2)
      if(x==y and y==z):
        a[total]+=1
      elif(x==y or y==z or z==x):
        a[total]+=3
      else:
        a[total]+=6
for l in range(1,N+1):
  print(a[l])

Buggy submission (verdict: Runtime Error):
N = int(input())
a = [0]*10**4
a = [0]+a
x = 0
y = 0
z = 0
total = 0
for i in range(1,N+1):
  x = i
  for j in range(i,N+1):
    y = j
    for k in range(i,j+1):
      z = k
      total = int(((x+y)**2+(y+z)**2+(z+x)**2)/2)
      if(x==y and y==z):
        a[total]+=1
      elif(x==y or y==z or z==x):
        a[total]+=3
      else:
        a[total]+=6
for l in range(1,N+1):
  print(a[l])
Problem p02640 (python)

Fixed submission:
x,y = map(int, input().split())
a = -y+4*x
b = y-2*x
if(a%2==0 and b%2==0 and a>=0 and b>=0):
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
x,y = map(int input().split())
a = -y+4x
b = y-2x
if(a%2==0 and b%2==0):
  print("Yes")
else:
  print("No")
Problem p02658 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
wa = 1
ka = 0
if(a.count(0)>=1):
  print(0)
  ka +=1
else:
  for i in range(n):
    wa = wa*a[i]
    if(wa>10**18):
      print("-1")
      ka +=1
      break
if(ka==0):
  print(wa)

Buggy submission (verdict: Runtime Error):
import numpy as np
 
N = int(input())
A = list(map(int, input().split()))  
a = 1
if 0 in A:
	print(0)
for i in range(N):
  a *= *A[i] 
  if (a > 10**18):
  	a = -1
print(a)

Buggy submission (verdict: Runtime Error):
import numpy as np
 
N = int(input())
A = list(map(int, input().split()))  
a = 1
if 0 in A:
	print(0)
for i in range(N):
  a = a*A[i] 
	if (a > 10**18):
  	a = -1
    return
print(a)
Problem p02718 (python)

Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*n
b = sorted(a,reverse=True)
c = sum(a)
if(b[m-1]>=c/(4*m)):
  print('Yes')
else:
  print("No")

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
a =list(map(int,input().split()))
b = sorted(a,reverse='True')n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*n
b = sorted(a,reverse=True)
c = sum(a)
if(b[m-1]>=c/(4*m)):
  print('Yes')
else:
  print("No")
Problem p02732 (python)

Fixed submission:
n = int(input())
a =list(map(int,input().split()))
d = [0]*(n+1)
answer = 0
for i in range(n):
  d[a[i]]+=1
for j in range(n+1):
  answer+= (d[j]*(d[j]-1))//2
for k in range(n):
  print(answer+1-(d[a[k]]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a =list(map(int,input().split()))
d = [0]*(n+1)
answer = 0
for i in range(n):
  d[a[i]]+=1
for j in range(n):
  answer+= (d[j]*(d[j]-1))//2
for k in range(n)  
  print(answer+1-(d[a[k]]))
Problem p02778 (python)

Fixed submission:
s = input()
for i in range(len(s)):
  print('x',sep='',end='')

Buggy submission (verdict: Runtime Error):
s = input()
for i in range(len(s)):
  s[i]='x'
print(s)
Problem p02993 (python)

Fixed submission:
s = int(input())
a = [0]*4
a[0] = s//1000 
s = s-a[0]*1000
a[1] = s//100
s = s-a[1]*100
a[2] = s//10
s = s-a[2]*10
a[3] = s
if(a[0]==a[1] or a[1]==a[2] or a[2]==a[3]):
  print("Bad")
else:
  print("Good")

Buggy submission (verdict: Runtime Error):
s = int(input())
s = str(s)
if(s[-4]==s[-3] or s[-3]==s[-2] or s[-2]==s[-1]):
  print("Bad")
else:
  print("Good")
Problem p03001 (python)

Fixed submission:
w,h,x,y=map(int,input().split())
a = 0
if(h==2*y and w ==2*x):
  a +=1
print(w*h/2,a)

Buggy submission (verdict: Runtime Error):
w,h,x,y=map(int,input().split())
a = 0
if(h==2*x and w ==2*y)
a +=1
print(w*h/2,a)
Problem p03246 (python)

Fixed submission:
n = int(input())
v = list(map(int, input().split())) 
a = 0
ab = 0
ac = 0
b = [0]*(10**5+10)
c = [0]*(10**5+10)
d = [0]*(10**5+10)
e = [0]*(10**5+10)
for i in range(n-1):
  if v[i]==v[i+1]:
    a +=1
if(a == n-1):
  print(int(n/2))
else:
  for i in range(0,n,2):
    b[v[i]] = b[v[i]] + 1
  for j in range(1,n+1,2):
    c[v[j]] =c[v[j]] + 1
  d = sorted(b,reverse=True)
  e = sorted(c,reverse=True)
  for s in range(10**5+1):
    if(d[0]==b[s]):
      ab = s
      break
  for t in range(10**5+1):
    if(e[0]==c[t]):
      ac = t
      break
  if(s==t):
    print(min(2*int(n/2)-d[0]-e[1],2*int(n/2)-d[1]-e[0]))
  else: 
    f = int(n/2)-d[0]
    g = int(n/2)-e[0]
    print(f+g)

Buggy submission (verdict: Runtime Error):
n = int(input())
v = list(map(int, input().split())) 
a = 0
b = [0]*(10**5+10)
c = [0]*10**5
d = [0]*10**5
e = [0]*10**5
for i in range(n-1):
  if v[i]==v[i+1]:
    a +=1
if(a == n-1):
  print(int(n/2))
else:
  for i in range(0,n,2):
    b[v[i]] = b[v[i]] + 1
  for j in range(1,n+1,2):
    c[v[j]] =c[v[j]] + 1
  d = sorted(b,reverse=True)
  e = sorted(c,reverse=True)
  f = int(n/2)-d[0]
  g = int(n/2)-e[0]
  print(f+g)
Problem p03773 (python)

Fixed submission:
x,y = map(int, input().split())
print((x+y)%24)

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
s = math.ceil(math.sqrt(n))
h = 10**5
for i in range(1,s+1):
  if n%i==0:
    if h > max(len(str(i)),len(str(int(n/i)))):
      h = max(len(str(i)),len(str(int(n/i))))
print(h)
Problem p02697 (python)

Fixed submission:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, M = read_values()

    if N % 2 == 1: 
        L = N // 2
        R = L + 1
        for _ in range(M):
            print(L, R)
            L -= 1
            R += 1
    else:
        L = 1
        R = M + 1
        for _ in range(M, 0, -2):
            print(L, R)
            L += 1
            R -= 1
        
        L = M + 2
        R = 2 * M + 1
        for _ in range(M - 1, 0, -2):
            print(L, R)
            L += 1
            R -= 1


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, M = read_values()

    if N % 2 == 1: 
        L = N // 2
        R = L + 1
        for _ in range(M):
            print(L, R)
            L -= 1
            R += 1
    else:
        F = [False] * N
        d = 1
        f = 0
        for _ in range(M):
            while F[f]:
                f += 1

            print(f + 1, f + d + 1)
            F[f + d] = True
            d += 1
            f += 1


if __name__ == "__main__":
    main()
Problem p02743 (python)

Fixed submission:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: x - 1, map(int, input().split()))


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def functional(N, mod):
    F = [1] * (N + 1)
    for i in range(N):
        F[i + 1] = (i + 1) * F[i] % mod
    return F


def main():
    a, b, c = read_values()
    r = c - a - b
    if r <= 0:
        print("No")
        return
    
    print("Yes" if (c - a - b) ** 2 > 4 * a * b else "No")

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: x - 1, map(int, input().split()))


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def functional(N, mod):
    F = [1] * (N + 1)
    for i in range(N):
        F[i + 1] = (i + 1) * F[i] % mod
    return F


def main():
    a, b, c = read_values()
    print("Yes" if c - a - b> 2 * (a * b) ** 0.5) else "No")


if __name__ == "__main__":
    main()
Problem p02848 (python)

Fixed submission:
N = int(input())
S = input()
res = ""
for s in S:
    res += chr((ord(s) - ord("A") + N) % 26 + ord("A"))
print(res)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
res = ""
for s in S:
    res += chr((ord(s) - ord("A") + n) % 26)
print(res)
Problem p02918 (python)

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

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

    pre = s

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

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

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

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

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

res = 0
pre = None
add = {2: 0}
if S[0] == S[-1] and S[0] == "L":
    add[0] = 1

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

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