ZhenlongDai/ACPR huggingface dataset:
Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
P = [int(x) for x in input().split()]

ans = 0
if P[0] == 1:
    temp = P[1]
    P[1] = P[0]
    P[0] = temp
    ans += 1
for i in range(1,N):
    if P[i] == i+1:
        temp = P[i]
        P[i] = P[i-1]
        P[i-1] = P[i]
        ans += 1
print(ans)

Failing test case input:
9
2 2 3 1 5 8 4 6 10

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
P = [int(x) for x in input().split()]

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

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
P = list(map(int, input().split()))
res = 0
for idx in range(len(P) - 1):
    if P[idx] == idx + 1:
        P[idx], P[idx+1] = P[idx+1], P[idx]
        res += 1
print(res)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
P = list(map(int, input().split()))
res = 0
for idx in range(len(P) - 1):
    if P[idx] == idx + 1:
        P[idx], P[idx+1] = P[idx+1], P[idx]
        res += 1

if P[-1] == N:
    res += 1

print(res)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = [int(j)!=i+1 for i,j in enumerate(input().split())]
res = 0
for i in range(n-1):
    if not a[i]:
        res += 1
        a[i],a[i+1] = True,True
print(res)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
a = [i+1!=int(j) for i,j in enumerate(input().split())]
res = 0
for i in range(n):
    if not a[i]:
        a[i] = True
        if i+1!=n:
            a[i+1] = True
        res += 1
print(res)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    if p[i] == i+1:
        try:
            p[i+1],p[i] = p[i],p[i+1]
        except:
            pass
        ans += 1

print(ans) 

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N):
    if p[i] == i+1:
        try:
            p[i+1],p[i] = p[i],p[i+1]
        except:
            pass
        ans += 1

print(ans)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
    a, b = P[i], P[i + 1]
    if a == i + 1:
        ans += 1
        P[i] = b
        P[i + 1] = a
print(ans)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N):
    if i == N-1:
        if P[i] == i+1:
            ans +=1
        break
    a, b = P[i], P[i + 1]
    if a == i + 1:
        ans += 1
        P[i] = b
        P[i + 1] = a
print(ans)
Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
A = list(map(int, input().split()))
each = [0] * N

for i in range(N):
  if i + 1 == A[i]:
    each[i] = 1
    

ans = 0
for i in range(N):
  if i < N - 1:
    if each[i] == 1:
      if each[i + 1] == 1:
        ans += 1
        each[i + 1] = 0
      else:
        ans += 1
  
print(ans)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int, input().split()))
each = [0] * N

for i in range(N):
  if i + 1 == A[i]:
    each[i] = 1
    

ans = 0
for i in range(N):
  if i < N - 1:
    if each[i] == 1:
      if each[i + 1] == 1:
        ans += 1
        each[i + 1] = 0
      else:
        ans += 1
  else:
    if each[i] == 1:
      ans += 1
  
print(ans)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = 9
PRR = [1, 2, 4, 9, 5, 8, 7, 3, 6]











N = int(input())
PRR = list(map(int, input().split()))


def calculate(n, prr):
    result = 0
    for i in range(n - 1):
        if prr[i] == (i + 1):
            prr[i], prr[i + 1] = prr[i + 1], prr[i]
            result += 1

    print(result)


calculate(N, PRR)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = 9
PRR = [1, 2, 4, 9, 5, 8, 7, 3, 6]

N = 5
PRR = [1, 2, 3, 4, 5]











N = int(input())
PRR = list(map(int, input().split()))


def calculate(n, prr):
    result = 0
    for i in range(n - 1):
        if prr[i] == (i + 1):
            prr[i], prr[i + 1] = prr[i + 1], prr[i]
            result += 1

    if prr[n - 1] == n:
        result += 1
    print(result)


calculate(N, PRR)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    P = list(map(int, input().strip().split()))
    return N, P


def solve(N, P):
    ans = 0
    p = deque(P)
    q = deque()
    q.append(p.popleft())
    for i in range(1, N):
        if i == q[-1]:
            c = q.pop()
            q.append(p.popleft())
            q.append(c)
            ans += 1
        else:
            q.append(p.popleft())
    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    P = list(map(int, input().strip().split()))
    return N, P


def solve(N, P):
    ans = 0
    p = deque(P)
    q = deque()
    q.append(p.popleft())
    for i in range(1, N):
        if i == q[-1]:
            c = q.pop()
            q.append(p.popleft())
            q.append(c)
            ans += 1
        else:
            q.append(p.popleft())
    if q[-1] == N:
        c = q.pop()
        d = q.pop()
        q.append(c)
        q.append(d)
        ans += 1
    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from sys import stdin
import bisect
from math import factorial

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

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

Failing test case input:
9
1 1 1 0 3 1 2 12 14

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
from sys import stdin
import bisect
from math import factorial

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

point = 0

for i in range(n-1):
    if li[i]-1 == i:
        li[i],li[i+1] = li[i+1],li[i]
        point += 1
if li[-1] == n:
    point += 1

