Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
import math

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append([i+1] - X[i])
    D.sort(reverse=True)
    D = D[math.ceil(N/2):]
    n = 0
    for d in D:
        n += d[1]
    print(n)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input=sys.stdin.readline
import math

def main():
    N,_ = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    D = []
    for i in range(len(X)-1):
        D.append(X[i+1] - X[i])
    D.sort(reverse=True)
    D = D[N-1:]
    n = 0
    for d in D:
        n += d
    print(n)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i,h in enumerate(H):
        if i == 0:
            continue
        m = max(0, i-K)
        for j in range(i-1, m-1, -1):
            dp[i] = min(dp[i], dp[j] + abs(h - H[j]))
    print(dp[N-1])

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i,h in enumerate(H):
        if i == 0:
            continue
        m = max(0, i-K)
        # 普通なら j=i~m のループを回すが、高速化のためこうする（配列アクセスを減らす）
        dp[i] = min([dpj + abs(h - hj) for dpj, hj in zip(dp[m:i], H[m:i])])
    print(dp[N-1])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i in range(1, N):
        m = max(0, i-K)
        for j in range(i-1, m-1, -1):
            dp[i] = min(dp[i], dp[j] + abs(H[i] - H[j]))
    print(dp[N-1])

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i,h in enumerate(H):
        if i == 0:
            continue
        m = max(0, i-K)
        # 普通なら j=i~m のループを回すが、高速化のためこうする（配列アクセスを減らす）
        dp[i] = min([dpj + abs(h - hj) for dpj, hj in zip(dp[m:i], H[m:i])])
    print(dp[N-1])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i in range(1, N):
        for j in range(K):
            x = i-(j+1)
            if x >= 0:
                dp[i] = min(dp[i], dp[x] + abs(H[i] - H[x]))
    print(dp[N-1])

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    H = list(map(int, input().split()))

    inf = 10**4*N
    dp = [inf]*N
    dp[0] = 0
    for i,h in enumerate(H):
        if i == 0:
            continue
        m = max(0, i-K)
        # 普通なら j=i~m のループを回すが、高速化のためこうする（配列アクセスを減らす）
        dp[i] = min([dpj + abs(h - hj) for dpj, hj in zip(dp[m:i], H[m:i])])
    print(dp[N-1])

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,W = map(int, input().split())
    I = []
    for _ in range(N):
        I.append(tuple(map(int, input().split())))

    dp = [[0]*(W+1) for _ in range(N+1)]
    for i in range(N):
        w = I[i][0]
        v = I[i][1]
        dpi = dp[i]
        dpi1 = dp[i+1]
        for j in range(W+1):
            dpij = dpi[j]
            if j - w >= 0:
                dpi1[j] = max(dpij, dpi[j-w] + v)
            else:
                dpi1[j] = dpij

    ans = 0
    for a in dp:
        ans = max(ans, max(a))
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,W = map(int, input().split())
    w = []
    v = []
    for _ in range(N):
        x,y = map(int, input().split())
        w.append(x)
        v.append(y)

    dp = [[0]*(W+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(W+1):
            if j - w[i] >= 0:
                dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i])
            else:
                dp[i+1][j] = dp[i][j]
                
    ans = 0
    for a in dp:
        ans = max(ans, max(a))
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,W = map(int, input().split())
    w = []
    v = []
    for _ in range(N):
        x,y = map(int, input().split())
        w.append(x)
        v.append(y)

    dp = [[0]*(W+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(W+1):
            if j - w[i] >= 0:
                dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i])
            else:
                dp[i+1][j] = dp[i][j]
                
    ans = 0
    for a in dp:
        ans = max(ans, max(a))
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,W = map(int, input().split())
    w = []
    v = []
    for _ in range(N):
        x,y = map(int, input().split())
        w.append(x)
        v.append(y)

    dp = [[0]*(W+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(W+1):
            if j - w[i] >= 0:
                dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i])
            else:
                dp[i+1][j] = dp[i][j]
                
    ans = 0
    for a in dp:
        ans = max(ans, max(a))
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    P = list(map(float, input().split()))
    dp = [[0]*(N+1) for _ in range(N+1)]  # i 枚目のコインを投げたとき j 枚が表の確率

    dp[1][0] = 1 - P[0]
    dp[1][1] = P[0]
    for i in range(2, N+1):
        for j in range(i+1):
            dp[i][j] = dp[i-1][j-1] * P[i-1] + dp[i-1][j] * (1 - P[i-1])

    ans = 0
    for i, p in enumerate(dp[N]):
        if i >= (N+1)//2:
            ans += p
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    P = list(map(float, input().split()))
    dp = [[0]*(N+1) for _ in range(N+1)]  # i 枚目のコインを投げたとき j 枚が表の確率

    dp[1][0] = 1 - P[0]
    dp[1][1] = P[0]
    for i in range(2, N+1):
        for j in range(i+1):
            dp[i][j] = dp[i-1][j-1] * P[i-1] + dp[i-1][j] * (1 - P[i-1])

    ans = 0
    for i, p in enumerate(dp[N]):
        if i >= (N+1)//2:
            ans += p
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

