Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N,M,X,Y=list(map(int,input().split()))
x=list(map(int,input().split()))
y=list(map(int,input().split()))

a=x_max+1
b=y_min

def judge(a,b):
    if(a>b):
        return print("No War")
    else:
        z=range(a,b+1)
        for i in z:
            if(i>X and i<=Y):
                return print("No War")
        else:
            return print("War")
        
judge(a,b)

Fixed submission:
N,M,X,Y=list(map(int,input().split()))
x=list(map(int,input().split()))
y=list(map(int,input().split()))

a=max(x)+1
b=min(y)

def judge(a,b):
    if(a>b):
        return print("War")
    else:
        z=range(a,b+1)
        for i in z:
            if(i>X and i<=Y):
                return print("No War")
        else:
            return print("War")
        
judge(a,b)
Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())
if K<=len(S):
    for s in S[:K]:
        if s!="1":
            break
    else:
        print("1")
else:
	for s in S:
    	if s!="1" :
        	print(s)
        	break

Fixed submission:
S=input()
K=int(input())
f=0
if(K<=len(S)):
    for s in S[:K]:
        if s!="1":
            break
    else:
        print("1")
        f=1
if f==0:      
    for s in S:
        if(s!="1"):
            print(s)
            break
Buggy python submission (verdict: Time Limit Exceeded):
n,m = list(map(int,input().split()))
data = []
for i in range(m):
    x,y = list(map(int,input().split()))
    data.append([x,y])


kens = [[]for i in range(n)]
for dat in data:
    index = dat[0]
    kens[index-1].append(dat[1])

for i in range(n):
    kens[i] = sorted(kens[i])


for dat in data:
    ken = dat[0]
    year = dat[1]
    rank = kens[ken-1].index(year)+1
    leng1 = 6 - len(str(ken))
    leng2 = 6 - len(str(rank))
    st = ''
    st2 = ''
    for i in range(leng1):
        st += '0'
    for i in range(leng2):
        st2 += '0'
    st+= str(ken)
    st2 += str(rank)
    print(st + st2)

Fixed submission:
N, M = list(map(int, input().split()))
kens = [[] for _ in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    kens[x - 1].append((y, i))

ans = [[]for i in range(M)]
for i,ken in enumerate(kens):
    ken = sorted(ken)
    ken_number = i + 1
    for j, city in enumerate(ken):
        output_order = city[1]
        rank = j + 1
        ans[output_order] = "%06d%06d" % (ken_number,rank)
for i in ans:
    print(i)
Buggy python submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]

cakes = []
ranking = [[] for i in range(8)]
ranking2 = []

for i in range(n):
    cakes.append(list(map(int,input().split())))

a = 0
for i in range(2):
    for j in range(2):
        for k in range(2):
            for x in range(n):
                kirei = cakes[x][0]
                oisii = cakes[x][1]
                ninki = cakes[x][2]
                if i ==1:
                    kirei = (-1)*kirei
                if j ==1:
                    oisii = (-1) * oisii
                if k==1:
                    ninki = (-1)*ninki
                sum = kirei + oisii + ninki
                ranking[a].append(sum)
            a += 1

for i in ranking:
    i = sorted(i)
    i = i[::-1]
    ranking2.append(i)

ans = 0

maxi = 0

for x in ranking2:
    ans = 0
    for i in range(m):
        ans += x[i]
    maxi = max(maxi,ans)
print(maxi)n,m = [int(i) for i in input().split()]

cakes = []
ranking = [[] for i in range(8)]
ranking2 = []

for i in range(n):
    cakes.append(list(map(int,input().split())))

a = 0
for i in range(2):
    for j in range(2):
        for k in range(2):
            for x in range(n):
                kirei = cakes[x][0]
                oisii = cakes[x][1]
                ninki = cakes[x][2]
                if i ==1:
                    kirei = (-1)*kirei
                if j ==1:
                    oisii = (-1) * oisii
                if k==1:
                    ninki = (-1)*ninki
                sum = kirei + oisii + ninki
                ranking[a].append(sum)
            a += 1

for i in ranking:
    i = sorted(i)
    i = i[::-1]
    ranking2.append(i)

ans = 0

maxi = 0

for x in ranking2:
    ans = 0
    for i in range(m):
        ans += x[i]
    maxi = max(maxi,ans)
print(maxi)

Fixed submission:
n,m = [int(i) for i in input().split()]

cakes = []
ranking = [[] for i in range(8)]
ranking2 = []

for i in range(n):
    cakes.append(list(map(int,input().split())))

