Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def modCom(n, k, mod):
    p, q = 1, 1
    for i in range(n-k+1, n+1):
        p = (p * i) % mod
    for i in range(2, k+1):
        q = (q * i) % mod
    return p * pow(q, mod-2, mod) % mod

mod = 10**9+7
K = int(input())
S = input()
L = len(S)
ans = 0
for i in range(K+1):
    ans += modCom(K-i+L-1, L-1, mod) * pow(25, K-i, mod) * pow(26, i, mod)
    ans %= mod
print(ans)

Fixed submission:
mod = 10**9+7
def factrial(N):
    fc = [1, 1] + [None] * N
    inv = [0, 1] + [None] * N
    fcInv = [1, 1] + [None] * N
    for i in range(2, N+1):
        fc[i] = i*fc[i-1]%mod
        inv[i] = mod-(inv[mod%i]*(mod//i))%mod
        fcInv[i] = fcInv[i-1]*inv[i]%mod
    return fc, fcInv

def modCom(n, k):
    return 0 if n < k else fc[n] * fcInv[k] * fcInv[n-k] % mod

K = int(input())
S = input()
L = len(S)
fc, fcInv = factrial(L+K)
ans = 0
for i in range(K+1):
    ans += modCom(K-i+L-1, L-1) * pow(25, K-i, mod) * pow(26, i, mod)
    ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
S = [int(x) for x in input()]
N = len(S)
l = 1
ans = 0
dic = defaultdict(int)
dic[0] = 1
m = 0
for x in S[::-1]:
    m = (x * l + m) % 2019
    ans += dic[m]
    l *= 10
    dic[m] += 1
print(ans)

Fixed submission:
from collections import defaultdict
S = [int(x) for x in input()]
N = len(S)
l = 1
ans = 0
dic = defaultdict(int)
dic[0] = 1
m = 0
for x in S[::-1]:
    m = (x * l + m) % 2019
    ans += dic[m]
    l = l * 10 % 2019
    dic[m] += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
INF = 500000
mod = 10**9+7

a, b, c = map(int, input().split())
if a*a+b*b+c*c > 2*(a*b+b*c+c*a) and c > 4:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
INF = 500000
mod = 10**9+7

l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())-1
print(l[K])

# a, b, c = input(), input(), input()
# la, lb, lc = len(a), len(b), len(c)
# for i in range(1, min(la, lb)):
#     S = ''
#     for j in range(i):
#         if a[la-i+j] == b[j]:
#             S += b[j]
#             continue
#         elif a[la-i+j] == '?':
#             S += b[j]
#         elif b[j] == '?':
#             S += c[j]
Buggy python submission (verdict: Runtime Error):
n,a,b=map(int,input().split())
m=10**9+7
def com(n,k,m):
  p,q=1,1
  for i in range(n-k,n+1):
    p*=i
    p%=m
  for i in range(1,k+1):
    q*=i
    q%=m
  return p*pow(q,m-2,m)%m
ans=(pow(2,n,m)-ca-cb)%m

Fixed submission:
n,a,b=map(int,input().split())
m=10**9+7
def com(n,k,m):
  p,q=1,1
  for i in range(n-k+1,n+1):
    p*=i
    p%=m
  for i in range(1,k+1):
    q*=i
    q%=m
  return p*pow(q,m-2,m)%m
ca,cb=com(n,a,m),com(n,b,m)
ans=(pow(2,n,m)-1-ca-cb)%m
print(ans)
Buggy python submission (verdict: Runtime Error):
N=2*10**5+1
mod=10**9+7
f=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(1,N):
  fi[i]=pow(f[i],mod-2,mod)
def com(n,k):
  return f[n]*fi[n-k]%mod*fi[k]%mod
def hcom(n,k):
  return com(n+k-1,n-1)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k,n)):
  ans=(ans+com(n,i)*hcom(n-i,i)%mod)%mod
print(ans)

Fixed submission:
N=2*10**5+1
mod=10**9+7
f=[None]*N
fi=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(N):
  fi[i]=pow(f[i],mod-2,mod)
def com(n,k):
  return f[n]*fi[n-k]%mod*fi[k]%mod
def hcom(n,k):
  return com(n+k-1,n-1)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k+1,n)):
  ans=(ans+com(n,i)*hcom(n-i,i)%mod)%mod
