Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

n,a,b,c = LI()
z = [a,b,c]
s = [sys.stdin.readline().rstrip() for _ in range(n)]
ans = []
for i in range(n):
    x = s[i]
    p = -1
    if x == 'AB': j=0;k=1;l=2
    elif x == 'AC': j=0;k=2;l=1
    elif x == 'BC': j=1;k=2;l=0
    if z[j] > z[k]:
        z[j] -= 1; z[k] += 1
        p = k
    else:
        z[j] += 1; z[k] -= 1
        p = j
    if (z[j] == 0 or z[k] == 0) and z[l] == 0:
        if i < n+1:
            y = s[i+1]
            if x == 'AB': j1=0;k1=1;l1=3
            elif x == 'AC': j1=0;k1=2;l1=1
            elif x == 'BC': j1=1;k1=2;l1=0
            if z[j1] == 0 and z[k1] == 0:
                if z[j] == 0:
                    z[j] += 2; z[k] -= 2
                    p = j
                else:
                    z[j] -= 2; z[k] += 2
                    p = k
    if min(z) < 0:
        print('No')
        exit(0)
    if p==0:ans.append('A')
    elif p==1:ans.append('B')
    else: ans.append('C')
print('Yes')
print(*ans,sep='\n')






Fixed submission:
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

n,a,b,c = LI()
z = [a,b,c]
s = [sys.stdin.readline().rstrip() for _ in range(n)]
ans = []
for i in range(n):
    x = s[i]
    p = -1
    if x == 'AB': j=0;k=1;l=2
    elif x == 'AC': j=0;k=2;l=1
    elif x == 'BC': j=1;k=2;l=0
    if z[j] > z[k]:
        z[j] -= 1; z[k] += 1
        p = k
    else:
        z[j] += 1; z[k] -= 1
        p = j
    if (z[j] == 0 or z[k] == 0) and z[l] == 0:
        if i < n-1:
            y = s[i+1]
            if y == 'AB': j1=0;k1=1;l1=2
            elif y == 'AC': j1=0;k1=2;l1=1
            elif y == 'BC': j1=1;k1=2;l1=0
            if z[j1] == 0 and z[k1] == 0:
                if z[j] == 0:
                    z[j] += 2; z[k] -= 2
                    p = j
                else:
                    z[j] -= 2; z[k] += 2
                    p = k
    if min(z) < 0:
        print('No')
        exit(0)
    if p==0:ans.append('A')
    elif p==1:ans.append('B')
    else: ans.append('C')
print('Yes')
print(*ans,sep='\n')
Buggy python submission (verdict: Runtime Error):
import sys,math,copy,queue,itertools,bisect
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))

N,M = LI()
A = LI()
ans = N - sum(A)
if ans < 0:
    ans = -1
print (ans)

Fixed submission:
import sys,math,copy,queue,itertools,bisect
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

R = NI()
print (2 * math.pi * R)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import copy
import queue
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
INF = 10**15
DD = ((1,0),(0,1),(-1,0),(0,-1))

S = input().rstrip()
n = int(input())

a = ['','']
d = 0
for _ in range(n):
    q = list(input().split())
    if q[0] == '1':
        d = 1-d
    else:
        if q[1] == '1':
            a[d] += q[2]
        else:
            a[1-d] += q[2]

if d == 1:
    S = ''.join(reversed(list(S + a[1]))) + a[0]
else:
    S = ''.join(reversed(list(a[0]))) + S + a[1]
print (S)

Fixed submission:
import sys
import math
import copy
import queue
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
INF = 10**15
DD = ((1,0),(0,1),(-1,0),(0,-1))

S = [x for x in input().rstrip()]
n = int(input())

a = [[],[]]
d = 0
for _ in range(n):
    q = list(input().split())
    if q[0] == '1':
        d = 1-d
    else:
        if q[1] == '1':
            a[d].append(q[2])
        else:
            a[1-d].append(q[2])

if d == 1:
    a[1].reverse()
    S.reverse()
    S = ''.join(a[1]) + ''.join(S) + ''.join(a[0])
else:
    a[0].reverse()
    S = ''.join(a[0]) + ''.join(S) + ''.join(a[1])