a = 0
for i in range(2):
    for j in range(2):
        for k in range(2):
            for x in range(n):
                kirei = cakes[x][0]
                oisii = cakes[x][1]
                ninki = cakes[x][2]
                if i ==1:
                    kirei = (-1)*kirei
                if j ==1:
                    oisii = (-1) * oisii
                if k==1:
                    ninki = (-1)*ninki
                sum = kirei + oisii + ninki
                ranking[a].append(sum)
            a += 1

for i in ranking:
    i = sorted(i)
    i = i[::-1]
    ranking2.append(i)

ans = 0

maxi = 0

for x in ranking2:
    ans = 0
    for i in range(m):
        ans += x[i]
    maxi = max(maxi,ans)
print(maxi)
Buggy python submission (verdict: Runtime Error):
a,b,c,d = [int(i) for i in input()]

print(min(a,b)+min(c,d))

Fixed submission:
a, b, c, d = [int(input()) for i in range(4)]

print(min(a, b) + min(c, d))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))

for i in range(n):
    sum = 0
    b = a.copy()
    b.pop(i)
    temp = 0
    for k in range(n-1):
        sum += abs(b[k] - temp)
        temp = b[k]
    sum += abs(temp)
    print(sum)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
a.insert(0,0)
a.append(0)

default= 0
tmp = 0
for i in range(n+2):
    default += abs(a[i]-tmp)
    tmp = a[i]

for i in range(1,n+1):
    if a[i-1] <= a[i] <= a[i+1] or a[i-1] >= a[i] >= a[i+1]:
        print(default)
    else:
        distance = abs(a[i] - a[i-1]) + abs(a[i+1] - a[i])
        sa = distance - abs(a[i+1] - a[i-1])
        print(default-sa)
Buggy python submission (verdict: Runtime Error):
a = int(input())
blue = []
count = []
for i in range(a):
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b):
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sort()
count.reverse()
if count[0] < 0:
    print("0")
else:
    print (count[0])

Fixed submission:
a,b,c = [int(i) for i in input().split()]
if a + b >= c:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
a = int(input())
blue = []
count = []
for i in range(a)
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b)
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sorted()
if count[0] < 0:
    print("0")
else:
    print (count[0])

Fixed submission:
a = int(input())
blue = []
count = []
for i in range(a):
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b):
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sort()
count.reverse()
if count[0] < 0:
    print("0")
else:
    print (count[0])

Buggy python submission (verdict: Runtime Error):
a = int(input())
blue = []
count = []
for i in range(a)
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b)
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

blue.sorted()
if blue[0] < 0:
    print("0")
else:
    print(blue[0])

Fixed submission:
a = int(input())
blue = []
count = []
for i in range(a):
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b):
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sort()
count.reverse()
if count[0] < 0:
    print("0")
else:
    print (count[0])

Buggy python submission (verdict: Runtime Error):
a = int(input())
blue = []
count = []
for i in range(a)
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b)
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

blue.sorted()
if blue[0] < 0:
    print("0")
else:
    print (blue[0])

Fixed submission:
a = int(input())
blue = []
count = []
for i in range(a):
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b):
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sort()
count.reverse()
if count[0] < 0:
    print("0")
else:
    print (count[0])
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = []
for i in range(2):
    A.append([int(i)for i in input().split()])
ans = 0
for i in range(N):
    ans += A[0][i]
ans += A[1][N-1]
max = ans
for i in range (N-1):
    ans = ans + A[1][i-2] - A[0][i-1]
    if ans > max:
        max = ans

print(max)

Fixed submission:
x = int(input())
y = int(input())
z = int(input())

res = (x-y) % z

print(res)
Buggy python submission (verdict: Time Limit Exceeded):
inp = input()
a,b,c,d = [int(x) for x in list(inp)]

def add(a,b):
    return a+b
def sub(a,b):
    return a-b
lis = [0,0,0]
count = 0
while True:
    sum = 0
    if lis[0] == 0:
        sum = add(a,b)
    else:
        sum = sub(a,b)
    if lis[1] == 0:
        sum = add(sum,c)
    else:
        sum = sub(sum,c)
    if lis[2] == 0:
        sum = add(sum, d)
    else:
        sum = sub(sum, d)
    if sum == 7:
        break
    count += 1
    lis[0] = count % 2
    lis[1] = count // 2
    lis[2] = count// 4

if lis[0] == 0:
    x = '+'
else:
    x = '-'
if lis[1] == 0:
    y = '+'
else:
    y = '-'
if lis[2] == 0:
    z = '+'
