Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02949 (python)

Fixed submission:
inf = float('inf')
V,E,P = map(int,input().split())
edge = []
for i in range(E):
    A,B,C = map(int,input().split())
    edge.append([A-1,B-1,P-C])

d = [inf]*V
d[0] = 0
for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge[j]
        if d[From] != inf and d[To]>d[From]+cost:
            d[To]=d[From]+cost
            update = True
    if not update:
        break

ans = d[V-1]

for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge[j]
        if d[From] != inf and d[To]>d[From]+cost:
            d[To]=d[From]+cost
            update = True
    if not update:
        break
loop = ans != d[V-1]

if loop:
    print(-1)

else:
    ans = -d[-1]
    print(max(0,ans))

Buggy submission (verdict: Runtime Error):
inf = float('inf')
V,E,P = map(int,input().split())
edge = []
edge_rev = []
for i in range(E):
    A,B,C = map(int,input().split())
    edge_rev.append([B-1,A-1,P-C])
    edge.append([A-1,B-1,P-C])

d = [inf]*V
d[0] = 0
for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge[j]
        if d[From] != inf and d[To]>d[From]+cost:
            d[To]=d[From]+cost
            update = True
    if not update:
        break

d_rev = [inf]*V
d_rev[V-1] = 0
for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge_rev[j]
        if d_rev[From] != inf and d_rev[To]>d_rev[From]+cost:
            d_rev[To]=d_rev[From]+cost
            update = True
    if not update:
        break

d_ = d.copy()
d_rev_ = d_rev.copy()

for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge[j]
        if d[From] != inf and d[To]>d[From]+cost:
            d[To]=d[From]+cost
            update = True
    if not update:
        break

for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge_rev[j]
        if d_rev[From] != inf and d_rev[To]>d_rev[From]+cost:
            d_rev[To]=d_rev[From]+cost
            update = True
    if not update:
        break

loop = False
for i in range(V):
    if d[i] != 0 and d_rev[i] != 0:
        loop = (d[i]+d_rev[i] != d_[i]+d_rev_[i]) or loop

if loop:
    print(-1)

else:
    ans = -d[-1]
    print(max(0,ans))
Problem p02956 (python)

Fixed submission:
from operator import itemgetter

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

mod = 998244353

N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
xy.sort(key=itemgetter(1))
for i in range(N):
    xy[i][1] = i+1
xy.sort(key=itemgetter(0))

pow2 = [1]*(N+1)
for i in range(N):
    pow2[i+1] = 2*pow2[i]%mod

ans = pow2[N-1]*N%mod