print(point)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import math

n = int(input())
p = [x-1 for x in list(map(int, input().split()))]

c = 0
flag = 0
ans = 0

for i in range(n):
    if p[i] == i and flag:
        c += 1
    elif p[i] == i:
        c += 1
        flag = 1
    elif p[i] != i and c > 0:
        if c == 1:
            ans += 1
        else:
            ans += math.floor(c/2)
        
        c = 0
        flag = 0

ans += math.floor(c/2)

print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import math

n = int(input())
p = [x- 1 for x in map(int, input().split())]

cnt, flag = 0, 0
ans = 0
for i in range(n):
    if p[i] == i and flag:
        cnt += 1
    elif p[i] == i:
        cnt += 1
        flag = 1
    elif p[i] != i and flag:
        if cnt == 1:
            ans += 1
        else:
            ans += math.ceil(cnt/2)

        cnt, flag = 0, 0
if flag:
    if cnt == 1:
        ans += 1
    else:
        ans += math.ceil(cnt/2)

print(ans)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    if p[i] == i+1:
        p[i],p[i+1] = p[i+1],p[i]
        ans += 1
print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
p = list(map(int,input().split()))
ans = 0
c = 0
for i in range(N):
    if p[i] == i+1:
        c += 1
    else:
        ans += (c+1)
        c = 0
print(ans+(c+1)
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
p = na()
ans = 0

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

print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
p = na()
ans = 0

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

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
P=[1]+list(map(int,input().split()))

ans=0
for i in range(1,N):
    if P[i]==i:
        if i>1:
            P[i-1],P[i]=P[i],P[i-1]
            ans+=1
        else:
            P[i+1],P[i]=P[i],P[i+1]
            ans+=1
print(ans)

Failing test case input:
9
2 2 3 1 5 8 4 6 10

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N=int(input())
P=[1]+list(map(int,input().split()))

ans=0
for i in range(1,N+1):
    if P[i]==i:
        if (i<N and i+1==P[i+1]) or i==1:
            P[i+1],P[i]=P[i],P[i+1]
            ans+=1
        else:
            P[i-1],P[i]=P[i],P[i-1]
            ans+=1
print(ans)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n-1):
	if a[i]==i+1:
		ans+=1
		a[i],a[i+1]=a[i+1],a[i]
print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n-1):
	if a[i]==i+1:
		ans+=1
		if i==1 and a[0]==1:
			ans-=1
		a[i],a[i+1]=a[i+1],a[i]
if a[n-1]==n:
	ans+=1
print(ans)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
P = list(map(int, input().split()))
Q = P.copy()
cnt1 = 0
for i in range(N-1):
    if P[i] == i+1:
        tmp = P[i]
        P[i] = P[i+1]
        P[i+1] = tmp
        cnt1 += 1
cnt2 = 0
for i in range(N-1, -1, -1):
    if Q[i] == i+1:
        tmp = Q[i]
        Q[i] = Q[i-1]
        Q[i-1] = tmp
        cnt2 += 1
print(min(cnt1, cnt2))


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
P = list(map(int, input().split()))
Q = P.copy()
cnt1 = 0
for i in range(N-1):
    if P[i] == i+1:
        tmp = P[i]
        P[i] = P[i+1]
        P[i+1] = tmp
        cnt1 += 1
if P[-1] == N:
    cnt1 += 1

cnt2 = 0
for i in range(N-1, 0, -1):
    if Q[i] == i+1:
        tmp = Q[i]
        Q[i] = Q[i-1]
        Q[i-1] = tmp
        cnt2 += 1
if Q[0] == 1:
    cnt2 += 1
print(min(cnt1, cnt2))
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
input()
data=list(map(int,input().split()))
count=0
for i in range(len(data)-1):
    if data[i]==i+1:
        data[i],data[i+1]=data[i+1],data[i]
        count+=1
print(count)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
input()
data=list(map(int,input().split()))
count=0
for i in range(len(data)-1):
    if data[i]==i+1:
        data[i],data[i+1]=data[i+1],data[i]
        count+=1
if data[-1]==len(data):
    count+=1
print(count)

Problem: p03612
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N = int(input())
p = list(map(int, input().split()))
ans = 0
q = []
for i in range(N):
  if i+1 == p[i]:
    q += [0]
  else:
    q += [1]

for i in range(N-1):
  if (q[i+1] == 1 and q[i] == 0):
    ans += 1
    q[i] = 1
  elif (q[i+1] == 0 and q[i] == 1):
    ans += 1
    q[i+1] = 1
  elif q[i+1] == 0 and q[i] == 0:
    ans += 1
    q[i+1] = 1
    q[i] = 1
print(ans)
  

Failing test case input:
9
2 2 3 1 5 8 4 6 10

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
p = list(map(int, input().split()))
ans = 0
q = []
for i in range(N):
  if i+1 == p[i]:
    q += [0]
  else:
    q += [1]

for i in range(N):
  if q[i] == 0 and i < N-1:
    q[i], q[i+1] = 1, 1
    ans += 1
  elif q[i] == 0 and i == N-1:
    q[i], q[i-1] = 1, 1
    ans += 1