else:
    z = '-'
print('{}{}{}{}{}{}{}=7'.format(a,x,b,y,c,z,d))

Fixed submission:
inp = input()
a,b,c,d = [int(x) for x in list(inp)]

def add(a,b):
    return a+b
def sub(a,b):
    return a-b
lis = [0,0,0]
count = 0
while True:
    sum = 0
    if lis[0] == 0:
        sum = add(a,b)
    else:
        sum = sub(a,b)
    if lis[1] == 0:
        sum = add(sum,c)
    else:
        sum = sub(sum,c)
    if lis[2] == 0:
        sum = add(sum, d)
    else:
        sum = sub(sum, d)
    if sum == 7:
        break
    count += 1
    lis[0] = count % 2
    lis[1] = (count // 2) % 2
    lis[2] = (count// 4) % 2

if lis[0] == 0:
    x = '+'
else:
    x = '-'
if lis[1] == 0:
    y = '+'
else:
    y = '-'
if lis[2] == 0:
    z = '+'
else:
    z = '-'
print('{}{}{}{}{}{}{}=7'.format(a,x,b,y,c,z,d))
Buggy python submission (verdict: Runtime Error):
c1 = input().split()
c2 = input().split()
c=[]
a =‘YES’
for k in range(3):
    c.append([c1[k]]+[c2[k]])
for j in range(3):
    if c[0][j] = c[1][3-j-1]:
        continue
    else: 
        a=‘NO’
        break
print(a)

Fixed submission:
a = input()
b = input()
alist = list(a)
blist = list(b)
ans = "YES"
for i in range(3):
    if alist[i] == blist[2-i]:
        continue
    else:
        ans =("NO")
print(ans)
Buggy python submission (verdict: Runtime Error):
HM = input().split()
H, M = [int(HM[i]) for i in range(len(HM))]

S = [input() for i in range(H)]
print(S)
#print(T[0][1])
#S2 = list([S[i] for i in range(H)] for j in range(M))
S2 = []
for i in range(H):
    S2.append(list(S[i]))
print(S2)

def minesweeper():
    for i in range(H):
        for j in range(M):
            if S[i][j] == ".":
                c = count(i, j)
                #print(c)
                S2[i][j] = c
    for i in range(H):
        for j in range(M-1):
            print(S2[i][j], end="")
        print(S2[i][M-1])
    return 0


def count(x,y):
    c = 0
    #print("x={}".format(x))
    if (x >= 1 and y >= 1):
        if S[x-1][y-1] == '#':
            c += 1
            #print('1')
    if (x >= 1):
        if S[x-1][y] == "#":
            c += 1
            #print('2')
    if x >= 1 and y <= M - 2:
        if S[x-1][y+1] == "#":
            c += 1
            #print("x={}".format(x))
            #print('3')
    if (y >= 1):
        if S[x][y-1] == "#":
            c += 1
            #print('4')
    if (y <= M - 2):
        if S[x][y+1] == "#":
            c += 1
            #print('5')
    if (x <= H-2 and y >= 1):
        if S[x+1][y-1] == "#":
            c += 1
            #print('6')
    if (x <= H-2):
        if S[x+1][y] == "#":
            c += 1
            #print('7')
    if (x <= H-2 and y <= M-2):
        if S[x+1][y+1] == "#":
            c += 1
            #print('8')
    return c

minesweeper()

Fixed submission:
rng1 = -100
rng2 = 101
#print(input().split())
inp = [int(i) for i in input().split()]
#print(inp)
x, y, z = [inp[i] for i in range(len(inp))]
#print("x={},y={},z={}".format(x,y,z))

def dif1(x, y ,z):
    if not isinstance(x, int) or not isinstance(y, int) or not isinstance(z, int):
        return False
    elif x not in range(rng1, rng2) or y not in range(rng1, rng2)or z not in range(rng1, rng2):
        return False
    else:
        if y == z:
            return x
        if z == x:
            return y
        if x == y:
            return z
Ans = dif1(x,y,z)
print(Ans)
Buggy python submission (verdict: Runtime Error):
HM = input().split()
H, M = [int(HM[i]) for i in range(len(HM))]
#print(H,M)
S = [input().split() for i in range(H)]
#print(S)
#print(S[0][1])
S2 = S

def minesweeper():
    for i in range(H):
        for j in range(M):
            if S[i][j] == '.':
                C = count(i,j)
                S2[i][j] = C
    for i in range(H):
        for k in range(M-1):
            Shai = S2[i][k]
            print(Shai, end=" ")
        print(S2[i][M-1])
    return S2


def count(x,y):
    c = 0
    #print("x={}".format(x))
    if (x >= 1 and y >= 1):
        if S[x-1][y-1] == '#':
            c += 1
            #print('1')
    if (x >= 1):
        if S[x-1][y] == "#":
            c += 1
            #print('2')
    if x >= 1 and y <= M - 2:
        if S[x-1][y+1] == "#":
            c += 1
            #print("x={}".format(x))
            #print('3')
    if (y >= 1):
        if S[x][y-1] == "#":
            c += 1
            #print('4')
    if (y <= M - 2):
        if S[x][y+1] == "#":
            c += 1
            #print('5')
    if (x <= H-2 and y >= 1):
        if S[x+1][y-1] == "#":
            c += 1
            #print('6')
    if (x <= H-2):
        if S[x+1][y] == "#":
            c += 1
            #print('7')
    if (x <= H-2 and y <= M-2):
        if S[x+1][y+1] == "#":
            c += 1
            #print('8')
    return c

minesweeper()

Fixed submission:
HM = input().split()
H, M = [int(HM[i]) for i in range(len(HM))]

S = [input() for i in range(H)]
#print(S)
#print(T[0][1])
#S2 = list([S[i] for i in range(H)] for j in range(M))
S2 = []
for i in range(H):
    S2.append(list(S[i]))
#print(S2)

def minesweeper():
    for i in range(H):
        for j in range(M):
            if S[i][j] == ".":
                c = count(i, j)
                #print(c)
                S2[i][j] = c
    for i in range(H):
        for j in range(M-1):
            print(S2[i][j], end="")
        print(S2[i][M-1])
    return 0


def count(x,y):
    c = 0
    #print("x={}".format(x))
    if (x >= 1 and y >= 1):
        if S[x-1][y-1] == '#':
            c += 1
            #print('1')
    if (x >= 1):
        if S[x-1][y] == "#":
            c += 1
            #print('2')
    if x >= 1 and y <= M - 2:
        if S[x-1][y+1] == "#":
            c += 1
            #print("x={}".format(x))
            #print('3')
    if (y >= 1):
        if S[x][y-1] == "#":
            c += 1
            #print('4')
    if (y <= M - 2):
        if S[x][y+1] == "#":
            c += 1
            #print('5')
    if (x <= H-2 and y >= 1):
        if S[x+1][y-1] == "#":
            c += 1
            #print('6')
    if (x <= H-2):
        if S[x+1][y] == "#":
            c += 1
            #print('7')
    if (x <= H-2 and y <= M-2):
        if S[x+1][y+1] == "#":
            c += 1
            #print('8')
    return c

minesweeper()
Buggy python submission (verdict: Runtime Error):
n = int(input())
t = []
ans = 1
for i in range(n):
    t.append(int(input()))
def gcd(a,b):
    while b>0:
        a,b = b, a%b
    return int(a)
def lcm(a,b):
    return int(a*b/gcd(a,b))

for i in range(n):
    ans = lcm(ans,t[i])

print(ans)

Fixed submission:
n = int(input())
t = []
ans = 1
for i in range(n):
    t.append(int(input()))
def gcd(a,b):
    while b>0:
        a,b = b, a%b
    return a
def lcm(a,b):
    return a*b//gcd(a,b)

for i in range(n):
    ans = lcm(ans,t[i])

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
t = []
ans = 1
for i in range(n):
    t.append(int(input()))
def gcd(a,b):
    while b>0:
        a,b = b, a%b
    return a
def lcm(a,b):
    return a*b/gcd(a,b)

for i in range(n):
    ans = lcm(ans,t[i])

print(int(ans))

Fixed submission:
n = int(input())
t = []
ans = 1
for i in range(n):
    t.append(int(input()))
def gcd(a,b):
    while b>0:
        a,b = b, a%b
    return a
def lcm(a,b):
    return a*b//gcd(a,b)

for i in range(n):
    ans = lcm(ans,t[i])

print(ans)
Buggy python submission (verdict: Runtime Error):
N=int(input())
if N%2==0:
  print(N//2*(N-1))
elif:
  print((N-1)//2*N)

Fixed submission:
N=int(input())
print((N-1)*N//2)

Buggy python submission (verdict: Runtime Error):
N=int(input())
print((N-1)*int(N/2)))

Fixed submission:
N=int(input())
print((N-1)*N//2)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
print(sum([i for i in range(1,N)]))

Fixed submission:
N=int(input())
print((N-1)*N//2)
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
a=[int(input()) for _ in range(M)]
D=1000000007

DP=[0 for _ in range(N+10)]
DP[0]=1
for i in range(N):
  for j in [1,2]:
    if i+j not in a:
      DP[i+j]+=DP[i]
      
print(DP[N]%D)

Fixed submission:
N,M=map(int, input().split())
a=[True for _ in range(N+10)]
for _ in range(M):
  a[int(input())]=False
D=1000000007

DP=[0 for _ in range(N+10)]
DP[0]=1
for i in range(N):
  for j in [1,2]:
    if a[i+j]:
      DP[i+j]+=DP[i]
      
print(DP[N]%D)
Buggy python submission (verdict: Runtime Error):
N,K=int(input().split())

print(N-(K-1))

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

print(N-(K-1))
Buggy python submission (verdict: Runtime Error):
R,G,B,N=map(int,input().split())

tmp=0
for i in range(N//R+1):
  for j in range((N-R*i)//G+1):
    if and (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)


Fixed submission:
R,G,B,N=map(int,input().split())

tmp=0
for i in range(N//R+1):
  for j in range((N-R*i)//G+1):
    if (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())

tmp=0
for i in range(N//R+1):
  for j in range((N-R*i)//G+1):
    if (R*i+G*j)<=N and (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)

Fixed submission:
R,G,B,N=map(int,input().split())

tmp=0
for i in range(N//R+1):
  for j in range((N-R*i)//G+1):
    if (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N=map(int,input().split())

tmp=0
for i in range(0,N+1):
  for j in range(0,N+1):
    if (R*i+G*j)<=N and (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)

Fixed submission:
R,G,B,N=map(int,input().split())

tmp=0
for i in range(N//R+1):
  for j in range((N-R*i)//G+1):
    if (N-(R*i+G*j))%B==0:
      tmp+=1
print(tmp)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = list(set(map(int, input().split())))
M = len(X)

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    tmp=X[old:]
    ans+=max(tmp)-min(tmp)
  print(ans)

Fixed submission:
N, M = map(int, input().split())
X = list(set(map(int, input().split())))
M = len(X)

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  Y=Y[:N-1]
  Y.sort()
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)

Fixed submission:
N, M = map(int, input().split())
X = list(set(map(int, input().split())))
M = len(X)

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  Y=Y[:N-1]
  Y.sort()
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = list(map(int, input().split()))

if N>M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    tmp=X[old:]
    ans+=max(tmp)-min(tmp)
  print(ans)

Fixed submission:
N, M = map(int, input().split())
X = list(set(map(int, input().split())))
M = len(X)

if N>=M:
  print(0)
else:
  X.sort()
  Y=[]
  for i in range(len(X)-1):
    Y.append([i,X[i+1]-X[i]])
  Y.sort(reverse=True, key=lambda x:x[1])
  Y=Y[:N-1]
  Y.sort()
  ans=0
  old=0
  for i in range(N-1):
    tmp=X[old:Y[i][0]+1]
    ans+=max(tmp)-min(tmp)
    old=Y[i][0]+1
  else:
    if old<len(X):
      tmp=X[old:]
      ans+=max(tmp)-min(tmp)
  print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = len(bin(K)[2:])
bA=[bin(a)[2:].zfill(L) for a in A]

ans=''
for i in range(L):
    if sum([int(bA[i][j]) for j in range(N)])<=N//2:
      ans=ans+'1'
    else:
      ans=ans+'0'

ans=int(ans,2)
res=0
for a in A:
  res+=(a^ans)
print(res)

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

MAX_DIGIT_LENGTH=50

dp=[[-1 for _ in range(2)] for _ in range(100)]
dp[0][0]=0

for d in range(MAX_DIGIT_LENGTH):
  mask=1<<((MAX_DIGIT_LENGTH-1)-d)
  
  tmp=0
  for a in A:
    if a&mask == mask:
      tmp+=1
      
  cost0=mask*tmp
  cost1=mask*(N-tmp)
    
  if dp[d][1]!=-1:
    dp[d+1][1]=max(dp[d][1]+max(cost0, cost1), dp[d+1][1])
  
  if dp[d][0]!=-1:
    if K&mask == mask:
      dp[d+1][1]=max(dp[d][0]+cost0, dp[d+1][1])
      dp[d+1][0]=max(dp[d][0]+cost1, dp[d+1][0])
    else:
      dp[d+1][0]=max(dp[d][0]+cost0, dp[d+1][0])
      
print(max(dp[MAX_DIGIT_LENGTH][0], dp[MAX_DIGIT_LENGTH][1]))