Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if s[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))


Fixed submission:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if s[i] == s[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))


Fixed submission:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = input()

def f1(s, l, r):
  return s[:l] + f2(s[l:r + 1]) + s[r + 1:]

def f2(s):
  t = list(s)
  t.reverse()
  return ''.join('R' if c == 'L' else 'L' for c in t)

def f3(s):
  before = s[0]
  result = 0
  for i in range(1, N):
    if s[i] == before:
      result += 1
    else:
       before = s[i]
  return result

def f4(s, c):
  lstart = -1
  llen = 0
  tstart = -1
  for i in range(N):
    if s[i] == c and tstart == -1:
      tstart = i
    if s[i] != c and tstart != -1:
      tlen = i - tstart
      if tlen > llen:
        lstart = tstart
        llen = tlen
      tstart = -1
  if tstart != -1:
    tlen = N - tstart
    if tlen > llen:
      lstart = tstart
      llen = tlen
  return (lstart, llen)

for _ in range(K):
  lstart, llen = f4(S, 'L')
  #print('X', lstart, llen)
  rstart, rlen = f4(S, 'R')
  #print('Y', rstart, rlen)
  if lstart == -1 or rstart == -1:
    break
  if lstart < rstart:
    #print(lstart + llen, rstart + rlen - 1)
    S = f1(S, lstart + llen, rstart + rlen - 1)
  else:
    #print(rstart + rlen, lstart + llen - 1)
    S = f1(S, rstart + rlen, lstart + llen - 1)
  #print(S)

print(f3(S))

Fixed submission:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

def f1(s, l, r):
  return s[:l] + f2(s[l:r + 1]) + s[r + 1]

def f2(s):
  t = list(s)
  t.reverse()
  return ''.join('R' if c == 'L' else 'L' for c in t)

def f3(s):
  before = s[0]
  result = 0
  for i in range(1, N):
    if s[i] == before:
      result += 1
    else:
       before = s[i]
  return result

def f4(s, c):
  lstart = -1
  llen = 0
  tstart = -1
  for i in range(N):
    if s[i] == c and tstart == -1:
      tstart = i
    if s[i] != c and tstart != -1:
      tlen = i - tstart
      if tlen > llen:
        lstart = tstart
        llen = tlen
  return (lstart, llen)

for _ in range(K):
  lstart, llen = f4(S, 'L')
  rstart, rlen = f4(S, 'R')
  if lstart < rstart:
    S = f2(S, lstart + llen, rstart + rlen - 1)
  else:
    S = f2(S, rstart + rlen, lstart + llen - 1)
  if lstart == -1 or rstart == -1:
    break

print(f3(S))

Fixed submission:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))
Buggy python submission (verdict: Runtime Error):
```python
n = int(input())
print(n * (n - 1) // 2)
```


