alexjercan/bugnet huggingface dataset:
Problem p02831 (Python) — buggy submission, verdict: Runtime Error

import fractions

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

print(a * b // fractions.gcd(a, b))


Error: 0

Fixed submission:

import math

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

print(a * b // math.gcd(a, b))
Problem p02831 (Python) — buggy submission, verdict: Runtime Error

import math

A, B = map(int, input().split())


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


# a,bの最小公倍数


def lcm(a, b):
    return a * b // gcd(a, b)


print(lcm(A, B))


Error: 0

Fixed submission:

import math

A, B = map(int, input().split())


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


print(lcm(A, B))
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
for i in range(N):
    x = a[i]
    if x == nm:
        nm += 1
    else:
        ans += 1

if ans == N:
    print(-1)
else:
    print(ans)


Error: TLE

Fixed submission:

N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
while a:
    x = a.pop(0)
    if x == nm:
        nm += 1
    else:
        ans += 1

if ans == N:
    print(-1)
else:
    print(ans)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

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

length = len(A)
find_next = 1

for i in range(N):
    if A[i] == find_next:
        find_next += 1

if find_next == 1:
    print(-1)
else:
    print(length - find_next + 1)


Error: TLE

Fixed submission:

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

length = len(A)
head = 0
i = 1

while True:
    if i in A:
        head = A.index(i)
        A = A[head:]
        i += 1
    else:
        break

if i == 1:
    print(-1)
else:
    print(length - i + 1)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

def main(n: int, a: list):
    ans = 0

    i = 1

    if 1 not in set(a):
        print(-1)
        return

    for _a in a:
        if _a != i:
            ans += 1
        else:
            i += 1

    print(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    main(n, a)


Error: TLE

Fixed submission:

def main(n: int, a: list):
    ans = 0

    i = 1

    if 1 not in a:
        print(-1)
        return

    while len(a) > 0:
        if a[0] != i:
            a.pop(0)
            ans += 1
            continue
        i += 1
        a.pop(0)

    print(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    main(n, a)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
a = list(map(int, input().split()))

now = 1
for ai in a:
    if ai == now:
        now += 1

if now == 1:
    print(-1)
else:
    print(N - now + 1)


Error: TLE

Fixed submission:

N = int(input())
a = list(map(int, input().split()))
remain = 1  # 残す整数
index = 0

if remain not in a:
    print(-1)
    exit()

while remain in a[index:]:
    index = a[index:].index(remain)
    remain += 1

print(N - remain + 1)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
lst = list(map(int, input().split()))
i = 1
for j in lst:
    if i == j:
        i += 1

print(-1 if i == 1 else n - i + 1)


Error: TLE

Fixed submission:

n = int(input())
lst = list(map(int, input().split()))
i, j = 0, 1
while i < len(lst):
    if lst[i] != j:
        lst.pop(i)
    else:
        i += 1
        j += 1

if not lst:
    print(-1)
else:
    print(n - len(lst))
Problem p02832 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
alist = list(map(int, input().split()))
now = 1
for a in alist:
    if a == now:
        now += 1
if now == 1:
    print(-1)
else:
    print(N - now + 1)


Error: WA

Fixed submission:

N = int(input())
alist = list(map(int, input().split()))
left = 0
for i in range(1, N + 1):
    try:
        old_left = left
        left = alist[left:].index(i)
        p = left + old_left
    except ValueError:
        print(i, p)
        if i == 1:
            print(-1)
        else:
            print(N - i + (N - p))
        break
else:
    print(0)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
j = 1

for i in map(int, input().split()):
    if i == j:
        j += 1

print((-1, n - j + 1)[j != 1])


Error: TLE

Fixed submission:

n = int(input())
a = tuple(map(int, input().split()))
pos = 0
j = 1

while True:
    for i, data in enumerate(a[pos:]):
        if data == j:
            pos += i + 1
            j += 1
            break
    else:
        print((-1, n - j + 1)[j != 1])
        break
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

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

ans = 0
num = 1
for a in A:
    if a == num:
        num += 1
    else:
        ans += 1

if ans == N:
    ans = -1

print(ans)


Error: TLE

Fixed submission:

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

ans = 0
i = 0
while i < len(A):
    if (i + 1) == A[i]:
        i += 1
    else:
        A.pop(i)
        ans += 1

if A:
    print(ans)
else:
    print(-1)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
a = list(map(int, input().split()))
ans = 0
x = 1
for i in a:
    if i == x:
        x += 1
    else:
        ans += 1
if x == 1:
    print(-1)
else:
    print(ans)


Error: TLE

Fixed submission:

N = int(input())
a = list(map(int, input().split()))
ans = 0
i = 1
j = 0
while j < N:
    if i not in a[j:]:
        ans += N - j
        break
    nj = a[j:].index(i)
    ans += nj - j
    i += 1
    j = nj + 1
if i == 1:
    print(-1)
else:
    print(ans)
Problem p02832 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
list_a = list(map(int, input().split()))

count = 1
for i in list_a:
    if i == count:
        count += 1

print(N - (count - 1) if count != 1 else -1)


Error: TLE

Fixed submission:

N = int(input())
list_a = list(map(int, input().split()))

i = 1
while True:
    if i in list_a:
        index = list_a.index(i)
        list_a = list_a[index:]
        i += 1
    else:
        break

print(N - (i - 1) if i != 1 else -1)
Problem p02833 (Python) — buggy submission, verdict: Runtime Error

import math


def main():
    N = int(input())

    if N == 0 or N == 1:
        ans = 0

    elif N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))


main()


Error: 0

Fixed submission:

import math


def main():
    N = int(input())

    if N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))