print(ans)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
p = list(map(int, input().split()))
count = 0

for i in range(N-1):
    if p[i]-1 == i:
        p[i],p[i+1] = p[i+1],p[i]
        count += 1
print(count)


Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
p = list(map(int, input().split()))
count = 0
i = 0

while N > i:
    if p[i]-1 == i:
        count += 1
        i += 1
    i+=1
print(count)

Problem: p03612
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
a = list(map(int, input().split()))

b=[]

for i in range(n):
    if(a[i]==i+1):
        b.append(1)
    else:
        b.append(0)
        

count = 0
if(n>=3):
    for i in range(n-2):
        prev = b[i]
        now = b[i+1]
        if(prev+now == 2):
            count+=1
            b[i+1]=0
        elif(prev==1 and now ==0):
            count+=1
            
    if(b[n-1]==1):
        count+=1
    print(count)
else:
    if(sum(b)>=1):
        print(1)
    else:
        print(0)


Failing test case input:
5
0 4 4 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int, input().split()))

b=[]

for i in range(n):
    if(a[i]==i+1):
        b.append(1)
    else:
        b.append(0)
        

count = 0
if(n>=3):
    for i in range(n-1):
        prev = b[i]
        now = b[i+1]
        if(prev+now == 2):
            count+=1
            b[i+1]=0
        elif(prev==1 and now ==0):
            count+=1
            
    if(b[n-1]==1):
        count+=1
    print(count)
else:
    if(sum(b)>=1):
        print(1)
    else:
        print(0)
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n-1):
    if i+1 == a[i]:
        b = a[i]
        c = a[i+1]
        a[i] = c
        a[i+1] = b
        ans += 1
print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
    if i+1 == a[i] and i != n-1:
        b = a[i]
        c = a[i+1]
        a[i] = c
        a[i+1] = b
        ans += 1
    elif i+1 == a[i]:
        ans += 1
print(ans)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=list(map(int,input().split()))
x=0
for i in range(n-1):
    if s[i]==i+1:
        s[i],s[i+1]=s[i+1],s[i]
        x+=1
print(x)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
s=list(map(int,input().split()))
x=0
for i in range(n-1):
    if s[i]==i+1:
        s[i],s[i+1]=s[i+1],s[i]
        x+=1
if s[-1]==n:x+=1
print(x)

Problem: p03612
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())
ps = list(map(int, input().split()))
res = 0
for i in range(n-1):
    if ps[i] == i+1:
        ps[i],ps[i+1] = ps[i+1],ps[i]
        res += 1
print(res)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
ps = list(map(int, input().split()))
res = 0
for i in range(n):
    if ps[i] == i+1:
        if i < n - 1:
            ps[i],ps[i+1] = ps[i+1],ps[i]
            res += 1
        else:
            ps[i], ps[0] = ps[0], ps[i]
            res += 1
print(res)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(N-1):
    if i+1==p[i]:
        temp=p[i+1]
        p[i+1]=p[i]
        p[i]=temp
        ans+=1
print(ans)

Failing test case input:
5
1 2 17 7 5

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(N-1):
    if i+1==p[i]:
        temp=p[i+1]
        p[i+1]=p[i]
        p[i]=temp
        ans+=1
if p[N-1]==N:
    ans+=1
print(ans)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
P = list(map(int,input().split()))
P = [int(i+1==P[i]) for i in range(n)]+[0]

ans = 0
ex = 0
for p in P:
  if p and ex:
    ans += 1
    ex = 0
  elif p:
    ex = 1
  elif ex:
    ans += 1
print(ans)

Failing test case input:
9
1 1 1 2 3 9 2 12 14

Expected output:
1


Actual output:
9


Fixed submission (verdict: Accepted):
n = int(input())
P = list(map(int,input().split()))
P = [int(i+1==P[i]) for i in range(n)]+[0]

ans = 0
ex = 0
for p in P:
  if p and ex:
    ans += 1
    ex = 0
  elif p:
    ex = 1
  elif ex:
    ans += 1
    ex = 0
print(ans)
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = list(map(int, input().split()))
i = 0
ans = 0
while i < N-1:
  if A[i] != i+1:
    pass
  else:
    k = A[i]-A[i-1]
    A[i] += -k
    A[i+1] += k
    ans += 1
  i += 1
print(ans)

Failing test case input:
5
1 1 17 0 0

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int, input().split()))
i = 0
ans = 0
while i < N-1:
  if A[i] != i+1:
    pass
  else:
    k = A[i+1]-A[i]
    A[i] += k
    A[i+1] += -k
    ans += 1
  i += 1
if A[N-1] == N:
  ans += 1
print(ans)

Problem: p03612
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
a = list(map(int, input().split()))

b=[]

for i in range(n):
    if(a[i]==i+1):
        b.append(1)
    else:
        b.append(0)