a,b,c = map(int, input().split())
hp = 2
ans = 0
while True:
    if a == 0 and b == 0 and c == 0:
        break
    if hp == 1 and a == 0 and b == 0:
        break
    if hp > 1 and c > 0:
        ans += 1
        c -= 1
        hp -= 1
    if hp < 2 or c == 0:
        if a > 0:
            a -= 1
            hp = max(2, hp+1)
        elif b > 0:
            b -= 1
            hp = max(2, hp+1)
            ans += 1

print(ans)


Fixed submission:
import sys
input=sys.stdin.readline

a,b,c = map(int, input().split())

if a >= c-1 or b >= c-1 or a+b >= c-1:
    print(b+c)
else:
    print(b+(a+b)+1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

N,M = map(int, input().split())

pref = [[0] for i in range(N+1)]
import collections
q = collections.deque()

for i in range(M):
    p,y = map(int, input().split())
    pref[p].append(y)
    q.append((p,y))
for p in pref:
    p.sort()

for p,y in q:
    print("%06d%06d" %(p, pref[p].index(y)))


Fixed submission:
import sys
input=sys.stdin.readline

N,M = map(int, input().split())

pref = [[0] for i in range(N+1)]
import collections
q = collections.deque()
 
for i in range(M):
    p,y = map(int, input().split())
    pref[p].append(y)
    q.append((p,y))
for p in pref:
    p.sort()

import bisect
for p,y in q:
    print("%06d%06d" %(p, bisect.bisect_left(pref[p], y)))
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

input = open(sys.argv[1], "r").readline

def main():
    N = int(input())
    V1 = {}
    V2 = {}
    V = list(map(int, input().split()))
    for i,v in enumerate(V):
        if i%2 == 0:
            V1[v] = V1[v] + 1 if v in V1 else 1
        else:
            V2[v] = V2[v] + 1 if v in V2 else 1

    V1 = sorted(V1.items(), key=lambda x:x[1], reverse=True)
    V2 = sorted(V2.items(), key=lambda x:x[1], reverse=True)

    if V1[0] != V2[0]:
        ans = 0
        for v in V1[1:]:
            ans += v[1]
        for v in V2[1:]:
            ans += v[1]
        print(ans)
    else:
        a1 = 0
        for v in V1[1:]:
            a1 += v[1]
        a1 += V2[0][1]
        for v in V2[2:]:
            a1 += v[1]
        a2 = 0
        a2 += V1[0][1]
        for v in V1[2:]:
            a2 += v[1]
        for v in V2[1:]:
            a1 += v[1]
        print(min(a1,a2))

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N = int(input())
    V1 = {}
    V2 = {}
    V = list(map(int, input().split()))
    for i,v in enumerate(V):
        if i%2 == 0:
            V1[v] = V1[v] + 1 if v in V1 else 1
        else:
            V2[v] = V2[v] + 1 if v in V2 else 1

    V1 = sorted(V1.items(), key=lambda x:x[1], reverse=True)
    V2 = sorted(V2.items(), key=lambda x:x[1], reverse=True)

    if V1[0] != V2[0]:
        ans = 0
        for v in V1[1:]:
            ans += v[1]
        for v in V2[1:]:
            ans += v[1]
        print(ans)
    else:
        a1 = 0
        for v in V1[1:]:
            a1 += v[1]
        a1 += V2[0][1]
        for v in V2[2:]:
            a1 += v[1]
        a2 = 0
        a2 += V1[0][1]
        for v in V1[2:]:
            a2 += v[1]
        for v in V2[1:]:
            a2 += v[1]
        print(min(a1,a2))

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

import fractions

n,x = map(int, input().split())
a = list(map(int, input().split()))

d = []
for i in a:
    d.append(abs(i-x))
m = 10**9
for i in d:
    for j in d:
        m = min(m, fractions.gcd(i,j))
print(m)


Fixed submission:
import sys
input=sys.stdin.readline

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

n,x = map(int, input().split())
a = list(map(int, input().split()))

m = 10**9
for i in a:
    m = min(m, abs(i-x))

d = make_divisors(m)
d.reverse()
for i in d:
    b = True
    for j in a:
        if abs(j-x) % i != 0:
            b = False
            break
    if b:
        print(i)
        break
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int,input().split())
    ans = 0
    if K%2==0:
        a0 = K//2
    else:
        a0 = K
    for a in range(a0, N+1, a0):
        for _ in range(K - a%K, N+1, K):
            for _ in range(K - a%K, N+1, K):
                ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int,input().split())
    r1 = 0
    for i in range(1,N+1):
        if i%K == 0:
            r1 += 1
    if K%2 == 1:
        print(r1**3)
    else:
        r2 = 0
        for i in range(1,N+1):
            if i%K == K//2:
                r2 += 1
        print(r1**3+r2**3)
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,K = map(int,input().split())
    ans = 0
    if K%2==0:
        a0 = K//2
    else:
        a0 = K
    for a in range(a0, N+1, a0):
        for b in range(K - a%K, N+1, K):
            for c in range(K - a%K, N+1, K):
                if (b+c)%K == 0:
#                    print(a,b,c)
                    ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int,input().split())
    r1 = 0
    for i in range(1,N+1):
        if i%K == 0:
            r1 += 1
    if K%2 == 1:
        print(r1**3)
    else:
        r2 = 0
        for i in range(1,N+1):
            if i%K == K//2:
                r2 += 1
        print(r1**3+r2**3)
    
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

input = open(sys.argv[1],"r").readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    N, M = map(int, input().split())
    P = list(map(int, input().split()))
    uf = UnionFind(N+1)
    for i in range(M):
        x, y = map(int, input().split())
        uf.union(x, y)
    ans = 0
    for r in uf.roots():
        ms = uf.members(r)
        p = set()
        for m in ms:
            p.add(P[m-1])
#        print(p, ms)
        ans += len(p & set(ms))
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)


def main():
    N, M = map(int, input().split())
    P = list(map(int, input().split()))
    uf = UnionFind(N)
    for i in range(M):
        x, y = map(int, input().split())
        uf.union(x-1, y-1)
    ans = 0
    for i in range(N):
        if uf.same(P[i]-1, i):
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

H,W=map(int, input().split())
C = [[] for i in range(H)]

for h in range(H):
    C[h] = list(input().strip())
#print(*C)

for h in range(H):
    for w in range(W):
        if C[w][h] == "#":
            if h == 0 or (h>0 and C[h-1][w] == "."):
                if w == 0 or (w>0 and C[h][w-1] == "."):
                    if w == W-1 or (w<W-1 and C[h][w+1] == "."):
                        if h == H-1 or (h<H-1 and C[h+1][w] == "."):
                            print("No")
                            exit()
print("Yes")


Fixed submission:
import sys
input=sys.stdin.readline

H,W=map(int, input().split())
C = [[] for i in range(H)]

for h in range(H):
    C[h] = list(input().strip())
#print(*C)

for h in range(H):
    for w in range(W):
        if C[h][w] == "#":
            if h == 0 or (h>0 and C[h-1][w] == "."):
                if w == 0 or (w>0 and C[h][w-1] == "."):
                    if w == W-1 or (w<W-1 and C[h][w+1] == "."):
                        if h == H-1 or (h<H-1 and C[h+1][w] == "."):
                            print("No")
                            exit()
print("Yes")

Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

H,W=map(int, input().split())
C = [[] for i in range(H)]

for h in range(H):
    C[h] = list(input().strip())
#print(*C)

for h in range(H):
    for w in range(W):
        if C[w][h] == "#":
            if h == 0 or (h>0 and C[h-1][w] == "."):
                if w == 0 or (w>0 and C[h][w-1] == "."):
                    if w == W-1 or (w+1<W and C[h][w+1] == "."):
                        if h == H-1 or (h+1<h and C[h+1][w] == "."):
                            print("No")
                            exit()
print("Yes")


Fixed submission:
import sys
input=sys.stdin.readline

H,W=map(int, input().split())
C = [[] for i in range(H)]

for h in range(H):
    C[h] = list(input().strip())
#print(*C)

for h in range(H):
    for w in range(W):
        if C[h][w] == "#":
            if h == 0 or (h>0 and C[h-1][w] == "."):
                if w == 0 or (w>0 and C[h][w-1] == "."):
                    if w == W-1 or (w<W-1 and C[h][w+1] == "."):
                        if h == H-1 or (h<H-1 and C[h+1][w] == "."):
                            print("No")
                            exit()
