qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import bisect

n = int(input())
s = input()
q = int(input())

alpha_map = {i:[] for i in alpha}
for i,c in enumerate(s):
    bisect.insort(alpha_map[c],i+1)
    
for _ in range(q):
    x,y,z = map(str,input().split())
    if int(x) == 1:
        y = int(y)
        bef = s[y-1]
        if bef == z:
            continue
        alpha_map[bef].pop(bisect.bisect_left(alpha_map[bef],y))
        bisect.insort(alpha_map[z],y)
        s = s[:y-1]+z+s[y:]
    else:
        count = 0
        for i,j in alpha_map.items():
            idx = bisect.bisect_left(j,int(y))
            if idx < len(j) and j[idx] <= int(z):
                    count += 1
        print(count)

Fixed code:
import bisect

n = int(input())
s = list(input())
q = int(input())

alpha_map = {chr(i):[] for i in range(97,123)}
for i,c in enumerate(s):
    bisect.insort(alpha_map[c],i+1)
    
for _ in range(q):
    x,y,z = map(str,input().split())
    if int(x) == 1:
        y = int(y)
        bef = s[y-1]
        if bef == z:
            continue
        alpha_map[bef].pop(bisect.bisect_left(alpha_map[bef],y))
        bisect.insort(alpha_map[z],y)
        s[y-1] = z
    else:
        count = 0
        for i,j in alpha_map.items():
            idx = bisect.bisect_left(j,int(y))
            if idx < len(j) and j[idx] <= int(z):
                    count += 1
        print(count)
Buggy code:
import bisect

n = int(input())
s = list(input())
q = int(input())
l = [[] for i in range(26)]
for i in range(n):
    l[ord(s[i])-ord("a")].append(i)
print(l)

for i in range(q):
    x,y,z = input().split()
    if int(x) == 1:
        if s[int(y)-1] == z:
            continue
        index = bisect.bisect_left(l[ord(s[int(y)-1])-ord("a")],int(y)-1)
        l[ord(s[int(y)-1])-ord("a")].pop(index)
        index = bisect.bisect_left(l[ord(z)-ord("a")],int(y)-1)
        l[ord(z)-ord("a")].insert(index,int(y)-1)
    if int(x) == 2:
        count = 0
        for j in l:
            index = bisect.bisect_left(j,int(y)-1)
            if index < len(j) and j[index] < int(z):
                count += 1
        print(count)





Fixed code:
import bisect

n = int(input())
s = list(input())
q = int(input())
l = [[] for i in range(26)]
for i in range(n):
    l[ord(s[i])-ord("a")].append(i)


for i in range(q):
    x,y,z = input().split()
    if int(x) == 1:
        if s[int(y)-1] == z:
            continue
        index = bisect.bisect_left(l[ord(s[int(y)-1])-ord("a")],int(y)-1)
        l[ord(s[int(y)-1])-ord("a")].pop(index)
        index = bisect.bisect_left(l[ord(z)-ord("a")],int(y)-1)
        l[ord(z)-ord("a")].insert(index,int(y)-1)
        s[int(y)-1] = z
    if int(x) == 2:
        count = 0
        for j in l:
            index = bisect.bisect_left(j,int(y)-1)
            if index < len(j) and j[index] < int(z):
                count += 1
        print(count)
Buggy code:
n=int(input())+1
w = input()
data=[0]*n*2
def update(i,x):
    i+=n
    data[i]=x
    i//=2
    while i:
      data[i]=data[i*2]|data[i*2+1]
      i//=2
def query(l,r):
    l+=n
    r+=n
    s=0
    while l<r:
      if l&1 == 1:
        s|=data[l]
        l+=1
      if r&1 == 1:
        r-=1
        s|=data[r]
      l//=2
      r//=2
    return bin(s).count('1')
def word_num(a):
    return 1<<(ord(a)-97)

for i in range(n-1):
    data[i+n] = word_num(w[i])    
for i in range(n-1,0,-1):
    data[i]=data[i*2]|data[i*2 + 1]
print(list(map(lambda x:bin(x),data)))      

