alexjercan/bugnet huggingface dataset:
Problem p02850 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env pypy3

import itertools


UNDEF = 0


def color_edges(n, edges):
    adj_list = [set() for _ in range(n)]
    for i, (a, b) in enumerate(edges):
        adj_list[a].add(i)
        adj_list[b].add(i)
    colors = [UNDEF] * (n - 1)
    for v in range(n):
        adj_es = adj_list[v]
        num_adj = len(adj_es)
        cur_cols = set(colors[i] for i in adj_es if colors[i] != UNDEF)
        new_cols = set()
        for i in itertools.count(1):
            if len(cur_cols) + len(new_cols) >= num_adj:
                break
            elif i not in cur_cols:
                new_cols.add(i)
        for u in adj_es:
            if colors[u] == UNDEF:
                colors[u] = new_cols.pop()
    return max(colors), colors


def main():
    n = int(input())
    edges = []
    for _ in range(n - 1):
        a, b = (int(z) - 1 for z in input().split())
        edges.append((a, b))
    k, cs = color_edges(n, edges)
    print(k)
    print(*cs, sep="\\n")


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

#!/usr/bin/env pypy3

import itertools


UNDEF = 0


def mex(s):
    for i in itertools.count(1):
        if i not in s:
            return i


def color_edges(n, edges):
    adj_list = [set() for _ in range(n)]
    for i, (a, b) in enumerate(edges):
        adj_list[a].add(i)
        adj_list[b].add(i)
    colors = [UNDEF for _ in range(n - 1)]
    for v in range(n):
        adj_es = adj_list[v]
        num_adj = len(adj_es)
        cur_cols = set(colors[i] for i in adj_es if colors[i] != UNDEF)
        new_cols = set()
        while len(cur_cols) < num_adj:
            nc = mex(cur_cols)
            cur_cols.add(nc)
            new_cols.add(nc)
        for u in adj_es:
            if colors[u] == UNDEF:
                colors[u] = new_cols.pop()
    return max(colors), colors


def main():
    n = int(input())
    edges = []
    for _ in range(n - 1):
        a, b = (int(z) - 1 for z in input().split())
        edges.append((a, b))
    k, cs = color_edges(n, edges)
    print(k)
    print(*cs, sep="\\n")


if __name__ == "__main__":
    main()
Problem p02850 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

N, *AB = map(int, open(0).read().split())

E = [set() for _ in range(N + 1)]
for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
    E[a].add((b, i))
    E[b].add((a, i))

K = max(len(e) for e in E)
A = [0] * (N - 1)
Q = deque([(-1, 1)])
while Q:
    p, v = Q.popleft()
    c = 0
    for u, i in E[v]:
        E[u].remove((v, i))
        c += 1 + (c + 1 == p)
        Q.append((c, u))
        A[i] = c

print(K)
for a in A:
    print(a)


Error: TLE

Fixed submission:

from collections import deque

N, *AB = map(int, open(0).read().split())

E = [set() for _ in range(N + 1)]
for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
    E[a].add((b, i))
    E[b].add((a, i))

K = max(len(e) for e in E)
A = [0] * (N - 1)
Q = deque([(-1, 1)])
S = {c for c in range(1, K + 1)}
while Q:
    p, v = Q.popleft()
    C = S - {p}
    for u, i in E[v]:
        E[u].remove((v, i))
        c = C.pop()
        Q.append((c, u))
        A[i] = c

print(K)
for a in A:
    print(a)
Problem p02851 (Python) — buggy submission, verdict: Runtime Error

from collections import Counter, deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
INF = 10**18

cnt = Counter()
cnt[0] = 1
que = deque([0])
sumR = 0
ans = 0
for right, a in enumerate(A, start=1):
    if len(que) >= K:
        cnt[que.popleft()] -= 1
    sumR = (a + sumR) % K
    D = (sumR - right) % K
    ans += cnt[D]
    cnt[D] += 1
    que.append(D)
print(ans)


Error: 0

Fixed submission:

from collections import Counter, deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
INF = 10**18

cnt = Counter()
cnt[0] = 1
que = deque([-INF] * (K - 1) + [0])
sumR = 0
ans = 0
for right, a in enumerate(A, start=1):
    cnt[que.popleft()] -= 1
    sumR = (a + sumR) % K
    D = (sumR - right) % K
    ans += cnt[D]
    cnt[D] += 1
    que.append(D)
print(ans)
Problem p02851 (Python) — buggy submission, verdict: Time Limit Exceeded

n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    if r in s:
        s[r].append(i + 1)
    else:
        s[r] = [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)


Error: TLE

Fixed submission:

n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    s[r] = s.get(r, []) + [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)
Problem p02852 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
s = input()
out_s = "1" * (m)
if out_s in s:
    print(-1)
    exit()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")


Error: TLE

Fixed submission:

n, m = map(int, input().split())
s = input()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")
Problem p02852 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
S = list(map(int, reversed(input())))
now = 0
ans = []
while now + M < N:
    last = now
    now += M
    while S[now]:
        now -= 1
    if last == now:
        print(-1)
        break
    ans.append(now - last)
else:
    print(*reversed(ans + [N - now]))


Error: TLE

Fixed submission:

from collections import deque

N, M = map(int, input().split())
S = input()[::-1]

stack = deque([[0, M + 1]])
now = 0
while stack:
    now, d = stack.pop()
    d -= 1
    if not d:
        continue
    if now + d >= N:
        stack.append([now, d - (now + d - N)])
        print(*map(lambda s: s[1], reversed(stack)))
        quit()
    stack.append([now, d])
    if S[now + d] == "0":
        stack.append([now + d, M + 1])
print(-1)
Problem p02852 (Python) — buggy submission, verdict: Runtime Error

N, M = map(int, input().split())
S = input()
S = S[::-1]

dp = [-1] * (N + 1)
dp[0] = 0

pos = 0
while pos < N:

    p = pos
    update = False

    for i in range(1, M + 1):
        if p + i > N:
            break
        if S[p + i] == "0":
            update = True
            pos = p + i
            dp[pos] = dp[p] + 1

    if not update:
        break

if dp[N] == -1:
    print(-1)
    exit()

dp = dp[::-1]
num = dp[0]
ans = []
pos = 0

while num > 0:
    for i in range(1, M + 1):
        if dp[pos + i] == num - 1:
            num -= 1
            pos += i
            ans.append(str(i))
            break

ans = " ".join(ans)
print(ans)


Error: 0

Fixed submission:

N, M = map(int, input().split())
S = input()
S = S[::-1]

dp = [-1] * (N + 1)
dp[0] = 0

pos = 0
while pos < N:

    p = pos
    update = False

    for i in range(1, M + 1):
        if p + i > N:
            break
        if S[p + i] == "1":
            continue
        update = True
        pos = p + i
        dp[pos] = dp[p] + 1

    if not update:
        break

if dp[N] == -1:
    print(-1)
    exit()

dp = dp[::-1]
num = dp[0]
ans = []
pos = 0

while num > 0:
    for i in range(1, M + 1):
        if dp[pos + i] == num - 1:
            num -= 1
            pos += i
            ans.append(str(i))

ans = " ".join(ans)
print(ans)
Problem p02853 (Python) — buggy submission, verdict: Runtime Error

x, y = map(int, input().split())
ans = 0
if x <= 3:
    ans += 400000 - x * 100000
if y <= 3:
    ans += 400000 - y * 100000
if x == y == 1:
    ans += 400000
print(ans)


Error: 0

Fixed submission:

x, y = map(int, input().split())
ans = 0
if x <= 3:
    ans += 400_000 - x * 100_000
if y <= 3:
    ans += 400_000 - y * 100_000
if x == y == 1:
    ans += 400_000
print(ans)
Problem p02853 (Python) — buggy submission, verdict: Runtime Error

def main(x: int, y: int):
    award = 0

    if x == y == 1:
        award += 400000

    for i in [x, y]:
        if i == 1:
            award += 300000
        elif i == 2:
            award += 200000
        elif i == 3:
            award += 100000

    print(award)


if __name__ == "__main__":
    x, y = map(int, input().split())

    main(x, y)


Error: 0

Fixed submission:

def main(x: int, y: int):
    award: int = 0

    if (x and y) == 1:
        award += 400000

    for i in [x, y]:
        if i == 1:
            award += 300000
        elif i == 2:
            award += 200000
        elif i == 3:
            award += 100000

    print(award)


if __name__ == "__main__":
    x, y = map(int, input().split())

    main(x, y)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
SUMA = sum(a)

for i in range(n):
    if tmp + a[i] >= SUMA / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key += 1
ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
for i in range(n):
    if tmp + a[i] >= sum(a) / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key = 0
for i in range(n):
    tmp += a[i]
    if tmp >= sum(a) / 2:
        key = i + 1
        break

ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))


Error: TLE

Fixed submission:

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

ans, sumA2 = abs(sum(A) - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sum(A) - sumA2 * 2))

print(ans)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

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

ans = 10**11
sum_A = sum(A)
l = 0

for a in A:
    l += a
    if abs(l - (sum_A - l)) < ans:
        ans = abs(l - (sum_A - l))

