qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n = int(input())
ints = [int(num) for num in set(input().split())]
res = []
for x in range(min(ints), max(ints)+1):
    total = 0
    for num in ints:
        total += (x - num)**2
    
    res.append(total)

ans = min(res)
print(ans)


Fixed code:
n = int(input())
ints = [int(num) for num in list(input().split())]
res = []
for x in range(min(ints), max(ints)+1):
    total = 0
    for num in ints:
        total += (x - num)**2
    
    res.append(total)

ans = min(res)
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))

avg = sum(A) // N
ans = 0
for i, a in enumerate(A):
    ans += (a - avg) ** 2

print(ans)


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

avg = round(sum(A) / N)
ans = 0
for i, a in enumerate(A):
    ans += (a - avg) ** 2

print(ans)
Buggy code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    listcost = []
  
    for i in range(min(a),max(a)):
        cost = 0
        for j in a:
            if i != j:
                cost += (i-j) ** 2
        listcost.append(cost)
    print(min(listcost))
  
  
if __name__=='__main__':
  main()

Fixed code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    listcost = []
  
    for i in range(min(a),max(a)+1):
        cost = 0
        for j in a:
            if i != j:
                cost += (i - j) ** 2
        listcost.append(cost)
    print(min(listcost))
  
  
if __name__=='__main__':
  main()
Buggy code:
n = int(input())
a = [int(e) for e in input().split()]
ans = float("inf")
MIN, MAX = min(a), max(a)
for i in range(MIN, MAX):
    cost = 0
    for j in a:
        cost = cost + (i-j)**2
    ans = min(ans, cost)
print(cost)

Fixed code:
n = int(input())
a = [int(e) for e in input().split()]
ans = float("inf")
MIN, MAX = min(a), max(a)
for i in range(MIN, MAX+1):
    cost = 0
    for j in a:
        cost = cost + (i-j)**2
    ans = min(ans, cost)
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split(' ')))
ans = []
if A.count(A[0]) == 1:
    print(0)
    exit(0)
for i in range(1,101):
    cost = 0
    for j in range(N):
        if A[j] == i:
            continue
        else:
            cost += (A[j] - i)**2
    ans.append(cost)
import heapq
heapq.heapify(ans)
print(heapq.heappop(ans))

Fixed code:
N = int(input())
A = list(map(int, input().split(' ')))
ans = []
if A.count(A[0]) == len(A):
    print(0)
    exit(0)
for i in range(-100,101):
    cost = 0
    for j in range(N):
        if A[j] == i:
            continue
        else:
            cost += (A[j] - i)**2
    ans.append(cost)
import heapq
heapq.heapify(ans)
print(heapq.heappop(ans))
Buggy code:
import math

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

avg_ceil = math.ceil(sum(nums) / len(nums))
avg_floor = math.floor(sum(nums) / len(nums))

cost_ceil = 0
cost_floor = 0

for x in nums:
    if x == avg_ceil:
        continue
    cost_ceil += (x - avg_ceil)**2

for x in nums:
    if x == avg_floor:
        continue
    cost_floor += (x - avg_floor)**2

if cost_ceil <= cost_floor:
    print(cost_ceil)
else
    print(cost_floor)



Fixed code:
import math

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

avg_ceil = math.ceil(sum(nums) / len(nums))
avg_floor = math.floor(sum(nums) / len(nums))

cost_ceil = 0
cost_floor = 0

for x in nums:
    if x == avg_ceil:
        continue
    cost_ceil += (x - avg_ceil)**2

for x in nums:
    if x == avg_floor:
        continue
    cost_floor += (x - avg_floor)**2

if cost_ceil <= cost_floor:
    print(cost_ceil)
else:
    print(cost_floor)
Buggy code:
n=int(input())
integers=list(map(int,input().split()))

sql=[x*2 for x in integers]
s=sum(integers)
t=sum(sql)
m=min(s%n, n-s%n)

