qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(r)

Fixed code:
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(min(calc(left), calc(right)))
Buggy code:
n = int(input())
X = list(map(int,input().split()))

ans = 9999999
for p in range(1,n+1):
    s = 0
    for x in X:
        s += (x - p) ** 2
    ans = min(ans, s)
    
print(ans)

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

ans = 10000000
for p in range(1,101):
    s = 0
    for x in X:
        s += (x - p) ** 2
    ans = min(ans, s)
    
print(ans)
Buggy code:
n=int(input())
x=list(map(int,input().split()))
imax=int(max(x))
imin=int(min(x))
lis=[]
re=0
for k in range(imin,imax+1):
  for i in x:
    re=re+(x[i]-k)**2
  lis.append(re)
  re=0
print(min(lis))

Fixed code:
n=int(input())
x=list(map(int,input().split()))
imax=int(max(x))
imin=int(min(x))
lis=[]
re=0
for k in range(imin,imax+1):
  for i in x:
    re=re+(i-k)**2
  lis.append(re)
  re=0
print(min(lis))
Buggy code:
n = int(input())
xs = list(map(int, input().split(" "))) + list(range(100 - n))

for i in range(1, 100):
    print(sum([(xs[j] - i)**2 for j in range(n)]))

Fixed code:
n = int(input())
xs = list(map(int, input().split(" "))) + list(range(100 - n))

ans = 9999999999999999
for i in range(1, 100):
    ans = min(ans, sum([(xs[j] - i)**2 for j in range(n)]))

print(ans)
Buggy code:
n = int(input())
x = list(map(int, input().split()))
av = int(sum(x)/n)
print(sum([(i-av)**2 for i in x]))

Fixed code:
n = int(input())
x = list(map(int, input().split()))
av = round(sum(x)/n)
print(sum([(i-av)**2 for i in x]))
Buggy code:
import numpy as np
n = int(input())
l = np.array(map(int, input().split()))

ans = np.sum((l - round(np.mean(l)))**2)
print(ans)

Fixed code:
import numpy as np
n = int(input())
l = list(map(int, input().split()))

l = np.array(l)
ans = np.sum((l - round(np.mean(l)))**2)
print(int(ans))
Buggy code:
n = int(n)
seq = [int(x) for x in input().split()]
d = round(sum(seq) / len(seq))
print(sum([(x-d)**2 for x in seq]))


Fixed code:
n = int(input())
seq = [int(x) for x in input().split()]
d = round(sum(seq) / len(seq))
print(sum([(x-d)**2 for x in seq]))
Buggy code:
n = int(input())
a = list(map(int, input().split()))
ans = 10000000
for i in range(n):
  s = 0
  for j in range(n):
    s += pow((a[j]-(i+1)), 2)
  ans = min(s, ans)
print(ans)
      

Fixed code:
n = int(input())
a = list(map(int, input().split()))
ans = 10000000
l = 0
for i in range(100):
  s = 0
  for j in range(n):
    s += pow((a[j]-(i+1)), 2)
  ans = min(s, ans)
  if(ans == s):
    l = i+1
print(ans)
Buggy code:
n=int(input())
x=list(map(int,input().split()))
x.sort()
def sump(x,p):
  s=0
  for a in x:
    s+=(a-p)**2
  return s
m=min(x)
M=max(x)
ans=[]
for p in range(m,M+1):
  ans.append(sump(x,p))
print(max(ans))

Fixed code:
n=int(input())
x=list(map(int,input().split()))
x.sort()
def sump(x,p):
  s=0
  for a in x:
    s+=(a-p)**2
  return s
m=min(x)
M=max(x)
ans=[]
for p in range(m,M+1):
  ans.append(sump(x,p))
print(min(ans))
Buggy code:
N,*X=map(int,open(0).read().split());print(sum(map(lambda x:(round(sum(X)/N,0)-x)**2,X)))

Fixed code:
N,*X=map(int,open(0).read().split());print(int(sum(map(lambda x:(round(sum(X)/N,0)-x)**2,X))))
Buggy code:
n = int(input())
x = list(map(int, input().split(' ')))
p = int(sum(x) / n)
r = 0
for i in x:
    r += (i - p) * (i - p)