count = 0
for i in range(n-2):
    prev = b[i]
    now = b[i+1]
    if(prev+now == 2):
        count+=1
        b[i+1]=0
    elif(prev==1 and now ==0):
        count+=1
        
if(b[n-1]==1):
    count+=1
    
print(count)

Failing test case input:
5
0 4 4 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int, input().split()))

b=[]

for i in range(n):
    if(a[i]==i+1):
        b.append(1)
    else:
        b.append(0)
        

count = 0
if(n>=3):
    for i in range(n-1):
        prev = b[i]
        now = b[i+1]
        if(prev+now == 2):
            count+=1
            b[i+1]=0
        elif(prev==1 and now ==0):
            count+=1
            
    if(b[n-1]==1):
        count+=1
    print(count)
else:
    if(sum(b)>=1):
        print(1)
    else:
        print(0)

Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

N = int(input())
P = list(map(int, input().split()))
l = list(range(1, N+1))
L = []
for i in range(N):
    L.append(P[i] != l[i])
ans = 0
for i in range(N-1):
    if L[i] and L[i+1]:
        continue
    ans += 1
    L[i] = True
    L[i+1] = True
print(ans)

Failing test case input:
9
2 2 3 1 5 8 4 6 10

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):

N = int(input())
P = list(map(int, input().split()))
l = list(range(1, N+1))
ans = 0
for i in range(N-1):
    if P[i] == l[i]:
        ans += 1
        P[i], P[i+1] = P[i+1], P[i]
if P[-1] == l[-1]:
    ans += 1
print(ans)
Problem: p03612
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
P = list(map(int,input().split()))


ans = 0
for j in range(N):
    p = P[j]
    i = j + 1
    if p == i:
        if i == 1:
            ans += 1
            P[j], P[j+1] = P[j+1], P[j]
        else:
            ans += 1
            P[j-1], P[j] = P[j], P[j-1]

print(ans)


Failing test case input:
9
2 2 3 1 5 8 4 6 10

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
P = list(map(int,input().split()))

ans = 0
if True:
    for j in range(N):
        p = P[j]
        i = j + 1
        if p == i:
            if i != N:
                ans += 1
                P[j], P[j+1] = P[j+1], P[j]
            else:
                ans += 1
                P[j-1], P[j] = P[j], P[j-1]
print(ans)
Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):
def minimum_cost_sort(A):
  cost = 0
  B = sorted(A)
  for i in range(len(B)):
    x = A.index(B[i])
    cnt = 0
    tmp = 0
    while x != i:
      y = A.index(B[x])
      A[x], A[y] = A[y], A[x]
      tmp += A[x] + A[y]
      cnt += 1
      x = y
    if cnt > 0:
      cost += tmp
      
  return cost

N = int(input())
A = list(map(int, input().split()))
print(minimum_cost_sort(A))

Failing test case input:
5
0 5 3 1 2

Expected output:
12


Actual output:
13


Fixed submission (verdict: Accepted):
def minimum_cost_sort(A):
  cost = 0
  B = sorted(A)
  for i in range(len(B)):
    x = A.index(B[i])
    cnt = 0
    tmp = 0
    while x != i:
      y = A.index(B[x])
      A[x], A[y] = A[y], A[x]
      tmp += A[x] + A[y]
      cnt += 1
      x = y
    if cnt > 0:
      
      
      
      
      cost += min(tmp, (B[i] + B[0]) * 2 + tmp - (B[i] - B[0]) * cnt)
  return cost

N = int(input())
A = list(map(int, input().split()))
print(minimum_cost_sort(A))

Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):
import copy
n = int(input())
a = list(map(int, input().split()))
b = copy.deepcopy(a)
b.sort()
loop = []
minp = min(a)
t = [0 for i in range(n)]
while True:
    i = t.index(0)
    j = len(loop)
    loop.append([a[i]])
    t[i] = 1
    while True:
        i = b.index(a[i])
        if t[i] == 1:
            break
        t[i] = 1
        loop[j].append(a[i])
    if sum(t) == n:
        break
    j += 1
print(loop)
cost = 0
for i in loop:
    if len(i) == 1:
        continue
    ps = sum(i)
    pl = len(i)
    pm = min(i)
    p1 = ps + (pl - 2) * pm
    p2 = ps + pm + (pl + 1) * minp
    cost += min(p1, p2)
print(cost)


Failing test case input:
5
0 26 3 1 19

Expected output:
47


Actual output:
[[0], [26, 19, 1], [3]]
47


Fixed submission (verdict: Accepted):
import copy
n = int(input())
a = list(map(int, input().split()))
b = copy.deepcopy(a)
b.sort()
loop = []
minp = min(a)
t = [0 for i in range(n)]
while True:
    i = t.index(0)
    j = len(loop)
    loop.append([a[i]])
    t[i] = 1
    while True:
        i = b.index(a[i])
        if t[i] == 1:
            break
        t[i] = 1
        loop[j].append(a[i])
    if sum(t) == n:
        break
    j += 1
