qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n = int(input())
a = list(map(int, input().split()))
a = [a[i] - i - 1 for i in range(n)]

b = sorted(a)
if n % 2 != 0:
    b = b[n // 2]
else:
    b = (b[(n + 1) // 2] + b[(n - 1) // 2]) / 2

a = [abs(a[i] - b) for i in range(n)]

ans = sum(a)
print(ans)


Fixed code:
n = int(input())
a = list(map(int, input().split()))
a = [a[i] - i - 1 for i in range(n)]

b = sorted(a)
if n % 2 != 0:
    b = b[n // 2]
else:
    b = (b[n // 2] + b[n // 2 - 1]) // 2

a = [abs(a[i] - b) for i in range(n)]
ans = sum(a)
print(ans)
Buggy code:
n = int(input())
x = list(map(int,input().split()))
for i in range(n):
    x[i]-=i+1
x.sort()
l=len(x)
if n%2==0:
    b = int((x[l/2-1]+x[l/2])/2)
else:
    b = x[int((l-1)/2)]
ans=0
for i in range(n):
    ans+=abs(x[i]-b)
print(ans)

Fixed code:
n = int(input())
x = list(map(int,input().split()))
for i in range(n):
    x[i]-=i+1
x.sort()
if n%2==0:
    b = (x[n//2-1]+x[n//2])//2
else:
    b = x[(n-1)//2]
ans=0
for i in range(n):
    ans+=abs(x[i]-b)
print(ans)
Buggy code:
def median(l):
    n = len(l)
    ls = sorted(l)
    if n == 1: return ls[0]
    if n%2 == 0:
        m1 = ls[int(n/2)-1]
        m2 = ls[int(n/2)]
        return (m1+m2)/2
    
    return ls[int(n/2)-1]

N = int(input())
A = [int(i) for i in input().split()]

A_ = [A[i] - i - 1 for i in range(N)]

A_m = median(A_)
A_m1 = math.floor(A_m)
A_m2 = math.ceil(A_m)

A_b = [sum([abs(a - i) for a in A_]) for i in (A_m1, A_m2)]

print(min(A_b))

Fixed code:
import math

def median(l):
    n = len(l)
    ls = sorted(l)
    if n%2 == 0:
        m1 = ls[int(n/2)-1]
        m2 = ls[int(n/2)]
        return (m1+m2)/2
    
    return ls[int(n/2)]

N = int(input())
A = [int(i) for i in input().split()]

A_ = [A[i] - i - 1 for i in range(N)]

A_m = median(A_)
A_m1 = math.floor(A_m)
A_m2 = math.ceil(A_m)

A_b = [sum([abs(a - i) for a in A_]) for i in (A_m1, A_m2)]

print(min(A_b))
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = [a[i]-i-1 for i in range(n)]
b.sort()
m = b[n//2]
ans = 0
for x in b:
  ans += abs(x-m)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  a[i] -= i+1
a.sort()
b = a[n//2]
ans = 0
for i in range(n):
  ans += abs(a[i]-b)
print(ans)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
tmp = sorted([abs(A[i-1] - i) for i in range(1, N+1)])
mid = tmp[N//2]
print(sum(abs(t - mid) for t in tmp))


Fixed code:
N = int(input())
A = list(map(int,input().split()))
tmp = sorted([A[i-1]-i for i in range(1,N+1)])
mid = tmp[N//2]
print(sum([abs(t-mid) for t in tmp]))
Buggy code:
# C - Linear Approximation
# https://beta.atcoder.jp/contests/abc102/tasks/arc100_a

# C_Linear_Approximation.py

# 7
# 1 1 1 1 2 3 4
#import sys

N = int(input())
a = list(map(int, input().strip().split(" ")))

for i in range(N):
    a[i] = a[i] - (i+1)

a.sort()
if N % 2 != 0:
    mid = (N-1)/2 +1
else:
    mid = N / 2
b = a[mid]

sadness_left = b * (mid-1) - sum(a[:mid])
sadness = sum(a) - b * N + sadness_left * 2

print(sadness)


Fixed code:
# C - Linear Approximation
# https://beta.atcoder.jp/contests/abc102/tasks/arc100_a

# C_Linear_Approximation.py

# 7
# 1 1 1 1 2 3 4
#import sys

N = int(input())
a = list(map(int, input().strip().split(" ")))

for i in range(N):
    a[i] = a[i] - (i+1)

a.sort()
if N % 2 != 0:
    mid = (N-1)//2
else:
    mid = N // 2 -1
b = a[mid]

sadness_left = b * mid - sum(a[:mid])
sadness = sum(a) - b * N + sadness_left * 2

print(sadness)
Buggy code:
import numpy as np
n = int(input())
a = list(map(int,input().split()))
r = np.arange(n) + 1
d = a - r
b = int(np.median(d))
print(np.sum(np.abs(d + b)))

Fixed code:
import numpy as np
n = int(input())
a = list(map(int,input().split()))
r = np.arange(n) + 1
d = a - r
b = int(np.ceil(np.median(d)))
print(np.sum(np.abs(d - b)))
Buggy code:
N = int(input())
A = sorted(list(map(int, input().split())))
M = int((N + 1)/2)
ans = 0

for i in range(N):
    t = A[i] - A[M]
    if t < 0:
        t = (-1) * t
    ans += t

print(ans)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = []

for i in range(N):
    B.append(A[i] - i - 1)

B = sorted(B)
M = B[int((N - 1)/2)]
ans = 0

for i in range(N):
    t = B[i] - M
    if t < 0:
        t = (-1) * t
    ans += t

print(ans)
Buggy code:
def med(data):
    N = len(data)
    data.sort()
    if N % 2 == 0:
        median1 = N/2
        median2 = N/2 + 1
        median1 = int(median1) - 1
        median2 = int(median2) - 1
        median = (data[median1] + data[median2]) / 2
        return median
    else:
        median = (N + 1) / 2
        median = int(median) - 1
        median = data[median]
        return median

import sys
N = int(input())
lists = [int(i) for i in input().split()]
sok = sys.maxsize
list2 = [x - j - 1 for j, x in enumerate(lists)]
b = med(list2)
kanashi = 0
for xx in list2:
    kanashi += abs(xx - b)
print(kanashi)


Fixed code:
def med(data):
    N = len(data)
    data.sort()
    if N % 2 == 0:
        median1 = N/2
        median2 = N/2 + 1
        median1 = int(median1) - 1
        median2 = int(median2) - 1
        median = (data[median1] + data[median2]) / 2
        return median
    else:
        median = (N + 1) / 2
        median = int(median) - 1
        median = data[median]
        return median

import sys
N = int(input())
lists = [int(i) for i in input().split()]
sok = sys.maxsize
list2 = [x - j - 1 for j, x in enumerate(lists)]
b = med(list2)
kanashi = 0
for xx in list2:
    kanashi += abs(xx - b)
print(int(kanashi))
Buggy code:
import numpy as np

N = int(input())
A = [int(i) for i in input().split()]

A_minus_i = [A[i] - (i+1) for i in range(N)]

# print(A)
# print(A_minus_i)

bf = np.average(A_minus_i)
# print(bf)

# b_l = int(bf)+1
# b_m = int(bf)
# b_s = int(bf)-1
#
# score_l = sum([abs(a - b_l) for a in A_minus_i])
# score_m = sum([abs(a - b_m) for a in A_minus_i])
# score_s = sum([abs(a - b_s) for a in A_minus_i])

score = sum([abs(a - bf) for a in A_minus_i])

print(int(score)+1)


Fixed code:
import numpy as np
import statistics

N = int(input())
A = [int(i) for i in input().split()]

A_minus_i = [A[i] - (i+1) for i in range(N)]

# print(A)
# print(A_minus_i)

bf = statistics.median(A_minus_i)
# print(bf)

# b_l = int(bf)+1
# b_m = int(bf)
# b_s = int(bf)-1
#
# score_l = sum([abs(a - b_l) for a in A_minus_i])
# score_m = sum([abs(a - b_m) for a in A_minus_i])
# score_s = sum([abs(a - b_s) for a in A_minus_i])

score = sum([abs(a - bf) for a in A_minus_i])

print(int(score))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = A[i] - i - 1
A = sorted(A)
medN = N//2

x = 0
for i in range(N):
    x = x + abs(A[i] - A[halfN])
print(x)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = A[i] - i - 1
A = sorted(A)
medN = N//2

x = 0
for i in range(N):
    x = x + abs(A[i] - A[medN])
print(x)
Buggy code:
def run(n, a):
    a = [a[i]-i-1 for i in range(len(a))]
    a.sort()
    m = a[int((n-1)/2)]
    ret = 0
    for ai in a:
        print('ai - m', (ai - m))
        ret += abs(ai - m)
    return ret


def read_line():
    n = int(input())
    a = list(map(int, input().split()))
    return (n, a)


def main():
    n, a = read_line()
    print(run(n, a))


if __name__ == '__main__':
    main()


Fixed code:
def run(n, a):
    a = [a[i]-i-1 for i in range(len(a))]
    a.sort()
    m = a[int((n-1)/2)]
    ret = 0
    for ai in a:
        ret += abs(ai - m)
    return ret


def read_line():
    n = int(input())
    a = list(map(int, input().split()))
    return (n, a)


def main():
    n, a = read_line()
    print(run(n, a))


if __name__ == '__main__':
    main()
Buggy code:
def calc_sadness(b):
    ans = 0
    for a in A:
        ans += abs(a - b)
    return ans


N = int(input())
A = []
for idx, a in enumerate(map(int, input().split())):
    A.append(a - idx - 1)
A.sort()

if N % 2 == 0:
    b = [[N // 2]]
else:
    b = [A[N // 2 - 1], A[N // 2]]

print(min(map(calc_sadness, b)))

Fixed code:
def calc_sadness(b):
    ans = 0
    for a in A:
        ans += abs(a - b)
    return ans


N = int(input())
A = []
for idx, a in enumerate(map(int, input().split())):
    A.append(a - idx - 1)
A.sort()

if N % 2 == 0:
    b = [A[N // 2]]
else:
    b = [A[N // 2 - 1], A[N // 2]]

print(min(map(calc_sadness, b)))
Buggy code:
import statistics
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
ans = float('inf')
medi = statistics.median(A)
for b in range(medi-1, medi+2):
  ans = min(ans, sum([abs(A[i]-b) for i in range(N)]))
print(ans)


Fixed code:
import statistics
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
ans = float('inf')
medi = int(statistics.median(A))
for b in range(medi-1, medi+2):
  ans = min(ans, sum([abs(A[i]-b) for i in range(N)]))
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = [A[i]-i-1 for i in range(N)]

B.sort()
b = B[N//2]
print(sum([A[i]-i-b-1 for i in range(N)]))

Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = [A[i]-i-1 for i in range(N)]

B.sort()
b = B[N//2]
print(sum([abs(A[i]-i-b-1) for i in range(N)]))
Buggy code:
n = int(input())
a = list(map(int, input().split()))

b = []

for i in range(n):
  b.append(a[i]-(i+1))

# print(b)
b.sort()
# print(b)

min_b=b[n//2]
print(min_b)

ans = 0
for i in range(n):
  ans+=abs(a[i]-(min_b+(i+1)))
  # print(ans)
print(ans)

Fixed code:
n = int(input())
a = list(map(int, input().split()))

b = []

for i in range(n):
  b.append(a[i]-(i+1))

# print(b)
b.sort()
# print(b)

min_b=b[n//2]
# print(min_b)

ans = 0
for i in range(n):
  ans+=abs(a[i]-(min_b+(i+1)))
  # print(ans)
print(ans)
Buggy code:
from statistics import median

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

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

b = median(A)

ans = 0
for a in A:
  ans += abs(a-b)

print(ans)

Fixed code:
from statistics import median

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

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

b = int(median(A))
ans = 0

for a in A:
  ans += abs(a-b)

print(ans)
Buggy code:
from statistics import median as m
n=int(input())
A=list(map(int,input().split()))
B=sorted([A[i]-i-1 for i in range(n)])
m(B)=mb
print(int(sum([abs(b-mb) for b in B])))

Fixed code:
from statistics import median as m
n=int(input())
A=list(map(int,input().split()))
B=sorted([A[i]-i-1 for i in range(n)])
mb=m(B)
print(int(sum([abs(b-mb) for b in B])))
Buggy code:
N = int(input())
A = list(map(int,input().split()))
B = [A[i] - (i+1) for i in range(N)]

B.sort()

print(sum(abs(x - B[2//N]) for x in B))

Fixed code:
N = int(input())
A = list(map(int,input().split()))
B = [A[i] - (i+1) for i in range(N)]

B.sort()

print(sum([abs(x - B[N//2]) for x in B]))
Buggy code:
n = int(input())
a = list(map(int , input().split()))
b = [a[i] - (i+1) for i in range(n)]
print(b)
b.sort()

ans = 10 ** 10
for j in range(3):
    bi = b[((n-1)//2+j)%n]
    s = 0
    for i in range(n):
        s += abs(b[i] - bi)
    print(((n-1)//2+j)%n, s)
    ans = min(ans, s)
print(ans)

Fixed code:
n = int(input())
a = list(map(int , input().split()))
b = [a[i] - (i+1) for i in range(n)]

b.sort()
#print(b)
#ans = 10 ** 10
bi = b[n//2]
s = 0
for i in range(n):
    s += abs(a[i] - (i + 1 + bi))
#print(((n-1)//2+j)%n, s)
#ans = min(ans, s)
print(s)

"""
for j in range(3):
    bi = b[(n//2+j)%n]
    s = 0
    for i in range(n):
        s += abs(a[i] - (bi + i + 1))
    #print(((n-1)//2+j)%n, s)
    ans = min(ans, s)
print(ans)
"""
Buggy code:
import sys
input = sys.stdin.readline

N = int(input().strip())
A = [int(x) - (i + 1) for i, x in enumerate(input().strip().split())]
B = sorted(A)[(len(A)+1)//2-1]

print(sum([a - B for a in A]))


Fixed code:
import sys
input = sys.stdin.readline

N = int(input().strip())
A = [int(x) - (i + 1) for i, x in enumerate(input().strip().split())]
B = sorted(A)[(len(A)+1)//2-1]

print(sum([abs(a - B) for a in A]))
Buggy code:
import statistics as st
import numpy as np

def test():
    ans = 0
    n = int(input())
    a = np.array(list(map(int,input().split())))
    i = np.array([i for i in range(1,n+1)])
    b = st.median(a-i)
    for k in range(n):
        ans += abs(a[k]-i[k]-b)
    print(ans)  


if __name__ == "__main__":
    test()


Fixed code:
import statistics as st
import numpy as np

def test():
    ans = 0
    n = int(input())
    a = np.array(list(map(int,input().split())))
    i = np.array([i for i in range(1,n+1)])
    b = st.median(a-i)
    for k in range(n):
        ans += abs(a[k]-i[k]-b)
    print(int(ans))  


if __name__ == "__main__":
    test()
Buggy code:
N = int(input())
A = list(map(int, input().split()))
A.sort()

A_ = [a--1-i for i,a in enumerate(A)]
b = int(0.5 * (A_[len(A_)//2] + A_[len(A_)//2 - (len(A_)+1)%2]))

print(sum([abs(a-b) for a in A_]))



Fixed code:
N = int(input())
A = list(map(int, input().split()))
A_ = [a--1-i for i,a in enumerate(A)]
A_.sort()

b = int(0.5 * (A_[len(A_)//2] + A_[len(A_)//2 - (len(A_)+1)%2]))

print(sum([abs(a-b) for a in A_]))
Buggy code:
from statistics import median
n = int(input())
a = list(map(int, input().split()))
b = [a[i]-i-1 for i in range(n)]
B = median(b)
ans = 0
for i in range(n):
    ans += abs(b[i]-B)
print(ans)

Fixed code:
from statistics import median
n = int(input())
a = list(map(int, input().split()))
b = [a[i]-i-1 for i in range(n)]
B = median(b)
ans = 0
for i in range(n):
    ans += abs(b[i]-B)
print(int(ans))
Buggy code:
import statistics as stc

n=int(input())
 
a = list(map(int, input().split()))

#a[i]-(b+1) = a[i]-i-b
a_ = [a[i]-(i+1) for i in range(len(a))]

b=stc.median(a)

def sad(a,b):
    score=0
    for i in range(1,len(a)+1):
        #score+= abs(a[i]-(b+i))
        score+= abs(a_[i]-b)

res = sad(a,b)
print(res)

Fixed code:
import statistics as stc

n=int(input())
 
a = list(map(int, input().split()))

#a[i]-(b+1) = a[i]-i-b
a_ = [a[i]-(i+1) for i in range(len(a))]

b=stc.median(a_)

def sad(a,b):
    b=int(b)
    score=0
    for i in range(len(a)):
        score+= abs(a[i]-b)
    return score

res = sad(a_,b)
print(res)
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
  B.append(A[i]-(i+1))
B.sort()
if N%2==0:
  a=N/2
else:
  a=(N+1)/2
b=B[a-1]
for i in range(N):
  B[N-1]=abs(B[N-1]-b)
print(B)
    
  
  

Fixed code:
N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
  B.append(A[i]-(i+1))
B.sort()
if N%2==0:
  a=N/2
  b=int((B[int(a)]+B[int(a-1)])/2)
else:
  a=(N+1)/2
  b=B[int(a-1)]
for i in range(N):
  B[i]=abs(B[i]-b)
print(sum(B))
Buggy code:
from statistics import median
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
for i in range(N):
  B[i]=A[i]-i
b=median(B)
ans=0
for i in range(N):
  ans+=abs(B[i]-b)
print(ans)

Fixed code:
from statistics import median
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
for i in range(N):
  B[i]=A[i]-i
b=int(median(B))
ans=0
for i in range(N):
  ans+=abs(B[i]-b)
print(ans)
Buggy code:
import statistics

N = int(input())

A = list(map(int, input().split()))
c = 0

B = [A[i]-(i+1) for i in range(len(A))]
b = statistics.median(B)

for i, a in enumerate(A):
    c += abs(a - (b + i+1))

print(c)


Fixed code:
import statistics

N = int(input())

A = list(map(int, input().split()))
c = 0

B = [A[i]-(i+1) for i in range(len(A))]
b = int(statistics.median(B))

for i, a in enumerate(A):
    c += abs(a - (b + i+1))

print(c)
Buggy code:
import statistics


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

A = [a - i for i, a in enumerate(A)]

M = int(statistics.median(A_b))
R = []

for m in [M-2. M-1, M, M+1, M+2]:
    R.append(sum([abs(a - m) for a in A]))

print(min(R))


Fixed code:
import statistics


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

A = [a - i for i, a in enumerate(A)]

M = int(statistics.median(A))
R = []

for m in [M-2, M-1, M, M+1, M+2]:
    R.append(sum([abs(a - m) for a in A]))

print(min(R))
Buggy code:
from statistics import median
n = int(input())
nums = list(map(int, input().split()))
bs = nums.copy()
tmp = 0

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


med = median(bs)

ans = 0
for i in range(n):
    ans += abs(bs[i] - med)

print(ans)

Fixed code:
from statistics import median
n = int(input())
nums = list(map(int, input().split()))
bs = nums.copy()
tmp = 0

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


med = median(bs)

ans = 0
for i in range(n):
    ans += abs(bs[i] - med)

print(int(ans))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
A_ = [a-i-1 for i,a in enumerate(A)]
print (A_)
A_.sort()
print (A_)
b = A_[N//2]
print (b)
print(sum([abs(ai-b) for ai in A_]))

Fixed code:
N = int(input())
A = list(map(int, input().split()))
A_ = [a-i-1 for i,a in enumerate(A)]

A_.sort()

b = A_[N//2]

print(sum([abs(ai-b) for ai in A_]))
Buggy code:
from statistics import median
n = int(input())
a = [int(i) for i in input().split()]
b = []

for i in range(n):
    b.append(a[i] - (i+1))

m = median(b)

sum = 0
for i in range(n):
    sum += abs(a[i] - (m+i+1))

print(sum)



Fixed code:
from statistics import median
n = int(input())
a = [int(i) for i in input().split()]
b = []

for i in range(n):
    b.append(a[i] - (i+1))

m = int(median(b))

sum = 0
for i in range(n):
    sum += abs(a[i] - (m+i+1))

print(sum)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
b=[]

for i in range(n):
  b.append(abs(a[i]-i))
  
b.sort()

pin=b[n//2]

ans=0

for i in b:
  ans+=abs(pin-i)
  

print(ans)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
b=[]

for i in range(n):
  b.append(a[i]-i)
  
b.sort()

pin=b[n//2]

ans=0

for i in b:
  ans+=abs(pin-i)
  

print(ans)
Buggy code:
import statistics
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	seq = []
	for i in range(1, n+1):
		seq.append(abs(i-a[i-1]))
	seq = sorted(seq)
	m = statistics.median(seq)
	ans = 0
	for i in seq:
		ans += abs(i-m)
	print(ans)
resolve()

Fixed code:
import statistics
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	seq = []
	for i in range(1, n+1):
		seq.append(i-a[i-1])
	seq = sorted(seq)
	m = int(statistics.median(seq))
	ans = 0
	for i in seq:
		ans += abs(i-m)
	print(ans)
resolve()
Buggy code:


def solve():
    N = int(input())
    As = list(map(int, input().split()))

    Bs = []

    for i in range(N):
        Bs.append(As[i]-i-1)

    Bs.sort()
    b = Bs[N//2]

    ans = 0

    for i in range(N):
        ans += Bs[i]-b

    print(ans)


# Solve
if __name__ == "__main__":
    solve()


Fixed code:


def solve():
    N = int(input())
    As = list(map(int, input().split()))

    Bs = []

    for i in range(N):
        Bs.append(As[i]-i-1)

    Bs.sort()
    b = Bs[N//2]

    ans = 0

    for i in range(N):
        ans += abs(Bs[i]-b)

    print(ans)


# Solve
if __name__ == "__main__":
    solve()
Buggy code:
N = int(input())
A = list(map(int, input().split()))
B = [A[i] - 1 - i for i in range(N)]
B.sort()
if N % 2 == 0:
    med = [int((B[int(N / 2) - 1] + B[int(N / 2)]) / 2)]
else:
    med = [B[int((N + 1) / 2) - 1]]
S = 0
for i in range(N):
    S += abs(B[i] - b)
print(S)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
B = [A[i] - 1 - i for i in range(N)]
B.sort()
if N % 2 == 0:
    med = int((B[int(N / 2) - 1] + B[int(N / 2)]) / 2)
else:
    med = B[int((N + 1) / 2) - 1]
S = 0
for i in range(N):
    S += abs(B[i] - med)
print(S)
Buggy code:
import numpy as np
N = int(input())
A = list(map(lambda x:int(x),input().split(" ")))
npA = np.array(A)
B = npA-(1+np.arange(len(npA)))
opt_b = statistics.median(B)
print(int(np.sum(np.abs(B-opt_b))))

Fixed code:
import numpy as np
from statistics import median
N = int(input())
A = list(map(lambda x:int(x),input().split(" ")))
npA = np.array(A)
B = npA-(1+np.arange(len(npA)))
opt_b = median(B)
print(int(np.sum(np.abs(B-opt_b))))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=a[i]-(i+1)
temp.sort()
ave=temp/n
print(ave)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=a[i]-(i+1)
temp.sort()
ave=int(n/2)
ans=0
for i in range(0,n):
  ans=ans+abs(temp[i]-temp[ave])
print(ans)
Buggy code:
import statistics

n = int(input())
a = [int(n) for n in input().split(" ")]

sub_a = [a_n - i - 1 for i, a_n in enumerate(a)]
median = statistics.median(sub_a)
print(sum([abs(sub_a-median) for sub_a_n in sub_a]))

Fixed code:
import statistics

n = int(input())
a = [int(n) for n in input().split(" ")]

sub_a = [a_n - i - 1 for i, a_n in enumerate(a)]
median = statistics.median(sub_a)
print(int(sum([abs(sub_a_n-median) for sub_a_n in sub_a])))
Buggy code:
import math
import statistics
N = int(input())
L = [i for i in map(int,input().split())]
Q = []
for i in range(len(L)):
  Q.append(L[i] - (i+1))
b = statistics.median(Q)
a = b - 1
c = b + 1
res = 0
res2 = 0
res3 = 0
for i in range(N):
  res += abs(L[i] - (i+1) - b)
  res2 += abs(L[i] - (i+1) - a)
  res3 += abs(L[i] - (i+1) - c)
print(min(res,res2,res3))

Fixed code:
import math
import statistics
N = int(input())
L = [i for i in map(int,input().split())]
Q = []
for i in range(len(L)):
  Q.append(L[i] - (i+1))
b = statistics.median(Q)
a = b - 1
c = b + 1
res = 0
res2 = 0
res3 = 0
for i in range(N):
  res += abs(L[i] - (i+1) - b)
  res2 += abs(L[i] - (i+1) - a)
  res3 += abs(L[i] - (i+1) - c)
print(int(min(res,res2,res3)))
Buggy code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort()
    B = [A[i-1] - i for i in range(1,N+1)]
    b = B[N // 2] if N%2 == 1 else round((B[N // 2 - 1] + B[N // 2]) / 2)
    print(sum([abs(bb -b) for bb in B]))
    return

if __name__ == "__main__":
    main()

Fixed code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    B = [A[i-1] - i for i in range(1,N+1)]
    B.sort()
    b = B[N // 2] if N%2 == 1 else round((B[N // 2 - 1] + B[N // 2]) / 2)
    print(sum([abs(bb -b) for bb in B]))
    return

if __name__ == "__main__":
    main()
Buggy code:
N = int(input())
A = [int(x)-i for x,i in zip(input().split(),range(1,N+1))]
from math import ceil,floor
    
A.sort()


if N%2 == 0:
    b = round(A[floor(N/2)]+A[ceil(N/2)])
else:
    b = A[floor(N/2)]
ans = 0
for a in A:
    ans += abs(a-b)
print(ans)

Fixed code:
N = int(input())
A = [int(x)-i for x,i in zip(input().split(),range(1,N+1))]
from math import ceil,floor
    
A.sort()


if N%2 == 0:
    b = round((A[floor(N/2)]+A[ceil(N/2)])/2)
else:
    b = A[floor(N/2)]
ans = 0
for a in A:
    ans += abs(a-b)
print(ans)
Buggy code:
def main():
    from statistics import median
    n, *a, = map(int, open(0).read().split())
    a = [x - i for i, x in enumerate(a, 1)]
    m = median(a)
    ans = sum([abs(x - m) for x in a])
    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
def main():
    from statistics import median

    n, *a, = map(int, open(0).read().split())
    a = [x - i for i, x in enumerate(a, 1)]
    a.sort()
    m = int(median(a))

    ans = sum([abs(x - m) for x in a])
    print(ans)


if __name__ == '__main__':
    main()
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=[]
total=0
for i in range(N):
    B.append(A[i]-i-1)

B.sort()
print(B)
if len(A)%2==1:
    for i in range(N):
        total+=abs(A[i]-(B[len(B)//2]+i+1))
        #print(B[len(B)//2+1],total)
else:
    for i in range(N):
        total+=abs(A[i]-(B[len(B)//2]+i+1))
print(total)


Fixed code:
N=int(input())
A=list(map(int,input().split()))
B=[]
total=0
for i in range(N):
    B.append(A[i]-i-1)

B.sort()
#print(B)
if len(A)%2==1:
    for i in range(N):
        total+=abs(A[i]-(B[len(B)//2]+i+1))
        #print(B[len(B)//2+1],total)
else:
    for i in range(N):
        total+=abs(A[i]-(B[len(B)//2]+i+1))
print(total)
Buggy code:
import math
import statistics
N = int(input())

A = input().rstrip().split()
#print(A)
i = 0
kari =[]
for j in A:
    i +=1
    B = int(j)-i
    kari.append(B)
#print(kari)
#print(statistics.median(kari))
b = statistics.median(kari)

sum = 0
for i in kari:
    sum+=i-b
print(sum)

Fixed code:
import math
import statistics
N = int(input())

A = input().rstrip().split()
#print(A)
i = 0
kari =[]
for j in A:
    i +=1
    B = int(j)-i
    kari.append(B)
#print(kari)
b = statistics.median(kari)

sum = 0
for i in kari:
    sum+=abs(i-b)
print(int(sum))