print(r)

Fixed code:
n = int(input())
x = list(map(int, input().split(' ')))
p = int(round(sum(x) / n))
r = 0
for i in x:
    r += (i - p) * (i - p)
print(r)
Buggy code:
N = input()
List = [int(i) for i in input().split()]



ans = 10**18
def rally(p):
    n = 0
    for a in range(N):
        n += (p-List[a])**2
    return n

for i in range(1,101):
    ans = min(rally(i),ans)

print(ans)


Fixed code:
N = int(input())
List = [int(i) for i in input().split()]


ans = 10**18
def rally(p):
    n = 0
    for a in range(N):
        n += (int(p)-List[a])**2
    return n

for i in range(1,101):
    ans = min(rally(i),ans)

print(ans)
Buggy code:
import numpy as np

def main():
    N = int(input())
    A = sorted(map(int, input().split()))
    mean = np.mean(A)
    x = np.array(A) - mean
    print(int(np.dot(x, x)))

main()

Fixed code:
import numpy as np
 
def main():
    N = int(input())
    A = sorted(map(int, input().split()))
    mean = int(round(np.mean(A)))
    x = np.array(A) - mean
    print(np.dot(x, x))
 
main()
Buggy code:
n = int(input())
l = list(map(int, input().split()))
sl = [0]

ma, mi = max(l), min(l)

for x in range(mi, ma):
  s = 0
  for i in l:
    s += (i - x)**2 
  sl.append(s)

print(min(sl))

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

ma, mi = max(l), min(l)
if ma == mi:
  sl.append(0)

for x in range(mi, ma):
  s = 0
  for i in l:
    s += (i - x)**2 
  sl.append(s)

print(min(sl))
Buggy code:
N = int(input())
X = list(map(int, input().split()))

avg = sum(X)//N

E1 = sum(list(map(lambda x: (x - avg)**2, X)))
E2 = sum(list(map(lambda x: (x - avg - 1)**2, X)))

print(min(E1m E2))

Fixed code:
N = int(input())
X = list(map(int, input().split()))

avg = sum(X)//N

E1 = sum(list(map(lambda x: (x - avg)**2, X)))
E2 = sum(list(map(lambda x: (x - avg - 1)**2, X)))

print(min(E1,E2))
Buggy code:
import numpy as np
N = input()
X = np.array([i for i in map(int,input().split())])

print(np.sum((X-np.round(sum(X)/len(X)))**2))

Fixed code:
import numpy as np
N = input()
X = np.array([i for i in map(int,input().split())])

print(int(np.sum((X-np.round(sum(X)/len(X)))**2)))
Buggy code:
N = int(input())
X = list(map(int, input().split()))
X_sum = sum(X)

if X_sum % N == 0:
    div = round(X_sum / N)
    ans = 0
    for i in range(N):
        ans += (X[i] - div) ** 2
    print(ans)


Fixed code:
N = int(input())
X = list(map(int, input().split()))
div = round(sum(X) / N)
ans = 0
for i in range(N):
    ans += (X[i] - div) ** 2
print(ans)
Buggy code:
def dist(x, p):
    return (x - p)**2