cost = 0
for i in loop:
    if len(i) == 1:
        continue
    ps = sum(i)
    pl = len(i)
    pm = min(i)
    p1 = ps + (pl - 2) * pm
    p2 = ps + pm + (pl + 1) * minp
    cost += min(p1, p2)
print(cost)
Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):
input()
w=list(map(int,input().split()))
x=min(w)
m={k:v for k,v in zip(sorted(w),w)if k!=v}
l=list(m.keys())
p=[]
while l:
 a=l.pop();b=m[a];l.remove(b)
 if a==m[b]:p+=[[a,b]]
 else:
  t=[a,b]
  while 1:
   c=m[b];t+=[c]
   if c==a:p+=[t];break
   else:b=c;l.remove(c)
n=0
for s in p:
 n+=sum(s);t=len(s);y=min(s)
 if t>2:n+=min((t-2)*y,y+(t+1)*x)
print(n)


Failing test case input:
5
0 19 3 1 2

Expected output:
26


Actual output:
45


Fixed submission (verdict: Accepted):
input()
w=list(map(int,input().split()))
x=min(w)
m={k:v for k,v in zip(sorted(w),w)if k!=v}
l=list(m.keys())
p=[]
while l:
 a=l.pop();b=m[a];l.remove(b)
 if a==m[b]:p+=[[a,b]]
 else:
  t=[b]
  while 1:
   c=m[b];t+=[c]
   if c==a:p+=[t];break
   else:b=c;l.remove(c)
n=0
for s in p:
 n+=sum(s);t=len(s);y=min(s)
 if t>2:n+=min((t-2)*y,y+(t+1)*x)
print(n)

Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):




n = int(input())
line = input()
L = [int(x) for x in line.split()]

flags = [False] * n
loops = []

S = sorted(L)
for i, x in enumerate(L):
    k = S.index(x)
    if flags[k]:
        continue
    if i == k:
        flags[i] = True
        continue

    loop = []
    while x not in loop:
        flags[k] = True
        loop.append(x)
        x = L[k]
        k = S.index(x)
    loops.append(loop)

cost = 0
for loop in loops:
    m = min(loop)
    cost += m * (len(loop)-1) + sum(loop) - m

print(cost)

Failing test case input:
5
0 91 14 3 11

Expected output:
122


Actual output:
125


Fixed submission (verdict: Accepted):
n = int(input())
line = input()
L = [int(x) for x in line.split()]

flags = [False] * n
loops = []

S = sorted(L)
I = [S.index(x) for x in L]
for idx, x in enumerate(I):
    if flags[x]:
        continue
    if x == idx:
        flags[idx] = True
        continue

    ws = []
    while S[x] not in ws:
        flags[x] = True
        ws.append(S[x])
        x = I[x]
    loops.append(ws)

cost = 0
for loop in loops:
    a = sum(loop) + (len(loop) - 2) * min(loop)
    b = sum(loop) + min(loop) + (len(loop) + 1) * min(L)
    cost += min(a, b)

print(cost)
Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):

n = int(input())
A = list(map(int, input().split()))
B = A[:]
B.sort()
C = [True for i in range(n)]
minA = min(A)

j = 0
a = []
total = 0
for i in range(n):
    circle = []
    j = i
    while C[j]:
        C[j] = False
        j = B.index(A[j])
        circle.append(A[j])
    n_c = len(circle)
    if n_c == 2:
        total += (circle[0]+circle[1])
    if n_c > 2:
        minc = min(circle)
        cirSum = sum(circle)
        total += min( cirSum + (n-2) * minc , cirSum + minc + (n+1)*minA)
    a.append(circle)
    

print(total)


Failing test case input:
5
1 5 3 8 2

Expected output:
17


Actual output:
21


Fixed submission (verdict: Accepted):

n = int(input())
A = list(map(int, input().split()))
B = A[:]
B.sort()
C = [True for i in range(n)]
minA = min(A)

j = 0
a = []
total = 0
for i in range(n):
    circle = []
    j = i
    while C[j]:
        C[j] = False
        j = B.index(A[j])
        circle.append(A[j])
    n_c = len(circle)
    if n_c == 2:
        total += (circle[0]+circle[1])
    if n_c > 2:
        minc = min(circle)
        cirSum = sum(circle)
        total += min( cirSum + (n_c-2) * minc , cirSum + minc + (n_c+1)*minA)
    a.append(circle)
    

print(total)

Problem: p02278
Buggy submission (Python3, verdict: Wrong Answer):
import itertools


WAIT, PRESORT, SORTED = 0, 1, 2
cost = 0

def swap(w1, w2):
    global cost
    cost += w1[WAIT] + w2[WAIT]
    w1[PRESORT], w2[PRESORT] = w2[PRESORT], w1[PRESORT]

n = input()
w = [[wi, i, None] for i, wi in enumerate(map(int, input().split()))]
s = sorted(w)

for i, wi in enumerate (s):
    wi[SORTED] = i

for si in s:
    tmp = si
    t = []
    while tmp[SORTED] != si[PRESORT]:
        tmp = w[tmp[SORTED]]
        t += [tmp]
    
    for ti in t[::-1]:
        swap(si, ti)