Fixed submission:
n = int(input())
print((n + 1) * n // 2 - n)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
s = input()
t = input()
for c in set(t):
  if s.find(c) == -1:
    print(-1)
    sys.exit()
p = 0
slen = len(s)
for i in range(1, 10 ** 5 + 1):
  for j in range(slen):
    if s[j] == t[p]:
      p += 1
      if p == len(t):
        print((i - 1) * slen + j + 1)
        sys.exit()


Fixed submission:
import sys

s = input()
t = input()

tset = set(t)
slen = len(s)

if len(tset - set(s)) != 0:
    print(-1)
    sys.exit()

s2t = [i - ord('a') for i in (s + s).encode('ascii')]
lut = [[-1 for _ in range(slen)] for _ in range(26)]
for c in tset:
  b = ord(c) - ord('a')
  j = 0
  for i in range(slen * 2 - 1, -1, -1):
    if s2t[i] == b:
      j = 0
    else:
      j += 1
    if i < slen:
      lut[b][i] = j

p = 0
for c in t:
  b = ord(c) - ord('a')
  p += lut[b][p % slen] + 1
print(p)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
s = input()
t = input()
for c in set(t):
  if s.find(c) == -1:
    print(-1)
    sys.exit()
p = 0
slen = len(s)
for i in range(1, 10 ** 5 + 1):
  for j in range(slen):
    if s[j] == t[p]:
      p += 1
      if p == len(t):
        print((i - 1) * slen + j + 1)
        sys.exit()


Fixed submission:
import sys

s = input()
t = input()

tset = set(t)
slen = len(s)

if len(tset - set(s)) != 0:
    print(-1)
    sys.exit()

s2t = [i - ord('a') for i in (s + s).encode('ascii')]
lut = [[-1 for _ in range(slen)] for _ in range(26)]
for c in tset:
  b = ord(c) - ord('a')
  j = 0
  for i in range(slen * 2 - 1, -1, -1):
    if s2t[i] == b:
      j = 0
    else:
      j += 1
    if i < slen:
      lut[b][i] = j

p = 0
for c in t:
  b = ord(c) - ord('a')
  p += lut[b][p % slen] + 1
print(p)
Buggy python submission (verdict: Runtime Error):
def main():
  from collections import Counter
  from sys import stdin
  readline = stdin.readline()
  N = int(readline())
  t = Counter([''.join(sorted(readline()[:-1])) for _ in range(N)])
  print(sum(t[k] * (t[k] - 1) // 2 for k in t))
main()


Fixed submission:
n = int(input())
s = [input() for _ in range(n)]
for i in range(n):
  s[i] = ''.join(sorted(s[i]))
t = {}
for i in range(n):
  if s[i] in t:
    t[s[i]] += 1
  else:
    t[s[i]] = 1
print(sum(t[k] * (t[k] -1) // 2 for k in t))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
  s[i] = ''.join(sorted(s[i]))
for i in range(n - 1):
  for j in range(i + 1, n):
    if s[i] == s[j]:
      result += 1
print(result)

Fixed submission:
n = int(input())
s = [input() for _ in range(n)]
for i in range(n):
  s[i] = ''.join(sorted(s[i]))
t = {}
for i in range(n):
  if s[i] in t:
    t[s[i]] += 1
  else:
    t[s[i]] = 1
print(sum(t[k] * (t[k] -1) // 2 for k in t))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
  s[i] = ''.join(sorted(s[i]))
for i in range(n - 1):
  for j in range(i + 1, n):
    if s[i] == s[j]:
      result += 1
print(result)


Fixed submission:
n = int(input())
s = [input() for _ in range(n)]
for i in range(n):
  s[i] = ''.join(sorted(s[i]))
t = {}
for i in range(n):
  if s[i] in t:
    t[s[i]] += 1
  else:
    t[s[i]] = 1
print(sum(t[k] * (t[k] -1) // 2 for k in t))
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split()))

t = [0] * N
for i in range(N - 1, -1, -1):
    t[i] = (sum(t[2 * (i + 1) - 1::i]) % 2) ^ a[i]

print(sum(t))
print(*[i + 1 for i in range(N) if t[i] == 1])


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

t = [0] * (N + 1)
for i in range(N, 0, -1):
    t[i] = (sum(t[2 * i::i]) % 2) ^ a[i - 1]

print(sum(t))
print(*[i for i in range(1, N + 1) if t[i] == 1])

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

t = [0] * N
for i in range(N, -1, -1):
    t[i] = (sum(t[2 * i - 1::i]) % 2) ^ a[i - 1]

print(sum(t))
print(*[i + 1 for i in range(N) if t[i] == 1])


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

t = [0] * (N + 1)
for i in range(N, 0, -1):
    t[i] = (sum(t[2 * i::i]) % 2) ^ a[i - 1]

print(sum(t))
print(*[i for i in range(1, N + 1) if t[i] == 1])
Buggy python submission (verdict: Time Limit Exceeded):
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  ir = i % 2019
  for j in range(i + 1, r + 1):
    t = (ir * j) % 2019
    if t < m:
      m = t
print(m)

Fixed submission:
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  ir = i % 2019
  for j in range(i + 1, r + 1):
    t = (ir * j) % 2019
    if t < m:
      m = t
    if m == 0:
      break
  if m == 0:
    break
print(m)

Buggy python submission (verdict: Time Limit Exceeded):
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  for j in range(i + 1, r + 1):
    t = (i * j) % 2019
    if t < m:
      m = t
print(m)

Fixed submission:
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  ir = i % 2019
  for j in range(i + 1, r + 1):
    t = (ir * j) % 2019
    if t < m:
      m = t
    if m == 0:
      break
  if m == 0:
    break
print(m)

Buggy python submission (verdict: Time Limit Exceeded):
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  for j in range(i + 1, r + 1):
    t = (i * j) % 2019
    if t < m:
      m = t
print(m)

Fixed submission:
l, r = [int(e) for e in input().split()]
m = 2019
for i in range(l, r):
  ir = i % 2019
  for j in range(i + 1, r + 1):
    t = (ir * j) % 2019
    if t < m:
      m = t
    if m == 0:
      break
  if m == 0:
    break
print(m)
Buggy python submission (verdict: Runtime Error):
from fraction import gcd

a, b, c, d = [int(e) for e in input().split()]

def f(a, b, n):
  return b // n - (a - 1) // n

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)


Fixed submission:
a, b, c, d = [int(e) for e in input().split()]

def gcd(a, b):
  while True:
    t = b % a
    if t == 0:
      return a
    b, a = a, t

def f(a, b, n):
  x = (a // n) * n
  if x < a:
    x += n
  if x > b:
    return 0
  result = 0
  if a <= x <= b:
    result += 1
  result += (b - x) // n
  return result

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)

Buggy python submission (verdict: Runtime Error):
a, b, c, d = [int(e) for e in input().split()]

def gcd(a, b):
  while True:
    t = b % a
    if t == 0:
      return a
    b, a = a, t

def f(a, b, n):
  x = (a // n) * n
  if x < a:
    x += n
  if x > b:
    return 0
  result = 0
  if a <= x <= b:
    result += 1
  result += (b - x) // n
  return result

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, gcd(c * d))
print(b - a + 1 - acc)


Fixed submission:
a, b, c, d = [int(e) for e in input().split()]

def gcd(a, b):
  while True:
    t = b % a
    if t == 0:
      return a
    b, a = a, t

def f(a, b, n):
  x = (a // n) * n
  if x < a:
    x += n
  if x > b:
    return 0
  result = 0
  if a <= x <= b:
    result += 1
  result += (b - x) // n
  return result

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)
Buggy python submission (verdict: Runtime Error):
from functools import cmp_to_key
def cmp(x, y):
  if x[0] < y[0]:
    return -1
  elif x[0] > y[0]:
    return 1
  else:
    return int(y[1]) - int(x[1])
n = int(input())
d = [input().split() for _ in range(n)]
for i in range(n):
  d[i].append(i + 1)
d.sort(key = cmp_to_key(cmp))
for i in range(n):
print(d[i][2])


Fixed submission:
from functools import cmp_to_key
def cmp(x, y):
  if x[0] < y[0]:
    return -1
  elif x[0] > y[0]:
    return 1
  else:
    return int(y[1]) - int(x[1])
n = int(input())
d = [input().split() for _ in range(n)]
for i in range(n):
  d[i].append(i + 1)
d.sort(key = cmp_to_key(cmp))
for i in range(n):
  print(d[i][2])
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
t = [1] * n
for _ in range(m):
  l, r = map(int, input().split())
  for i in range(0, l - 1):
    t[i] = 0
  for i in range(r, n):
    t[i] = 0
print(sum(t))


Fixed submission:
n, m = map(int, input().split())
x = 0
y = n - 1
for _ in range(m):
  l, r = map(int, input().split())
  x = max(x, l - 1)
  y = min(y, r - 1)
print(max(y - x + 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
t = [1] * n
for _ in range(m):
  l, r = map(int, input().split())
  for i in range(0, l - 1):
    t[i] = 0
  for i in range(r, n):
    t[i] = 0
print(sum(t))


Fixed submission:
n, m = map(int, input().split())
x = 0
y = n - 1
for _ in range(m):
  l, r = map(int, input().split())
  x = max(x, l - 1)
  y = min(y, r - 1)
print(max(y - x + 1, 0))
Buggy python submission (verdict: Runtime Error):
n = int(input())
v = [int(input()) for _ in range(n)]
c = [int(input()) for _ in range(n)]
result = 0
for i in range(n):
  if v[i] > c[i]:
    result += v[i] - c[i]
print(result)


Fixed submission:
n = int(input())
v = [int(e) for e in input().split()]
c = [int(e) for e in input().split()]
result = 0
for i in range(n):
  if v[i] > c[i]:
    result += v[i] - c[i]
print(result)
Buggy python submission (verdict: Runtime Error):
# 累積和
N, K = map(int, input().split())
S = input()

p = '1'
count = 0
cs = []
for c in S:
  if c != p:
    cs.append(count)
    p = c
    count = 1
  else:
    count += 1
cs.append(count)
if p != '0':
  cs.append(0)

if len(cs) < 2 * K:
  print(N)

for i in range(1, len(cs)):
  cs[i] += cs[i - 1]

result = cs[2 * K]
for i in range(2, len(cs) - 2 * K, 2):
  t = cs[2 * K + i] - cs[i - 1]
  if t > result:
    result = t
print(result)


Fixed submission:
n, k = map(int, input().split())
s = list(map(int, input()))
j = 1
r = 0
t = []
for i in range(n):
  if s[i] == j:
    r += 1
  else:
    t.append(r)
    r = 1
    j ^= 1
t.append(r)
if j == 0:
  t.append(0)
if k >= (len(t) - 1) // 2:
  print(sum(t))
else:
  result = sum(t[:2 * k + 1])
  j = sum(t[:2 * k + 1])
  i = 2
  while i + 2 * k < len(t):
    j += t[i + 2 * k - 1] + t[i + 2 * k] - (t[i - 2] + t[i - 1])
    result = max(result, j)
    i += 2
  print(result)

Buggy python submission (verdict: Runtime Error):
# 累積和
N, K = map(int, input().split())
S = input()

p = '1'
count = 0
cs = []
for c in S:
  if c != p:
    cs.append(count)
    p = c
    count = 1
  else:
    count += 1
cs.append(count)

if len(cs) < 2 * K:
  print(N)

for i in range(1, len(cs)):
  cs[i] += cs[i - 1]

result = cs[2 * K]
for i in range(2, len(cs) - 2 * K, 2):
  t = cs[2 * K + i] - cs[i - 1]
  if t > result:
    result = t
print(result)


Fixed submission:
n, k = map(int, input().split())
s = list(map(int, input()))
j = 1
r = 0
t = []
for i in range(n):
  if s[i] == j:
    r += 1
  else:
    t.append(r)
    r = 1
    j ^= 1
t.append(r)
if j == 0:
  t.append(0)
if k >= (len(t) - 1) // 2:
  print(sum(t))
else:
  result = sum(t[:2 * k + 1])
  j = sum(t[:2 * k + 1])
  i = 2
  while i + 2 * k < len(t):
    j += t[i + 2 * k - 1] + t[i + 2 * k] - (t[i - 2] + t[i - 1])
    result = max(result, j)
    i += 2
  print(result)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
result = 0
while True:
  t = len(s)
  for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
      result += 2
      s = s[:i] + s[i+2:]
      break
  if len(s) == t:
    break
print(result)


Fixed submission:
s = input()
t = len([c for c in s if c == '0'])
print(2 * min(t, len(s) - t))

Buggy python submission (verdict: Runtime Error):
s = input()
result = 0
while True:
  t = len(s)
  for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
      result += 2
      s = s[:i] + s[i+2:]
      print(s)
      break
  if len(s) == t:
    break
print(result)


Fixed submission:
s = input()
t = len([c for c in s if c == '0'])
print(2 * min(t, len(s) - t))
Buggy python submission (verdict: Runtime Error):
def find(parent, i):
  t = parent[i]
  if t < 0:
    return i
  t = find(parent, t)
  parent[i] = t
  return t

def unite(parent, i, j):
  i = find(parent, i)
  j = find(parent, j)
  if i == j:
    return
  parent[j] += parent[i]
  parent[i] = j

n, m = map(int, input().split())
parent = [-1] * n
inconvenience = n * (n - 1) // 2
data = [[int(c) - 1 for c in input().split()] for _ in range(m)]
data.reverse()
result = []
for a, b in data:
  result.append(inconvenience)
  if find(parent, a) != find(parent, b):
    inconvenience -= parent[find(parent, a)] * parent[find(parent, b)]
  unite(parent, a, b)
result.reverse()
for i in result:
  print(i)


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5)

def find(parent, i):
  t = parent[i]
  if t < 0:
    return i
  t = find(parent, t)
  parent[i] = t
  return t

def unite(parent, i, j):
  i = find(parent, i)
  j = find(parent, j)
  if i == j:
    return
  parent[j] += parent[i]
  parent[i] = j

n, m = map(int, input().split())
parent = [-1] * n
inconvenience = n * (n - 1) // 2
data = [[int(c) - 1 for c in input().split()] for _ in range(m)]
data.reverse()
result = []
for a, b in data:
  result.append(inconvenience)
  if find(parent, a) != find(parent, b):
    inconvenience -= parent[find(parent, a)] * parent[find(parent, b)]
  unite(parent, a, b)
result.reverse()
for i in result:
  print(i)
Buggy python submission (verdict: Runtime Error):
N = int(input())
L = map(int, input().split())

if max(L) < sum(L) - max(L):
    print('Yes')
else:
    print('No')


Fixed submission:
n = int(input())
l = [int(e) for e in input().split()]
if max(l) < sum(l) - max(l):
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
from sys import sys
n, m, *x = map(int, stdin.read().split())
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:-n]) if n < m else 0)


Fixed submission:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m-n]) if n < m else 0)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m-n]) if n < m else 0)
print(result)