main()
Problem p02833 (Python) — buggy submission, verdict: Runtime Error

N = int(input())

if N % 2 != 0:
    print(0)
else:
    count = N // 10
    for j in range(1, 50):
        count += N // ((5**j) * 10)
    print(count)


Error: 0

Fixed submission:

N = int(input())

if N % 2 != 0:
    print(0)

else:
    count = N // 10
    for j in range(1, 50):
        count += N // ((5**j) * 10)

print(count)
Problem p02834 (Python) — buggy submission, verdict: Runtime Error

from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    dist = [-1 for _ in range(N)]  # 前処理
    dist[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            que.append(i), frag.add(i)
    return dist


n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]

near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)

node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans - 1)


Error: TypeError: 'int' object is not subscriptable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02834/Python/s347111194.py", line 35, in <module>
    print(ans[0] - 1)
TypeError: 'int' object is not subscriptable


Fixed submission:

from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    dist = [-1 for _ in range(N)]  # 前処理
    dist[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            que.append(i), frag.add(i)
    return dist


n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]

near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)

node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans[0] - 1)
Problem p02834 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**5)


def dfs(v, parent, depth):
    parents[v] = parent
    self_depths[v] = depth
    sd = 0
    for u in links[v]:
        if u == parent:
            continue
        res = dfs(u, v, depth + 1)
        sd = max(sd, res + 1)

    subtree_depths[v] = sd
    return sd


def solve(u):
    ans = 0
    catch = self_depths[u] // 2
    while self_depths[u] > catch:
        ans = max(ans, self_depths[u] + subtree_depths[u] - 1)
        u = parents[u]
    return ans


n, u, v = map(int, input().split())
u -= 1
v -= 1
links = [set() for _ in range(n)]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)

parents = [-1] * n
self_depths = [0] * n
subtree_depths = [0] * n
dfs(v, -1, 0)
# print(parents)
# print(self_depths)
# print(subtree_depths)
print(solve(u))


Error: 0

Fixed submission:

import sys