print((m**2-s**2)//n+t)

Fixed code:
n=int(input())
integers=list(map(int,input().split()))
 
sql=[x**2 for x in integers]
s=sum(integers)
t=sum(sql)
m=min(s%n, n-s%n)
 
print((m**2-s**2)//n+t)
Buggy code:
N = int(input())
input_list = [int(i) for i in input().split()]

ans = 10 ** 20

for i in range(min(input_list), max(input_list)+1):
    ans1 = 0
    for input in input_list:
        ans1 += (i - input) ** 2
    print(ans1)
    ans = min(ans, ans1)

print(ans)

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

ans = 10 ** 20

for i in range(min(input_list), max(input_list)+1):
    ans1 = 0
    for input in input_list:
        ans1 += (i - input) ** 2
    ans = min(ans, ans1)

print(ans)
Buggy code:
n = int(input())
a_list = list(map(int, input().split()))

b = sum(a_list) // n
ans1 = sum([(a-b)^2 for a in a_list])

b += 1
ans2 = sum([(a-b)^2 for a in a_list])

print(min(ans1,ans2))

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

b = sum(a_list) // n
ans1 = sum([(a-b)**2 for a in a_list])

b += 1
ans2 = sum([(a-b)**2 for a in a_list])

print(min(ans1,ans2))
Buggy code:
n = int(input())
nlist= list(map(int,input().split()))

nmax = max(nlist)
nmin = min(nlist)

costs = []

for i in range(len(nlist)):
    p = 0
    for j in range(len(nlist)):
        p = p + (nlist[i]-nlist[j]) ** 2
    costs.append(p)
    
print(min(costs))
    

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

nmax = max(nlist)
nmin = min(nlist)

costs = []

for i in range(nmin,nmax + 1):
    p = 0
    for j in range(len(nlist)):
        p = p + (i-nlist[j]) ** 2
    costs.append(p)
    
print(min(costs))
Buggy code:
N=int(input())
List = list(map(int, input().split()))
sumS = 0
for i in range(N):
  sumS +=List[i]
trial = sumS // N
mid = 0
res = 10000000
for i in range(N):
  trial += i
  mid = 0 
  for j in range(N):
    mid += (trial - List[i])**2
  res = max(res, mid)
print(res)

Fixed code:
N=int(input())
List = list(map(int, input().split()))
sumS = 0
for i in range(N):
  sumS +=List[i]
trial = sumS // N
mid = 0
res = 10000000
for i in range(N):
  trial += i
  mid = 0 
  for j in range(N):
    mid += (trial - List[j])**2
  res = min(res, mid)
print(res)
Buggy code:
a = input()
b = list(map(int, input().split()))
c = round(sum(b) / len(b))
sum = 0
for i in range(len(b)):
    sum += (b[i] - c) ^ 2
print(sum)

Fixed code:
a = input()
b = list(map(int, input().split()))
c = round(sum(b) / len(b))
s = 0
for i in range(len(b)):
    s += ((b[i] - c) ** 2)
print(s)
Buggy code:
import math
import sys
from functools import reduce
input = sys.stdin.readline

def fullsearch(a):
    for i in range(1, 101):
        yield reduce(lambda total, x: (x - i) ** 2 + total, a, 0)

def main():
    input()
    A = [int(c) for c in input().split()]

    print(min(fullsearch(A)))

main()

Fixed code:
import math
import sys
from functools import reduce
input = sys.stdin.readline

def fullsearch(a):
    for i in range(-100, 101):
        yield reduce(lambda total, x: (x - i) ** 2 + total, a, 0)

def main():
    input()
    A = [int(c) for c in input().split()]

    print(min(fullsearch(A)))

main()
Buggy code:
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
    ttl3 += (i - avarage3) ** 2
print(min(ttl1, ttl2, ttl3))


Fixed code:
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
avarage3 = round(sum(A) / N)
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
    ttl3 += (i - avarage3) ** 2
print(min(ttl1, ttl2, ttl3))
Buggy code:
N = int(input())
A = list(map(int, input().split()))

mn = 10**9
for i in range(N):
  cost = 0
  for j in range(N):
    cost += (A[i]-A[j])**2
  mn = min(cost, mn)
print(mn)

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

mn = 10**9
for j in range(min(A), max(A)+1):
  cost = 0
  for i in range(N):
    cost += (A[i]-j)**2
  mn = min(cost, mn)
print(mn)
Buggy code:
N = int(input())
temp = input().rstrip().split(" ")
list1 = []
for i in range(N):
    list1.append(int(temp[i]))
list1.sort()
avg = sum(list1) / len(list1)
ans = 0
for i in range(N):
    ans += (list1[i] - avg)**2
print(int(ans))

Fixed code:
N = int(input())
temp = input().rstrip().split(" ")
list1 = []
for i in range(N):
    list1.append(int(temp[i]))
list1.sort()
avg = round(sum(list1) / len(list1))
ans = 0
for i in range(N):
    ans += (list1[i] - avg)**2
print(ans)
Buggy code:
import math

N = int(input())
A = [int(a) for a in input().split(" ")]
ave = sum(A) / N
can1 = math.floor(ave)
can2 = math.ceil(ave)

cost1 = sum([(a - can1) ** 2 for a in A])
cost2 = sum([(a - can2) ** 2 for a in A])
print(min[cost1, cost2])

Fixed code:
import math
 
N = int(input())
A = [int(a) for a in input().split(" ")]
ave = sum(A) / N
can1 = math.floor(ave)
can2 = math.ceil(ave)
 
cost1 = sum([(a - can1) ** 2 for a in A])
cost2 = sum([(a - can2) ** 2 for a in A])
print(min([cost1, cost2]))
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
cons = int(sum(A)/N)
ans = 0 
for i in range(N):
  ans += (A[i] - cons)**2 

print(ans)

Fixed code:
N = int(input())
A = [int(i) for i in input().split()]
cons = round(sum(A) / N )
ans = 0 
for i in range(N):
  ans += (A[i] - cons)**2 
 
print(ans)
Buggy code:
n= int(input())
a = list(map(int,input().split()))
from collections import defaultdict

d = defaultdict(int)

for i in range(n):
    d[a[i]] += 1

ans = []

for i in range(1,101):
    cnt = 0
    for j in d.keys():
        cnt += d[j]*(i-j)**2
    ans.append(cnt)

print(min(ans))


Fixed code:
n= int(input())
a = list(map(int,input().split()))
from collections import defaultdict

d = defaultdict(int)

for i in range(n):
    d[a[i]] += 1

ans = []

for i in range(-100,101):
    cnt = 0
    for j in d.keys():
        cnt += d[j]*(i-j)**2
    ans.append(cnt)

print(min(ans))
Buggy code:
def solve(s):
    sum1 = 0
    sum2 = 0
    length = 0
    for i in s.split():
        x = int(i)
        sum1 += x**2
        sum2 += x
        length += 1

    score = 10**8

    for y in range(100,-101, -1):
        tmp = sum1 - sum2*2*y + length*(y**2)
        score = min(tmp, score)

    return score

if __name__ == "__main__":
    n = int(input())
    s = input()
    solve(s)

Fixed code:
def solve(s):
    sum1 = 0
    sum2 = 0
    length = 0
    for i in s.split():
        x = int(i)
        sum1 += x**2
        sum2 += x
        length += 1

    score = 10**8

    for y in range(100,-101, -1):
        tmp = sum1 - sum2*2*y + length*(y**2)
        score = min(tmp, score)

    return score

if __name__ == "__main__":
    n = int(input())
    s = input()
    print(solve(s))
Buggy code:
import statistics
n = int(input())
a = [int(i) for i in input().split()]

m = ((sum(a)/n)*2 + 1) //2
# m = statistics.mean(a)
# m = int(m) + 1 if m % 1 > 0.5 else int(m)
r = 0
for i in a:
  r += (i - m)**2
  
print(r)


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

m = int(((sum(a)/n)*2 + 1) //2)
# m = statistics.mean(a)
# m = int(m) + 1 if m % 1 > 0.5 else int(m)
r = 0
for i in a:
  r += (i - m)**2
  
print(r)
Buggy code:
n = int(input())
a = list(map(int, input().split()))

ans = 1e9
for p in range(101):
  sub = 0
  for q in a:
    sub += pow(p - q, 2)
  ans = min(ans, sub)

print(ans)


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

ans = 1e9
for p in range(-100, 101):
  sub = 0
  for q in a:
    sub += pow(p - q, 2)
  ans = min(ans, sub)

print(ans)
Buggy code:
N = int(input())
lst = list(map(int, input().split()))
avg = round(sum(lst) // N)

result = 0
for i in lst:
    result += (i - avg)**2

print(result)


Fixed code:
N = int(input())
lst = list(map(int, input().split()))
avg = round(sum(lst) / N)

result = 0
for i in lst:
    result += (i - avg)**2

print(result)
Buggy code:
n = int(input())
a = list(map(int, input().split()))

print(min(sum(map(lambda x: (x - i) * (x - i), a)) for i in range(101)))

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

print(min(sum(map(lambda x: (x - i) * (x - i), a)) for i in range(-100, 101)))
Buggy code:
n=int(input())
A=list(map(int,input().split()))
m=sum(A)//n
print(max(sum(map(lambda x:(x-m)**2,A)),sum(map(lambda x:(x-m-1)**2,A)))

Fixed code:
n=int(input())
A=list(map(int,input().split()))
m=sum(A)//n
print(min(sum(map(lambda x:(x-m)**2,A)),sum(map(lambda x:(x-m-1)**2,A))))
Buggy code:
n=int(input())
a_list=[int(i) for i in input().split()]

center=sum(a_list)//n
ans=0
for a in a_list:
    ans+=(a-center)**2
print(ans)

Fixed code:
n=int(input())
a_list=[int(i) for i in input().split()]

center=round(sum(a_list)/n)
ans=0
for a in a_list:
    ans+=(a-center)**2
print(ans)
Buggy code:
n = int(input())
a = input()
a = list(map(int, a.split()))

#print(a) 

x_min = round(sum(a) / 3 )

cost = sum([(x_min - i) * (x_min - i) for i in a])

print(cost)

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

#print(a) 

x_min = round(sum(a) / n )

cost = sum([(x_min - i) * (x_min - i) for i in a])

print(cost)
Buggy code:
N = int(input())
A =[int(x) for x in input().split()]
result=float('inf')
for i in range(max(A)):
  s=0
  for j in range(N):
    s += (i-A[j])**2
  result=min(result,s)
print(result)

Fixed code:
N = int(input())
A =[int(x) for x in input().split()]
result=float('inf')
for i in range(min(A),max(A)+1):
  s=0
  for j in range(N):
    s += (i-A[j])**2
  result=min(result,s)
print(result)
Buggy code:

def m():
    def cost(x,y):
        return (x-y) ** 2
    n = int(input())
    a = list(map(int, input().split()))
    ans = 99999999
    for y in range(1, 101):
        c = 0
        for x in a:
            c += cost(x,y)
        ans = min(ans, c)
    return ans

print(m())

Fixed code:

def m():
    def cost(x,y):
        return (x-y) ** 2
    n = int(input())
    a = list(map(int, input().split()))
    ans = 99999999
    for y in range(-100, 101):
        c = 0
        for x in a:
            c += cost(x,y)
        ans = min(ans, c)
    return ans

print(m())
Buggy code:

N = int(input())
list = [int(a) for a in input().split()]

A = sum(list)//N
sum = 0
sum_new = 0

for i in range(-100:100):
    for j in range(N):
        sum = sum + (list[j] - A) ** 2
        sum_new = (list[j] - i) ** 2
        if(sum > sum_new):
            sum = sum_new
print(sum)


Fixed code:

N = int(input())
list = [int(a) for a in input().split()]

A = sum(list)//N
ans = 0

for k in range(N):
    ans = ans + (list[k] - A) ** 2

for i in range(-100,100):
    sum_new = 0
    for j in range(N):
        sum_new = sum_new + (list[j] - i) ** 2
    if(ans > sum_new):
        ans = sum_new

print(ans)
Buggy code:
N = input()
A = list(map(int, input().split()))

num = round(sum(A) // len(A))
cost = [(a - num)**2 for a in A]

print(sum(cost))

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

num = round(sum(A) / len(A))
cost = [(a - num)**2 for a in A]

print(sum(cost))
Buggy code:
n = int(input())
l = list(map(int, input().split()))

mn = min(l)
mx = max(l)

ms = 10**9
ll = [0]*n
for j in range(mn, mx+1):
  	for i in range(n):
      ll[i] = (l[i] - j)**2
    s = sum(ll)
    ms = min(ms, s)
    
print(ms)

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

mn = min(l)
mx = max(l)

ms = 10**9
ll = [0]*n
for j in range(mn, mx+1):
    for i in range(n):
        ll[i] = (l[i]-j)**2
        
    s = sum(ll)
    ms = min(ms, s)
    
print(ms)
Buggy code:
a=int(input())
b=list(map(int,input().split()))
cost=999999
for i in range(min(b),max(b)):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Fixed code:
a=int(input())
b=list(map(int,input().split()))
cost=9999999
for i in range(min(b),max(b)+1):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)
Buggy code:
N = int(input())
ans = 10**10
L = list(map(int,input().split()))
for i in range(-101,101): 
  tmp = 0
  for x in L:
    tmp += (ans - x) ** 2
  ans = min(ans,tmp)
print(ans)

Fixed code:
N = int(input())
ans = 10**10
L = list(map(int,input().split()))
for i in range(-101,101): 
  tmp = 0
  for x in L:
    tmp += (i - x) ** 2
  ans = min(ans,tmp)
print(ans)
Buggy code:
n = int(input())

list_n = [int(n) for n in input().split(' ')]

print(list_n)
A = 0

for n in list_n:
        A += (100-n)**2

for n in range(-100,100):
        B = 0
        for num in list_n:
                B += (n-num)**2
        if A > B:
                A = B

print(A)


Fixed code:
n = int(input())

list_n = [int(n) for n in input().split(' ')]

A = 0

for n in list_n:
        A += (100-n)**2

for n in range(-100,100):
        B = 0
        for num in list_n:
                B += (n-num)**2
        if A > B:
                A = B

print(A)
Buggy code:
import math

N = int(input())
a = list(map(int, input().split()))
avg = round(sum(a)/len(a))
sums = []
for i in range(1, avg+1):
    s = 0
    for j in range(N):
        s += (a[j]-avg)**2
    sums.append(s)
print(min(sums))

Fixed code:
N = int(input())
a = list(map(int, input().split()))
avg = round(sum(a)/N)
sums = []
for i in range(min(a), avg+1):
    s = 0
    for j in range(N):
        s += (i-a[j])**2
    sums.append(s)
print(min(sums))
Buggy code:
import math
N = int(input())
a = list(map(int,input().split())
s = 0
t = 0
for i in range(N):
    s += a[i]
    t += (a[i])**2
a = math.floor(s/N)
if (s/N) - a > 0.5:
    a += 1
ans = N*a**2 - 2*s*a + t
print(ans)

Fixed code:
import math
N = int(input())
a = list(map(int,input().split()))
s = 0
t = 0
for i in range(N):
    s += a[i]
    t += (a[i])**2
a = math.floor(s/N)
if (s/N) - a > 0.5:
    a += 1
ans = N*a**2 - 2*s*a + t
print(ans)
Buggy code:
n=int(input())
a=[int(i) for i in input().split()]

check=float('inf')
for i in range(-100,101):
    check2=0
    for j in a:
        check2+=(i-j)**2
    if check>check2:
        check=check2
        ans=i

print(ans)

Fixed code:
n=int(input())
a=[int(i) for i in input().split()]

check=float('inf')
for i in range(-100,101):
    ans=0
    for j in a:
        ans+=(i-j)**2
    if check>ans:
        check=ans

print(check)
Buggy code:
N = int(input())
A = list(map(int,input().split()))

Ans = 10**10
for i in range(-99,100):
    ans = 0
    for j in range(N):
        ans += (A[j]-i)**2
    Ans = min(Ans,ans)

print(Ans)

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

Ans = 10**10
for i in range(-100,101):
    ans = 0
    for j in range(N):
        ans += (A[j]-i)**2
    Ans = min(Ans,ans)

print(Ans)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
S = 100000000
for i in range(-100,101):
    T = 0
    for j in range(N):
        T += (A[j] - i)**2
    print("i",i,"T",T)
    if S > T:
        S = T
print(S)

Fixed code:
N = int(input())
A = list(map(int,input().split()))
S = 100000000
for i in range(-100,101):
    T = 0
    for j in range(N):
        T += (A[j] - i)**2
    if S > T:
        S = T
print(S)
Buggy code:
n=int(input())
hoge=list(map(int,input().split()))
ans=-1
for i in range(201):
  same=0
  for hg in hoge:
    same+=(i-hg)**2
  if(ans==-1):
    ans=same
  else:
    ans=min(same,ans)
print(ans)
  

Fixed code:
n=int(input())
hoge=list(map(int,input().split()))
ans=-1
for i in range(201):
  i=-100+i
  same=0
  for hg in hoge:
    same+=(i-hg)**2
  if(ans==-1):
    ans=same
  else:
    ans=min(same,ans)
print(ans)
Buggy code:
N=int(input())
s=list(map(int,input().split()))
t=0
w=0
for i in range(N):
    t+=s[i]
t=int(((t/N)*2+1)//2)
print(t)
for j in range(N):
    w+=(t-s[j])**2
print(w)    

Fixed code:
N=int(input())
s=list(map(int,input().split()))
t=0
w=0
for i in range(N):
    t+=s[i]
t=int(((t/N)*2+1)//2)
for j in range(N):
    w+=(t-s[j])**2
print(w)
Buggy code:
N  =int(input())
a = list(map(int,input().split()))
b = list(set(a))
ans = (10**9)
for av in range(min(a),max(a)):
    Sum = 0
    for i in range(N):
        Sum += a[i]**2-2*a[i]*av+av**2
    if Sum < ans:
        ans = Sum
print(ans)

Fixed code:
N  =int(input())
a = list(map(int,input().split()))
ans = (10**9)
for av in range(min(a),max(a)+1):
    Sum = 0
    for i in range(N):
        Sum += (a[i]-av)**2
    if Sum < ans:
        ans = Sum
print(ans)
Buggy code:
N = int(input())
l = [int(x) for x in input().split()]
cost = 1000000000000
for i in range(0,7):
  co = 0
  for j in range(N):
    co += (i - l[j]) ** 2
  if cost > co:
    cost = co
print(cost)

Fixed code:
N = int(input())
l = [int(x) for x in input().split()]
cost = 1000000000000
for i in range(-100,101):
  co = 0
  for j in range(N):
    co += (i - l[j]) ** 2
  if cost > co:
    cost = co
print(cost)
Buggy code:
def cul(li,s):
  for i in li:
    a += (i - s)^2
  return a

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

s1 = sum(li)//len(li)
s2 = s1 + 1

ans1 = cul(li,s1)
ans2 = cul(li,s2)

print(min(ans1,ans2))

Fixed code:
def cul(li,s):
  a = 0
  for i in li:
    a += (i - s)**2
  
  return a

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

s1 = sum(li)//len(li)
s2 = s1 + 1

ans1 = cul(li,s1)
ans2 = cul(li,s2)

print(min(ans1,ans2))
Buggy code:
n=int(input())
print(min([sum([(x-item)**2 for x in [int(x) for x in input().split()]]) for item in list(range(-100,101))]))

Fixed code:
n=int(input())
a=[int(x) for x in input().split()]
print(min([sum([(x-item)**2 for x in a]) for item in list(range(-100,101)
                                                         )
          ]
         )
     )
Buggy code:
N = int(input())
a = [int(_) for _ in input().split()]

floor = math.floor(sum(a) / len(a))
ceil = math.ceil(sum(a) / len(a))

ceil_sum = sum([(item - ceil) ** 2 for item in a])
floor_sum = sum([(item - floor) ** 2 for item in a])

print(min(ceil_sum, floor_sum, median_sum))

Fixed code:
import math
N = int(input())
a = [int(_) for _ in input().split()]

floor = math.floor(sum(a) / len(a))
ceil = math.ceil(sum(a) / len(a))

ceil_sum = sum([(item - ceil) ** 2 for item in a])
floor_sum = sum([(item - floor) ** 2 for item in a])

print(min(ceil_sum, floor_sum))
Buggy code:
N = int(input())
a = list(map(int, input().split()))

n = round(sum(a)//N)

ans = 0
for i in range(0,N):
    ans += (a[i] - n)**2

print(int(ans))

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

n = round(sum(a)/N)

ans = 0
for i in range(0,N):
    ans += (a[i] - n)**2

print(int(ans))
Buggy code:
n = int(input())
a = list(map(int, input()))
s = round(sum(a)/n)
ans = 0
for i in a:
  ans += (i-s)**2
print(ans)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
s = round(sum(a)/n)
ans = 0
for i in a:
  ans += (i-s)**2
print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))

print(n)
print(a)

costs = []

for i in range(-100, 101):
  cost = 0
  for j in range(n):
    cost += (a[j] - i) * (a[j] - i)
  costs.append(cost)

print(min(costs))

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

costs = []

for i in range(-100, 101):
  cost = 0
  for j in range(n):
    cost += (a[j] - i) * (a[j] - i)
  costs.append(cost)

print(min(costs))
Buggy code:
N = int(input())
a = [int(i) for i in input().split()]

li_ans = []
ans = 0

for j in range(min(a), max(a)):
    for k in a:
        ans += abs(k - j) ** 2
    else:
        li_ans.appned(ans)
        ans = 0

if len(li_ans) == 0:
    print(0)
else:
    print(min(li_ans))


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

li_ans = []
ans = 0

for j in range(min(a), max(a)):
    for k in a:
        ans += abs(k - j) ** 2
    else:
        li_ans.append(ans)
        ans = 0

if len(li_ans) == 0:
    print(0)
else:
    print(min(li_ans))
Buggy code:
N = int(input())

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

ans = []
sum = 0

for j in range(min(a), max(a)):
    for num in a:
        sum += (num - j) ** 2
    else:
        ans.append(sum)
        sum = 0

print(min(ans))


Fixed code:
N = int(input())

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

ans = []
sum = 0

for j in range(min(a), max(a)):
    for num in a:
        sum += (num - j) ** 2
    else:
        ans.append(sum)
        sum = 0

if len(ans) == 0:
    print(0)
else:
    print(min(ans))
Buggy code:
import itertools
import math
n=int(input())
a=list(map(int,input().split()))
ave=round(sum(a)/n)
#print(ave)
count=0
for i in range(n):
	count+=(a[i]-ave)**2
print(count)
print(round(-1.6))


Fixed code:
import itertools
import math
n=int(input())
a=list(map(int,input().split()))
ave=round(sum(a)/n)
#print(ave)
count=0
for i in range(n):
	count+=(a[i]-ave)**2
print(count)