print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = int(input())
A = [0] + list(map(int, input().split()))

B = {}
for i in range(len(A)):
    x = sum(A[0:i+1])
    if x not in B:
        B[x] = 1
    else:
        B[x] += 1

ans = 0
for i in B.values():
    if i >= 2:
        ans += combinations_count(i,2)

print(ans)


Fixed submission:
import sys
input=sys.stdin.readline

from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = int(input())
A = [0] + list(map(int, input().split()))

B = {}
x = 0
for i in A:
    x += i
    if x not in B:
        B[x] = 1
    else:
        B[x] += 1

ans = 0
for i in B.values():
    if i >= 2:
        ans += combinations_count(i,2)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

N = int(input())
A = [0] + list(map(int, input().split()))

B = []
for i in range(len(A)):
    tmp = A[0:i+1]
    B.append(sum(tmp))
B.sort()

ans = 0
for i in range(len(B)-1):
    for j in range(i+1,len(B)):
        if B[j]-B[i] == 0:
            ans += 1
print(ans)


Fixed submission:
import sys
input=sys.stdin.readline

from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = int(input())
A = [0] + list(map(int, input().split()))

B = {}
x = 0
for i in A:
    x += i
    if x not in B:
        B[x] = 1
    else:
        B[x] += 1

ans = 0
for i in B.values():
    if i >= 2:
        ans += combinations_count(i,2)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,C = map(int, input().split())
    X = []
    for _ in range(N):
        X.append(list(map(int, input().split())))
    
    cw = [0] # 時計回りに数えて i 番目までを食べた時に摂取できるカロリー
    ccw = [0] # 反時計回り
    for i in range(N):
        cw.append(cw[i] + X[i][1])
        ccw.append(ccw[i] + X[N-1-i][1])
    for i in range(N):
        cw[i+1] -= X[i][0]
        ccw[i+1] -= C - X[N-1-i][0]
    ans = 0
    # i 番目で向きを変えて、j 番目まで食べる場合を全探索
    # O(n^2) なのでアカンか…？
    for i in range(N+1):
        cwccw = cw[i]
        ccwcw = ccw[i]
        if i > 0:
            cwccw -= X[i-1][0] # 原点に戻るコスト
            ccwcw -= C - X[N-i][0]
        for j in range(N+1-i):
            ans = max(ans, cwccw + ccw[j])
            ans = max(ans, ccwcw + cw[j])

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline

def main():
    N,C = map(int, input().split())
    X = []
    for _ in range(N):
        X.append(list(map(int, input().split())))
    
    cw = [0] # 時計回りに数えて i 番目までを食べた時に摂取できるカロリー
    ccw = [0] # 反時計回り
    for i in range(N):
        cw.append(cw[i] + X[i][1])
        ccw.append(ccw[i] + X[N-1-i][1])
    for i in range(N):
        cw[i+1] -= X[i][0]
        ccw[i+1] -= C - X[N-1-i][0]

    ma = 0
    ga = []
    for i in range(0, N+1):
        ma = max(ma, cw[i])
        ga.append(ma)
    ma = 0
    gb = []
    for i in range(0, N+1):
        ma = max(ma, ccw[i])
        gb.append(ma)

    ans = 0
    for bi in range(N+1):
        tmp = ccw[bi] + ga[N-bi]
        if bi > 0:
            tmp -= C - X[N-bi][0]
        ans = max(ans, tmp)

    for ai in range(N+1):
        tmp = cw[ai] + gb[N-ai]
        if ai > 0:
            tmp -= X[ai-1][0]
        ans = max(ans, tmp)

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
import collections
import bisect

from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

def main():
    _ = int(input())
    A = list(map(int,input().split()))
    A.sort()
    cand = []
    while A:
        a = A.pop()
        if not A:
            break
        bi = bisect.bisect_left(A, a//2)
        br = A[bi-1]
        if bi < len(A):
            if abs(a/2-A[bi-1]) > abs(a/2-A[bi]):
                br = A[bi]
        x = combinations_count(a,br)
        cand.append((x,a,br))
#        print(A,a,a//2,bi,br,x)
    cand.sort(key=lambda x:x[0], reverse=True)
    print(cand[0][1], cand[0][2])

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline
import bisect

def main():
    _ = int(input())
    A = list(map(int,input().split()))
    A.sort()
    a = A.pop()
    bi = bisect.bisect_left(A, a//2)
    br = A[bi-1]
    if bi < len(A):
        if abs(a/2-A[bi-1]) > abs(a/2-A[bi]):
            br = A[bi]
    print(a, br)

if __name__ == '__main__':
    main()