n = int(input())
x = [int(i) for i in input().split()]
index = len(x) // 2
new_x = sorted(x)
p1 = - (- sum(x) // len(x))
p2 = sum(x) // len(x)
ans = [0, 0]
for i in new_x:
    ans[0] += dist(i, p1)
    ans[1] += dist(i, p2)
print(ans)

Fixed code:
def dist(x, p):
    return (x - p)**2


n = int(input())
x = [int(i) for i in input().split()]
index = len(x) // 2
new_x = sorted(x)
p1 = - (- sum(x) // len(x))
p2 = sum(x) // len(x)
ans = [0, 0]
for i in new_x:
    ans[0] += dist(i, p1)
    ans[1] += dist(i, p2)
print(min(ans))
Buggy code:
N=int(input())
X= list(map(int,input().split()))
cnt = 1000000
for j in range(100):
    t = 0
    for i in li:
        t += (i-(j+1))**2
    if t <= cnt:
        cnt = t
print(cnt)


Fixed code:
N=int(input())
X= list(map(int,input().split()))
cnt = 1000000
for j in range(100):
    t = 0
    for i in X:
        t += (i-(j+1))**2
    if t <= cnt:
        cnt = t
print(cnt)
Buggy code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    X = list(iim())

    av = 2 * (sum(X) + N) // N
    ans = 0
    for x in X:
        ans += (x - av)**2
    print(ans)


if __name__ == "__main__":
    resolve()


Fixed code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    X = list(iim())

    av = sum(X) / N
    av = int((2*av+1) // 2)
    ans = 0
    for x in X:
        ans += (x - av)**2
    print(ans)


if __name__ == "__main__":
    resolve()
Buggy code:
N = int(input())
X = list(map(int, input().split()))
start = min(X)
stop = max(X)
count = 0 
ans = 10**10
for i in range(start, stop+1):
    print(i)
    count = 0
    for x in X:
        count += (x-i)**2
    ans = min(count, ans)
print(ans)

Fixed code:
N = int(input())
X = list(map(int, input().split()))
start = min(X)
stop = max(X)
count = 0 
ans = 10**10
for i in range(start, stop+1):
    count = 0
    for x in X:
        count += (x-i)**2
    ans = min(count, ans)
print(ans)
Buggy code:
n=int(input())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(min(x),max(x)):
  now=0
  for x in X:
    now+=(i-x)*(i-x)
  ans=min(now,ans)
print(ans)

Fixed code:
n=int(input())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(min(X),max(X)+1):
  now=0
  for x in X:
    now+=(i-x)*(i-x)
  ans=min(now,ans)
print(ans)
Buggy code:
def main():
    n = int(input())
    x = list(map(int, input().split()))
    
    ans_tmp = 10000000000000
    for j in range(n):
        ans = 0
        for i in range(n):
            ans += (x[i]-(j+1))**2

        if ans_tmp >= ans:
            ans_tmp = ans
    
    print(ans_tmp)

if __name__ == '__main__':
    main()



Fixed code:
def main():
    n = int(input())
    x = list(map(int, input().split()))
    
    ans_tmp = 10000000000000
    for j in range(100):
        ans = 0
        for i in x:
            ans += (i-(j+1))**2

        if ans_tmp >= ans:
            ans_tmp = ans
    
    print(ans_tmp)

if __name__ == '__main__':
    main()
Buggy code:

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


sumDist = sum(x_list)
p = sumDist // N

ans = 0
for i in range(N):
    ans += (x_list[i] - p)**2
print(ans)

Fixed code:

N = int(input())

x_list = list(map(int, input().split())) 

p = round( sum(x_list) / N )
ans = 0

for i in range(N):
    # ans += round( (x_list[i] - p) ** 2 )
    ans +=  (x_list[i] - p) ** 2 

print(ans)
Buggy code:
import math


def inp():
    return(int(input()))


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


n = inp()
arr = invr()
arr = sorted(arr)
max = arr[n-1]
min = arr[0]

cost = 100000000000
print(cost)
for p in range(min, max+1):
    temp_cost = 0
    for xi in arr:
        temp_cost += (xi-p)**2
    if temp_cost < cost:
        cost = temp_cost
print(cost)


Fixed code:
import math


def inp():
    return(int(input()))


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


n = inp()
arr = invr()
arr = sorted(arr)
max = arr[n-1]
min = arr[0]

cost = 100000000000
for p in range(min, max+1):
    temp_cost = 0
    for xi in arr:
        temp_cost += (xi-p)**2
    if temp_cost < cost:
        cost = temp_cost
print(cost)
Buggy code:
N = int(input())
X = list(map(int, input().split()))

ans = 100000
for i in range(100):
  s = 0
  for j in range(N):
    s += (X[i]-i)**2
    
  ans = min(ans, s)
  
print(ans)

Fixed code:
N = int(input())
X = list(map(int, input().split()))
 
ans = 10000000000
for i in range(100):
  s = 0
  for j in range(N):
    s += (X[j]-i)**2
    
  ans = min(ans, s)
  
print(ans)
Buggy code:
n=int(input())
man=list(map,int(input().split()))
can=list[]
for i in range(1,101):
  for j in range(len(man)):
    p=(man[j]-i)**2
	pp+=p
  can.append(pp)
can.sort(key=int)
print(can[0])
  

Fixed code:
n=int(input())
man=list(map(int,input().split()))
can=[]
for i in range(1,101):
	pp=0
	for j in range(len(man)):
		bp=(man[j]-i)**2
		pp+=bp
	can.append(pp)
can.sort(key=int)
print(can[0])
Buggy code:
N = int(input())
X = list(map(int, input().split()))
sum = []
for n in range(1,N+1):
    s = 0
    for x in X:
        s = s + ((x-n)*(x-n))
    sum.append(s)

print(min(sum))

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

for n in range(1,101):
    s = 0
    for x in X:
            s = s + ((x-n)*(x-n))
    sum.append(s)

print(min(sum))
Buggy code:
import sys

n = int(input())
x = list(map(int, input().split()))
min_hp = sys.maxsize

for i in range(1, n+1):
    hp = 0    
    for j in range(n):
        hp += (i - x[j]) * (i - x[j])
    
    min_hp = min(hp, min_hp)
    
print(min_hp)

Fixed code:
import sys

n = int(input())
x = list(map(int, input().split()))
min_hp = sys.maxsize

for i in range(1, 100):
    hp = 0    
    for j in range(n):
        hp += (x[j] - i) * (x[j] - i)

    
    min_hp = min(hp, min_hp)
    
print(min_hp)
Buggy code:
N = int(input())
Xs = list(map(int, input().split()))

loss = 0

sum = 0
for X in Xs:
  sum += X
  
ave = sum/len(Xs)

for X in Xs:
  loss += (X-ave)**2

print(round(loss))

Fixed code:
N = int(input())
Xs = list(map(int, input().split()))
 
loss = 0
 
sum = 0
for X in Xs:
  sum += X
  
ave = round(sum/len(Xs))
 
for X in Xs:
  loss += (X-ave)**2
 
print(loss)
Buggy code:
N = int(input())
X = list(map(int,input().split()))

minimum = 10000000

for n in range(1,N+1):
  d = 0
  for i in range(N):
    d += (X[i]-n)**2
  print(d)
  if kyori < minimum:
    minimum = kyori
print(minimum)

Fixed code:
N = int(input())
X = list(map(int,input().split()))

minimum = 10000000

for n in range(1,max(X)+1):
  kyori = 0
  for i in range(N):
    kyori += (X[i]-n)**2
  if kyori < minimum:
    minimum = kyori
print(minimum)
Buggy code:
import numpy as np
from math import ceil, floor

n = int(input())
l = [int(input()) for x in range(n)]

c = ceil(np.array(l).mean())
f = floor(np.array(l).mean())


print(min(sum([(x-c)**2 for x in l]), sum([(x-f)**2 for x in l])))

Fixed code:
import numpy as np
from math import ceil, floor
 
n = int(input())
l = [int(x) for x in input().split()]
 
c = ceil(np.array(l).mean())
f = floor(np.array(l).mean())

print(min(sum([(x-c)**2 for x in l]), sum([(x-f)**2 for x in l])))
Buggy code:
N=int(input())
X=list(map(int,input().split()))
p=sum(X)//len(X)
ans=0
for i in range(len(X)):
    ans+=(X[i]-p)**2
print(ans)


Fixed code:
N=int(input())
X=list(map(int,input().split()))
p=round(sum(X)/len(X))
ans=0
for i in range(len(X)):
    ans+=(X[i]-p)**2
print(ans)
Buggy code:
n=int(input())
xs=list(map(int,input().split()))
p=sum(xs)//n
print(p)
hp,hq=0,0
for i in xs:
    hp+=(i-p)**2
    hq+=(i-p-1)**2
print(min(hp,hq))

Fixed code:
n=int(input())
xs=list(map(int,input().split()))
p=sum(xs)//n
hp,hq=0,0
for i in xs:
    hp+=(i-p)**2
    hq+=(i-p-1)**2
print(min(hp,hq))
Buggy code:
N=int(input())
List=[int(i) for i in input().split()]
P=int(round(sum(List)/N,0))
def length(x):
 return sum([(i-P-x)**2 for i in List])
i=0
data=length(0)
while True:
 if(length(i-1)<length(i)<length(i+1)):
  i-=1
 elif(length(i-1)>length(i)>length(i+1)):
  i+=1
 else:
  print((length(i))
  break

Fixed code:
N=int(input())
List=[int(i) for i in input().split()]
P=int(round(sum(List)/N,0))
def length(x):
 return sum([(i-P-x)**2 for i in List])

i=0
data=length(0)
while True:
 if(length(i-1)<length(i)<length(i+1)):
  i-=1
 elif(length(i-1)>length(i)>length(i+1)):
  i+=1
 else:
  print(length(i))
  break
Buggy code:

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

hp = [0 for i in range(101)]
for i in range(101):
    for j, x in enumerate(X):
        hp[i] = hp[i] + (x-(i+1))**2

print(hp)
print(min(hp))


Fixed code:

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

hp = [0 for i in range(101)]
for i in range(101):
    for j, x in enumerate(X):
        hp[i] = hp[i] + (x-(i+1))**2

print(min(hp))
Buggy code:
N = int(input())
X = [float(x) for x in input().split()]

P = round(sum(X)/N)
print(sum([(x-P)**2 for x in X]))

Fixed code:
N = int(input())
X = [float(x) for x in input().split()]

P = round(sum(X)/N)
print(int(sum([(x-P)**2 for x in X])))
Buggy code:
N=int(input())
X=list(map(int,input().split()))
ave=sum(X)//N
a=[(X[i]-ave)**2 for i in range(N)]
b=[(X[i]-ave+1)**2 for i in range(N)]
print(min(sum(a),sum(b)))

Fixed code:
N=int(input())
X=list(map(int,input().split()))
ave=sum(X)//N
a=[(X[i]-ave)**2 for i in range(N)]
b=[(X[i]-ave-1)**2 for i in range(N)]
print(min(sum(a),sum(b)))
Buggy code:
n = int(input())
x = [int(x) for x in input().split()]
print(x)
center = sum(x)//len(x)
center2 = center+1
ans, ans2 =0, 0
for i in x:
    ans += (i-center)**2
    ans2 += (i-center2)**2

print(ans) if ans<ans2 else print(ans2)


Fixed code:
n = int(input())
x = [int(x) for x in input().split()]
center = sum(x)//len(x)
center2 = center+1
ans, ans2 =0, 0
for i in x:
    ans += (i-center)**2
    ans2 += (i-center2)**2

print(ans) if ans<ans2 else print(ans2)
Buggy code:
n=input()
mk=input().split()
t=0
s=0
ans=0
for k in mk:
  pk=int(k)
  t+=pk
  s+=(pk**2)
px=round(t/n)
ans+=s+n*(px**2)-2*px*t
print(ans)


Fixed code:
n=int(input())
mk=input().split()
t=0
s=0
ans=0
for k in mk:
  pk=int(k)
  t+=pk
  s+=(pk**2)
px=round(t/n)
ans+=s+n*(px**2)-2*px*t
print(ans)
Buggy code:
N=int(input())
a=[int(x) for x in input().split()]
import math
import statistics
mean=statistics.mean(a)
m=round(mean,0)

sum=0
for x in a:
    t=(x-m)**2
    sum+=t
print(sum)

Fixed code:
N=int(input())
a=[int(x) for x in input().split()]
import math
import statistics
mean=statistics.mean(a)
m=round(mean,0)

sum=0
for x in a:
    t=(x-m)**2
    sum+=t
int_sum=int(sum)
print(int_sum)
Buggy code:
N = int(input())
Xn = [int(x) for x in input().split()]
P = max(Xn)
ans = float('inf')

for j in range(1,P):
  pos = 0
  for i in Xn:
    pos += float((i-j)**2)
    ans = min(pos, ans)

print(ans)
    

Fixed code:
N = int(input())
Xn = [int(x) for x in input().split()]

ans = float('inf')
for i in range(min(Xn),max(Xn)+1):
    pos = 0
    for j in Xn:
        pos += (j-i)**2
    ans = min(ans, pos)
print(ans)
Buggy code:
N = int(input())
X = list(map(int,input().split()))
tmp = 0
tmp2 = 0
ans = 10 ** 10
for i in range(1,101):
  for a in range(N):
    tmp = abs(X[a] - i) ** 2
    tmp2 = tmp
  ans = min(ans,tmp2)
print(str(ans))

Fixed code:
N = int(input())
X = list(map(int,input().split()))
tmp = 0
ans = 10 ** 10
for i in range(1,101):
  tmp = 0
  for a in range(N):
    tmp = tmp + (abs(X[a] - i)) ** 2
  ans = min(ans,tmp)
print(str(ans))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
ans=(int)1e10
for i in range(1,101):
  sum=0
  for x in a: sum+=abs(x-i)
  ans=min(ans,sum)
print(ans)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
ans=1e10
for i in range(1,101):
  sum=0
  for x in a: sum+=pow(abs(x-i),2)
  ans=min(ans,sum)
print(ans)
Buggy code:
n= int(input())
spot = input().split()
ans = []
for i in spot:
    spot.append(int(spot[0]))
    del spot[0]
if n == 1:
    print(0)
else:
    for s in range(min(spot),max(spot)):
        x = 0
    for t in spot:
        x += (s-t)**2
    ans.append(x)
if n != 1:
    print(min(ans))

Fixed code:
n= int(input())
spot = input().split()
ans = []
for i in spot:
    spot.append(int(spot[0]))
    del spot[0]
if n == 1:
    print(0)
else:
    if max(spot)==min(spot):
        ans.append(0)
    for s in range(min(spot),max(spot)):
        x = 0
        for t in spot:
            x += (s-t)**2
        ans.append(x)
if n != 1:
    ans.sort(reverse=False)
    print(ans[0])
Buggy code:
number = input()
coordinates = list(map(int, input().split()))
coordinates.sort()
coordinates.reverse()
#print(coordinates)
list = []
for i in range(coordinates[0]):
    total=0
    for j in range(number):
        total += (coordinates[j]-i)**2
    list.insert(0, total)
list.sort()
print(list[0])

Fixed code:
number = int(input())
coordinates = list(map(int, input().split()))
coordinates.sort()
coordinates.reverse()
#print(coordinates)
list = []
for i in range(coordinates[0]+1):
    total=0
    for j in range(number):
        total += (coordinates[j]-i)**2
    list.insert(0, total)
list.sort()
print(list[0])
Buggy code:
def main():
    N = int(input())
    x = list(map(int, input().split()))
    xx = sum(x)
    p = xx//N
    if (2*N*p+N-2*xx) < 0:
        ans = p+1
    else :
        ans = p
    kyori = 0
    for i in range(N):
        kyori += (x[i]-ans)**2
    print(kyori)


Fixed code:
def main():
    N = int(input())
    x = list(map(int, input().split()))
    xx = sum(x)
    p = xx//N
    if (2*N*p+N-2*xx) < 0:
        ans = p+1
    else :
        ans = p
    kyori = 0
    for i in range(N):
        kyori += (x[i]-ans)**2
    print(kyori)
main()
Buggy code:
n = int(input())
x = list(map(int,input().split()))
c = sum(x)//n
ans = 0
for i in x:
    ans +=(i-c)**2
print(ans)

Fixed code:
n = int(input())
x = list(map(int,input().split()))
c = round(sum(x)/n)
ans = 0
for i in x:
    ans +=(i-c)**2
print(ans)
Buggy code:
N = int(input())
x = list(map(int,input().split()))
l = []
for i in range(N):
    ans = (x[i] - N) ** 2
    l.append(ans)
print(sum(l))

Fixed code:
N = int(input())
x = list(map(int,input().split()))
l = []
for i in range(N):
    ans = (x[i] - round(sum(x)/N)) ** 2
    l.append(ans)
print(sum(l))
Buggy code:
n = int(input())
k = [int(x) for x in stdin.readline().rstrip().split()]

k.sort()

ave = round(sum(k) / len(k) )

#print(ave)

def calc_pow(n):
    i = pow(n - ave, 2) 
    return i
  
j = map(calc_pow, k)

print(sum(j))

Fixed code:
from sys import stdin
 
n = int(input())
k = [int(x) for x in stdin.readline().rstrip().split()]

k.sort()

ave = round(sum(k) / len(k) )

#print(ave)

def calc_pow(n):
    i = pow(n - ave, 2) 
    return i
  
j = map(calc_pow, k)

print(sum(j))
Buggy code:
n=int(input())
l=list(map(int,input().split()))
lmax=0
for i in range(n):
    if lmax<l[i]:
        lmax=l[i]
sumlist=[]

for i in range(lmax):
    sumlist.append(0)
total=0
for i in range(lmax):
    for j in range(n):
        t=(l[j]-i)*(l[j]-i)
        total+=t
    sumlist[i]=total
    total=0
print(sumlist)
min=99999999999999999999999999999999999
for i in range(lmax):
    if min>sumlist[i]:
        min=sumlist[i]
print(min)

Fixed code:
n=int(input())
l=list(map(int,input().split()))
lmax=0
for i in range(n):
    if lmax<l[i]:
        lmax=l[i]
sumlist=[]
for i in range(lmax):
    sumlist.append(0)
total=0
for i in range(lmax):
    for j in range(n):
        t=(l[j]-(i+1))*(l[j]-(i+1))
        total+=t
    sumlist[i]=total
    total=0
min=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
for i in range(lmax):
    if min>sumlist[i]:
        min=sumlist[i]
print(min)
Buggy code:
N = int(input())
X = sorted([int(x) for x in input().split()])

spot = sum(X) // len(X)
stamina = 0
for x in X:
    stamina += pow(x - spot, 2)

print(stamina)


Fixed code:
N = int(input())
X = sorted([int(x) for x in input().split()])

spot = round(sum(X) / len(X))
stamina = 0
for x in X:
    stamina += pow(x - spot, 2)

print(stamina)
Buggy code:
a = int(input())
b = list(map(int, input().split()))
p = sum(b) // a

hp = 0
for i in b:
    hp += (i - p) ** 2

print(hp)


Fixed code:
a = int(input())
b = list(map(int, input().split()))
p = round(sum(b) / a)

hp = 0
for i in b:
    hp += (i - p) ** 2

print(hp)
Buggy code:
n = int(input())
X = list(map(int, input().split()))
# n, k = map(int, input().split())
ans = []
for i in range(n+100):
    total = 0
    for j in range(n):
        print(i+1,X[j])
        diff = ((i+1)-X[j])**2
        total += diff
    ans.append(total)
print(min(ans))


Fixed code:
n = int(input())
X = list(map(int, input().split()))
# n, k = map(int, input().split())
ans = []
for i in range(n+100):
    total = 0
    for j in range(n):
        diff = ((i+1)-X[j])**2
        total += diff
    ans.append(total)
print(min(ans))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
k=0
s=9999999999999999999999999999
for i in range(1,n+1):
  k=0
  for j in range(n):
    k+=(a[j]-i)**2
  s=min(s,k)
print(s)


Fixed code:
n=int(input())
a=list(map(int,input().split()))
k=0
h=max(a)
s=9999999999999999999999999999
for i in range(1,h+1):
  k=0
  for j in range(n):
    k+=(a[j]-i)**2
  s=min(s,k)
print(s)
Buggy code:
n = int(input())
x = list(map(int, input().split()))
s = sum(x)
p = (s+n-1)/n
r = 0
for y in x:
    r += (y - p)**2
print(r)

Fixed code:
n = int(input())
x = list(map(int, input().split()))
s = sum(x)
p = (s+0.5*n)//n
r = 0
for y in x:
    r += (y - p)**2
print(int(r))