ans=[]
q=int(input())
for i in range(q):
    a,s,t=input().split()
    if a=='1':
        s=int(s)
        update(int(s)-1,word_num(t))
    if a=='2':
        s=int(s)
        t=int(t)
        ans.append(query(int(s)-1,int(t)))
for i in range(len(ans)):
    print(ans[i])


Fixed code:
n=int(input())+1
w = input()
data=[0]*n*2
def update(i,x):
    i+=n
    data[i]=x
    i//=2
    while i:
      data[i]=data[i*2]|data[i*2+1]
      i//=2
def query(l,r):
    l+=n
    r+=n
    s=0
    while l<r:
      if l&1 == 1:
        s|=data[l]
        l+=1
      if r&1 == 1:
        r-=1
        s|=data[r]
      l//=2
      r//=2
    return bin(s).count('1')
def word_num(a):
    return 1<<(ord(a)-97)

for i in range(n-1):
    data[i+n] = word_num(w[i])    
for i in range(n-1,0,-1):
    data[i]=data[i*2]|data[i*2 + 1]

ans=[]
q=int(input())
for i in range(q):
    a,s,t=input().split()
    if a=='1':
        s=int(s)
        update(int(s)-1,word_num(t))
    if a=='2':
        s=int(s)
        t=int(t)
        ans.append(query(int(s)-1,int(t)))
for i in range(len(ans)):
    print(ans[i])
Buggy code:
def main():
  import sys
  input=sys.stdin.buffer.readline
  n=int(input())+1
  data=[[0]*n*2for _ in range(26)]
  def update(i,x):
    i+=n
    for d in data:
      j=i
      if d[j]:
        d[j]=0
        j//=2
        while j:
          d[j]=d[j+j]|d[j-~j]
          j//=2
        break
    d=data[x]
    d[i]=1
    i//=2
    while i:
      d[i]=d[i+i]|d[i-~i]
      i//=2
  def query(l,r):
    l+=n
    r+=n
    s=[0]*26
    while l<r:
      if l&1:
        for i,d in enumerate(data):
          s[i]|=d[l]
        l+=1
      if r&1:
        r-=1
        for i,d in enumerate(data):
          s[i]|=d[r]
      l//=2
      r//=2
    return sum(s)
  for i,c in enumerate(input()[:-1],n):
    data[c-97][i]=1
  for d in data:
    for i in range(n-1,0,-1):
      d[i]=d[i+i]|d[i-~i]
  s=[]
  for _ in range(int(input())):
    q,a,b=input().split()
    if q[0]>49:
      s+=query(int(a)-1,int(b)),
    else:
      update(int(a)-1,b[0]-97)
  print(' '.join(map(str,s)))
main()

Fixed code:
def main():
  import sys
  input=sys.stdin.buffer.readline
  n=int(input())+1
  data=[[0]*n*2for _ in range(26)]
  def update(i,x):
    i+=n
    for d in data:
      j=i
      if d[j]:
        d[j]=0
        j//=2
        while j:
          d[j]=d[j+j]|d[j-~j]
          j//=2
        break
    d=data[x]
    d[i]=1
    i//=2
    while i:
      d[i]=d[i+i]|d[i-~i]
      i//=2
  def query(l,r):
    l+=n
    r+=n
    s=[0]*26
    while l<r:
      if l&1:
        for i,d in enumerate(data):
          s[i]|=d[l]
        l+=1
      if r&1:
        r-=1
        for i,d in enumerate(data):
          s[i]|=d[r]
      l//=2
      r//=2
    return sum(s)
  for i,c in enumerate(input()[:-1]):
    update(i,c-97)
  s=[]
  for _ in range(int(input())):
    q,a,b=input().split()
    if q[0]>49:
      s+=query(int(a)-1,int(b)),
    else:
      update(int(a)-1,b[0]-97)
  print('\n'.join(map(str,s)))
main()
Buggy code:
import sys
input=sys.stdin.buffer.readline
n=int(input())+1
data=[0]*n*2
def update(i,x):
  i+=n
  data[i]=x
  i//=2
  while i:
    data[i]=data[i*2]|data[i*2+1]
    i//=2
