Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
# D - Disjoint Set of Common Divisors
A, B = map(int, input().split())

from fractions import gcd

# nの素数判定
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

# nの約数列挙
def divisor(n):
    ass = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            ass.append(n//i)
    return ass #sortされていない

ans = 0
gcd1 = gcd(A, B)
for i in divisor(gcd1):
    if is_prime(i):
        ans += 1

print(ans+1)

Fixed submission:
def q_A(N):
	kisuu = 0
	for i in range(N):
		n = i + 1
		if n % 2 == 1:
			kisuu += 1
	density = kisuu / N
	print(density)

#q_A(1)
q_A(int(input()))
Buggy python submission (verdict: Time Limit Exceeded):
def q_C(N, arrA):
	res = [0] * N
	#sortA = sorted(arrA, reverse=True)
	for i in range(N):
		tmp = i + 1
		for j in range(N):
			if tmp == arrA[j]:
				res[i] = j + 1
				break
	print(*res)

N = int(input())
arrA = [int(x) for x in input().split()]
q_C(N, arrA)


Fixed submission:
def q_C(N, arrA):
	res = [0] * N
	for i in range(N):
		res[arrA[i] - 1] = i + 1
	print(*res)

N = int(input())
arrA = [int(x) for x in input().split()]
q_C(N, arrA)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h,w,a,b = map(int,input().split())
x = np.ones(w, int)
x[a:] = -1
y = np.ones(h, int)
y[b:] = -1
#x = np.array([1]*a + [-1]*(w-a), int)
#y = np.array([1]*b + [-1]*(h-b), int).reshape(-1,1)
#m = x * y.reshape(-1,1)
#print(m)
#m *= m > 0
#print(m)
for i in range(h):
  row = x * y[i]
  row *= row > 0
  print(*row, sep='')

Fixed submission:
h,w,a,b = map(int,input().split())
x = '1'*a + '0'*(w-a)
xinv = '0'*a + '1'*(w-a)
for i in range(h):
  row = x if i < b else xinv
  print(row)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h,w,a,b = map(int,input().split())
x = np.ones((w), int)
x[a:] = -1
y = np.ones(h, int)
y[b:] = -1
#x = np.array([1]*a + [-1]*(w-a), int)
#y = np.array([1]*b + [-1]*(h-b), int).reshape(-1,1)
m = x * y.reshape(-1,1)
#print(m)
m *= m > 0
#print(m)
for row in m:
  print(*row, sep='')

Fixed submission:
h,w,a,b = map(int,input().split())
x = '1'*a + '0'*(w-a)
xinv = '0'*a + '1'*(w-a)
for i in range(h):
  row = x if i < b else xinv
  print(row)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h,w,a,b = map(int,input().split())
x = np.array([1]*a + [-1]*(w-a), int)
y = np.array([1]*b + [-1]*(h-b), int).reshape(-1,1)
m = x * y
#print(m)
m *= m > 0
#print(m)
for row in m:
  print(*row, sep='')

Fixed submission:
h,w,a,b = map(int,input().split())
x = '1'*a + '0'*(w-a)
xinv = '0'*a + '1'*(w-a)
for i in range(h):
  row = x if i < b else xinv
  print(row)
Buggy python submission (verdict: Runtime Error):
# A - Weather Prediction
S = str(input())

weather = ('Sunny', 'Cloudy', 'Rainy')
if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[3])

Fixed submission:
# A - Weather Prediction
S = str(input())

weather = ('Sunny', 'Cloudy', 'Rainy')
if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[0])
Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())

tree = dict()
parent = [0] * (N+1)
for _ in range(N-1):
  a, b = map(int, input().split())
  parent[b] = a
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

action = [0] * (N+1)
for _ in range(Q):
  p, x = map(int, input().split())
  action[p] += x

counter = [0] * (N+1)

def score(i):
  global counter
  counter[i] = counter[parent[i]] + action[i]
  if i in tree:
    for ch in tree[i]:
      score(ch)

score(1)