def dfs(v, parent, depth):
    parents[v] = parent
    self_depths[v] = depth
    sd = 0
    for u in links[v]:
        if u == parent:
            continue
        res = dfs(u, v, depth + 1)
        sd = max(sd, res + 1)

    subtree_depths[v] = sd
    return sd


def solve(u, v):
    ans = 0
    catch = self_depths[u] // 2
    while self_depths[u] > catch:
        ans = max(ans, self_depths[u] + subtree_depths[u] - 1)
        u = parents[u]
    return ans


n, u, v = map(int, input().split())
u -= 1
v -= 1
links = [set() for _ in range(n)]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)

parents = [-1] * n
self_depths = [0] * n
subtree_depths = [0] * n
dfs(v, -1, 0)
# print(parents)
# print(self_depths)
# print(subtree_depths)
print(solve(u, v))
Problem p02834 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**6)

n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0


def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)


dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)


Error: 0

Fixed submission:

n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0


def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)


dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)
Problem p02834 (Python) — buggy submission, verdict: Runtime Error

# 写経
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, u, v = map(int, readline().split())
m = map(int, read().split())
AB = zip(m, m)

graph = [[] for _ in range(n + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)


def dfs(v):
    dist = [-1] * (n + 1)
    # 初期化
    stack = [v]
    # 初期位置を記憶
    dist[v] = 0
    # 初期位置の距離を0に
    while stack:
        # stackの中身がなくなるまで頑張る
        v = stack.pop()
        # stackから1つ要素を取り出す
        dw = dist[v] + 1
        # その要素の周りを考えるので、距離としてはその要素の距離に1を加えたものを考える
        for w in graph[v]:
            # 考えている要素に対して隣接している点に対して
            if dist[w] >= 0:
                # もし今まで訪れたことがあったら無視
                continue
            dist[w] = dw
            # 訪れたことがなかったら距離を更新
            stack.append(w)
            # 訪れたことがないところだったらstackに訪れる点リストとして保存
    return dist


answer = 0
du, dv = dfs(u), dfs(v)
for u, v in zip(du[1:], dv[1:]):
    if v - u > 0:
        x = v - 1
        if answer < x:
            answer = x
print(answer)


Error: TLE

Fixed submission:

# nlognの実装。駄目。
import numpy as np
from scipy.sparse.csgraph import dijkstra


def calc(d):
    if d == 1 or d == 0:
        return 0
    else:
        return int(d - 1)


delta = []
n, u, v = map(int, input().split())
Gn = np.full((n, n), float("inf"))
for i in range(n - 1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    Gn[a][b] = 1
    Gn[b][a] = 1
    Gn[i][i] = 0
minimum_u = dijkstra(Gn, indices=u - 1)
minimum_v = dijkstra(Gn, indices=v - 1)
for i in range(n):
    if minimum_v[i] - minimum_u[i] > 0 and i != u - 1 and i != v - 1:
        delta.append([i, minimum_v[i]])
if delta != []:
    delta_sorted = sorted(delta, key=lambda x: x[1])
    p = delta_sorted[-1][0]
    d = int(minimum_v[p])
    print(calc(d))
else:
    print(calc(minimum_u[v - 1]))
Problem p02834 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque


def bfs(s):
    d = [-1] * n
    p = [-1] * n
    dq = deque([s])
    d[s] = 0

    while dq:
        u = dq.popleft()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                p[v] = u
                dq.append(v)

    return d, p


n, takahashi, aoki = map(int, input().split())
takahashi -= 1
aoki -= 1
ab = [list(map(int, input().split())) for _ in range(n - 1)]

adj = [[] for _ in range(n)]
for a, b in ab:
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)

depth, par = bfs(takahashi)
aoki_move = depth[aoki] // 2 + 1
root = aoki
for _ in range(aoki_move):
    child = root
    root = par[root]

adj[child].remove(root)
adj[root].remove(child)

depth2, par2 = bfs(root)

aoki_move += max(depth2) - 1
print(aoki_move)


Error: TLE

Fixed submission:

n, U, V = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
U -= 1
V -= 1

g = [[] for _ in range(n)]
for a, b in ab:
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

WHITE = 0
GRAY = 1
BLACK = 2


def dfs(s):
    color = [WHITE] * n
    d = [-1] * n
    f = [-1] * n
    p = [-1] * n
    t = 0
    stack = [s]
    color[s] = GRAY
    d[s] = t
    t += 1
    while stack:
        u = stack[-1]
        for v in g[u]:
            if color[v] == WHITE:
                color[v] = GRAY
                d[v] = t
                p[v] = u
                t += 1
                stack.append(v)
                break
        else:
            color[u] = BLACK
            f[u] = t
            t -= 1
            stack.pop()

    return d


dv = dfs(V)

du = dfs(U)

ans = 0
for due, dve in zip(du, dv):
    if dve > due:
        ans = max(ans, dve - 1)

print(ans)
Problem p02835 (Python) — buggy submission, verdict: Runtime Error

a, b, c = map(int, input().split())
if a + b + c >= 22:
    print("bust")
else:
    print("win")


Error: EOFError: EOF when reading a line
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s614017612.py", line 2, in <module>
    A2 = input()
EOFError: EOF when reading a line


Fixed submission:

A1 = input()
A2 = input()
A3 = input()
Problem p02835 (Python) — buggy submission, verdict: Runtime Error

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

print("win" if sum((a, b, c)) < 22 else "bust")


Error: EOFError: EOF when reading a line
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s003175429.py", line 4, in <module>
    a, b, c = map(int, input().split())
EOFError: EOF when reading a line


Fixed submission:

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

print("win" if sum((a, b, c)) < 21 else "bust")
a, b, c = map(int, input().split())

print("win" if sum((a, b, c)) < 22 else "bust")
Problem p02835 (Python) — buggy submission, verdict: Runtime Error

str = [int(i) for i in input().split(" ")]
input_sum = sum(str)
if input_sum >= 22:
    print("bust")
else:
    print("win")


Error: ValueError: invalid literal for int() with base 10: '5 7 9'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s364545802.py", line 1, in <module>
    str = [int(input()) for i in range(3)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s364545802.py", line 1, in <listcomp>
    str = [int(input()) for i in range(3)]
ValueError: invalid literal for int() with base 10: '5 7 9'


Fixed submission:

str = [int(input()) for i in range(3)]
input_sum = sum(str)
if input_sum >= 22:
    print("true")
else:
    print("win")
Problem p02835 (Python) — buggy submission, verdict: Runtime Error

[A1, A2, A3] = list(map(int, input().split()))
if A1 + A2 + A3 >= 22:
    print("bust")
else:
    print("win")


Error: ValueError: invalid literal for int() with base 10: '5 7 9'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s212671316.py", line 1, in <module>
    A1 = int(input())
ValueError: invalid literal for int() with base 10: '5 7 9'


Fixed submission:

A1 = int(input())
A2 = int(input())
A3 = int(input())
if A1 + A2 + A3 >= 22:
    print("burst")
else:
    print("win")
Problem p02835 (Python) — buggy submission, verdict: Runtime Error

array = map(int, input().split())
if sum(array) >= 22:
    print("bust")
else:
    print("win")


Error: 0

Fixed submission:

array = map(int, input().split())
print(f'{ "bust" if sum(array) >= 22 else "win"}')
Problem p02837 (Python) — buggy submission, verdict: Runtime Error

N = int(input())

XY = []
for _ in range(N):
    A = int(input())
    tmp = []
    for _ in range(A):
        x, y = map(int, input().split())
        tmp.append((x - 1, y))
    XY.append(tmp)


def judge(bit):
    for i in range(N):
        if not (bit & (1 << i)):
            continue
        for x, y in XY[i]:
            if y == 1 and (not (bit & (1 << x))):
                return False
            if y == 0 and (bit & (1 << x)):
                return False
    return True


ans = 0
for bit in range(1 << N):
    if judge(bit):
        # print('bit', bit, bin(bit))
        cnt = 0
        for i in range(N):
            if bit & (1 << i):
                cnt += 1
        ans = max(ans, cnt)
print(ans)


Error: IndexError: list index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02837/Python/s690218686.py", line 15, in <module>
    if a[k] != b[k]:
IndexError: list index out of range


Fixed submission:

N = int(input())
S = []
for _ in range(N):
    s = list(input())
    s.sort()
    # s = ''.join(s)
    S.append(s)

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(10):
            a = S[i]
            b = S[j]
            if a[k] != b[k]:
                break
        else:
            ans += 1
print(ans)
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += (i - b[j] if a[i] >> j & 1 else b[j]) << j
    ans %= 10**9 + 7
    for j in range(60):
        b[j] += a[i] >> j & 1
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += 2**j * (i - b[j] if a[i] >> j & 1 else b[j])
        ans %= 10**9 + 7
    for j in range(len(bin(a[i])) - 2):
        b[j] += a[i] >> j & 1
print(ans)
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

n = int(input())
s = np.array(input().split(), np.int64)
mod = 10**9 + 7

res = 0
po2 = 1
for i in range(61):
    bit = (s >> i) & 1
    x = np.count_nonzero(bit)
    y = n - x
    res += x * y % mod * po2
    res %= mod
    po2 *= 2 % mod
print(res)


Error: TLE

Fixed submission:

n = int(input())
s = list(map(int, input().split()))
mod = 10**9 + 7
m = len(bin(max(s))) - 2
p = [0] * m
q = [0] * m

for i in s:
    for j in range(m):
        if (i >> j) & 1 == 1:
            p[j] += 1
        else:
            q[j] += 1

res = 0
r = 1
for k in range(m):
    res += p[k] * q[k] % mod * r
    res %= mod
    r = r * 2 % mod
print(res)
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

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

mod = 10**9 + 7

ans = 0
for i in range(61):
    zero = 0
    one = 0
    for ai in a:
        if ai >> i & 1:
            one += 1
        else:
            zero += 1
    ans += zero * one * 2**i
    ans %= mod

print(ans)


Error: TLE

Fixed submission:

from itertools import combinations

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

total = 0
for pat in combinations(range(n), 2):
    total += a[pat[0]] ^ a[pat[1]]
print(total % (10**9 + 7))
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

N = int(input())
A = np.array([int(_) for _ in input().split()], dtype=np.int64)
mod = 10**9 + 7
ans = 0
for b in range(60):
    B = (A >> b) & 1
    M = np.count_nonzero(B)
    ans += 2**b * M * (N - M)
    ans %= mod
print(ans)


Error: TLE

Fixed submission:

N = int(input())
A = [int(_) for _ in input().split()]
mod = 10**9 + 7
ans = 0
for b in range(60):
    M = sum((a >> b) & 1 for a in A)
    ans += 2**b * M * (N - M)
    ans %= mod
print(ans)
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np


def solve():
    ans = 0
    mask = 1
    for i in range(60):
        one_cnt = np.sum(A >> i & 1)
        tmp = one_cnt * (N - one_cnt)
        tmp %= MOD
        ans += tmp * mask
        ans %= MOD
        mask <<= 1
        mask %= MOD
    return ans


N, *A = map(int, open(0).read().split())
A = np.array(A, dtype=np.int64)
MOD = 1_000_000_007
print(solve())


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
MOD = 1_000_000_007

ans = 0
two_exp = 1
for i in range(60):
    one_cnt = 0
    zero_cnt = 0
    for a in A:
        if a >> i & 1:
            one_cnt += 1
        else:
            zero_cnt += 1
    ans += one_cnt * zero_cnt * two_exp
    ans %= MOD
    two_exp <<= 1
    two_exp %= MOD
print(ans)