def query(l,r):
  l+=n
  r+=n
  s=0
  while l<r:
    if l&1:
      s|=data[l]
      l+=1
    if r&1:
      r-=1
      s|=data[r]
    l//=2
    r//=2
  return bin(s).count('1')
for i,c in enumerate(input()[:-1]):update(i,2**(c-97))
s=[]
for _ in range(int(input())):
  q,a,b=input().split()
  if q[0]>49:s+=query(int(a)-1,int(b)),
  else:update(int(a)-1,2**(b[0]-97))
print(*s)

Fixed code:
import sys
input=sys.stdin.buffer.readline
n=int(input())+1
data=[0]*n*2
def update(i,x):
  i+=n
  data[i]=x
  i//=2
  while i:
    data[i]=data[i*2]|data[i*2+1]
    i//=2
def query(l,r):
  l+=n
  r+=n
  s=0
  while l<r:
    if l&1:
      s|=data[l]
      l+=1
    if r&1:
      r-=1
      s|=data[r]
    l//=2
    r//=2
  return sum(map(int,bin(s)[2:]))
for i,c in enumerate(input()[:-1]):
  update(i,2**(c-97))
for _ in range(int(input())):
  q,a,b=input().split()
  if q[0]>49:print(query(int(a)-1,int(b)))
  else:update(int(a)-1,2**(b[0]-97))
Buggy code:
from bisect import bisect_left, bisect_right
N = int(input())
S = input()
Q = int(input())

D = {chr(ord('a') + i): [] for i in range(26)}
for i, s in enumerate(S):
    D[s].append(i)

for q in range(Q):
    query = list(input().split())
    if query[0] == '1':
        i, x = int(query[1]), query[2]
        i -= 1
        s = S[i]
        if s == x:
            continue

        D[s].remove(i)
        next_i = bisect_left(D[x], i)
        D[x].insert(next_i, i)

    if query[0] == '2':
        l, r = int(query[1]), int(query[2])
        l, r = l - 1, r - 1
        ans = 0
        for i in range(26):
            s = chr(ord('a') + i)

            li = bisect_left(D[s], l)
            ri = bisect_right(D[s], r)

            if ri - li > 0:
                ans += 1

        print(ans)

print(D)


Fixed code:
from bisect import bisect_left, bisect_right
N = int(input())
S = list(input())
Q = int(input())

D = {chr(ord('a') + i): [] for i in range(26)}

for i, s in enumerate(S):
    D[s].append(i)

for q in range(Q):
    query = list(input().split())
    if query[0] == '1':
        i, x = int(query[1]), query[2]
        i -= 1
        s = S[i]
        if s == x:
            continue

        D[s].remove(i)
        next_i = bisect_left(D[x], i)
        D[x].insert(next_i, i)
        S[i] = x

    if query[0] == '2':
        l, r = int(query[1]), int(query[2])
        l, r = l - 1, r - 1
        ans = 0
        for i in range(26):
            s = chr(ord('a') + i)

            li = bisect_left(D[s], l)
            ri = bisect_right(D[s], r)

            if ri - li > 0:
                ans += 1

        print(ans)
Buggy code:
import sys
input = sys.stdin.readline
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
aas = "abcdefghijklmnopqrstuvwxyz"
N = int(input().strip())
S = list(input().strip())
Q = int(input().strip())
cbt = dict()
MX = 10**5
for c in aas:
    cbt[c] = Bit(MX)
    for i, a in enumerate(S):
        if a == c:
            cbt[c].add(i+1, 1)

for _ in range(Q):
    t, i, c = input().split()
    t = int(t)
    if t == 1:
        i = int(i)
        if S[i-1] != c:
            cbt[S[i-1]].add(i, -1)
            cbt[c].add(i, 1)
            S[i-1] = c
    else:
        l, r = int(i), int(c)
        s = 0
        for a in aas:
            bt = cbt[a]
            s += (bt.sum(r+1)-bt.sum(l-1)>0)
        print(s)