ans = ' '.join([str(c) for c in counter[1:]])
print(ans)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = [tuple(map(int, ln.strip().split())) for ln in readlines()]

    # 隣接リスト作成
    graph = [[] for _ in range(N+1)]
    for a, b in input_data[:N-1]:
        graph[a].append(b)
        graph[b].append(a)

    # クエリをもとにスコア加算　※頂点pだけに加算し、子へは加算しない
    val = [0] * (N+1)
    for p, x in input_data[N-1:]:
        val[p] += x

    # DFSで根元から順に足していく
    stack = [1]
    visited =set([1])
    while stack:
        x = stack.pop()
        for y in graph[x]:
            if y in visited: continue
            stack.append(y)
            visited.add(y)
            val[y] += val[x]

    print(*val[1:], sep=' ')

abc138_d()

Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
tree = dict()
for _ in range(N-1):
  a, b = map(int, input().split())
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

point = [0] * (N+1)

def act(node, add):
  point[node] = point[node] + add
  if node in tree:
    for ch in tree[node]:
      act(ch, add)

for _ in range(Q):
  p, x = map(int, input().split())
  act(p, x)

ans = ' '.join([str(n) for n in point[1:]])
print(ans)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = [tuple(map(int, ln.strip().split())) for ln in readlines()]

    # 隣接リスト作成
    graph = [[] for _ in range(N+1)]
    for a, b in input_data[:N-1]:
        graph[a].append(b)
        graph[b].append(a)

    # クエリをもとにスコア加算　※頂点pだけに加算し、子へは加算しない
    val = [0] * (N+1)
    for p, x in input_data[N-1:]:
        val[p] += x

    # DFSで根元から順に足していく
    stack = [1]
    visited =set([1])
    while stack:
        x = stack.pop()
        for y in graph[x]:
            if y in visited: continue
            stack.append(y)
            visited.add(y)
            val[y] += val[x]

    print(*val[1:], sep=' ')

abc138_d()
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
cntr = Counter(A)
ans = 'No'
if len(cntr) == 1 and cntr[0][0] == 0:
  ans = 'Yes'
elif n % 3 == 0:
  if len(cntr) == 3:
    k = list(cntr.keys())
    v = list(cntr.values())
    if k[0]^k[1]^k[2] == 0 and v[0] == v[1] and v[1] == v[2]:
      ans = 'Yes'
  elif len(cntr) == 2:
    if sorted(cntr.keys())[0] == 0 and cntr.get(0) == n//3:
      ans = 'Yes'
print(ans)

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
cntr = Counter(A)
ans = 'No'
if len(set(A)) == 1 and A[0] == 0:
  ans = 'Yes'
elif n % 3 == 0:
  if len(cntr) == 3:
    k = list(cntr.keys())
    v = list(cntr.values())
    if k[0]^k[1]^k[2] == 0 and v[0] == v[1] and v[1] == v[2]:
      ans = 'Yes'
  elif len(cntr) == 2:
    if sorted(cntr.keys())[0] == 0 and cntr.get(0) == n//3:
      ans = 'Yes'
print(ans)
Buggy python submission (verdict: Runtime Error):
N, D = map(int, input().split())
X = np.zeros((N, D), np.int8)
for i in range(N):
  X[i,:] = list(map(int, input().split()))

ans = 0
for i in range(N):
  for j in range(i+1, N):
    dist_sq = 0
    for x, y in zip(X[i], X[j]):
      dist_sq += (x - y) ** 2

    dist = dist_sq ** 0.5
    #print(i, j, dist)

    if dist - int(dist) == 0:
      ans += 1

print(ans)

Fixed submission:
N, D = map(int, input().split())
X = []
for i in range(N):
  X.append(list(map(int, input().split())))
#print(X)

ans = 0
for i in range(N):
  for j in range(i+1, N):
    dist_sq = 0
    for x, y in zip(X[i], X[j]):
      dist_sq += (x - y) ** 2

    dist = dist_sq ** 0.5
    #print(i, j, dist)

    if dist - int(dist) == 0:
      ans += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np

N, D = map(int, input().split())
X = np.zeros((N, D), np.int8)
for i in range(N):
  X[i,:] = list(map(int, input().split()))

#print(X)

ans = 0