bit = BIT(N)
for i in range(N):
    l1 = bit.sum(xy[i][1])
    l2 = i-l1
    r1 = xy[i][1]-l1-1
    r2 = N-xy[i][1]-l2
    bit.add(xy[i][1],1)
    ans += (pow2[l2]-1)*(pow2[r1]-1)*pow2[r2]*pow2[l1]
    ans += (pow2[r2]-1)*(pow2[l1]-1)*(pow2[l2]+pow2[r1]-1)
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
def bitsum(bit,i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= i&(-i)
    return s
    
def bitadd(bit,i,x):
    n = len(bit)
    while i <= n:
        bit[i] += x
        i += i&(-i)
    return bit

mod = 998244353

N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
xy.sort(key=lambda x:x[1])
for i in range(N):
    xy[i][1] = i+1
xy.sort()
for i in range(N):
    xy[i][0] = i

# x<xi,y<yi
l1 = [0]*N
# x<xi,y>yi
l2 = [0]*N
# x>xi,y<yi
r1 = [0]*N
# x>xi,y>yi
r2 = [0]*N

bitl = [0]*(N+1)
for i in range(N):
    l1[i] = bitsum(bitl,xy[i][1])
    l2[i] = i-l1[i]
    bitadd(bitl,xy[i][1],1)

bitr = [0]*(N+1)
for i in range(N):
    r1[N-i-1] = bitsum(bitr,xy[N-i-1][1])
    r2[N-i-1] = i-r1[N-i-1]
    bitadd(bitr,xy[N-i-1][1],1)

ans = pow(2,N-1,mod)*N%mod
for i in range(N):
    L1 = pow(2,l1[i],mod)
    L2 = pow(2,l2[i],mod)
    R1 = pow(2,r1[i],mod)
    R2 = pow(2,r2[i],mod)
    ans += L1*L2%mod*R1%mod*R2%mod
    ans -= L1*R1%mod
    ans -= L2*R2%mod
    ans -= L1*L2%mod
    ans -= R1*R2%mod
    ans += L1+L2+R1+R2-1
    ans %= mod

print(ans)
Problem p02974 (python)

Fixed submission:
mod = 10**9+7
N,K = map(int,input().split())
dp = [[[0]*(K+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 1

for i in range(1,N+1):
    for j in range(N+1):
        for k in range(K+1):
            if k-2*j >= 0:
                dp[i][j][k] = (2*j+1)*dp[i-1][j][k-2*j]
                if j != N:
                    dp[i][j][k] += (j+1)**2*dp[i-1][j+1][k-2*j]
                if j != 0:
                    dp[i][j][k] += dp[i-1][j-1][k-2*j]
            dp[i][j][k] %= mod

print(dp[N][0][K])

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
N,K = map(int,input().split())
dp = [[[0]*(K+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 1

for i in range(1,N+1):
    for j in range(N+1):
        for k in range(K+1):
            if k-2*j >= 0:
                dp[i][j][k] = (2*j+1)*dp[i-1][j][k-2*j]+(j+1)**2*dp[i-1][j+1][k-2*j]
                if j != 0:
                    dp[i][j][k] += dp[i-1][j-1][k-2*j]
            dp[i][j][k] %= mod

print(dp[N][0][K])
Problem p02985 (python)

Fixed submission:
from collections import deque
mod = 10**9+7
N,K = map(int,input().split())
e = [[] for _ in range(N)]
d = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
    d[(min(a,b),max(a,b))] = 0
ans = K
parents = deque([0])

n = [0]*N
while parents:
    p = parents.popleft()
    for c in e[p]:
        if d[(min(p,c),max(p,c))] == 0:
            d[(min(p,c),max(p,c))] = 1
            n[p] += 1
            n[c] += 1
            parents.append(c)
            ans *= K-n[p]
            ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
N,K = map(int,input().split())
g = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)


ans = K
def rec(c,p):
    global ans
    if c == 0:
        for i in range(len(g[0])):
            ans *= K-i-1
            ans %= mod
        for v in g[c]:
            if v != p:
                rec(v,c)
    else:
        for i in range(len(g[c])-1):
            ans *= K-i-2
            ans %= mod
        for v in g[c]:
            if v != p:
                rec(v,c)


rec(0,0)
print(ans)
Problem p03007 (python)

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

if N == 2:
    print(max(A)-min(A))
    print('{} {}'.format(max(A),min(A)))
else:
    p = [a for a in A if a>=0]
    n = [a for a in A if a<0]

    ans = []
    if len(n) == 0:
        p.sort()
        x = p.pop(0)
        y = p.pop()
        ans.append('{} {}'.format(x,y))
        n.append(x-y)
    if len(p) == 0:
        n.sort()
        x = n.pop()
        y = n.pop(0)
        ans.append('{} {}'.format(x,y))
        p.append(x-y)

    x = n.pop()
    while len(p)>1:
        y = p.pop()
        ans.append('{} {}'.format(x,y))
        x -= y
    y = x
    x = p[0]
    ans.append('{} {}'.format(x,y))
    x -= y
    while n:
        y = n.pop()
        ans.append('{} {}'.format(x,y))
        x -= y

    print(x)
    for s in ans:
        print(s)

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

p = [a for a in A if a>=0]
n = [a for a in A if a<0]

ans = []
if len(n) == 0:
    p.sort()
    x = p.pop(0)
    y = p.pop()
    ans.append('{} {}'.format(x,y))
    n.append(x-y)
if len(p) == 0:
    n.sort()
    x = n.pop()
    y = n.pop(0)
    ans.append('{} {}'.format(x,y))
    p.append(x-y)

x = n.pop()
while len(p)>1:
    y = p.pop()
    ans.append('{} {}'.format(x,y))
    x -= y
y = x
x = p[0]
ans.append('{} {}'.format(x,y))
x -= y
while n:
    y = n.pop()
    ans.append('{} {}'.format(x,y))
    x -= y

print(x)
for s in ans:
    print(s)
Problem p03043 (python)

Fixed submission:
import math
N,K = map(int,input().split())
ans = 0

for n in range(1,min(K,N+1)):
    ans += 2**(20-math.ceil(math.log2(K/n)))

ans /= 2**20

ans += max(N-K+1,0)
ans /= N

print(ans)

Buggy submission (verdict: Runtime Error):
import math
N,K = map(int,input().split())
ans = 0

for n in range(1,min(K,N+1)):
    ans += 2**(20-math.ceil(math.log2(K/n)))

ans /= 2**20

ans += max(N-K+1,0)
ans /= N

print(ans)
Problem p03063 (python)

Fixed submission:
N = int(input())
S = input()
w = [0]*N
b = [0]*N
s = S[::-1]
for i in range(N):
    b[i] = b[i-1]
    if S[i] == '#':
        b[i] += 1
    w[i] = w[i-1]
    if s[i] == '.':
        w[i] += 1

m = min(w[N-1],b[N-1])
for i in range(N-1):
    m = min(m,b[i]+w[N-i-2])

print(m)

Buggy submission (verdict: Runtime Error):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def prime_decomposition(n):
  i = 2
  d = {}
  while i * i <= n:
    while n % i == 0:
      n /= i
      if i not in d:
          d[i] = 0
      d[i] += 1
    i += 1
  if n > 1:
    if n not in d:
        d[n] = 1
  return d

def eratosthenes(n):
    prime = []
    limit = n**0.5
    numbers = [i for i in range(2,n+1)]
    while True:
        p = numbers[0]
        if limit <= p:
            return prime + numbers
        prime.append(p)
        numbers = [i for i in numbers if i%p != 0]
    return prime

N = int(input())
A = [int(input()) for i in range(N+1)]
A.reverse()
g = A[0]
for a in A:
     g = gcd(g,a)
d = prime_decomposition(g)
ans = [p for p in d]

prime = eratosthenes(N+2)

for p in prime:
    for n in range(p-1):
        s = 0
        for k in range(int((N-n)/(p-1))+1):
            s += A[n+k*(p-1)]
            s %= p
        if s != 0:
            break
    if s == 0:
        ans.append(p)

ans = list(set(ans))
ans.sort()

for p in ans:
    print(p)
Problem p03082 (python)

Fixed submission:
from collections import defaultdict
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

fact_memo = [0]*201
fact_memo[0] = 1
def fact(n):
    if fact_memo[n] == 0:
        fact_memo[n] = n*fact(n-1)%mod
    return fact_memo[n]

fact_inv_memo = [0]*201
fact_inv_memo[0] = 1
def fact_inv(n):
    if fact_inv_memo[n] == 0:
        fact_inv_memo[n] = pow(fact(n),mod-2,mod)
    return fact_inv_memo[n]

dp = defaultdict(lambda :[-1]*(X+1))
for n in range(N):
    for x in range(X+1):
        if n == 0:
            dp[n][x]=x%S[0]
        else:
            dp[n][x]=(dp[n-1][x%S[n]]+n*dp[n-1][x])%mod

print(dp[N-1][X])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import bisect
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

fact_memo = [0]*201
fact_memo[0] = 1
def fact(n):
    if fact_memo[n] == 0:
        fact_memo[n] = n*fact(n-1)%mod
    return fact_memo[n]

fact_inv_memo = [0]*201
fact_inv_memo[0] = 1
def fact_inv(n):
    if fact_inv_memo[n] == 0:
        fact_inv_memo[n] = pow(fact(n),mod-2,mod)
    return fact_inv_memo[n]

dp = defaultdict(lambda :-1)

def rec(n,x):
    if dp[(n,x)] != -1:
        return dp[(n,x)]
    if n == 0:
        dp[(n,x)] = x%S[0]
        return dp[(n,x)]
    s = S[:n+1]
    if x <= s[-1]:
        k = bisect.bisect(s,x)
        if k == 0:
            dp[(n,x)] = x*fact(n+1)%mod
        else:
            dp[(n,x)] = fact(n+1)*fact_inv(k)*rec(k-1,x)%mod
    else:
        res = x%s[0]*fact(n)%mod
        for i in range(1,n+1):
            res += fact(n)*fact_inv(i)*rec(i-1,x%s[i])%mod
        dp[(n,x)] = res%mod
    return dp[(n,x)]
        
print(rec(N-1,X))
Problem p03083 (python)

Fixed submission:
from collections import defaultdict
B,W = map(int,input().split())
mod = 10**9+7

fact = [0]*(10**6+1)
fact[0] = 1
for i in range(10**6):
    fact[i+1] = fact[i]*(i+1)%mod

def comb_(n,k):
    return fact[n]*pow(fact[k],mod-2,mod)*pow(fact[n-k],mod-2,mod)*pow(2,(n+1)*(mod-2),mod)%mod

f_B = defaultdict(lambda:pow(2,mod-2,mod))
f_W = defaultdict(lambda:pow(2,mod-2,mod))
g = defaultdict(lambda:pow(2,W*(mod-2),mod))

for i in range(B,B+W):
    f_B[i+1] = f_B[i]-comb_(i-1,B-1)*pow(2,mod-2,mod)
    f_B[i+1] %= mod

for i in range(W,B+W):
    f_W[i+1] = f_W[i]-comb_(i-1,W-1)*pow(2,mod-2,mod)
    f_W[i+1] %= mod

for i in range(W+1,B+W):
    g[i+1] = g[i] + comb_(i-1,W-1)
    g[i+1] %= mod

for i in range(1,B+W+1):
    if i < W+1:
        print(f_B[i])
    else:
        print((f_B[i]+f_W[i]+g[i]-pow(2,mod-2,mod))%mod)

Buggy submission (verdict: Runtime Error):

def comb_(n,k):
    return fact(n)*fact_inv(k)*fact_inv(n-k)*pow(2,(n+1)*(mod-2),mod)%mod

f_B = defaultdict(lambda:pow(2,mod-2,mod))
f_W = defaultdict(lambda:pow(2,mod-2,mod))
g = defaultdict(lambda:pow(2,W*(mod-2),mod))

for i in range(B,B+W):
    f_B[i+1] = f_B[i]-comb_(i-1,B-1)*pow(2,mod-2,mod)
    f_B[i+1] %= mod

for i in range(W,B+W):
    f_W[i+1] = f_W[i]-comb_(i-1,W-1)*pow(2,mod-2,mod)
    f_W[i+1] %= mod

for i in range(W+1,B+W):
    g[i+1] = g[i] + comb_(i-1,W-1)
    g[i+1] %= mod

for i in range(1,B+W+1):
    if i < W+1:
        print(f_B[i])
    else:
        print((f_B[i]+f_W[i]+g[i]-pow(2,mod-2,mod))%mod)
Problem p03096 (python)

Fixed submission:
from collections import defaultdict
import bisect
mod = 10**9+7
N = int(input())
S = []
s = ''
for i in range(N):
    c = input()
    if s != c:
        S.append(c)
    s = c

d = defaultdict(lambda:[])
dd = defaultdict(lambda:{})
dcum = defaultdict(lambda:[0])
for i in range(len(S)):
    d[S[i]].append(i)
    dd[S[i]][i] = len(d[S[i]])-1
    dcum[S[i]].append(0)
n = len(S)
dp = [0]*(n+1)
dp[-1]=1

for i in range(n-1,-1,-1):
    l = d[S[i]]
    j = dd[S[i]][i]+1
    dp[i] += dp[i+1]
    dp[i] += dcum[S[i]][j]
    dp[i] %= mod
    dcum[S[i]][j-1] += dp[i+1]+dcum[S[i]][j]
print(dp[0])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
import bisect
mod = 10**9+7
N = int(input())
S = ''
s = ''
for i in range(N):
    c = input()
    if s != c:
        S = S+c
    s = c

d = defaultdict(lambda:[])
for i in range(len(S)):
    d[S[i]].append(i)

val = [0]*(len(S)+1)
val[-1]=1
def rec(n):
    if val[n] != 0:
        return val[n]
    s = S[n]
    res = rec(n+1)
    l = d[s]
    i = bisect.bisect(l,n)
    for j in range(i,len(l)):
        res += rec(l[j]+1)
        res %= mod
    val[n]=res
    return res
print(rec(0))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import bisect
mod = 10**9+7
N = int(input())
S = ''
s = ''
for i in range(N):
    c = input()
    if s != c:
        S = S+c
    s = c

d = defaultdict(lambda:[])
for i in range(len(S)):
    d[S[i]].append(i)

val = [0]*(len(S)+1)
val[-1]=1
def rec(n):
    if val[n] != 0:
        return val[n]
    s = S[n]
    res = rec(n+1)
    l = d[s]
    i = bisect.bisect(l,n)
    for j in range(i,len(l)):
        res += rec(l[j]+1)
        res %= mod
    val[n]=res
    return res
print(rec(0))
Problem p03125 (python)

Fixed submission:
A,B = map(int,input().split())
if B%A==0:
    print(A+B)
else:
    print(B-A)

Buggy submission (verdict: Runtime Error):
A,B = map(int,inpit().split())
if B%A==0:
    print(A+B)
else:
    print(B-A)
Problem p03128 (python)

Fixed submission:
d = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
d_ = {}
for i in range(M):
    d_[d[A[i]]] = A[i]
diff = [(d_[key],key) for key in d_]
diff.sort()

DP = ['']*(10**4+1)
for a,l in diff:
    DP[l] = str(a)
    
for i in range(N+1):
    for a,l in diff:
        if i-l >= 0:
            if len(DP[i-l])+1>=len(DP[i]) and DP[i-l]!='':
                    DP[i] = str(a)+DP[i-l]

ans = list(DP[N])
ans.sort()
ans.reverse()
ans = ''.join(ans)
print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
c = [0,2,5,5,4,5,6,3,7,6]

dp = [-INF]*(N+1)
dp[0] = 0
for a in A:
    if c[a] <= N:
        dp[c[a]] = 1

for i in range(N+1):
    for a in A:
        if i-c[a]>=0:
            dp[i] = max(dp[i],dp[i-c[a]]+1)

ans = ''
n = N
for a in A:
    while dp[n] == dp[n-c[a]]+1:
        n -= c[a]
        ans = ans + str(a)
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
cost = [0,2,5,5,4,5,6,3,7,6]
dp = ['']*(N+1)
for a in A:
    dp[cost[a]] = str(a)
for i in range(N+1):
    for a in A:
        if dp[i-cost[a]] != '':
            if len(dp[i]) <= len(dp[i-cost[a]]+str(a)):
                dp[i] = dp[i-cost[a]]+str(a)
        ans = dp[N]
ans = list(map(int,list(ans)))
ans.sort(reverse=True)
print(''.join(list(map(str,ans))))

Buggy submission (verdict: Runtime Error):
d = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
d_ = {}
for i in range(M):
    d_[d[A[i]]] = A[i]
diff = [(d_[key],key) for key in d_]
diff.sort()

DP = ['']*(N+1)
for a,l in diff:
    DP[l] = str(a)
    
for i in range(N+1):
    for a,l in diff:
        if i-l >= 0:
            if len(DP[i-l])+1>=len(DP[i]) and DP[i-l]!='':
                    DP[i] = str(a)+DP[i-l]

ans = list(DP[N])
ans.sort()
ans.reverse()
ans = ''.join(ans)
print(ans)
Problem p03138 (python)

Fixed submission:
from collections import defaultdict
N,K = map(int,input().split())
A = list(map(lambda x:bin(int(x))[2:],input().split()))
l = 0
for i in range(N):
    l = max(l,len(A[i]))
count = defaultdict(lambda:0)
for i in range(N):
    for j in range(len(A[i])):
        if A[i][-j-1] == '1':
            count[j] += 1
l = max(count.keys())

def rec(x):
    if x=='':
        return 0
    if '0' not in x:
        res = 0
        for i in range(len(x)):
            res += max(count[i],N-count[i])*2**i
        return res
    if x[0] == '1':
        return max(rec('1'*(len(x)-1))+count[len(x)-1]*2**(len(x)-1),rec(x[1:])+(N-count[len(x)-1])*2**(len(x)-1))
    else:
        return rec(x[1:])+count[len(x)-1]*2**(len(x)-1)
ans = rec(bin(K)[2:])
for i in range(len(bin(K)[2:]),l+1):
    ans += count[i]*2**i
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = map(int,input().split())
A = np.int32(input().split())

d = [0]*40
for k in range(40):
    d[k] = np.sum(min(A&2**k,1))

dd = [0]*41
for k in range(40):
    dd[k] = dd[k-1]+max(d[k],N-d[k])*2**k

dp = [0]*(40)

for k in range(40):
    if K&2**k == 0:
        dp[k] = dp[k-1]+d[k]*2**k
    else:
        dp[k] = max(dp[k-1]+(N-d[k])*2**k,dd[k-1]+d[k]*2**k)

print(dp[-1])

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

d = [0]*40
for a in A:
    for k in range(len(bin(a))-1):
        d[k] += min(a&2**k,1)

dd = [0]*len(bin(K))
for k in range(len(dd)-1):
    dd[k] = dd[k-1]+max(d[k],N-d[k])*2**k

dp = [0]*(len(bin(K))-1)

for k in range(len(dp)):
    if K&2**k == 0:
        dp[k] = dp[k-1]+d[k]*2**k
    else:
        dp[k] = max(dp[k-1]+(N-d[k])*2**k,dd[k-1]+d[k]*2**k)

print(dp[-1])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N,K = map(int,input().split())
K = bin(K)[2:]
A = list(map(lambda x:bin(int(x))[2:],input().split()))
count = defaultdict(lambda:0)
for i in range(N):
    for j in range(len(A[i])):
        if A[i][-j-1] == '1':
            count[j] += 1
def rec(x):
    if x=='':
        return 0
    if '0' not in x:
        res = 0
        for i in range(len(x)):
            res += max(count[i],N-count[i])*2**i
        return res
    if x[0] == '1':
        return max(rec('1'*(len(x)-1))+count[len(x)-1]*2**(len(x)-1),rec(x[1:])+(N-count[len(x)-1])*2**(len(x)-1))
    else:
        return rec(x[1:])+count[len(x)-1]*2**(len(x)-1)
ans = rec(K)
for i in range(len(K),l+1):
    ans += count[i]*2**i
print(ans)
Problem p03142 (python)

Fixed submission:
from collections import deque

def topological_sort(V, to):
    cnt = [0]*V
    for i in range(V):
        for j in to[i]:
            cnt[j] += 1
    Q = deque(i for i in range(V) if cnt[i]==0)
    res = []
    while Q:
        i = Q.popleft()
        res.append(i)
        for k in to[i]:
            cnt[k] -= 1
            if cnt[k] == 0:
                Q.append(k)
    return res

N,M = map(int,input().split())
to = [[] for i in range(N)]
From = [[] for i in range(N)]
for i in range(N-1+M):
    x, y = list(map(int,input().split()))
    to[x-1].append(y-1)
    From[y-1].append(x-1)
s = topological_sort(N, to)
s = [(s[i],i) for i in range(N)]
s.sort()
for i in range(N):
    k = -1
    I = -1
    for j in From[i]:
        if k < s[j][1]:
            k = s[j][1]
            I = j
    print(I+1)

Buggy submission (verdict: Runtime Error):
from collections import deque

def topological_sort(V, to):
    cnt = [0]*V
    for i in range(V):
        for j in to[i]:
            cnt[j] += 1
    Q = deque(i for i in range(V) if cnt[i]==0)
    res = []
    while len(Q) > 0:
        i = Q.popleft()
        res.append(i)
        for k in to[i]:
            cnt[k] -= 1
            if cnt[k] == 0:
                Q.append(k)
    return s

N,M = map(int,input().split())
to = [[] for i in range(N)]
From = [[] for i in range(N)]
for i in range(N-1+M):
    x, y = list(map(int,input().split()))
    to[x-1].append(y-1)
    From[y-1].append(x-1)
s = topological_sort(N, to)
s = [(s[i],i) for i in range(N)]
s.sort()
for i in range(N):
    k = -1
    I = -1
    for j in From[i]:
        if k < s[j][1]:
            k = s[j][1]
            I = j
    print(I+1)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
to = [[] for i in range(N)]
From = [[] for i in range(N)]
for i in range(N-1+M):
    x, y = list(map(int,input().split()))
    to[x-1].append(y-1)
    From[y-1].append(x-1)
s = topological_sort(N, to)
s = [(s[i],i) for i in range(N)]
s.sort()
for i in range(N):
    k = -1
    I = -1
    for j in From[i]:
        if k < s[j][1]:
            k = s[j][1]
            I = j
    print(I+1)
Problem p03145 (python)

Fixed submission:
a,b,c = map(int,input().split())
print(a*b*c//2//max(a,b,c))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate
N,K = map(int,input().split())
td = [tuple(map(int,input().split())) for i in range(N)]

td.sort(key=lambda x:-x[1])
td = [(x[0]-1,x[1]) for x in td]
t = [-1]*N
for i in range(N):
    t[td[i][0]] = 0
for i in range(K):
    t[td[:K][i][0]] = 1

t_used = [i for i in range(N) if t[i]==1]
t_not_used = [i for i in range(N) if t[i]==0]

d_top = [0]*N
d_top_candi = [0]*N
d_rest = []
d_candi = []

for i in range(K):
    if t[td[i][0]] == 1:
        if d_top[td[i][0]] == 0:
            d_top[td[i][0]] = td[i][1]
        else:
            d_rest.append(td[i][1])
for i in range(K,N):
    if t[td[i][0]] == 0:
        if d_top_candi[td[i][0]] == 0:
            d_top_candi[td[i][0]] = 1
            d_candi.append(td[i][1])

ans = len(t_used)**2+sum(d_top)
d_rest.sort()
d_candi.sort(key=lambda x:-x)

d_rest_cum = list(accumulate(d_rest))
d_candi_cum = list(accumulate(d_candi))
ans += d_rest_cum[-1]

for k in range(min(len(d_rest),len(d_candi))):
    ans = max(ans + (k+1)*(k+1+2*len(t_used)) + d_candi_cum[k] - d_rest_cum[k],ans)

print(ans)