Fixed code:
import sys
input = sys.stdin.readline
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        if i == 0:
            return 0
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
aas = "abcdefghijklmnopqrstuvwxyz"
N = int(input().strip())
S = list(input().strip())
Q = int(input().strip())
cbt = dict()
MX = 5 * 10**5 + 2
for c in aas:
    cbt[c] = Bit(MX)
    for i, a in enumerate(S):
        if a == c:
            cbt[c].add(i+1, 1)

for _ in range(Q):
    t, i, c = input().split()
    t = int(t)
    if t == 1:
        i = int(i)
        if S[i-1] != c:
            cbt[S[i-1]].add(i, -1)
            cbt[c].add(i, 1)
            S[i-1] = c
    else:
#        print(S)
        l, r = int(i), int(c)
#        print(l, r)
        s = 0
        for a in aas:
            bt = cbt[a]
#            print(a, bt.sum(r),bt.sum(l-1))
            s += (bt.sum(r)-bt.sum(l-1)>0)
        print(s)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
HP = 0
minHP = float('inf')
for i in range(n):
    for j in range(n):
        HP += (a[j] - (i + 1)) ** 2
    if minHP > HP:
        minHP = HP
    HP = 0
print(minHP)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
minHP = float('inf')
for i in range(max(a)):
    HP = 0
    for j in range(n):
        HP += (a[j] - (i + 1)) ** 2
    if minHP > HP:
        minHP = HP
print(minHP)
Buggy code:
n = int(input())

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

l = min(xs)
r = max(xs)

costs = []
for i in range(l, r):
    cost = 0
    for j in xs:
        cost += (j - i) * (j - i)
    costs.append(cost)
    print(cost)

print(min(costs))



Fixed code:
n = int(input())

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

l = min(xs)
r = max(xs)

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

print(min(costs))
Buggy code:
import math
from statistics import mean, median,variance,stdev
N = int(input())
X = list(map(int,input().split()))
a = sum(X)
ave = round(a/N)
b = 0
for i in range(N):
    b +=(X[i]-20)**2
print(b)

Fixed code:
import math
from statistics import mean, median,variance,stdev
N = int(input())
X = list(map(int,input().split()))
a = sum(X)
ave = round(a/N)
b = 0
for i in range(N):
    b +=(X[i]-ave)**2
print(b)
Buggy code:
N = int(input())
X = list(int,input().split())
m = sum(X)//N
def fun(X,m):
  s = 0
  for i in X:
    s += (i-m)**2
  return s

print(min(fun(X,m),fun(X,m+1)))


Fixed code:
N = int(input())
X = list(map(int,input().split()))
m = sum(X)//N
def fun(X,m):
  s = 0
  for i in X:
    s += (i-m)**2
  return s

print(min(fun(X,m),fun(X,m+1)))
Buggy code:
n=int(input())
x=list(map(int,input().split()))
p=round(sum(x)/n)
m=[]
for i in x:
  m.append((x-m)**2)
print(sum(m))

Fixed code:
n=int(input())
x=list(map(int,input().split()))
p=round(sum(x)/n)
m=[]
for i in x:
  m.append((i-p)**2)
print(sum(m))
Buggy code:
n = int(input())
ls = list(map(int,input().split()))

ls = sorted(ls)

ans = 10000000000000
x = 0
for i in range(ls[0],ls[-1]+1):
  tmp = 0
  for a in ls:
    tmp += (a-i)**2
  if ans > tmp:
    x = i
    ans = tmp
print(x)


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

ls = sorted(ls)

ans = 10000000000000
x = 0
for i in range(ls[0],ls[-1]+1):
  tmp = 0
  for a in ls:
    tmp += (a-i)**2
  if ans > tmp:
    x = i
    ans = tmp
print(ans)
Buggy code:
n=int(input())
x=list(map(int, input().split()))
p=0
t=0
for i in range(n):
  p+=x[i]
p=p//n
for i in range(n):
  t+=(x[i]-p)**2
print(t)

Fixed code:
n=int(input())
x=list(map(int, input().split()))
p=0
t=0
for i in range(n):
  p+=x[i]
p=round(p/n)
for i in range(n):
  t+=(x[i]-p)**2
print(t)
Buggy code:
n = int(input())
x = list(map(int,input().split()))
ans = 69*100**3
for i in range(101):
  tmp = 0
  for j in range(n):
    tmp += (i-j)**2
  ans = min(ans, tmp)