for i in range(N):
  for j in range(i+1, N):
    dist = np.linalg.norm(X[i] - X[j])
    #print(i, j, dist)
    if dist - int(dist) == 0:
      ans += 1

print(ans)

Fixed submission:
N, D = map(int, input().split())
X = []
for i in range(N):
  X.append(list(map(int, input().split())))
#print(X)

ans = 0
for i in range(N):
  for j in range(i+1, N):
    dist_sq = 0
    for x, y in zip(X[i], X[j]):
      dist_sq += (x - y) ** 2

    dist = dist_sq ** 0.5
    #print(i, j, dist)

    if dist - int(dist) == 0:
      ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for _ in range(M):
  A.append(int(input()))

div = 1e9 + 7
ans = 0
prev1 = 1
prev2 = 0

for n in range(1, N+1):
  if n in A:
    path = 0
  else:
    path = int((prev1 + prev2) % div)

  prev1, prev2 = path, prev1

print(path)

Fixed submission:
N, M = map(int, input().split())
skip = [False] * (N+1)
for _ in range(M):
  skip[int(input())] = True

div = 1e9 + 7
path = 0
prev1 = 1
prev2 = 0

for n in range(1, N+1):
  if skip[n]:
    path = 0
  else:
    path = int((prev1 + prev2) % div)

  prev1, prev2 = path, prev1

print(path)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for _ in range(M):
  A.append(int(input()))

div = 1e9 + 7
step = [0] * (N+1)
step[0] = 1

for n in range(1, N+1):
  if n in A:
    continue

  if n == 1:
    step[n] = 1
  else:
    step[n] = int((step[n-2] + step[n-1]) % div)

ans = step[N]
print(ans)

Fixed submission:
N, M = map(int, input().split())
skip = [False] * (N+1)
for _ in range(M):
  skip[int(input())] = True

div = 1e9 + 7
path = 0
prev1 = 1
prev2 = 0

for n in range(1, N+1):
  if skip[n]:
    path = 0
  else:
    path = int((prev1 + prev2) % div)

  prev1, prev2 = path, prev1

print(path)
Buggy python submission (verdict: Runtime Error):
N, A, B, C, D = map(int, input().split())
S = str(input())

Sarr = [True if s == '.' else False for s in S]
#print(Sarr)

def canmove(st):
  global N, Sarr
  can = [False] * N
  can[st-1] = True
  for i in range(st, N):
    if Sarr[i]:
      if can[i-1] or can[i-2]:
        can[i] = True
  return can

Acan = canmove(A)
Bcan = canmove(B)
#print(Acan)
#print(Bcan)

ans = Acan[C-1] and Bcan[D-1]
#print(ans)

def canpass(st, ed):
  global Sarr
  for i in range(st, ed-1):
    if Sarr[i] and Sarr[i+1] and Sarr[i+2]:
      return True
  return False

if C > D:
  ans = ans and canpass(B-1, D+1)
#print(ans)

if ans:
  print('Yes')
else:
  print('No')

Fixed submission:
N, A, B, C, D = map(int, input().split())
S = str(input())

Sarr = [True if s == '.' else False for s in S]
#print(Sarr)

def canmove(st):
  global N, Sarr
  can = [False] * N
  can[st-1] = True
  for i in range(st, N):
    if Sarr[i]:
      if can[i-1] or can[i-2]:
        can[i] = True
  return can

Acan = canmove(A)
Bcan = canmove(B)
#print(Acan)
#print(Bcan)

ans = Acan[C-1] and Bcan[D-1]
#print(ans)

def canpass(st, ed):
  global Sarr
  for i in range(st, ed-1):
    if Sarr[i] and Sarr[i+1] and Sarr[i+2]:
      return True
  return False

if C > D:
  ans = ans and canpass(B-2, D)
#print(ans)

if ans:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    # _AB_ , B_A , B_ , _A の個数をそれぞれ数える
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  # これは確定
    if sted > 0:
        ans += (sted - 1)  # x個でx-1組のABができる
        if st > 0:
            ans += 1  # B_A + B_ で1組
            st -= 1
        if ed > 0:
            ans += 1  # _A + B_A で1組
            ed -= 1
    ans += min(st, ed)  # _A + B_ で1組
    print(ans)