Fixed submission:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m-n]) if n < m else 0)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
result = 0
for i in range(n - m):
  result += y[i]
print(result)


Fixed submission:
n, m = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = [x[i] - x[i - 1] for i in range(1, m)]
y.sort()
print(sum(y[:m-n]) if n < m else 0)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = list(map(int, input().split()))
t = [0] * 40
for a in alist:
  for i in range(40):
    t[i] += a & 1
    a >>= 1
result = 0
t2 = [None] * 40
m = 1
for l in range(40):
  t2[l] = [m * t[l], m * (n - t[l])]
  m <<= 1
for i in range(0, k + 1):
  j = i
  m = 1
  s = 0
  for l in range(40):
    s += t2[l][j & 1]
    j >>= 1
  result = max(result, s)
print(result)


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

bcs = [0] * 41
for i in range(N):
  a = A[i]
  for j in range(41):
    if a & (1 << j) != 0:
      bcs[j] += 1

X = 0
for i in range(40, -1, -1):
  if bcs[i] >= N - bcs[i]:
    continue  
  t = 1 << i
  if X + t <= K:
    X += t

result = 0
for i in range(N):
  result += X ^ A[i]
print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = list(map(int, input().split()))
t = [0] * 40
for a in alist:
  for i in range(40):
    t[i] += a & 1
    a >>= 1