print (S)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import copy
import queue
input = sys.stdin.readline
n = int(input())
s = list(input().rstrip())
q = int(input())
for _ in range(q):
    t,i,c = input().split()

    if t == '1':
        s[int(i)-1] = c
    else:
        ss = set(s[int(i)-1:int(c)])
        print (len(ss))

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N = NI()

    R = 2**(N.bit_length())
    st = [0] * (R*2)

    def update(i,s):
        x = 2 ** (ord(s) - ord('a'))
        i += R-1
        st[i] = x
        while i > 0:
            i = (i-1) // 2
            st[i] = st[i*2+1] | st[i*2+2]

    def query(a,b,n,l,r):
        ret = 0
        if a <= l and r <= b: return st[n]
        if a < r and b > l:
            vl = query(a,b,n*2+1,l,(l+r)//2)
            vr = query(a,b,n*2+2,(l+r)//2,r)
            ret = vl | vr
        return ret

    for i,s in enumerate(sys.stdin.readline().rstrip()):
        update(i+1,s)
    Q = NI()
    for _ in range(Q):
        c,a,b = sys.stdin.readline().split()
        if c == '1':
            update(int(a),b)
        else:
            ret = query(int(a),int(b)+1,0,0,R)
            cnt = 0
            b = 1
            for i in range(26):
                cnt += (b & ret) > 0
                b <<= 1
            print(cnt)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
N = input()
c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0:
            if N[i-1] > '5':
                c -= 2
print (c)


Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = [0] + list(map(int,SI()))
    ans = 0
    for i in range(len(N)-1,-1,-1):

        if N[i] < 6 and not(N[i] == 5 and (i > 0 and N[i-1] >= 5)):
            ans += N[i]
        else:
            ans += 10 - N[i]
            N[i-1] += 1
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N = input()
c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0 and N[i-1] > '5':
            c -= 2
print (c)


Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = [0] + list(map(int,SI()))
    ans = 0
    for i in range(len(N)-1,-1,-1):

        if N[i] < 6 and not(N[i] == 5 and (i > 0 and N[i-1] >= 5)):
            ans += N[i]
        else:
            ans += 10 - N[i]
            N[i-1] += 1
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N = input()
n = int(N)

c = 0
for i in range(len(N)):
    s = N[i]
    i = int(s)
    if i <= 5:
        c += i
    else:
        c += 1 + (10 - i)
        if i > 0 and N[i-1] > '5':
            c -= 2
print (c)


Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = [0] + list(map(int,SI()))
    ans = 0
    for i in range(len(N)-1,-1,-1):

        if N[i] < 6 and not(N[i] == 5 and (i > 0 and N[i-1] >= 5)):
            ans += N[i]
        else:
            ans += 10 - N[i]
            N[i-1] += 1
    print(ans)
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
for i in range(len(A)-1):
    if A[i+1:].count(A[i]) > 0:
        print ('NO')
        exit (0)
print ('YES')

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
S = set(A)
if len(A) == len(S):
    print ('YES')
else:
    print ('NO')
Buggy python submission (verdict: Runtime Error):
import math
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        for x in range(1,N+1):
            d = 10 ** int(math.log10(x)) 
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Fixed submission:
N,M = map(int,input().split())
if N == M:
  print ('Yes')
else:
  print ('No')
Buggy python submission (verdict: Time Limit Exceeded):
import math
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        for x in range(1,N+1):
            d = 10 ** int(math.log10(x)) 
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Fixed submission:
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        d = 1
        for x in range(1,N+1):
            if x % (d *10) == 0:
                d = d * 10
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        for x in range(1,N+1):
            d = 10 ** int(math.log10(x)) 
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Fixed submission:
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        d = 1
        for x in range(1,N+1):
            if x % (d *10) == 0:
                d = d * 10
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys,queue,math,copy
from functools import reduce
from fractions import gcd
input = sys.stdin.readline
LI = lambda : [int(x) for x in input().split()]
LI_2 = lambda : [int(x) / 2 for x in input().split()]

#lcm = lambda a : reduce(lambda x, y : x * y // gcd(x,y), a)
N,M = LI()
A = LI_2()

lcm = 1
for a in A:
    lcm *= a // gcd(lcm,a)

for a in A:
    if (lcm // a) % 2 == 0:
        print (0)
        exit (0)

print (math.ceil((M//lcm) / 2))

Fixed submission:
import sys,queue,math,copy
from fractions import gcd
input = sys.stdin.readline
LI = lambda : [int(x) for x in input().split()]
LI_2 = lambda : [int(x) / 2 for x in input().split()]

N,M = LI()
A = LI_2()

lcm = 1
for a in A:
    c = gcd(lcm,a)
    if (lcm//c) % 2 == 0:
        print (0)
        exit (0)
    lcm *= (a//c)

print (math.ceil((M//lcm) / 2))

Buggy python submission (verdict: Runtime Error):
import sys,queue,math,copy
from functools import reduce
from fractions import gcd
input = sys.stdin.readline
LI = lambda : [int(x) for x in input().split()]
LI_2 = lambda : [int(x) / 2 for x in input().split()]

lcm = lambda a : reduce(lambda x, y : x * y // gcd(x,y), a)
N,M = LI()
A = LI_2()
c = lcm(A)
for i in A:
    if (c // i) % 2 == 0:
        print (0)
        exit (0)
print (math.ceil(M//c) / 2))

Fixed submission:
import sys,queue,math,copy
from fractions import gcd
input = sys.stdin.readline
LI = lambda : [int(x) for x in input().split()]
LI_2 = lambda : [int(x) / 2 for x in input().split()]

N,M = LI()
A = LI_2()

lcm = 1
for a in A:
    c = gcd(lcm,a)
    if (lcm//c) % 2 == 0:
        print (0)
        exit (0)
    lcm *= (a//c)

print (math.ceil((M//lcm) / 2))
Buggy python submission (verdict: Runtime Error):
x = int(input())
while True:
    if x % 2 != 0:    
        for m in range(3,int(x**0.5)+4,2):
            if x % m == 0 and x < m:
                break
        else:
            break
    x += 1
print (x)

Fixed submission:
s = input().split()
print (s[1]+s[0])
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a =  [int(n) for n in input().split()]
b = [i + j for i in a for j in a ]
b.sort(reverse=True)
print(sum(b[:m]))

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    A = LI()
    a = sorted(A,reverse=True)

    high = a[0] * 2
    low = 0
    while high > low + 1:
        mid = (high + low) // 2

        cnt = 0
        j = N - 1
        for i in range(N):
            while a[i] + a[j] < mid:
                j -= 1
                if j < 0: break
            else:
                cnt += (j+1)
                continue
            break
        if cnt <= M:
            high = mid
        else:
            low = mid

    s = [a[0]]
    for i in range(1,N):
        s.append(s[-1]+a[i])

    ans = 0
    cnt = 0
    m = a[0] * 2
    j = N - 1
    for i in range(N):
        while a[i] + a[j] < low:
            j -= 1
            if j < 0: break
        else:
            ans += a[i] * (j+1) + s[j]
            cnt += j + 1
            m = min(m,a[i] + a[j])
            continue
        break

    print(ans - (cnt-M) * m)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a =  [int(n) for n in input().split()]
b = [i + j for i in a for j in a ]
b.sort(reverse=True)
print(sum(b[:m]))

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    A = LI()
    a = sorted(A,reverse=True)

    high = a[0] * 2
    low = 0
    while high > low + 1:
        mid = (high + low) // 2

        cnt = 0
        j = N - 1
        for i in range(N):
            while a[i] + a[j] < mid:
                j -= 1
                if j < 0: break
            else:
                cnt += (j+1)
                continue
            break
        if cnt <= M:
            high = mid
        else:
            low = mid

    s = [a[0]]
    for i in range(1,N):
        s.append(s[-1]+a[i])

    ans = 0
    cnt = 0
    m = a[0] * 2
    j = N - 1
    for i in range(N):
        while a[i] + a[j] < low:
            j -= 1
            if j < 0: break
        else:
            ans += a[i] * (j+1) + s[j]
            cnt += j + 1
            m = min(m,a[i] + a[j])
            continue
        break

    print(ans - (cnt-M) * m)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N,M,V,P = LI()
    A = LI()
    A.sort()

    ans = 0
    ac = list(itertools.accumulate(A))
    bi = bisect.bisect_left(A,A[N-P+1])
    for i in range(N):
        if i >= N-P:
            ans += 1
            continue
        if M+A[i] < A[N-P]: continue
        if V <= P:
            ans += 1
            continue

        w = ((N-P-i) * (M+A[i])) - (ac[N-P] - ac[i])
        if w >= M * (V-P-i):
            ans += 1
    print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N,M,V,P = LI()
    A = LI()
    A.sort()

    ans = 0
    ac = list(itertools.accumulate(A))
    for i in range(N):
        if i >= N-P:
            ans += 1
            continue
        if M+A[i] < A[N-P]: continue
        if V <= P:
            ans += 1
            continue

        w = ((N-P-i) * (M+A[i])) - (ac[N-P] - ac[i])
        if w >= M * (V-P-i):
            ans += 1
    print(ans)
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys,queue,math,copy
input = sys.stdin.readline
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
N = int(input())
A = LI()
c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[i] = c.count(A[i])
    c[c.index(A[i])] += 1
ans = 1
for i in range(N):
    ans = (ans * cnt[i]) % MOD
print (ans)

Fixed submission:
import sys,queue,math,copy
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]
N = int(input())
A = LI()


c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[i] = c.count(A[i])
    if cnt[i] == 0:
        print (0)
        exit (0)
    for j in range(3):
        if c[j] == A[i]:
            c[j] += 1
            break
ans = 1
for i in range(N):
    ans = (ans * cnt[i]) % MOD
print (ans)

Buggy python submission (verdict: Runtime Error):
import sys,queue,math,copy
input = sys.stdin.readline
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
N = int(input())
A = LI()
c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[i] = c.count(A[i])
    c[c.index(A[i])] += 1
ans = 1
for i in range(N):
    ans = (ans * cnt[i]) % MOD
print (ans)

Fixed submission:
import sys,queue,math,copy
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]
N = int(input())
A = LI()


c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[i] = c.count(A[i])
    if cnt[i] == 0:
        print (0)
        exit (0)
    for j in range(3):
        if c[j] == A[i]:
            c[j] += 1
            break
ans = 1
for i in range(N):
    ans = (ans * cnt[i]) % MOD
print (ans)
Buggy python submission (verdict: Runtime Error):
class edge:
    def __init__(self,_from,_to):
        self._from = _from
        self._to = _to
        self._color = 0

n = int(input())
e_all = []
max_b = 0
for i in range(n):
    a,b = map(int,input().split())
    e_all.append(edge(a-1,b-1))
    max_b =  max(b, max_b)

v = [[] for _ in range(max_b)]
for i in range(n):
    e = e_all[i]
    j = 0
    while True:
        if len(v[e._from]) <= j + 1:
            v[e._from].append(False)
        if v[e._from][j] == False:
            while len(v[e._to]) <= j + 1:
                v[e._to].append(False)
            if v[e._to][j] == False:
                v[e._from][j] = True
                v[e._to][j] = True
                e._color = j + 1
                break
        j += 1
cnt = []
for i in range(max_b):
    cnt.append(v[i].count(True))

print (max(cnt))
for i in range(n):
    print (e_all[i]._color)

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N = NI()
    root = [[] for _ in range(N)]
    node = [0] * N
    color = [0] * (N-1)
    for i in range(N-1):
        a,b = LI()
        root[a-1].append((b-1,i))
        root[b-1].append((a-1,i))

    q = [(0,0)]
    ans = 0
    while q:
        i,c = q.pop()
        node[i] = 1
        next_c = 1
        for d,x in root[i]:
            if node[d]: continue
            if next_c == c: next_c += 1
            color[x] = next_c
            ans = max(ans,next_c)
            q.append((d,next_c))
            next_c += 1
    print(ans)
    print(*color,sep='\n')

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
S = [0] + list(map(int,input().split()))

for i in range(1,N+1):
    S[i] += S[i-1]
for i in range(N+1):
    S[i] -= i
    S[i] %= K
cnt = 0
for i in range(N):
    cnt += S[i+1:min(N+1,i+K)].count(S[i])
print (cnt)


Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,K = LI()
    A = LI()

    c = 0
    d = collections.Counter()
    d[0] = 1
    ans = 0
    r = [0] * (N+1)
    for i,x in enumerate(A):
        if i >= K-1:
            d[r[i-(K-1)]] -= 1
        c = (c + x - 1) % K
        ans += d[c]
        d[c] += 1
        r[i+1] = c

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    key = [0]
    box = [0]
    for _ in range(M):
        k,_ = LI()
        key.append(k)
        b = 0
        for c in LI():
            b += 2**(c-1)
        box.append(b)

    dp = [[INF] * (2**N) for _ in range(M+1)]
    dp[0][0] = 0
    for i in range(1,M+1):
        for j in range(2**N):
            dp[i][j] = min(dp[i-1][j],dp[i-1][j - (j&box[i])]+key[i])
    if dp[-1][-1] == INF:
        print(-1)
    else:
        print(dp[-1][-1])
if __name__ == '__main__':
    main()

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**8
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    key = [0]
    box = [0]
    for _ in range(M):
        k,_ = LI()
        key.append(k)
        b = 0
        for c in LI():
            b += 2**(c-1)
        box.append(b)

    dp = [[INF] * (2**N) for _ in range(M+1)]
    dp[0][0] = 0
    for i in range(1,M+1):
        for j in range(2**N):
            dp[i][j] = min(dp[i-1][j],dp[i-1][j - (j&box[i])]+key[i])
    if dp[-1][-1] == INF:
        print(-1)
    else:
        print(dp[-1][-1])
if __name__ == '__main__':
    main()