print(cost)

Failing test case input:
5
0 15 3 1 2

Expected output:
22


Actual output:
23


Fixed submission (verdict: Accepted):
import itertools


WAIT, PRESORT, SORTED = 0, 1, 2
cost = 0

def swap(w1, w2):
    global cost
    cost += w1[WAIT] + w2[WAIT]
    w1[PRESORT], w2[PRESORT] = w2[PRESORT], w1[PRESORT]
    

n = input()
w = [[wi, i, None] for i, wi in enumerate(map(int, input().split()))]
s = sorted(w)
minw = s[0]
for i, wi in enumerate (s):
    wi[SORTED] = i
    
for si in s:
    tmp = si
    t = []
    while tmp[SORTED] != si[PRESORT]:
        tmp = w[tmp[SORTED]]
        t += [tmp]

    if len(t ) and minw != si and si[WAIT] * (len(t) - 2) > minw[WAIT] * (len(t) + 2):
        swap(minw, si)
        for ti in t[::-1]:
            swap(minw, ti)
        swap(minw, si)
    else:
        for ti in t[::-1]:
            swap(si, ti)
    
print(cost)
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
if count == n-2:
    print(count + 1)
elif count == n-1:
  	print(count)
else:
    print(count + 2*k)


Failing test case input:
9 2
RRRRLLRLL

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
print(min(n-1, count + 2*k))

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k = map(int, input().split())
s = input()
segment = 0
now = 'w'
ans = 0
for i in range(len(s)):
    if s[i]!=now:
        segment+=1
        now = s[i]
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment+t, ans+t
print(ans)


Failing test case input:
6 5
LRRLRL

Expected output:
5


Actual output:
11


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
s = input()
segment = 0
now = '
ans = 0
for i in s:
    if i!=now:
        segment+=1
        now = i
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment-t, ans+t
print(ans)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K = map(int, input().split())
S = input()

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(max(count-2*K,1))


Failing test case input:
9 0
LRRLLLRRR

Expected output:
5


Actual output:
4


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

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(N-max(count-2*K,1))

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N ,K =map(int,input().split())
S ="R" +input() +"L"
coun = 0
foo = 0
for i in range(N+1):
    if S[i:i+2] =="RL":
        coun += 1
        if i==0 or i==N:
            foo  +=1
if K >= coun:
    print(N)
elif coun -K <= foo:
    print(N-(coun-K))
else:
    print(N-2*(coun-K) +foo)

Failing test case input:
6 3
RRLLRL

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
N ,K =map(int,input().split())
S ="R" +input() +"L"
coun = 0
foo = 0
for i in range(N+1):
    if S[i:i+2] =="RL":
        coun += 1
        if i==0 or i==N:
            foo  +=1
if K >= coun:
    print(N-1)
elif coun -K <= foo:
    print(N-(coun-K))
else:
    print(N-2*(coun-K) +foo)
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n_k_str = input()
n_k_list = n_k_str.split(" ")
N = int(n_k_list[0])
K = int(n_k_list[1])
S = list(input())

pre_s = S.pop(0)
pre_index = -1
result = 0
for i,s in enumerate(S):
    if s != pre_s:
        temp = i - pre_index - 1
        print(temp)
        result += temp
        pre_index = i
    pre_s = s

temp = len(S) - pre_index - 1
result += temp

result += K*2
if result >= N:
    result = N - 1

print(result)

Failing test case input:
9 0
RRRLLRLRL

Expected output:
3


Actual output:
2
1
0
0
0
3


Fixed submission (verdict: Accepted):
n_k_str = input()
n_k_list = n_k_str.split(" ")
N = int(n_k_list[0])
K = int(n_k_list[1])
S = list(input())

pre_s = S.pop(0)
pre_index = -1
result = 0
for i,s in enumerate(S):
    if s != pre_s:
        temp = i - pre_index - 1
        result += temp
        pre_index = i
    pre_s = s

temp = len(S) - pre_index - 1
result += temp

result += K*2
if result >= N:
    result = N - 1

print(result)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k = map(int,input().split())

B = input()
happy = 0
for i in range(n):
  if B[i]=="L" and i != 0 :
    if B[i-1] == "L":
      happy +=1
  if B[i]=="R" and i != n-1 :
    if B[i-1] == "R":
      happy +=1
print(min(happy+2*k, n-1))


Failing test case input:
9 2
LLRLRLRRR

Expected output:
7


Actual output:
6


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())

B = input()
happy = 0
for i in range(n):
  if B[i]=="L" and i != 0 :
    if B[i-1] == "L":
      happy +=1
  if B[i]=="R" and i != n-1 :
    if B[i+1] == "R":
      happy +=1
print(min(happy+2*k, n-1))

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N,K = map(int,input().split())
S = list(input())

group = N-1
for i in range(1, N-1):
  if S[i-1] != S[i]:
    group -= 1
    
print(min(N-1, group+2*K))    

Failing test case input:
13 4
LRRLRLRRLRLLR

Expected output:
11