print(ans)

Fixed code:
n = int(input())
x = list(map(int,input().split()))
ans = 69*100**3
for i in range(101):
  tmp = 0
  for j in range(n):
    tmp += (i-x[j])**2
  ans = min(ans, tmp)
print(ans)
Buggy code:
n=int(input())
x=list(map(int,input().split()))
m=1000000
for i in range(n):
    su=0
    for j in range(n):
        su+=(x[j]-i-1)**2
    if m>su:
        m=su
print(su)

Fixed code:
n=int(input())
x=list(map(int,input().split()))
m=1000000
for i in range(100):
    su=0
    for j in range(n):
        su+=(x[j]-i-1)**2
    if m>su:
        m=su
print(m)
Buggy code:
N = int(input())
X = list(map(int,input().split()))
ans_list = []
for i in range(1,max(X)+1):
  ans = sum([(i-j)**2  for j in X])
  ans_list.append(ans)
  print(i)
print(min(ans_list))
  
  

Fixed code:
N = int(input())
X = list(map(int,input().split()))
ans_list = []
for i in range(1,max(X)+1):
  ans = sum([(i-j)**2  for j in X])
  ans_list.append(ans)
print(min(ans_list))
Buggy code:
n = int(input())
x = [int(i) for i in input().split()]
l, r = 0, 0
for i in x:
    l = min(l, i)
    r = max(l, i)
ans = 10e9
for i in range(l, r + 1):
    cost = 0
    for j in range(n):
        cost += (x[j] - i) * (x[j] - i)
        print(i, j)
    ans = min(ans, cost)
print(ans)


Fixed code:
n = int(input())
x = [int(i) for i in input().split()]
l, r = x[0], x[0]
for i in x:
    l = min(l, i)
    r = max(r, i)
ans = 10e9
for i in range(l, r + 1):
    cost = 0
    for j in range(n):
        cost += (x[j] - i) ** 2
    ans = min(ans, cost)
print(ans)
Buggy code:
import numpy as np
import math
N=int(input())
X=list(map(float,input().split()))
Xmin=int(math.floor(np.min(X)));Xmax=int(math.ceil(np.max(X)))
P=np.array([i for i in range (Xmin,Xmax+1)])
Sum=np.zeros(len(P),dtype=float)
for i in range(len(P)):
    Sum[i]=np.sum((np.array(X)-P[i])*(np.array(X)-P[i]))
print(np.min(Sum))


Fixed code:
import numpy as np
import math
N=int(input())
X=list(map(float,input().split()))
Xmin=int(math.floor(np.min(X)));Xmax=int(math.ceil(np.max(X)))
P=np.array([i for i in range (Xmin,Xmax+1)])
Sum=np.zeros(len(P),dtype=float)
for i in range(len(P)):
    Sum[i]=np.sum((np.array(X)-P[i])*(np.array(X)-P[i]))
print(int(np.min(Sum)))
Buggy code:
n = int(input())
x_list = list(map(int, input().split()))

x_ave = sum(x_list) // len(x_list)

diff_sum = 0
for x in x_list:
    diff = abs(x - x_ave)**2
    diff_sum += diff
print(diff_sum)


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

x_ave = round(sum(x_list) / len(x_list))
diff_sum = 0
for x in x_list:
    diff = abs(x - x_ave)**2
    diff_sum += diff
print(diff_sum)
Buggy code:
n=int(input())
x=list(map(int,input().split()))
x.sort()
min = 10**10
su=0
print(x)
print(x[0],x[n-1])
for j in range(x[0],x[n-1]):
    for i in x:
        su=(i-j)**2+su
    
    if min>su:
        min=su
    su = 0
print(min)
        

Fixed code:
n=int(input())
x=list(map(int,input().split()))
x.sort()
min = 10**15
su=0
for j in range(x[0],x[n-1]):
    for i in x:
        su=(i-j)**2+su  
    if min>su:
        min=su
    su = 0
if min==10**15:
    print(0)
else:
    print(min)
Buggy code:

def mean(x):
    sum=0
    for _ in x:
        sum+=_
    return sum/len(x)

def rss(x,xbar):
    sum=0
    for _ in x:
        sum+=(_-xbar)**2
    return sum

def main(n,x):
    ans=0
    xbar=mean(x)
    xbar_int=xbar//1
    if(xbar-xbar_int >0.5):
        xbar_int+=1
    ans=rss(x,xbar_int)
    return int(ans)

def main2(n,x):
    xmin=min(x)
    xmax=max(x)
    minsum=1000000

    for p in range(xmin,xmax+1):
        sum=0
        for xx in x:
            sum+=(xx-p)**2
        if(sum<minsum):
            minsum=sum

    return minsum

if __name__=='__main__':
    n,x=readinput()
    ans=main(n,x)
    print(ans)

Fixed code:
def readinput():
    n=int(input())
    x=list(map(int,input().split()))
    return n,x

def mean(x):
    sum=0
    for _ in x:
        sum+=_
    return sum/len(x)

def rss(x,xbar):
    sum=0
    for _ in x:
        sum+=(_-xbar)**2
    return sum

def main(n,x):
    ans=0
    xbar=mean(x)
    xbar_int=xbar//1
    if(xbar-xbar_int >0.5):
        xbar_int+=1
    ans=rss(x,xbar_int)
    return int(ans)

def main2(n,x):
    xmin=min(x)
    xmax=max(x)
    minsum=1000000

    for p in range(xmin,xmax+1):
        sum=0
        for xx in x:
            sum+=(xx-p)**2
        if(sum<minsum):
            minsum=sum

    return minsum

if __name__=='__main__':
    n,x=readinput()
    ans=main2(n,x)
    print(ans)
Buggy code:
n = int(input())
xi = list(map(int, input().split()))

p = int(sum(xi) / n)
hp = 0
for x in xi:
    hp += (x - p) ** 2

print(hp)

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

p = round(sum(xi) / n)
hp = 0
for x in xi:
    hp += (x - p) ** 2

print(hp)
Buggy code:
n = int(input())
za = list(map(int,input().split()))
print(za)
za_max = max(za)
za_min = min(za)
kouho = 0
kouho_sum = 0
ans = 9999999999999999999999999999999999999999999999999999999999999999999999999999
for zahyou in range(za_min,za_max+1):
    for i in za:
        kouho = (i - zahyou)*(i - zahyou)
        kouho_sum = kouho_sum + kouho
    if(ans > kouho_sum):
        ans = kouho_sum
        kouho_sum = 0
print(ans)

Fixed code:
n = int(input())
za = list(map(int,input().split()))
za_max = max(za)
za_min = min(za)
kouho = 0
kouho_sum = 0
ans = 9999999999999999999999999999999999999999999999999999999999999999999999999999
for zahyou in range(za_min,za_max+1):
    for i in za:
        kouho = (i - zahyou)*(i - zahyou)
        kouho_sum = kouho_sum + kouho
    if(ans > kouho_sum):
        ans = kouho_sum
        kouho_sum = 0
print(ans)
Buggy code:
import math
N = int(input())
X = list(map(int,input().split()))
P = math.ceil(sum(X)/N)
P1 = P - 1
hp = 0

for x in X:
  hp_x = (P-x)**2
  hp2_x = (P1 - x)**2
  hp = hp + hp_x
  hp2 = hp2 + hp2_x
print(min(hp,hp2))

Fixed code:
import math
N = int(input())
X = list(map(int,input().split()))
P = math.ceil(sum(X)/N)
P1 = P - 1
hp = 0
hp2 = 0

for x in X:
  hp_x = (P-x)**2
  hp2_x = (P1 - x)**2
  hp = hp + hp_x
  hp2 = hp2 + hp2_x
print(min(hp,hp2))
Buggy code:
# input
N = int(input())
X = list(map(int, input().split()))

# process/output
L = min(X)
R = max(X)

ans = int(float('inf'))
for i in range(L, R+1):
    cost = 0
    for x in X:
        cost += (x-i)**2
    
    if cost < ans:
        ans = cost

print(ans)


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

# process
L = min(X)
R = max(X)

