Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N,K=int(input().split())
ans = 0
while (N > 0):
  N //= K
  ans += 1
print(ans)

Fixed submission:
N,K=map(int, input().split())
ans = 0
while (N > 0):
  N //= K
  ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())

maxi = -1
sc = 0

for i in range(N):
	A, B = map(int, input().split())
	if A > mini:
		mini = A
		sc = B
print(mini + sc)


Fixed submission:
N = int(input())

maxi = -1
sc = 0

for i in range(N):
	A, B = map(int, input().split())
	if A > maxi:
		maxi = A
		sc = B
print(maxi + sc)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
lr = []
for _ in range(k):
    l, r = map(int, input().split())
    lr.append((l, r))
mod = 998244353
dp = [0]*(2*n+1)
dp[0] = 1
dp[1] = -1
now = 1
for i in range(n):
    for l, r in lr:
        dp[i+l] += now
        dp[i+r+1] -= now
    now += dp[i+1]
print(sum(dp[:n])%mod)

Fixed submission:
n, k = map(int, input().split())
lr = []
for _ in range(k):
    l, r = map(int, input().split())
    lr.append((l, r))
mod = 998244353
dp = [0]*(2*n+1)
dp[0] = 1
dp[1] = -1
now = 1
for i in range(n):
    for l, r in lr:
        dp[i+l] += now
        dp[i+r+1] -= now
    now += dp[i+1]
    now %= mod
ans = 0
for i in range(n):
    ans += dp[i]
    ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
query = []
for _ in range(q):
    t, x = map(int, input().split())
    query.append((t-1, x))
black = (n-2)**2
rc = [[n, n] for _ in range(n)]
mrc = [n, n]
for t, x in query:
    black -= rc[x][t]-2
    if x < mrc[t]:
        mrc[t] = x
        for i in range(rc[x][t]):
            rc[i][1-t] = x 
print(black)

Fixed submission:
from collections import deque
n, q = map(int, input().split())
query = []
for _ in range(q):
    t, x = map(int, input().split())
    query.append((t-1, x))
black = (n-2)**2
rc = [deque([(n, n)]), deque([(n, n)])]
mrc = [n, n]

def bisect(li, x):
    ok = -1
    ng = len(li)
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if li[mid][0]<x:
            ok = mid
        else:
            ng = mid
    return ok+1

for t, x in query:
    tmp = rc[t][bisect(rc[t], x)][1]
    black -= tmp-2
    if x < mrc[t]:
        mrc[t] = x
        rc[1-t].appendleft((tmp, x))