result = 0
t2 = [None] * 40
m = 1
for l in range(40):
  t2[l] = [m * t[l], m * (n - t[l])]
  m <<= 1
for i in range(0, k + 1):
  j = i
  m = 1
  s = 0
  for l in range(40):
    s += t2[l][j & 1]
    j >>= 1
  result = max(result, s)
print(result)


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

bcs = [0] * 41
for i in range(N):
  a = A[i]
  for j in range(41):
    if a & (1 << j) != 0:
      bcs[j] += 1

X = 0
for i in range(40, -1, -1):
  if bcs[i] >= N - bcs[i]:
    continue  
  t = 1 << i
  if X + t <= K:
    X += t

result = 0
for i in range(N):
  result += X ^ A[i]
print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = list(map(int, input().split()))
t = [0] * 40
for a in alist:
  for i in range(40):
    t[i] += a & 1
    a >>= 1
result = 0
for i in range(0, k + 1):
  j = i
  m = 1
  s = 0
  for l in range(40):
    if j & 1 == 0:
      s += m * t[l]
    else:
      s += m * (n - t[l])
    j >>= 1
    m <<= 1
  result = max(result, s)
print(result)


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

bcs = [0] * 41
for i in range(N):
  a = A[i]
  for j in range(41):
    if a & (1 << j) != 0:
      bcs[j] += 1