ans = float('inf')
for i in range(L, R+1):
    cost = 0
    for x in X:
        cost += (x-i)**2
    
    if cost < ans:
        ans = cost

# output
print(int(ans))
Buggy code:
import collections

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

x = collections.Counter(X)

s = min(list(x.keys()))
e = max(list(x.keys()))

xl = list(x)

m = [0]*(e+1-s)

for i in range(s, e+1):
    p = 0
    for j in range(N):
        p += pow(xl[j]-i,2) * x[xl[j]]
    m[i-s] = p

print(min(m))

Fixed code:
import collections

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

x = collections.Counter(X)

N = len(x)
s = min(list(x.keys()))
e = max(list(x.keys()))

xl = list(x)

m = [0]*(e+1-s)

for i in range(s, e+1):
    p = 0
    for j in range(N):
        p += pow(xl[j]-i,2) * x[xl[j]]
    m[i-s] = p

print(min(m))
Buggy code:
n = int(input())
x = list(map(int,input().split()))
buff = 0
min = 999999
for i in range(1,n+1):
  buff = 0
  for j in x:
    #print(j,"j",i,"i")
    #print(((j - i)**2))
    buff += ((j - i)**2)
  if(min > buff):
    min = buff
print(min)

Fixed code:
n = int(input())
x = list(map(int,input().split()))
buff = 0
min = 999999
for i in range(1,101):
  buff = 0
  for j in x:
    buff += ((j - i)**2)
  if(min > buff):
    min = buff
print(min)
Buggy code:
N=int(input())
X=list(map(int,input().split()))
s=0
sum=0
for i in X:
    sum+=i

sum=round(sum/len(X),0)
print(sum)
for i in X:
    tmp=(i-sum)**2
    s=s+tmp
print(s)


Fixed code:
N=int(input())
X=list(map(int,input().split()))
s=0
sum=0
for i in X:
    sum+=i

sum=round(sum/len(X))

for i in X:
    tmp=(i-sum)**2
    s+=tmp
print(s)
Buggy code:
n = int(input())
x = list(map(int, input().split()))

x.sort()

mean = sum(x)/n
med = int(n / 2)
print(med)

if mean - int(mean) < 0.5:
    mean = int(mean)
elif mean - int(mean) > 0.5:
    mean = int(mean) + 1
else:
    if n % 2 == 0:
        if (x[med-1] + x[med])/2 <= mean:
            mean = int(mean)
        else:
            mean = int(mean) + 1
    else:
        if x[med] <= mean:
            mean = int(mean)
        else:
            mean = int(mean) + 1
sum = 0

for i in range(n):
    sum += (x[i]-mean)**2

print(sum)

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

x.sort()

mean = sum(x)/n
med = int(n / 2)

if mean - int(mean) < 0.5:
    mean = int(mean)
elif mean - int(mean) > 0.5:
    mean = int(mean) + 1
else:
    if n % 2 == 0:
        if (x[med-1] + x[med])/2 <= mean:
            mean = int(mean)
        else:
            mean = int(mean) + 1
    else:
        if x[med] <= mean:
            mean = int(mean)
        else:
            mean = int(mean) + 1
sum = 0

for i in range(n):
    sum += (x[i]-mean)**2

print(sum)
Buggy code:
N=int(input())
lst=list(map(int,input().split()))
P=round(lst/N)
print(sum[(i-P)**2 for i in lst])

Fixed code:
N=int(input())
lst=list(map(int,input().split()))
P=round(sum(lst)/N)
print(sum([(i-P)**2 for i in lst]))
Buggy code:
N = int(input())
X = list(map(int, input().split()))
sum = 0
min_sum = 100000000
P =1
if P <= 100:
  for n in X:
    sum = sum +(n-P)**2
  if sum <= min_sum:
    min_sum = sum  
print(min_sum)

Fixed code:
N = int(input())
X = list(map(int, input().split()))
sum = 0
min_sum = 100000000
P = 1
while P <= 100:
  for n in X:
    sum = sum +(n-P)**2
  if sum <= min_sum:
    min_sum = sum
  P = P + 1
  sum = 0
print(min_sum)