Actual output:
12


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

group = 0
for i in range(1, N):
  if S[i-1] == S[i]:
    group += 1
    
print(min(N-1, group+2*K))
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k = [int(v) for v in input().split()]
s = input()

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 0:
    ans += 2
    x -= 1
    k -= 1
while k > 0 and y > 1:
    ans += 1
    y -= 1
    k -= 1

print(ans)


Failing test case input:
9 2
RRRRLLRLL

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):
n, k = [int(v) for v in input().split()]
s = input()

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 1:
    ans += 2
    x -= 1
    k -= 1
if k > 0 and x == 1:
    if y == 0:
        ans += 1
    else:
        ans += 2
    k -= 1
if k > 0 and y == 2:
    ans += 1

print(ans)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k=map(int,input().split())
s=input()
x=s.count('RL')-1
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' or s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      exit()
print(cnt)


Failing test case input:
13 8
LRRLRLRRLRLLR

Expected output:
12


Actual output:

Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
s=input()
x=s.count('RL')
y=0
cnt=0
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
if s[0]=='L' and s[n-1]=='R':
  y+=1
while k>0:
  if x>0:
    x-=1
    cnt+=2
    k-=1
  else:
    if y>0:
      cnt+=1
      y-=1
      k-=1
    else:
      break
print(min(n-1,cnt))

Problem: p02918
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, K = map(int, input().split())
S = list(input())
Y = 0
if S[0] == 'L':
    Y += 1
if S[N-1] == 'R':
    Y += 1

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

if K - X > 0:
    count += X*2
    count += 1 if Y > 0 else 0
else:
    count += K*2
print(count)

Failing test case input:
9 5
RRRLRLRLL

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
S = list(input())
Y = 0
if N == 1:
    print(0)
    exit(0)

if S[0] == 'L':
    Y += 1
if S[N-1] == 'R':
    Y += 1

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

count += K*2
print(min(N-1, count))
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K = list(map(int, input().split()))
S = list(input())

RL = []

l = 0
r = 0

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

while count > 0 and K > 0:
    K -= 1
    count -= 2
    ans += 2

if count <= 0:
    ans -= 1

print(ans)

Failing test case input:
6 3
LRRLRL

Expected output:
5


Actual output:
4


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

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

while count > 0 and K > 0:
    if count == 1:
        count -= 1
        ans += 1
        K -= 1
    else:
        K -= 1
        count -= 2
        ans += 2

print(ans)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N , K = map(int, input().split())
S = list(input())
gap=0
for i in range(N-1):
    if(S[i]!=S[i+1]):
        gap+=1
P=len(S)-1-gap
for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
print(P)

Failing test case input:
13 9
RLLRLRRLRLRRL

Expected output:
12


Actual output:
21


Fixed submission (verdict: Accepted):
N , K = map(int, input().split())
S = list(input())
gap=0
for i in range(N-1):
    if(S[i]!=S[i+1]):
        gap+=1

P=len(S)-1-gap
if(gap==0):
    print(P)
    exit()

for i in range(K):
    if(gap>1):
        P+=2
    else:
        P+=1
        break
    gap-=2
    if(gap==0):
        break

print(P)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K = map(int, input().split())
S = input()


LR = 0

for j in range(N-1):
    if S[j] == "L" and S[j+1]== "R":
        LR += 1

ans = min(N-1, N - (LR-K)*2)

print(ans)


Failing test case input:
9 1
LRRRRLRLL

Expected output:
6


Actual output:
7


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




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

ans = min([N-1, ans+2*K])

print(ans)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


MOD = 10 ** 9 + 7

N, K = map(int, input().split())
S = 'R' + input() + 'L'

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

ans = min(ans + 2 * K, len(S) - 1)

print(ans)


Failing test case input:
9 6
RRRLRLRLL

Expected output:
8


Actual output:
10


Fixed submission (verdict: Accepted):


MOD = 10 ** 9 + 7

N, K = map(int, input().split())
S = 'R' + input() + 'L'

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

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

print(ans)
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1

for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
Rs.append(count)
ans = 0
if K == 0:
  ans = sum(Rs)
else:
  if K >= len(Rs) - 2:
      ans = N-1
  elif K == len(Rs) - 3:
      ans = N-2
  else:
      ans = sum(Rs) + 2 * K

print(ans)

Failing test case input:
9 3
LLRLRLRRR

Expected output:
8


Actual output:
7


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1
 
for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
Rs.append(count)
ans = 0

if S[0] == S[N-1]:
    if K >= (len(Rs)-1) 
        ans = N-1
    else:
        ans = sum(Rs) + 2 * K
else:
    if K >= len(Rs) 
        ans = N-1
    else:
        ans = sum(Rs) + 2 * K
  
print(ans)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N,K=map(int,input().split())
S=input()

cnt = 0
bef = "M"
for i in range(0,len(S)):
    if i == len(S) - 1 and S[i] == "R":
        cnt = cnt
    elif S[i-1] == "L" and S[i] == "L":
        cnt += 1
    elif S[i] == "R" and S[i+1] == "R":
        cnt += 1
        
    bef = S[i]
    i += 1