print(ans)

Buggy python submission (verdict: Runtime Error):
N=2*10**5+2
mod=10**9+7
f=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(1,N):
  fi[i]=pow(f[i],mod-2,mod)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k,n)):
  ans+=f[n]*fi[k]%mod*fi[k]%mod*f[n+1]*%mod*fi[i-1]%mod
  ans%=mod
print(ans)

Fixed submission:
N=2*10**5+1
mod=10**9+7
f=[None]*N
fi=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(N):
  fi[i]=pow(f[i],mod-2,mod)
def com(n,k):
  return f[n]*fi[n-k]%mod*fi[k]%mod
def hcom(n,k):
  return com(n+k-1,n-1)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k+1,n)):
  ans=(ans+com(n,i)*hcom(n-i,i)%mod)%mod
print(ans)

Buggy python submission (verdict: Runtime Error):
N=2*10**5+1
mod=10**9+7
f=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(1,N):
  fi[i]=pow(f[i],mod-2,mod)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k,n)):
  ans+=f[n]*fi[k]%mod*fi[k]%mod*f[n+1]*%mod*fi[i-1]%mod
  ans%=mod
print(ans)

Fixed submission:
N=2*10**5+1
mod=10**9+7
f=[None]*N
fi=[None]*N
f[0]=1
for i in range(1,N):
  f[i]=i*f[i-1]%mod
for i in range(N):
  fi[i]=pow(f[i],mod-2,mod)
def com(n,k):
  return f[n]*fi[n-k]%mod*fi[k]%mod
def hcom(n,k):
  return com(n+k-1,n-1)
n,k=map(int,input().split())
ans=1
for i in range(1,min(k+1,n)):
  ans=(ans+com(n,i)*hcom(n-i,i)%mod)%mod
print(ans)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=[int(x) for x in input().input()]
flg=0
for x in a:
  if x%2==0 and x%3 and x%5:
    flg=1
if flg:
  print("DENIED")
else:
  print("APPROVED")

Fixed submission:
n=int(input())
a=[int(x) for x in input().split()]
flg=0
for x in a:
  if x%2==0 and x%3 and x%5:
    flg=1
if flg:
  print("DENIED")
else:
  print("APPROVED")
Buggy python submission (verdict: Runtime Error):
s,t=input().split
a,b=map(int,input().split())
u=input()
if s==u:
  print(b)
else:
  print(a)

Fixed submission:
s, t = input().split()
a, b = map(int, input().split())
u = input()
if s == u:
  print(a-1, b)
else:
  print(a, b-1)
Buggy python submission (verdict: Runtime Error):
import sys
# sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
mod = 998244353

N = [int(x) for x in input()]
K = int(input())
dp = [[[0]*2 for j in range(K+1)] for i in range(len(N)+1)]
dp[0][0][1] = 1
for i in range(len(N)):
    dp[i+1][0][0] = 1
    if N[i] == 0:
        for k in range(1, K+1):
            dp[i+1][k][0] = dp[i][k][0]+dp[i][k-1][0]*9
            dp[i+1][k][1] = dp[i][k][1]
    else:
        for k in range(1, K+1):
            dp[i+1][k][0] = dp[i][k][0]+dp[i][k-1][0]*9+dp[i][k-1][1]*(N[i]-1)+dp[i][k][1]
        for k in range(K-1, -1, -1):
            if dp[i][k][1]:
                print(i, k, N[i])
                dp[i+1][k+1][1] = dp[i][k][1]
                break
print(dp)
print(dp[len(N)][K][0]+dp[len(N)][K][1])


Fixed submission:
N = [int(x) for x in input()]
K = int(input())
dp = [[[0]*2 for j in range(K+1)] for i in range(len(N)+1)]
dp[0][0][1] = 1
for i in range(len(N)):
    dp[i+1][0][0] = 1
    if N[i] == 0:
        for k in range(1, K+1):
            dp[i+1][k][0] = dp[i][k][0]+dp[i][k-1][0]*9
            dp[i+1][k][1] = dp[i][k][1]
    else:
        for k in range(1, K+1):
            dp[i+1][k][0] = dp[i][k][0]+dp[i][k-1][0]*9+dp[i][k-1][1]*(N[i]-1)+dp[i][k][1]
        for k in range(K-1, -1, -1):
            if dp[i][k][1]:
                dp[i+1][k+1][1] = dp[i][k][1]
                break