diverta19_c()

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    # _AB_ , B_A , B_ , _A の個数をそれぞれ数える
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  # これは確定
    if sted > 0:
        ans += (sted - 1)  # x個でx-1組のABができる
        if st > 0:
            ans += 1  # B_A + B_ で1組
            st -= 1
        if ed > 0:
            ans += 1  # _A + B_A で1組
            ed -= 1
    ans += min(st, ed)  # _A + B_ で1組
    print(ans)

diverta19_c()
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)

if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(counts)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(counts)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)

Fixed submission:
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)
if s[-1] == '0':
  acc.append(acc[-1])

ans = 1
if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(acc)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(acc)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

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

B = []
Btmp = [Bin[0]]
for i in range(1, N):
  if Bin[i] == 1:
    B.append(Btmp)
    Btmp = []
  Btmp.append(Bin[i])
else:
  B.append(Btmp)
#print(B)
#print(list(reversed(B)))

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return reversed(ret)

A = []
for Bsub in reversed(B):
  a = solve(Bsub)
  A.extend(a)
#print(A)
for a in A:
  print(a)

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

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return list(reversed(ret))

A = solve(B)
#print(A)

if A == None:
  print(-1)
else:
  for a in A:
    print(a)
Buggy python submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))

H = H + [10**9]
dp = [-1] * N

def dfs(pos):
  global N, H, dp

  if pos >= N:
    return 10**9
  if pos == N - 1:
    return 0
  if dp[pos] != -1:
    return dp[pos]

  step1 = dfs(pos+1) + abs(H[pos+1] - H[pos])
  step2 = dfs(pos+2) + abs(H[pos+2] - H[pos])
  dp[pos] = min(step1, step2)
  #print(pos, step1, step2)
  return(dp[pos])

ans = dfs(0)
#print(dp)
print(ans)

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

cost = [0] * N
for i in range(1, N):
  d1 = cost[i-1] + abs(H[i] - H[i-1])
  d2 = cost[i-2] + abs(H[i] - H[i-2]) if i > 1 else 1e6
  cost[i] = min(d1, d2)
#print(cost)
ans = cost[N-1]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))

cost = [0] * N
for i in range(1, N):
  #print('i=', i)
  st = max(i - K, 0)
  ci = 10**18

  for j in range(st, i):
    if cost[j] < ci:
      cj = cost[j] + abs(H[i] - H[j])
      #print('cj=', cj)
      ci = min(cj, ci)

  #print('ci=', ci)
  cost[i] = ci

#print(cost)
ans = cost[N-1]
print(ans)

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

cost = [0] * N
for i in range(1, N):
  #print('i=', i)
  st = max(i - K, 0)
  ci = 10**18

  for j in range(st, i):
    if cost[j] < ci:
      cj = cost[j] + abs(H[i] - H[j])
      #print('cj=', cj)
      ci = min(cj, ci)

  #print('ci=', ci)
  cost[i] = ci

#print(cost)
ans = cost[N-1]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, W = map(int, input().split())

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

for j in range(W+1):
  dp[0][j] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w])
    else:
      dp[i+1][w] = dp[i][w]

ans = dp[N][W]
print(ans)

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

for j in range(W+1):
  dp[0][j] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w])
    else:
      dp[i+1][w] = dp[i][w]

ans = dp[N][W]
print(ans)

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

import numpy as np
weight = np.empty((110), int)
value = np.empty((110), int)
dp = np.zeros((110, 100010), int)
#print(weight.shape, value.shape, dp.shape)

for i in range(N):
  weight[i], value[i] = map(int, input().split())

dp[0, :W+1] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1, w] = max(dp[i, w-weight[i]] + value[i], dp[i, w])
    else:
      dp[i+1, w] = dp[i, w]

ans = dp[N, W]
print(ans)

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

for j in range(W+1):
  dp[0][j] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w])
    else:
      dp[i+1][w] = dp[i][w]

ans = dp[N][W]
print(ans)

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