print(min(cnt + 2 * K, N - 1))

Failing test case input:
6 1
LRLRRL

Expected output:
3

Actual output:
4


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

cnt = 0
for i in range(0,len(S)):
    if i == len(S) - 1 and S[i] == "R":
        cnt = cnt
    elif i > 0 and  S[i-1] == "L" and S[i] == "L":
        cnt += 1
    elif S[i] == "R" and S[i+1] == "R":
        cnt += 1
    i += 1
print(min(cnt + 2 * K, N - 1))

Problem: p02918
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, K = map(int, input().split())
S = input()
X = S.count("RL")
Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1

ans = 0
for a, b in zip(S, S[1:]):
    if a == b:
        ans += 1
ans += 2*min(K, X)
if K > X:
    ans += min(Y, K-X)
print(ans)


Failing test case input:
9 6
RRRLRLRLL

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
S = input()
X = S.count("RL")
Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1

ans = 0
for a, b in zip(S, S[1:]):
    if a == b:
        ans += 1
ans += 2*min(K, X)
if K > X:
    ans += min(Y, K-X)
print(min(N-1, ans))
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


import sys

import re

def solve(N, K, S):
    x = len(re.findall(r'RL', S))
    y = (S[0] == 'L') + (S[-1] == 'R')
    ans = N - 2 * x - y
    exe = min(x, K)
    K -= exe
    ans += 2 * exe
    while K and y:
        ans += 1
        K -= 1
        y -= 1
    return ans


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


Failing test case input:
9 2
RRRRLLLLR

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):


import sys

import re

def solve(N, K, S):
    x = len(re.findall(r'RL', S))
    y = (S[0] == 'L') + (S[-1] == 'R')
    ans = N - 2 * x - y
    exe = min(x, K)
    K -= exe
    ans += 2 * exe
    while K and y:
        ans += 1
        K -= 1
        y -= 1
    return min(ans, N-1)


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Problem: p02918
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from itertools import groupby

N,K = map(int,input().split())
S = input()
gnum = len(list(groupby(S)))
if gnum-1<=K:
  print(N-1)
elif gnum-2==K:
  print(N-2)
else:
  print(N-(gnum-2*K))

Failing test case input:
9 3
LLRRRLRLR

Expected output:
8


Actual output:
9


Fixed submission (verdict: Accepted):
from itertools import groupby

N,K = map(int,input().split())
S = input()
gnum = len(list(groupby(S)))
if gnum<=2*K-1:
  print(N-1)
else:
  print(min(N-(gnum-2*K),N-1))

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


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

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


Failing test case input:
6 0
LRRLRL

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):


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)

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
if count == n-2:
    print(count + 1)
else:
    print(count + 2*k)

Failing test case input:
6 3
RLRLRL

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
print(min(n-1, count + 2*k))
Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1

happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(happyCount)


Failing test case input:
6 4
RLRLRL

Expected output:
5


Actual output:
7


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1

happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(min(n - 1, happyCount))

Problem: p02918
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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(happy + 2 * k )

Failing test case input:
10 2
RRRRRLLLLL

Expected output:
9


Actual output:
12


Fixed submission (verdict: Accepted):
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 )))

Problem: p02918
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, K = map(int, input().split())
S = str(input())
cnt = 0
l = 0
r = 0

for i in range(N-1):
    if i == 0 and S[i] == "L":
        l += 1
    if S[i] == "R" and S[i+1] == "L":
        cnt += 1
if S[N-1] == "R":
    r += 1

unhappy = 0
if cnt + l + r <= K:
    unhappy = 1
elif cnt > K:
    unhappy = 2 * (cnt - K) + l + r
else:
    unhappy = l + r
print(N - unhappy)



Failing test case input:
9 3
LLRLRLRRR

Expected output:
8


Actual output:
7


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
S = str(input())
cnt = 0
l = 0
r = 0

for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        cnt += 1
if S[0] == "L":
    l += 1
if S[N-1] == "R":
    r += 1
    
unhappy = 0
if cnt + max(l,r) <= K:
    unhappy = 1
else:
    unhappy = 2 * (cnt - K) + l + r
print(N - unhappy)

Problem: p02918
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):





N,K = map(int, input().split())
Sn = str(input())
Rn = [1]
for i in range(1,N):
  if Sn[i] == Sn[i-1]:
    Rn[-1] += 1
  else:
    Rn.append(1)
Rn.sort(reverse=True)

if len(Rn) > 2*K:
  print(sum(Rn[0:2*K]))
else:
  print(N-1)

Failing test case input:
9 1
LLRLLRRRR

Expected output:
7


Actual output:
6


Fixed submission (verdict: Accepted):





N,K = map(int, input().split())
Sn = str(input())
Rn = [0]
for i in range(1,N):
  if Sn[i] == Sn[i-1]:
    Rn[-1] += 1
  else:
    Rn.append(0)
Rn.sort(reverse=True)

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