print(ans)


Error: TLE

Fixed submission:

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

ans = 10**11

for i in range(n):
    if abs(sum(A[:i]) - sum(A[i:])) < ans:
        ans = abs(sum(A[:i]) - sum(A[i:]))

print(ans)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
sticks = list(map(int, input().split()))
sticksSum = sum(sticks)
diffMin = sum(sticks)
plusSticks = 0
for i in range(len(sticks)):
    plusSticks += sticks[i]
    diffMin = min(diffMin, abs(2 * plusSticks - sticksSum))
print(diffMin)


Error: TLE

Fixed submission:

N = int(input())
sticks = list(map(int, input().split()))
diffMin = sum(sticks)
for i in range(len(sticks)):
    diff = abs(sum(sticks[:i]) - sum(sticks[i:]))
    if diff < diffMin:
        diffMin = diff
print(diffMin)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
A = tuple(map(int, input().split(" ")))

S = sum(A)
ans = S
s = 0
for i in range(N):
    s += A[i]
    ans = min(ans, abs(s - (S - s)))
print(ans)


Error: TLE

Fixed submission:

N = int(input())
A = tuple(map(int, input().split(" ")))
d = {i: (sum(A[:i]), sum(A[i:])) for i in range(N)}

print(min(abs(value[0] - value[1]) for key, value in d.items()))
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))
m = 0
s = []
suma = sum(a)
for i in range(len(a)):
    m += a[i]
    s.append(abs(suma - m * 2))
print(min(s))


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
m = 0
s = []
for i in range(len(a)):
    m += a[i]
    s.append(abs(sum(a) - m * 2))
print(min(s))
Problem p02854 (Python) — buggy submission, verdict: Runtime Error

from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])

count = 0
ironbar = list(accumulate(A))

min_value = ironbar[-1]
for i in range(N):
    bet = abs(ironbar[i] - (ironbar[-1] - ironbar[i]))

    if min_value > bet:
        min_value = bet
    else:
        print(min_value)
        exit()


Error: 0

Fixed submission:

from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])

count = 0
while True:
    # 中心に最も近い点を探す
    ironbar = list(accumulate(A))
    center = sum(A) // 2

    i = 0
    j = len(A) - 1

    while i + 1 < j:
        check = ironbar[(i + j) // 2]
        if check < center:
            i = (i + j) // 2
        elif check > center:
            j = (i + j) // 2
        else:
            print(count)
            exit()

    if check > center:
        A[j] -= 1
    else:
        A[j] += 1

    count += 1
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))
x = 0
y = sum(a)
ans = 2020202020
for i in range(n - 1):
    x += a[i]
    y -= a[i]
    ans = min(ans, abs(x - y))
print(ans)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
x = 2020202020
for i in range(1, n):
    x = min(x, abs(sum(a[:i]) - sum(a[i:])))
print(x)
Problem p02854 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
arr = list(map(int, input().split()))
first = 0
second = sum(arr)
min_delta = second - first
for i in range(N):
    first += arr[i]
    second -= arr[i]
    min_delta = min(abs(second - first), min_delta)

print(min_delta)


Error: TLE

Fixed submission:

N = int(input())
arr = list(map(int, input().split()))
min_delta = sum(arr)
for i in range(1, N):
    min_delta = min(abs(sum(arr[:i]) - sum(arr[i:])), min_delta)

print(min_delta)
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

r = int(input())
ans = r * r
print(ans)


Error: TypeError: can't multiply sequence by non-int of type 'str'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s972374104.py", line 2, in <module>
    ans = r * r
TypeError: can't multiply sequence by non-int of type 'str'


Fixed submission:

r = input()
ans = r * r
print(ans)
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

r = int(input())
print(r**2)