import numpy as np
weight = np.empty((110), int)
value = np.empty((110), int)
dp = np.zeros((110, 10010), int)
#print(weight.shape, value.shape, dp.shape)

for i in range(N):
  weight[i], value[i] = map(int, input().split())

dp[0, :W+1] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1, w] = max(dp[i, w-weight[i]] + value[i], dp[i, w])
    else:
      dp[i+1, w] = dp[i, w]

ans = dp[N, W]
print(ans)

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

for j in range(W+1):
  dp[0][j] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w])
    else:
      dp[i+1][w] = dp[i][w]

ans = dp[N][W]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def abc114_c():
  n = int(input())

  def bit_mask(n):
    b, d = 4, 9  # bit, digit
    arr = []
    for i in range(d-1, -1, -1):
      arr.append(n // (b**i))
      n = n % (b**i)
    ret = str(int(''.join(map(str, arr))))
    return ret

  def decode753(arr):
    ret = ''.join(map(str, arr)).replace('3','7').replace('2','5').replace('1','3')
    return int(ret)

  ans = 0
  b, d = 4, 9
  for m in range(b**d):
    mask = bit_mask(m)
    if mask.count('0') == 0 and mask.count('1') > 0 and mask.count('2') > 0 and mask.count('3') > 0:
      x = decode753(mask)
      if x <= n: ans += 1
      else: break
  print(ans)

abc114_c()

Fixed submission:
def abc114_c():
  n = int(input())

  def bit_mask(n):
    b, d = 4, 9  # bit, digit
    arr = []
    for i in range(d-1, -1, -1):
      arr.append(n // (b**i))
      n = n % (b**i)
    ret = str(int(''.join(map(str, arr))))
    return ret

  def decode753(arr):
    ret = ''.join(map(str, arr)).replace('3','7').replace('2','5').replace('1','3')
    return int(ret)

  ans = 0
  b, d = 4, 9
  for m in range(b**d):
    mask = bit_mask(m)
    if mask.count('0') == 0 and mask.count('1') > 0 and mask.count('2') > 0 and mask.count('3') > 0:
      x = decode753(mask)
      if x <= n: ans += 1
      else: break
  print(ans)

abc114_c()
Buggy python submission (verdict: Runtime Error):
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*30
    cand = B
    for i in range(30, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()

Fixed submission:
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*41
    cand = B.copy()
    for i in range(40, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx.copy()
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()
Buggy python submission (verdict: Runtime Error):
def tenka18beg_c():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A_desc = sorted(A, reverse=True)

    if N % 2 == 1:
        tot1, tot2 = 0, 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot1 += 2*a
                tot2 += 2*a
            elif i == N//2 - 1:
                tot1 += a
                tot2 += 2*a
            elif i == N//2:
                tot1 += a
                tot2 -= a
            elif i == N//2 + 1:
                tot1 -= 2*a
                tot2 -= a
            elif N//2 + 1 < i:
                tot1 -= 2*a
                tot2 -= 2*a
        ans = max(ans, max(tot1, tot2))
    else:
        tot = 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot += 2*a
            elif i == N//2 - 1:
                tot += a
            elif i == N//2:
                tot -= a
            elif N//2 < i:
                tot -= 2*a
        ans = tot
    print(ans)

tenka18beg_c()

Fixed submission:
def tenka18beg_c():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A_desc = sorted(A, reverse=True)

    if N % 2 == 1:
        tot1, tot2 = 0, 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot1 += 2*a
                tot2 += 2*a
            elif i == N//2 - 1:
                tot1 += a
                tot2 += 2*a
            elif i == N//2:
                tot1 += a
                tot2 -= a
            elif i == N//2 + 1:
                tot1 -= 2*a
                tot2 -= a
            elif N//2 + 1 < i:
                tot1 -= 2*a
                tot2 -= 2*a
        ans = max(tot1, tot2)
    else:
        tot = 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot += 2*a
            elif i == N//2 - 1:
                tot += a
            elif i == N//2:
                tot -= a
            elif N//2 < i:
                tot -= 2*a
        ans = tot
    print(ans)

tenka18beg_c()