X = 0
for i in range(40, -1, -1):
  if bcs[i] >= N - bcs[i]:
    continue  
  t = 1 << i
  if X + t <= K:
    X += t

result = 0
for i in range(N):
  result += X ^ A[i]
print(result)
Buggy python submission (verdict: Runtime Error):
# DP(配るDP)
N = int(input())
h = list(map(int, input().split()))

dp = [float('inf')] * N
dp[0] = 0
for i in range(N - 2):
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i])
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i])
i = N - 2
dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i])
print(dp[N - 1])


Fixed submission:
n = int(input())
h = [int(e) for e in input().split()]
cost = [0] * n
cost[1] = abs(h[1] - h[0])
for i in range(2, n):
  cost[i] = min(abs(h[i] - h[i - 1]) + cost[i - 1], abs(h[i] - h[i - 2]) + cost[i - 2])
print(cost[n - 1])

Buggy python submission (verdict: Runtime Error):
from functools import cmp_to_key
n, w = [int(e) for e in input().split()]
d = [[int(e) for e in input().split()] for i in range(n)]
d.sort(key = cmp_to_key(lambda x, y: x[1] / x[0] - y[1] / y[0]))
t = {}
t[w] = 0
for i in range(n):
  keys = list(sorted(t.keys())) # sort しないと品物iを複数回ナップサックに詰める可能性がある
  tw, tv = d[i]
  for j in keys:
    nw = j - tw
    if nw < 0:
      continue
    nv = t[j] + tv
    if nw in t:
      if nv > t[nw]:
        for k in list(t.keys()):
          if k <= nw and t[k] < nv:
            del t[k]
        t[nw] = nv
    else:
      Flag = False
      for k in t:
        if nw <= k and nv <= t[k]:
          Flag = True
          break
      if Flag:
        continue
      for k in list(t.keys()):
        if k <= nw and t[k] < nv:
          del t[k]
      t[nw] = nv
        
print(max(t.values()))


Fixed submission:
n = int(input())
h = [int(e) for e in input().split()]
cost = [0] * n
cost[1] = abs(h[1] - h[0])
for i in range(2, n):
  cost[i] = min(abs(h[i] - h[i - 1]) + cost[i - 1], abs(h[i] - h[i - 2]) + cost[i - 2])
print(cost[n - 1])