print(dp[len(N)][K][0]+dp[len(N)][K][1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import *
INF = 500000
mod = 10**9+7

H, W = map(int, input().split())
f = [input() for i in range(H)]
es = defaultdict(lambda : INF)
for i in range(H):
    for j in range(W-1):
        if f[i][j] == '.' and f[i][j+1] == '.':
            es[((i, j), (i, j+1))] = 1
            es[((i, j+1), (i, j))] = 1
for i in range(H-1):
    for j in range(W):
        if f[i][j] == '.' and f[i+1][j] == '.':
            es[((i, j), (i+1, j))] = 1
            es[((i+1, j), (i, j))] = 1
V = []
for i in range(H):
    for j in range(W):
        if f[i][j] == '.':
            es[((i, j), (i, j))] = 0
            V.append((i, j))
for k in V:
    for i in V:
        for j in V:
            tmp = es[(i, k)] + es[(k, j)]
            if tmp < es[(i, j)]:
                es[(i, j)] = tmp
ans = 0
for i in V:
    for j in V:
        if ans < es[(i, j)]:
            ans = es[(i, j)]
print(ans)


Fixed submission:
import sys
from collections import *
from heapq import *
INF = 500000
mod = 10**9+7

def dijkstra(s):
    dist = defaultdict(lambda : INF)
    dist[s], h = 0, []
    heappush(h, (0, s))
    while (len(h)):
        dv, v = heappop(h)
        if dv > dist[v]:
            continue
        for u in es[v]:
            tmp = dv+1
            if tmp < dist[u]:
                dist[u] = tmp
                heappush(h, (tmp, u))
    return dist

H, W = map(int, input().split())
f = [input() for i in range(H)]
V = []
for i in range(H):
    for j in range(W):
        if f[i][j] == '.':
            V.append((i, j))
es = defaultdict(list)
for i in range(H):
    for j in range(W-1):
        if f[i][j] == '.' and f[i][j+1] == '.':
            es[(i, j)].append((i, j+1))
            es[(i, j+1)].append((i, j))
for i in range(H-1):
    for j in range(W):
        if f[i][j] == '.' and f[i+1][j] == '.':
            es[(i, j)].append((i+1, j))
            es[(i+1, j)].append((i, j))
ans = 0
for v in V:
    ans = max(ans, max(dijkstra(v).values()))
print(ans)
Buggy python submission (verdict: Runtime Error):
n = input()
print(input().count("ABC"))


Fixed submission:
k, x = map(int, input().split())
if 500*k >= x:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Runtime Error):
k, x = map(int, input().split())
if 500*k >= X:
  print("Yes")
else:
  print("No")

Fixed submission:
k, x = map(int, input().split())
if 500*k >= x:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
n = input()
print(inpur().count("ABC"))

Fixed submission:
n = input()
print(input().count("ABC"))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
import functools
import itertools
import math
INF = float('inf')
MOD = 10**9+7

M = 60
N = int(input())
A = [int(x) for x in input().split()]
f = [[0] * M for i in range(N+1)]
for i in range(N):
    a = A[i]
    for j in range(M):
        f[i+1][j] = f[i][j] + (a & 1)
        a >>= 1
ans = 0
for i in range(N):
    k = 1
    for j in range(M):
        if (A[i]>>j) & 1:
            ans += k * (N - i - 1 - f[N][j] + f[i+1][j])
        else:
            ans += k * (f[N][j] - f[i+1][j])
        ans %= MOD
        k <<= 1
        k %= MOD
print(ans)



Fixed submission:
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
import functools
import itertools
import math
INF = float('inf')
MOD = 10**9+7

M = 60
N = int(input())
A = [int(x) for x in input().split()]
f = [[0] * M for i in range(N+1)]
for i in range(N):
    a = A[i]
    for j in range(M):
        f[i+1][j] = f[i][j] + (a & 1)
        a >>= 1
ans = 0
for i in range(N):
    k = 1
    for j in range(M):
        if (A[i]>>j) & 1:
            ans += k * (N - i - 1 - f[N][j] + f[i+1][j])
        else:
            ans += k * (f[N][j] - f[i+1][j])
        ans %= MOD
        k <<= 1
        k %= MOD
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [int(x) for x in input().split()]
S = sum(A)
L = A[0]
m = S-L
k = 1
for i in range(1, N-1):
  L += A[i]
  if S-L < m:
    k = i+1
print(abs(S-2*sum(A[:k]))



Fixed submission:
N = int(input())
A = list(map(int, input().split()))
allL = sum(A)
L, mL = 0, float('inf')
for i in range(N):
    L += A[i]
    if abs(2*L-allL) < mL:
        mL = abs(2*L-allL)
    else:
        break
print(mL)
Buggy python submission (verdict: Runtime Error):
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
C = Counter(D)
ans = 1
for i in range(1, max(D)+1):
    if C[i] == 0:
        ans = 0
        break
    else:
        ans *= pow(C[i-1], C[i], MOD)
        ans %= MOD
print(ans)

Fixed submission:
from collections import Counter
MOD = 998244353

N = int(input())
D = list(map(int, input().split()))
C = Counter(D)
if D[0] != 0 or C[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1, max(D)+1):
    if C[i] == 0:
        ans = 0
        break
    else:
        ans *= pow(C[i-1], C[i], MOD)
        ans %= MOD
print(ans)


# import sys
# sys.setrecursionlimit(10000000)
# def input():
#     return sys.stdin.readline()[:-1]
# from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
# INF = float('inf')
# # MOD = 10**9+7
# MOD = 998244353
#
# N = int(input())
# D = list(map(int, input().split()))
# if D[0] != 0:
#     print(0)
# C = Counter(D)
# ans = 1
# for i in range(1, max(D)+1):
#     if C[i] == 0:
#         ans = 0
#         break
#     else:
#         ans *= pow(C[i-1], C[i], MOD)
#         ans %= MOD
# print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
# from bisect import *
# from collections import *
from heapq import *
# import itertools
import math
# INF = 10**9
# MOD = 10**9+7

N, K = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
f = [int(x) for x in input().split()]
f.sort(reverse = 1)
h = [(-x*y, x, y) for x, y in zip(a, f)]
heapify(h)
while K > 0:
    v, x, y = heappop(h)
    vn, xn, yn = h[0]
    xd = int(xn*yn/y)
    if xd*y == -vn:
        xd -= 1
    if x-xd <= K:
        K -= x-xd
        heappush(h, (-xd*y, xd, y))
    else:
        heappush(h, (-(x-K)*y, x-K, y))
        break
print(-h[0][0])


# cnt = defaultdict(int)
# for x in f:
#     cnt[x] += 1
# cnt.sort(key = lambda v : v[0])
# lst = defaultdict(list)
# l = 0
# for k, v in cnt:
#     lst[k] = Counter(a[l:l+cnt[k]])
#     l += cnt[k]
# h = [(cnt[k]*lst[k][-1][0], k) for k in cnt]
# h.heapify()
# while K > 0:
#     v, k = heappop(h)
#     if v == 0:
#         break


Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
# from bisect import *
from collections import *
from heapq import *
# import itertools
import math
# INF = 10**9
# MOD = 10**9+7

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
F = [int(x) for x in input().split()]
F.sort(reverse = 1)
l, r = -1, 10**12
while l+1 < r:
    x = (l+r)//2
    c = 0
    for a, f in zip(A, F):
        if a*f > x:
            c += max(0,a-x//f)
    if c > K:
        l = x
    else:
        r = x
print(r)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
INF = 10**10
MOD = 10**9+7

N, K = map(int, input().split())
P = [int(x) for x in input().split()]
s = set()
for i in range(N-K+1):
    s.add(tuple(P[:i]+sorted(P[i:i+K])+P[i+K:]))
print(len(s))


Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
INF = 10**10
MOD = 10**9+7

class UnionFind:

    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.height = [0] * n
        self.size = [1] * n

    def find(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

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

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        if self.height[x] < self.height[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = self.par[x]
            self.size[x] += self.size[y]
            if self.height[x] == self.height[x]:
                self.height[x] += 1

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

def slide_min(a, k):
    n = len(a)
    b = [None] * (n-k+1)
    deq = deque()
    for i in range(n):
        while len(deq) > 0 and a[i] <= a[deq[-1]]:
            deq.pop()
        deq.append(i)
        if i-k+1 >= 0:
            b[i-k+1] = a[deq[0]]
            if deq[0] == i-k+1:
                deq.popleft()
    return b

def slide_max(a, k):
    n = len(a)
    b = [None] * (n-k+1)
    deq = deque()
    for i in range(n):
        while len(deq) > 0 and a[i] >= a[deq[-1]]:
            deq.pop()
        deq.append(i)
        if i-k+1 >= 0:
            b[i-k+1] = a[deq[0]]
            if deq[0] == i-k+1:
                deq.popleft()
    return b

N, K = map(int, input().split())
P = [int(x) for x in input().split()]
uf1 = UnionFind(N)
for i in range(N-1):
    if P[i] < P[i+1]:
        uf1.unite(i, i+1)
vs = []
for i in range(N-K+1):
    if uf1.same(i, i+K-1):
        vs.append(i)
uf2 = UnionFind(N)
if len(vs) > 1:
    for v1, v2 in zip(vs, vs[1:]):
        uf2.unite(v1, v2)
m, M = slide_min(P, K), slide_max(P, K)
for i in range(N-K):
    if P[i] == m[i] and P[i+K] == M[i+1]:
        uf2.unite(i, i+1)
s = set()
for i in range(N-K+1):
    s.add(uf2.find(i))
print(len(s))
Buggy python submission (verdict: Runtime Error):
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 0
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt < 2:
    f = 1
S = deepcopy(T)
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1
while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1
while 0 < r and S[r-1] == 0:
    r -= 1
while l < r and k < K:
    k += 1
    for i in range(l+1, r):
        S[i] ^= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
ans1 = 0
i = 0
while i < N-1:
    c = 0
    while i < N-1:
        if S[i] == S[i+1]:
            i += 1
            c += 1
        else:
            ans1 += c
            c = 0
            i += 1
ans1 += c

S = deepcopy(T)
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1
while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1
while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    for i in range(l+1, r):
        S[i] ^= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
ans2 = 0
i = 0
while i < N-1:
    c = 0
    while i < N-1:
        if S[i] == S[i+1]:
            i += 1
            c += 1
        else:
            ans2 += c
            c = 0
            i += 1
ans2 += c
print(max(ans1, ans2, f*N-1))


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 1
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt//2 <= K:
    f = 1
ans1 = ans2 = N-cnt
# print(ans1, ans2, cnt)
S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1
# ans1 += l
while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1
# ans1 += N-r-2
while 0 < r and S[r-1] == 0:
    r -= 1
# print(ans1)
while l < r and k < K:
    k += 1
    ans1 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1

S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1
# ans2 += l
while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1
# ans2 += N-r-2
while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    ans2 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
print(max(ans1, ans2, f*N-1))
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from collections import *
import math
from bisect import *
import functools
INF = float('inf')
mod = 10**9+7

class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0 for _ in range(N+1)]
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
bit = BIT(N+1)
cnt = 0
for a in A:
    cnt += bit.sum(N)-bit.sum(a)
    bit.add(a, 1)
X = 0
for a in A:
    X += bit.sum(a-1)
print((cnt*K+X*(K-1)*K//2)%mod)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from collections import *
import math
from bisect import *
import functools
INF = float('inf')
mod = 10**9+7

class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0 for _ in range(N+1)]
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
bit = BIT(2001)
cnt = 0
for a in A:
    cnt += bit.sum(2000)-bit.sum(a)
    bit.add(a, 1)
X = 0
for a in A:
    X += bit.sum(a-1)
print((cnt*K+X*(K-1)*K//2)%mod)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *

N, Q = map(int, input().split())
es = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    es[a].append(b)
cnt = [0] * (N+1)
for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
def EulerTour(v, p, c):
    for u in es[v]:
        if u != p:
            EulerTour(u, v, c+cnt[v])
    cnt[v] += c
EulerTour(1, 0, 0)
print(*cnt[1:])


Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *

N, Q = map(int, input().split())
es = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    es[a].append(b)
cnt = [0] * (N+1)
for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
def EulerTour(v, p, c):
    for u in es[v]:
        if u != p:
            EulerTour(u, v, c+cnt[v])
    cnt[v] += c
EulerTour(1, 0, 0)
print(*cnt[1:])