print(black)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -10**9
for i in range(n):
    scores = [0]
    seen = set([i])
    now = i
    loop = False
    for _ in range(k):
        now = p[now]-1
        scores.append(c[now])
        if now in seen:
            loop = True
            break
        seen.add(now)
    for i in range(len(scores)-1):
        scores[i+1] += scores[i]
    tmp = max(scores[1:])
    ans = max(ans, tmp)
    if not loop or scores[-1]<=0:
        continue
    tmp = scores[-1]*(k//(len(scores)-1))
    rem = k%(len(scores)-1)
    tmp += max(scores[:rem+1])
    ans = max(ans, tmp)
print(ans)

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -10**9
for i in range(n):
    scores = [0]
    seen = set([i])
    now = i
    loop = False
    for _ in range(k):
        now = p[now]-1
        scores.append(c[now])
        if now in seen:
            loop = True
            break
        seen.add(now)
    for i in range(len(scores)-1):
        scores[i+1] += scores[i]
    tmp = max(scores[1:])
    ans = max(ans, tmp)
    if (not loop) or scores[-1]<=0:
        continue
    tmp = scores[-1]*(k//(len(scores)-1))
    rem = k%(len(scores)-1)
    tmp += max(scores[:rem+1])
    ans = max(ans, tmp)
    tmp = scores[-1]*(k//(len(scores)-1)-1)
    tmp += max(scores)
    ans = max(ans, tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
c = input()
l = 0
r = n-1
ans = 0
while l<r:
    while l<n and c[l]=='R':
        l+=1
    while l>=0 and c[r]=='W':
        r-=1
    if l>r:
        break
    ans += 1
    l += 1
    r -= 1
print(ans)

Fixed submission:
n = int(input())
c = input()
l = 0
r = n-1
ans = 0
while l<r:
    while l<n and c[l]=='R':
        l+=1
    while r>=0 and c[r]=='W':
        r-=1
    if l>r:
        break
    ans += 1
    l += 1
    r -= 1
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
x = input()
ones = 0
for i in x:
    if i=='1':
        ones += 1
mods = [0, 0]
for i in range(n):
    if x[n-i-1]=='1':
        if ones>1:
            mods[0] += pow(2, i, ones-1)
        mods[1] += pow(2, i, ones+1)
if ones>1:
    mods[0] %= ones-1
mods[1] %= ones+1
def f(x):
    if x==0:
        return 1
    cnt = 1
    while x>0:
        p = bin(x).count('1')
        x %= p
        cnt += 1
    return cnt
for i in range(n-1, -1, -1):
    if x[n-i-1]=='1' and ones==1:
        print(0)
        continue
    if x[n-i-1]=='1':
        init = (mods[0] - pow(2, i, ones-1))%(ones-1)
    else:
        init = (mods[1] + pow(2, i, ones+1))%(ones+1)
    print(f(init))

Fixed submission:
from heapq import heappop, heappush
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
    n = int(input())
    ans = 0
    camels0 = [[] for _ in range(n)]
    camels1 = [[] for _ in range(n)]
    for _ in range(n):
        k, l, r = map(int, input().split())
        if l>r:
            camels0[k-1].append(l-r)
        elif l<r and k<n:
            camels1[n-k-1].append(r-l)
        ans += min(l, r)
    h = []
    for i in range(n):
        for j in camels0[i]:
            heappush(h, j)
        while len(h)>i+1:
            heappop(h)
    ans += sum(h)
    h = []
    for i in range(n):
        for j in camels1[i]:
            heappush(h, j)
        while len(h)>i+1:
            heappop(h)
    ans += sum(h)
    print(ans)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left, bisect_right
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
na = a[:bisect_left(a, 0)]
pa = a[bisect_right(a, 0):][::-1]
mod = 10**9+7
if k%2==1 and len(pa)==0:
    print(sum(na[::-1][:k])%mod)
    exit(0)
na += [0,0]
pa += [0,0]
nn = 0
pn = 0
ans = 1
for i in range(k//2):
    if pa[pn]*pa[pn+1]>=na[nn]*na[nn+1]:
        ans *= pa[pn]*pa[pn+1]
        pn += 2
    else:
        ans *= na[nn]*na[nn+1]
        nn += 2
    ans %= mod
if k%2==1:
    ans *= pa[pn]
print(ans)

Fixed submission:
from bisect import bisect_left, bisect_right
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
na = a[:bisect_left(a, 0)]
pa = a[bisect_right(a, 0):][::-1]
mod = 10**9+7
if k==n:
    ans = 1
    for i in a:
        ans *= i
        ans %= mod
    print(ans)
    exit(0)
if k%2==1 and len(pa)==0:
    ans = 1
    for i in a[::-1][:k]:
        ans *= i
        ans %= mod
    print(ans)
    exit(0)
if k==1:
    print(a[-1])
    exit(0)
if k%2==1:
    rem = pa[-1]
    pa = pa[:-1]
na += [0]*n
pa += [0]*n
nn = 0
pn = 0
ans = 1
for i in range(k//2):
    if i==k//2-1 and k%2==1:
        break
    if pa[pn]*pa[pn+1]>=na[nn]*na[nn+1]:
        ans *= pa[pn]*pa[pn+1]
        pn += 2
    else:
        ans *= na[nn]*na[nn+1]
        nn += 2
    ans %= mod
if k%2==1:
    ans *= max(pa[pn]*pa[pn+1]*max(pa[pn+2], rem), na[nn]*na[nn+1]*max(pa[pn], rem))
print(ans%mod)
Buggy python submission (verdict: Runtime Error):
from time import time
from random import randrange
start = time()
d = int(input())
c = list(map(int, input().split()))
s = []
for _ in range(d):
    s.append(list(map(int, input().split())))
t = []
v = 0
last = [-1]*26
for i in range(d):
    bst = -10**10
    for j in range(26):
        tmp = s[i][j]
        for k in range(26):
            if j!=k:
                tmp -= c[k]*(i-last[k])
        if tmp>bst:
            bst = tmp
            bst_idx = j
    last[bst_idx] = i
    t.append(bst_idx+1)
    v += s[i][bst_idx]
    for j in range(26):
        v -= c[j]*(i-last[j])
mx = v
while time()-start<1.9:
    newt = t[:]
    nd = randrange(d)
    now = newt[nd]
    nq = randrange(25)
    newt[nd] = nq if nq<now else nq+1
    v = 0
    last = [-1]*26
    for i in range(d):
        v += s[i][newt[i]]
        last[newt[i]] = i
        for j in range(26):
            v -= c[j]*(i-last[j])
    if v>mx:
        mx = v
        t = newt
for i in t:
    print(i)

Fixed submission:
d = int(input())
c = list(map(int, input().split()))
s = []
for _ in range(d):
    s.append(list(map(int, input().split())))
for i in range(d):
    print(i%26+1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 1
for i in a:
    ans *= i
if ans > 10**18:
    print(-1)
else:
    print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 1
if a.count(0)>0:
    print(0)
    exit()
for i in a:
    ans *= i
    if ans>10**18:
        print(-1)
        break
else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
ma = [None]*n
ma[0] = 1
for i in range(n-1):
    ma[i+1] = (ma[i]-a[i])*2
node = a[-1]
ans = node
for i in range(n-1, -1, -1):
    mi = (node+1)//2
    if ma[i]-a[i]<mi:
        print(-1)
        exit()
    node = min(ma[i]-a[i], node) + a[i]
    ans += node
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
if n==0:
    if a[0]==1:
        print(1)
    else:
        print(-1)
    exit()
ma = [None]*n
ma[0] = 1
for i in range(n-1):
    ma[i+1] = (ma[i]-a[i])*2
node = a[-1]
ans = node
for i in range(n-1, -1, -1):
    mi = (node+1)//2
    if ma[i]-a[i]<mi:
        print(-1)
        exit()
    node = min(ma[i]-a[i], node) + a[i]
    ans += node
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
ab = []
for _ in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
di = {}
def gcd(a, b):
    while b!=0:
        a, b = b, a%b
    return abs(a)
for a, b in ab:
    g = gcd(a, b)
    a, b = a//g, b//g
    if a*b>0:
        ind = (abs(a), abs(b))
    elif a*b<0:
        ind = (abs(b), abs(a))
    else:
        ind = 0
    if ind in di:
        x, y = di[ind]
    else:
        x = y = 0
    if a*b==0:
        if b!=0:
            di[0] = (x+1, y)
        elif a!=0:
            di[0] = (x, y+1)
    else:
        if a*b>0:
            di[ind] = (x+1, y)
        else:
            di[ind] = (x, y+1)
mod = 10**9+7
ans = 1
for i, j in di.values():
    ans *= (pow(2, i, mod)+pow(2, j, mod)-1)
    ans %= mod
print(ans-1)

Fixed submission:
n = int(input())
ab = []
for _ in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
di = {}
z = 0
def gcd(a, b):
    while b!=0:
        a, b = b, a%b
    return a
for a, b in ab:
    if a*b!=0:
        g = gcd(abs(a), abs(b))
        a, b = a//g, b//g
    if a*b>0:
        ind = (abs(a), abs(b))
    elif a*b<0:
        ind = (abs(b), abs(a))
    else:
        ind = 0
    if ind in di:
        x, y = di[ind]
    else:
        x = y = 0
    if a*b==0:
        if b!=0:
            di[ind] = (x+1, y)
        elif a!=0:
            di[ind] = (x, y+1)
        else:
            z += 1
    else:
        if a*b>0:
            di[ind] = (x+1, y)
        else:
            di[ind] = (x, y+1)
mod = 10**9+7
ans = 1
for i, j in di.values():
    ans *= (pow(2, i, mod)+pow(2, j, mod)-1)
    ans %= mod
print((ans+z-1)%mod)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
mods = [0]*2019
mods[0] = 1
accu = 0
for i, j in enumerate(reversed(s)):
    accu += int(j)*10**i
    accu %= 2019
    mods[accu] += 1
ans = 0
for i in mods:
    ans += i*(i-1)//2
print(ans)

Fixed submission:
s = input()
mods = [0]*2019
mods[0] = 1
accu = 0
for i, j in enumerate(reversed(s)):
    accu += int(j)*pow(10, i, 2019)
    accu %= 2019
    mods[accu] += 1
ans = 0
for i in mods:
    ans += i*(i-1)//2
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k, c = map(int, input().split())
s = input()
accu = []
tmp = 0
r = 0
for cc in s:
    r -= 1
    if cc=='x' or r>0:
        accu.append(tmp)
        continue
    tmp += 1
    accu.append(tmp)
    r = c+1


for i, cc in enumerate(s):
    if cc=='x':
        continue
    if i>0 and accu[i]==accu[i-1]:
        continue
    if i==0:
        tmp = 0
    else:
        tmp = accu[i-1]
    r = 0
    for j in range(i+1, n):
        r -= 1
        if s[j]=='x' or r>0:
            continue
        tmp += 1
        if tmp >= k:
            break
        r = c+1
    else:
        if tmp<k:
            print(i+1)


Fixed submission:
n, k, c = map(int, input().split())
s = input()
fo = []
ba = []
r = 0
for ind, cc in enumerate(s):
    ind += 1
    r -= 1
    if cc=='x' or r>0:
        continue
    fo.append(ind)
    if len(fo)==k:
        break
    r = c+1
r = 0
for ind, cc in enumerate(reversed(s)):
    ind = n-ind
    r -= 1
    if cc=='x' or r>0:
        continue
    ba.append(ind)
    if len(ba)==k:
        break
    r = c+1

for f, b in zip(fo, reversed(ba)):
    if f==b:
        print(f)

Buggy python submission (verdict: Time Limit Exceeded):
n, k, c = map(int, input().split())
s = input()
accu = []
tmp = 0
r = 0
for cc in s:
    r -= 1
    if cc=='x' or r>0:
        accu.append(tmp)
        continue
    tmp += 1
    accu.append(tmp)
    r = c+1


for i, cc in enumerate(s):
    if cc=='x':
        continue
    if i>0 and accu[i]==accu[i-1]:
        continue
    if i==0:
        tmp = 0
    else:
        tmp = accu[i-1]
    r = 0
    for j in range(i+1, n):
        r -= 1
        if s[j]=='x' or r>0:
            continue
        tmp += 1
        if tmp >= k:
            break
        r = c+1
    else:
        if tmp<k:
            print(i+1)


Fixed submission:
n, k, c = map(int, input().split())
s = input()
fo = []
ba = []
r = 0
for ind, cc in enumerate(s):
    ind += 1
    r -= 1
    if cc=='x' or r>0:
        continue
    fo.append(ind)
    if len(fo)==k:
        break
    r = c+1
r = 0
for ind, cc in enumerate(reversed(s)):
    ind = n-ind
    r -= 1
    if cc=='x' or r>0:
        continue
    ba.append(ind)
    if len(ba)==k:
        break
    r = c+1

for f, b in zip(fo, reversed(ba)):
    if f==b:
        print(f)
Buggy python submission (verdict: Time Limit Exceeded):
n, d, a = map(int, input().split())
xh = []
for _ in range(n):
    x, h = map(int, input().split())
    xh.append([x, h])

xh.sort(key=lambda x:x[0])
ind = 0
ans = 0
while ind<n:
    end = xh[ind][0]+2*d
    cnt = (xh[ind][1]+a-1)//a
    ans += cnt
    ind += 1
    i = ind
    while i<n and xh[i][0]<=end:
        xh[i][1] -= a*cnt
        i += 1
    while ind<n and xh[ind][1]<=0:
        ind += 1
print(ans)

Fixed submission:
n, d, a = map(int, input().split())
xh = []
for _ in range(n):
    x, h = map(int, input().split())
    h = (h+a-1)//a
    xh.append([x, h])

xh.sort(key=lambda x:x[0])

def bisect(x):
    i_min=0
    i_max=n
    while i_min<i_max:
        i = (i_min+i_max)//2
        if xh[i][0]<=x:
            i_min = i+1
        else:
            i_max = i
    return i_min

ans = 0
accu = 0
out = [0]*(n+1)
for i, (x, h) in enumerate(xh):
    accu -= out[i]
    hp = h-accu
    if hp<=0:
        continue
    accu += hp
    ans += hp
    out[bisect(x+2*d)] += hp 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, d, a = map(int, input().split())
xh = []
for _ in range(n):
    x, h = map(int, input().split())
    xh.append([x, h])

xh.sort(key=lambda x:x[0])
ind = 0
ans = 0
while ind<n:
    end = xh[ind][0]+2*d
    cnt = (xh[ind][1]+a-1)//a
    ans += cnt
    i = ind
    while i<n and xh[i][0]<=end:
        xh[i][1] = max(xh[i][1]-a*cnt, 0)
        i += 1
    while ind<n and xh[ind][1]==0:
        ind += 1
print(ans)

Fixed submission:
n, d, a = map(int, input().split())
xh = []
for _ in range(n):
    x, h = map(int, input().split())
    h = (h+a-1)//a
    xh.append([x, h])

xh.sort(key=lambda x:x[0])

def bisect(x):
    i_min=0
    i_max=n
    while i_min<i_max:
        i = (i_min+i_max)//2
        if xh[i][0]<=x:
            i_min = i+1
        else:
            i_max = i
    return i_min

ans = 0
accu = 0
out = [0]*(n+1)
for i, (x, h) in enumerate(xh):
    accu -= out[i]
    hp = h-accu
    if hp<=0:
        continue
    accu += hp
    ans += hp
    out[bisect(x+2*d)] += hp 
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = 0
  for j in a:
    tmp += (j>>i)&1
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = map(int, input().split())
digits = [0]*60
for i in a:
  for j in range(60):
    digits[j] += i%2
    i//=2
    if i==0:
      break
ans = 0
for i in range(60):
  ans += digits[i]*(n-digits[i])*(2**i)
  ans %= 10**9+7
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(lambda x:format(int(x), '060b') , input().split()))
n1 = [0]*60
for i in a:
  for j in range(60):
    if i[59-j]=='1':
      n1[j] += 1
ans = 0
for i in range(60):
  ans += n1[i]*(n-n1[i])*2**i
  ans %= 10**9+7
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[int(i) for i in input().split()]
s=0
for i in range(n-1):
    for j in range(i, n):
        s+=a[i]^a[j]
        s%=1000000007
print(s%1000000007)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=[int(i) for i in input().split()]
s=0
for i in range(n-1):
    for j in range(i, n):
        s+=a[i]^a[j]
        s%=s
print(s%1000000007)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[int(i) for i in input().split()]
s=0
for i in range(n-1):
    for j in range(i, n):
        s+=a[i]^a[j]
print(s%1000000007)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=[int(i) for i in input.split()]
s=0
for i in range(n-1):
    for j in range(i, n):
        s+=a[i]^a[j]
print(s%1000000007)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(60):
  tmp = sum([1 for j in a if (j>>i)&1])
  ans += tmp*(n-tmp)*(2**i)
  ans %= 10**9+7
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from scipy.misc import comb

x, y = map(int, input().split())
s, l = min(x,y), max(x,y)
if 2*s-l<0 or (2*s-l)%3!=0:
  print(0)
else:
  r = (2*s-l)//3
  n = 2*r+l-s
  print(comb(n,r,exact=True)%1000000007)

Fixed submission:
import sys

x, y = map(int, input().split())
s, l = min(x,y), max(x,y)

if 2*s-l<0 or (2*s-l)%3!=0:
    print(0)
    sys.exit()

r = (2*s-l)//3
n = (s+l)//3
mod = 10**9+7
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]
for i in range(2, n+1):
    g1.append((g1[-1] * i) % mod)
    inv.append((-inv[mod%i] * (mod//i)) % mod)
    g2.append((g2[-1] * inv[-1]) % mod)

def comb(n, r):
    if r<0 or r>n:
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

print(comb(n, r))

Buggy python submission (verdict: Time Limit Exceeded):
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

x, y = map(int, input().split())
s, l = min(x,y), max(x,y)
if 2*s-l<0 or (2*s-l)%3!=0:
  print(0)
else:
  r = (2*s-l)//3
  n = 2*r+l-s
  print(comb(n,r)%1000000007)

Fixed submission:
import sys

x, y = map(int, input().split())
s, l = min(x,y), max(x,y)

if 2*s-l<0 or (2*s-l)%3!=0:
    print(0)
    sys.exit()

r = (2*s-l)//3
n = (s+l)//3
mod = 10**9+7
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]
for i in range(2, n+1):
    g1.append((g1[-1] * i) % mod)
    inv.append((-inv[mod%i] * (mod//i)) % mod)
    g2.append((g2[-1] * inv[-1]) % mod)

def comb(n, r):
    if r<0 or r>n:
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

print(comb(n, r))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)

def solve(x):
    lk = k
    for i, j in zip(a, f):
        if i*j<=x:
            continue
        lk -= (j*(i+1)-x-1)//j
        if lk<0:
            return False
    return True

ok = 10**12
ng = -1
while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if solve(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)

def solve(x):
    lk = k
    for i, j in zip(a, f):
        if i*j<=x:
            continue
        lk -= (j*(i+1)-x-1)//j
        if lk<0:
            return False
    return True

ok = 10**12
ng = -1
while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if solve(mid):
        ok = mid
    else:
        ng = mid

print(ok)
Buggy python submission (verdict: Time Limit Exceeded):
n, k, q = map(int, input().split())
a = []
for _ in range(q):
  a.append(int(input()))
for i in range(1, n+1):
  s = 'Yes' if k-q+a.count(i)>0 else 'No'
  print(s)

Fixed submission:
n, k, q = map(int, input().split())
a = [0]*n
for _ in range(q):
  a[int(input())-1] += 1
for i in range(n):
  s = 'Yes' if k-q+a[i]>0 else 'No'
  print(s)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
  a.sort()
  a[-1]//=2
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
while m>0:
  a[0]//=2
  for i in range(1, m):
    if i<n and a[i] >= a[0]:
      a[i]//=2
    else:
      a.sort(reverse=True)
      m-=i
      break
  else:
    break
print(sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
# O(NM), absolutely TLE
n, m = map(int, input().split())
ab = []
for _ in range(n):
    ab.append(list(map(int, input().split())))

ab.sort(key=lambda x:x[1], reverse=True)

ans = 0
for i in range(1, m+1):
    if len(ab)==0:
        break
    for j in range(len(ab)):
        if ab[j][0]<=i:
            _, tmp = ab.pop(j)
            ans += tmp
            break
print(ans)

Fixed submission:
import heapq
n, m = map(int, input().split())
ab = []
for _ in range(n):
    ab.append(list(map(int, input().split())))

ab.sort(key=lambda x:x[0])

heap = []
ind = 0
ans = 0
for i in range(1, m+1):
    while ind<len(ab):
        if ab[ind][0]==i:
            heapq.heappush(heap, -ab[ind][1])
            ind += 1
        else:
            break
    if len(heap)>0:
        ans += heapq.heappop(heap)
    elif ind==len(ab)-1:
        break
print(-ans)