Error: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s447174511.py", line 2, in <module>
    print(r**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


Fixed submission:

r = input()
print(r**2)
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

r = int(input())
print(r**2)


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s774524260.py", line 1, in <module>
    r = int(input)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'


Fixed submission:

r = int(input)
print(r**2)
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

r = int(input().strip())
print(r * r)


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s880229188.py", line 1, in <module>
    r = int(input().split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'


Fixed submission:

r = int(input().split())
print(r * r)
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

r = int(input())

print(int(r**2))


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s021145542.py", line 1, in <module>
    r = int(input)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'


Fixed submission:

r = int(input)

print(int(r**2))
Problem p02859 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
print(N**2)


Error: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s490869549.py", line 2, in <module>
    print(N**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


Fixed submission:

N = input()
print(N**2)
Problem p02860 (Python) — buggy submission, verdict: Runtime Error

def main():
    N = int(input())
    S = input()
    if N % 2 == 1:
        print("No")
        quit()
    n = N // 2
    yesno = "Yes" if S[:n] == S[n:] else "No"
    print(yesno)


if __name__ == "__main__":
    main()


Error: TypeError: slice indices must be integers or None or have an __index__ method
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s888782335.py", line 16, in <module>
    main()
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s888782335.py", line 11, in main
    yesno = "Yes" if S[:n] == S[n:] else "No"
TypeError: slice indices must be integers or None or have an __index__ method


Fixed submission:

import sys


def main():
    N = int(input())
    S = input()
    if N % 2 == 1:
        print("No")
        sys.exit()
    n = N / 2
    yesno = "Yes" if S[:n] == S[n:] else "No"
    print(yesno)


if __name__ == "__main__":
    main()
Problem p02860 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n // 2):
        if s[i] != s[n // 2 + i]:
            ans = "No"
            break
print(ans)


Error: TypeError: 'float' object cannot be interpreted as an integer
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s022164402.py", line 6, in <module>
    for i in range(n / 2):
TypeError: 'float' object cannot be interpreted as an integer


Fixed submission:

n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n / 2):
        if s[i] != s[n / 2 + i]:
            ans = "No"
            break
print(ans)
Problem p02860 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
S = input()
if N % 2 != 0:
    print("No")
else:
    arr = list(S)
    s_len = len(arr) // 2
    if arr[s_len:] == arr[:s_len]:
        print("Yes")
    else:
        print("No")


Error: ValueError: invalid literal for int() with base 10: 'abcabc'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s186953339.py", line 2, in <module>
    S = int(input())
ValueError: invalid literal for int() with base 10: 'abcabc'


Fixed submission:

N = int(input())
S = int(input())
if N % 2 != 0:
    print("No")
else:
    arr = list(S)
    s_len = len(arr) // 2
    if arr[s_len:] == arr[:s_len]:
        print("Yes")
    else:
        print("No")
Problem p02861 (Python) — buggy submission, verdict: Runtime Error

import sys
import math


def len(lhs, rhs):
    return math.sqrt((lhs[0] - rhs[0]) ** 2 + (lhs[1] - rhs[1]) ** 2)


def solve(n: int, cities):
    sum = 0
    for i1, city1 in enumerate(cities):
        for i2, city2 in enumerate(cities):
            if i1 != i2:
                sum += len(city1, city2)
    return sum / n


def main():
    inputs = list(sys.stdin)
    n = int(inputs.pop(0))
    cities = [[int(n) for n in s.split(" ")] for s in inputs]
    print(solve(n, cities))


main()


Error: WA

Fixed submission:

import sys
import math
from typing import List, Tuple


def len(lhs: Tuple[int, int], rhs: Tuple[int, int]):
    return math.sqrt((lhs[0] - rhs[0]) ** 2 + (lhs[1] - rhs[1]) ** 2)


def solve(n: int, cities: List[Tuple[int, int]]):
    sum = 0
    for i1, city1 in enumerate(cities):
        for i2, city2 in enumerate(cities):
            if i1 != i2:
                sum += len(city1, city2)
    return sum / n


def main():
    inputs = list(sys.stdin)
    n = int(inputs.pop(0))
    cities = [[int(n) for n in s.split(" ")] for s in inputs]
    print(solve(n, cities))


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

from itertools import permutations

import numpy as np

N = int(input())

xys = []
for _ in range(N):
    xys.append(list(map(int, input().split())))

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print("{:.10f}".format(ret))


Error: TLE

Fixed submission:

from itertools import permutations

import numpy as np

N = int(input())

xys = []
for _ in range(N):
    xys.append(list(map(int, input().split())))

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print(f"{ret:.10f}")
Problem p02861 (Python) — buggy submission, verdict: Time Limit Exceeded

import itertools

n = int(input())

route = [list(map(int, input().split())) for _ in range(n)]
results = []
for v in itertools.permutations(route, r=n):
    distance = 0
    for i in range(n - 1):
        start, goal = v[i], v[i + 1]
        distance += ((goal[0] - start[0]) ** 2 + (goal[1] - start[1]) ** 2) ** 0.5
    results.append(distance)
print(sum(results) / len(results))


Error: TLE

Fixed submission:

import numpy as np
import itertools

n = int(input())

route = [np.array(list(map(int, input().split()))) for _ in range(n)]
results = []
for v in itertools.permutations(route, r=n):
    distance = 0
    for start, goal in zip(v[:-1], v[1:]):
        distance += np.linalg.norm(goal - start)
    results.append(distance)
print(np.mean(results))